diff --git a/icu4c/source/i18n/dtitvfmt.cpp b/icu4c/source/i18n/dtitvfmt.cpp index 8bb6c6e9f4b..492fff73d08 100644 --- a/icu4c/source/i18n/dtitvfmt.cpp +++ b/icu4c/source/i18n/dtitvfmt.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2008-2010, International Business Machines Corporation and +* Copyright (C) 2008-2011, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* * @@ -416,6 +416,50 @@ DateIntervalFormat::getDateFormat() const { } +void +DateIntervalFormat::adoptTimeZone(TimeZone* zone) +{ + if (fDateFormat != NULL) { + fDateFormat->adoptTimeZone(zone); + } + // The fDateFormat has the master calendar for the DateIntervalFormat and has + // ownership of any adopted TimeZone; fFromCalendar and fToCalendar are internal + // work clones of that calendar (and should not also be given ownership of the + // adopted TimeZone). + if (fFromCalendar) { + fFromCalendar->setTimeZone(*zone); + } + if (fToCalendar) { + fToCalendar->setTimeZone(*zone); + } +} + +void +DateIntervalFormat::setTimeZone(const TimeZone& zone) +{ + if (fDateFormat != NULL) { + fDateFormat->setTimeZone(zone); + } + // The fDateFormat has the master calendar for the DateIntervalFormat; + // fFromCalendar and fToCalendar are internal work clones of that calendar. + if (fFromCalendar) { + fFromCalendar->setTimeZone(zone); + } + if (fToCalendar) { + fToCalendar->setTimeZone(zone); + } +} + +const TimeZone& +DateIntervalFormat::getTimeZone() const +{ + if (fDateFormat != NULL) { + return fDateFormat->getTimeZone(); + } + // If fDateFormat is NULL (unexpected), create default timezone. + return *(TimeZone::createDefault()); +} + DateIntervalFormat::DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo, const UnicodeString* skeleton, diff --git a/icu4c/source/i18n/unicode/dtitvfmt.h b/icu4c/source/i18n/unicode/dtitvfmt.h index 750454f703f..2ff8b8ede2e 100644 --- a/icu4c/source/i18n/unicode/dtitvfmt.h +++ b/icu4c/source/i18n/unicode/dtitvfmt.h @@ -1,5 +1,5 @@ /******************************************************************************** -* Copyright (C) 2008-2010, International Business Machines Corporation and +* Copyright (C) 2008-2011, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* * @@ -257,7 +257,7 @@ public: * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs, * "EEE, d MMM - EEE, d MMM, yyyy" for month differs, * "EEE, d - EEE, d MMM, yyyy" for day differs, - * @param skeleton the skeleton on which interval format based. + * @param skeleton the skeleton on which the interval format is based. * @param locale the given locale * @param status output param set to success/failure code on exit * @return a date time interval formatter which the caller owns. @@ -489,6 +489,28 @@ public: */ const DateFormat* getDateFormat(void) const; + /** + * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar. + * @return the time zone associated with the calendar of DateIntervalFormat. + * @draft ICU 4.8 + */ + virtual const TimeZone& getTimeZone(void) const; + + /** + * Sets the time zone for the calendar used by this DateIntervalFormat object. The + * caller no longer owns the TimeZone object and should not delete it after this call. + * @param zoneToAdopt the TimeZone to be adopted. + * @draft ICU 4.8 + */ + virtual void adoptTimeZone(TimeZone* zoneToAdopt); + + /** + * Sets the time zone for the calendar used by this DateIntervalFormat object. + * @param zone the new time zone. + * @draft ICU 4.8 + */ + virtual void setTimeZone(const TimeZone& zone); + /** * Return the class ID for this class. This is useful only for comparing to * a return value from getDynamicClassID(). For example: diff --git a/icu4c/source/test/intltest/dtifmtts.cpp b/icu4c/source/test/intltest/dtifmtts.cpp index 3a117183061..3353f850d06 100644 --- a/icu4c/source/test/intltest/dtifmtts.cpp +++ b/icu4c/source/test/intltest/dtifmtts.cpp @@ -25,6 +25,7 @@ #include "unicode/dtintrv.h" #include "unicode/dtitvinf.h" #include "unicode/dtitvfmt.h" +#include "unicode/timezone.h" @@ -262,6 +263,56 @@ void DateIntervalFormatTest::testAPI() { delete dtitvfmt; + //====== test setting time zone + logln("Testing DateIntervalFormat set & format with different time zones, get time zone"); + status = U_ZERO_ERROR; + dtitvfmt = DateIntervalFormat::createInstance("MMMdHHmm", Locale::getEnglish(), status); + if ( U_SUCCESS(status) ) { + UDate date1 = 1299090600000.0; // 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo + UDate date2 = 1299115800000.0; // 2011-Mar-02 1730 in US/Pacific, 2011-Mar-03 1030 in Asia/Tokyo + + DateInterval * dtitv12 = new DateInterval(date1, date2); + TimeZone * tzCalif = TimeZone::createTimeZone("US/Pacific"); + TimeZone * tzTokyo = TimeZone::createTimeZone("Asia/Tokyo"); + UnicodeString fmtCalif = UnicodeString("Mar 2 10:30 – Mar 2 17:30"); + UnicodeString fmtTokyo = UnicodeString("Mar 3 03:30 – Mar 3 10:30"); + + dtitvfmt->adoptTimeZone(tzCalif); + res.remove(); + pos = 0; + status = U_ZERO_ERROR; + dtitvfmt->format(dtitv12, res, pos, status); + if ( U_SUCCESS(status) ) { + if ( res.compare(fmtCalif) != 0 ) { + errln("ERROR: DateIntervalFormat::format for tzCalif, expect " + fmtCalif + ", get " + res); + } + } else { + errln("ERROR: DateIntervalFormat::format for tzCalif, status %s", u_errorName(status)); + } + + dtitvfmt->setTimeZone(*tzTokyo); + res.remove(); + pos = 0; + status = U_ZERO_ERROR; + dtitvfmt->format(dtitv12, res, pos, status); + if ( U_SUCCESS(status) ) { + if ( res.compare(fmtTokyo) != 0 ) { + errln("ERROR: DateIntervalFormat::format for fmtTokyo, expect " + fmtTokyo + ", get " + res); + } + } else { + errln("ERROR: DateIntervalFormat::format for tzTokyo, status %s", u_errorName(status)); + } + + if ( dtitvfmt->getTimeZone() != *tzTokyo ) { + errln("ERROR: DateIntervalFormat::getTimeZone returns mismatch."); + } + + delete tzTokyo; // tzCalif was owned by dtitvfmt which should have deleted it + delete dtitv12; + delete dtitvfmt; + } else { + errln("ERROR: DateIntervalFormat::createInstance(\"MdHH\", Locale::getEnglish(), ...), status %s", u_errorName(status)); + } //====== test format in testFormat() //====== test DateInterval class (better coverage)