2 *******************************************************************************
3 * Copyright (C) 2007-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "simplethread.h"
14 #include "unicode/timezone.h"
15 #include "unicode/simpletz.h"
16 #include "unicode/calendar.h"
17 #include "unicode/strenum.h"
18 #include "unicode/smpdtfmt.h"
19 #include "unicode/uchar.h"
20 #include "unicode/basictz.h"
21 #include "unicode/tzfmt.h"
22 #include "unicode/localpointer.h"
26 static const char* PATTERNS[] = {
29 "Z", // equivalent to "xxxx"
30 "ZZZZ", // equivalent to "OOOO"
50 static const int NUM_PATTERNS = sizeof(PATTERNS)/sizeof(const char*);
52 static const UChar ETC_UNKNOWN[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0};
54 static const UChar ETC_SLASH[] = { 0x45, 0x74, 0x63, 0x2F, 0 }; // "Etc/"
55 static const UChar SYSTEMV_SLASH[] = { 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x56, 0x2F, 0 }; // "SystemV/
56 static const UChar RIYADH8[] = { 0x52, 0x69, 0x79, 0x61, 0x64, 0x68, 0x38, 0 }; // "Riyadh8"
58 static UBool contains(const char** list, const char* str) {
59 for (int32_t i = 0; list[i]; i++) {
60 if (uprv_strcmp(list[i], str) == 0) {
68 TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
71 logln("TestSuite TimeZoneFormatTest");
74 TESTCASE(0, TestTimeZoneRoundTrip);
75 TESTCASE(1, TestTimeRoundTrip);
76 TESTCASE(2, TestParse);
77 TESTCASE(3, TestISOFormat);
78 TESTCASE(4, TestFormat);
79 default: name = ""; break;
84 TimeZoneFormatTest::TestTimeZoneRoundTrip(void) {
85 UErrorCode status = U_ZERO_ERROR;
87 SimpleTimeZone unknownZone(-31415, ETC_UNKNOWN);
88 int32_t badDstOffset = -1234;
89 int32_t badZoneOffset = -2345;
91 int32_t testDateData[][3] = {
100 Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString)"UTC"), status);
101 if (U_FAILURE(status)) {
102 dataerrln("Calendar::createInstance failed: %s", u_errorName(status));
106 // Set up rule equivalency test range
108 cal->set(1900, UCAL_JANUARY, 1);
109 low = cal->getTime(status);
110 cal->set(2040, UCAL_JANUARY, 1);
111 high = cal->getTime(status);
112 if (U_FAILURE(status)) {
113 errln("getTime failed");
118 UDate DATES[(sizeof(testDateData)/sizeof(int32_t))/3];
119 const int32_t nDates = (sizeof(testDateData)/sizeof(int32_t))/3;
121 for (int32_t i = 0; i < nDates; i++) {
122 cal->set(testDateData[i][0], testDateData[i][1], testDateData[i][2]);
123 DATES[i] = cal->getTime(status);
124 if (U_FAILURE(status)) {
125 errln("getTime failed");
130 // Set up test locales
131 const Locale testLocales[] = {
138 const Locale *LOCALES;
142 LOCALES = testLocales;
143 nLocales = sizeof(testLocales)/sizeof(Locale);
145 LOCALES = Locale::getAvailableLocales(nLocales);
148 StringEnumeration *tzids = TimeZone::createEnumeration();
149 int32_t inRaw, inDst;
150 int32_t outRaw, outDst;
152 // Run the roundtrip test
153 for (int32_t locidx = 0; locidx < nLocales; locidx++) {
154 UnicodeString localGMTString;
155 SimpleDateFormat gmtFmt(UnicodeString("ZZZZ"), LOCALES[locidx], status);
156 if (U_FAILURE(status)) {
157 dataerrln("Error creating SimpleDateFormat - %s", u_errorName(status));
160 gmtFmt.setTimeZone(*TimeZone::getGMT());
161 gmtFmt.format(0.0, localGMTString);
163 for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
165 SimpleDateFormat *sdf = new SimpleDateFormat((UnicodeString)PATTERNS[patidx], LOCALES[locidx], status);
166 if (U_FAILURE(status)) {
167 dataerrln((UnicodeString)"new SimpleDateFormat failed for pattern " +
168 PATTERNS[patidx] + " for locale " + LOCALES[locidx].getName() + " - " + u_errorName(status));
169 status = U_ZERO_ERROR;
173 tzids->reset(status);
174 const UnicodeString *tzid;
175 while ((tzid = tzids->snext(status))) {
176 TimeZone *tz = TimeZone::createTimeZone(*tzid);
178 for (int32_t datidx = 0; datidx < nDates; datidx++) {
180 FieldPosition fpos(0);
182 sdf->setTimeZone(*tz);
183 sdf->format(DATES[datidx], tzstr, fpos);
185 // Before parse, set unknown zone to SimpleDateFormat instance
186 // just for making sure that it does not depends on the time zone
188 sdf->setTimeZone(unknownZone);
191 ParsePosition pos(0);
192 Calendar *outcal = Calendar::createInstance(unknownZone, status);
193 if (U_FAILURE(status)) {
194 errln("Failed to create an instance of calendar for receiving parse result.");
195 status = U_ZERO_ERROR;
198 outcal->set(UCAL_DST_OFFSET, badDstOffset);
199 outcal->set(UCAL_ZONE_OFFSET, badZoneOffset);
201 sdf->parse(tzstr, *outcal, pos);
204 const TimeZone &outtz = outcal->getTimeZone();
205 UnicodeString outtzid;
206 outtz.getID(outtzid);
208 tz->getOffset(DATES[datidx], false, inRaw, inDst, status);
209 if (U_FAILURE(status)) {
210 errln((UnicodeString)"Failed to get offsets from time zone" + *tzid);
211 status = U_ZERO_ERROR;
213 outtz.getOffset(DATES[datidx], false, outRaw, outDst, status);
214 if (U_FAILURE(status)) {
215 errln((UnicodeString)"Failed to get offsets from time zone" + outtzid);
216 status = U_ZERO_ERROR;
219 if (uprv_strcmp(PATTERNS[patidx], "V") == 0) {
220 // Short zone ID - should support roundtrip for canonical CLDR IDs
221 UnicodeString canonicalID;
222 TimeZone::getCanonicalID(*tzid, canonicalID, status);
223 if (U_FAILURE(status)) {
224 // Uknown ID - we should not get here
225 errln((UnicodeString)"Unknown ID " + *tzid);
226 status = U_ZERO_ERROR;
227 } else if (outtzid != canonicalID) {
228 if (outtzid.compare(ETC_UNKNOWN, -1) == 0) {
229 // Note that some zones like Asia/Riyadh87 does not have
230 // short zone ID and "unk" is used as fallback
231 logln((UnicodeString)"Canonical round trip failed (probably as expected); tz=" + *tzid
232 + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
233 + ", time=" + DATES[datidx] + ", str=" + tzstr
234 + ", outtz=" + outtzid);
236 errln((UnicodeString)"Canonical round trip failed; tz=" + *tzid
237 + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
238 + ", time=" + DATES[datidx] + ", str=" + tzstr
239 + ", outtz=" + outtzid);
242 } else if (uprv_strcmp(PATTERNS[patidx], "VV") == 0) {
243 // Zone ID - full roundtrip support
244 if (outtzid != *tzid) {
245 errln((UnicodeString)"Zone ID round trip failued; tz=" + *tzid
246 + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
247 + ", time=" + DATES[datidx] + ", str=" + tzstr
248 + ", outtz=" + outtzid);
250 } else if (uprv_strcmp(PATTERNS[patidx], "VVV") == 0 || uprv_strcmp(PATTERNS[patidx], "VVVV") == 0) {
251 // Location: time zone rule must be preserved except
252 // zones not actually associated with a specific location.
253 // Time zones in this category do not have "/" in its ID.
254 UnicodeString canonical;
255 TimeZone::getCanonicalID(*tzid, canonical, status);
256 if (U_FAILURE(status)) {
257 // Uknown ID - we should not get here
258 errln((UnicodeString)"Unknown ID " + *tzid);
259 status = U_ZERO_ERROR;
260 } else if (outtzid != canonical) {
261 // Canonical ID did not match - check the rules
262 if (!((BasicTimeZone*)&outtz)->hasEquivalentTransitions((BasicTimeZone&)*tz, low, high, TRUE, status)) {
263 if (canonical.indexOf((UChar)0x27 /*'/'*/) == -1) {
264 // Exceptional cases, such as CET, EET, MET and WET
265 logln((UnicodeString)"Canonical round trip failed (as expected); tz=" + *tzid
266 + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
267 + ", time=" + DATES[datidx] + ", str=" + tzstr
268 + ", outtz=" + outtzid);
270 errln((UnicodeString)"Canonical round trip failed; tz=" + *tzid
271 + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
272 + ", time=" + DATES[datidx] + ", str=" + tzstr
273 + ", outtz=" + outtzid);
275 if (U_FAILURE(status)) {
276 errln("hasEquivalentTransitions failed");
277 status = U_ZERO_ERROR;
283 UBool isOffsetFormat = (*PATTERNS[patidx] == 'Z'
284 || *PATTERNS[patidx] == 'O'
285 || *PATTERNS[patidx] == 'X'
286 || *PATTERNS[patidx] == 'x');
287 UBool minutesOffset = FALSE;
288 if (*PATTERNS[patidx] == 'X' || *PATTERNS[patidx] == 'x') {
289 minutesOffset = (uprv_strlen(PATTERNS[patidx]) <= 3);
292 if (!isOffsetFormat) {
293 // Check if localized GMT format is used as a fallback of name styles
294 int32_t numDigits = 0;
295 for (int n = 0; n < tzstr.length(); n++) {
296 if (u_isdigit(tzstr.charAt(n))) {
300 isOffsetFormat = (numDigits > 0);
302 if (isOffsetFormat || tzstr == localGMTString) {
303 // Localized GMT or ISO: total offset (raw + dst) must be preserved.
304 int32_t inOffset = inRaw + inDst;
305 int32_t outOffset = outRaw + outDst;
306 int32_t diff = outOffset - inOffset;
308 diff = (diff / 60000) * 60000;
311 errln((UnicodeString)"Offset round trip failed; tz=" + *tzid
312 + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
313 + ", time=" + DATES[datidx] + ", str=" + tzstr
314 + ", inOffset=" + inOffset + ", outOffset=" + outOffset);
317 // Specific or generic: raw offset must be preserved.
318 if (inRaw != outRaw) {
319 errln((UnicodeString)"Raw offset round trip failed; tz=" + *tzid
320 + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
321 + ", time=" + DATES[datidx] + ", str=" + tzstr
322 + ", inRawOffset=" + inRaw + ", outRawOffset=" + outRaw);
341 const Locale* locales; // Static
342 int32_t nLocales; // Static
343 UBool quick; // Static
344 UDate START_TIME; // Static
345 UDate END_TIME; // Static
349 class TestTimeRoundTripThread: public SimpleThread {
351 TestTimeRoundTripThread(IntlTest& tlog, LocaleData &ld, int32_t i)
352 : log(tlog), data(ld), index(i) {}
354 UErrorCode status = U_ZERO_ERROR;
355 UBool REALLY_VERBOSE = FALSE;
357 // These patterns are ambiguous at DST->STD local time overlap
358 const char* AMBIGUOUS_DST_DECESSION[] = { "v", "vvvv", "V", "VV", "VVV", "VVVV", 0 };
360 // These patterns are ambiguous at STD->STD/DST->DST local time overlap
361 const char* AMBIGUOUS_NEGATIVE_SHIFT[] = { "z", "zzzz", "v", "vvvv", "V", "VV", "VVV", "VVVV", 0 };
363 // These patterns only support integer minutes offset
364 const char* MINUTES_OFFSET[] = { "X", "XX", "XXX", "x", "xx", "xxx", 0 };
366 // Workaround for #6338
367 //UnicodeString BASEPATTERN("yyyy-MM-dd'T'HH:mm:ss.SSS");
368 UnicodeString BASEPATTERN("yyyy.MM.dd HH:mm:ss.SSS");
370 // timer for performance analysis
373 UBool expectedRoundTrip[4];
376 StringEnumeration *tzids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status);
377 if (U_FAILURE(status)) {
378 if (status == U_MISSING_RESOURCE_ERROR) {
379 /* This error is generally caused by data not being present. However, an infinite loop will occur
380 * because the thread thinks that the test data is never done so we should treat the data as done.
382 log.dataerrln("TimeZone::createTimeZoneIDEnumeration failed - %s", u_errorName(status));
383 data.numDone = data.nLocales;
385 log.errln("TimeZone::createTimeZoneIDEnumeration failed: %s", u_errorName(status));
391 UDate times[NUM_PATTERNS];
392 for (int32_t i = 0; i < NUM_PATTERNS; i++) {
396 int32_t testCounts = 0;
399 umtx_lock(NULL); // Lock to increment the index
400 for (int32_t i = 0; i < NUM_PATTERNS; i++) {
401 data.times[i] += times[i];
402 data.testCounts += testCounts;
404 if (data.index < data.nLocales) {
410 umtx_unlock(NULL); // Unlock for other threads to use
413 log.logln((UnicodeString) "Thread " + index + " is done.");
417 log.logln((UnicodeString) "\nThread " + index + ": Locale: " + UnicodeString(data.locales[locidx].getName()));
419 for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
420 log.logln((UnicodeString) " Pattern: " + PATTERNS[patidx]);
423 UnicodeString pattern(BASEPATTERN);
424 pattern.append(" ").append(PATTERNS[patidx]);
426 SimpleDateFormat *sdf = new SimpleDateFormat(pattern, data.locales[locidx], status);
427 if (U_FAILURE(status)) {
428 log.errcheckln(status, (UnicodeString) "new SimpleDateFormat failed for pattern " +
429 pattern + " for locale " + data.locales[locidx].getName() + " - " + u_errorName(status));
430 status = U_ZERO_ERROR;
434 UBool minutesOffset = contains(MINUTES_OFFSET, PATTERNS[patidx]);
436 tzids->reset(status);
437 const UnicodeString *tzid;
439 timer = Calendar::getNow();
441 while ((tzid = tzids->snext(status))) {
442 if (uprv_strcmp(PATTERNS[patidx], "V") == 0) {
443 // Some zones do not have short ID assigned, such as Asia/Riyadh87.
444 // The time roundtrip will fail for such zones with pattern "V" (short zone ID).
445 // This is expected behavior.
446 const UChar* shortZoneID = ZoneMeta::getShortID(*tzid);
447 if (shortZoneID == NULL) {
450 } else if (uprv_strcmp(PATTERNS[patidx], "VVV") == 0) {
451 // Some zones are not associated with any region, such as Etc/GMT+8.
452 // The time roundtrip will fail for such zone with pattern "VVV" (exemplar location).
453 // This is expected behavior.
454 if (tzid->indexOf((UChar)0x2F) < 0 || tzid->indexOf(ETC_SLASH, -1, 0) >= 0
455 || tzid->indexOf(SYSTEMV_SLASH, -1, 0) >= 0 || tzid->indexOf(RIYADH8, -1, 0) >= 0) {
460 BasicTimeZone *tz = (BasicTimeZone*) TimeZone::createTimeZone(*tzid);
461 sdf->setTimeZone(*tz);
463 UDate t = data.START_TIME;
464 TimeZoneTransition tzt;
465 UBool tztAvail = FALSE;
468 while (t < data.END_TIME) {
471 expectedRoundTrip[0] = TRUE;
474 int32_t fromOffset = tzt.getFrom()->getRawOffset() + tzt.getFrom()->getDSTSavings();
475 int32_t toOffset = tzt.getTo()->getRawOffset() + tzt.getTo()->getDSTSavings();
476 int32_t delta = toOffset - fromOffset;
478 UBool isDstDecession = tzt.getFrom()->getDSTSavings() > 0 && tzt.getTo()->getDSTSavings() == 0;
479 testTimes[0] = t + delta - 1;
480 expectedRoundTrip[0] = TRUE;
481 testTimes[1] = t + delta;
482 expectedRoundTrip[1] = isDstDecession ?
483 !contains(AMBIGUOUS_DST_DECESSION, PATTERNS[patidx]) :
484 !contains(AMBIGUOUS_NEGATIVE_SHIFT, PATTERNS[patidx]);
485 testTimes[2] = t - 1;
486 expectedRoundTrip[2] = isDstDecession ?
487 !contains(AMBIGUOUS_DST_DECESSION, PATTERNS[patidx]) :
488 !contains(AMBIGUOUS_NEGATIVE_SHIFT, PATTERNS[patidx]);
490 expectedRoundTrip[3] = TRUE;
493 testTimes[0] = t - 1;
494 expectedRoundTrip[0] = TRUE;
496 expectedRoundTrip[1] = TRUE;
500 for (int32_t testidx = 0; testidx < testLen; testidx++) {
502 // reduce regular test time
503 if (!expectedRoundTrip[testidx]) {
511 FieldPosition fpos(0);
512 sdf->format(testTimes[testidx], text, fpos);
514 UDate parsedDate = sdf->parse(text, status);
515 if (U_FAILURE(status)) {
516 log.errln((UnicodeString) "Parse failure for text=" + text + ", tzid=" + *tzid + ", locale=" + data.locales[locidx].getName()
517 + ", pattern=" + PATTERNS[patidx] + ", time=" + testTimes[testidx]);
518 status = U_ZERO_ERROR;
522 int32_t timeDiff = (int32_t)(parsedDate - testTimes[testidx]);
523 UBool bTimeMatch = minutesOffset ?
524 (timeDiff/60000)*60000 == 0 : timeDiff == 0;
526 UnicodeString msg = (UnicodeString) "Time round trip failed for " + "tzid=" + *tzid + ", locale=" + data.locales[locidx].getName() + ", pattern=" + PATTERNS[patidx]
527 + ", text=" + text + ", time=" + testTimes[testidx] + ", restime=" + parsedDate + ", diff=" + (parsedDate - testTimes[testidx]);
528 // Timebomb for TZData update
529 if (expectedRoundTrip[testidx]) {
530 log.errln((UnicodeString) "FAIL: " + msg);
531 } else if (REALLY_VERBOSE) {
536 tztAvail = tz->getNextTransition(t, FALSE, tzt);
541 // Test the date in the middle of two transitions.
542 t += (int64_t) ((tzt.getTime() - t) / 2);
551 times[patidx] += (Calendar::getNow() - timer);
567 TimeZoneFormatTest::TestTimeRoundTrip(void) {
568 int32_t nThreads = threadCount;
569 const Locale *LOCALES;
571 int32_t testCounts = 0;
573 UErrorCode status = U_ZERO_ERROR;
574 Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString) "UTC"), status);
575 if (U_FAILURE(status)) {
576 dataerrln("Calendar::createInstance failed: %s", u_errorName(status));
580 const char* testAllProp = getProperty("TimeZoneRoundTripAll");
581 UBool bTestAll = (testAllProp && uprv_strcmp(testAllProp, "true") == 0);
583 UDate START_TIME, END_TIME;
584 if (bTestAll || !quick) {
585 cal->set(1900, UCAL_JANUARY, 1);
587 cal->set(1990, UCAL_JANUARY, 1);
589 START_TIME = cal->getTime(status);
591 cal->set(2015, UCAL_JANUARY, 1);
592 END_TIME = cal->getTime(status);
594 if (U_FAILURE(status)) {
595 errln("getTime failed");
599 UDate times[NUM_PATTERNS];
600 for (int32_t i = 0; i < NUM_PATTERNS; i++) {
604 // Set up test locales
605 const Locale locales1[] = {Locale("en")};
606 const Locale locales2[] = {
607 Locale("ar_EG"), Locale("bg_BG"), Locale("ca_ES"), Locale("da_DK"), Locale("de"),
608 Locale("de_DE"), Locale("el_GR"), Locale("en"), Locale("en_AU"), Locale("en_CA"),
609 Locale("en_US"), Locale("es"), Locale("es_ES"), Locale("es_MX"), Locale("fi_FI"),
610 Locale("fr"), Locale("fr_CA"), Locale("fr_FR"), Locale("he_IL"), Locale("hu_HU"),
611 Locale("it"), Locale("it_IT"), Locale("ja"), Locale("ja_JP"), Locale("ko"),
612 Locale("ko_KR"), Locale("nb_NO"), Locale("nl_NL"), Locale("nn_NO"), Locale("pl_PL"),
613 Locale("pt"), Locale("pt_BR"), Locale("pt_PT"), Locale("ru_RU"), Locale("sv_SE"),
614 Locale("th_TH"), Locale("tr_TR"), Locale("zh"), Locale("zh_Hans"), Locale("zh_Hans_CN"),
615 Locale("zh_Hant"), Locale("zh_Hant_TW")
619 LOCALES = Locale::getAvailableLocales(nLocales);
622 nLocales = sizeof(locales1)/sizeof(Locale);
625 nLocales = sizeof(locales2)/sizeof(Locale);
630 data.testCounts = testCounts;
632 data.locales = LOCALES;
633 data.nLocales = nLocales;
635 data.START_TIME = START_TIME;
636 data.END_TIME = END_TIME;
639 #if (ICU_USE_THREADS==0)
640 TestTimeRoundTripThread fakeThread(*this, data, 0);
643 TestTimeRoundTripThread **threads = new TestTimeRoundTripThread*[threadCount];
645 for (i = 0; i < nThreads; i++) {
646 threads[i] = new TestTimeRoundTripThread(*this, data, i);
647 if (threads[i]->start() != 0) {
648 errln("Error starting thread %d", i);
655 if (data.numDone == nLocales) {
661 SimpleThread::sleep(1000);
664 for (i = 0; i < nThreads; i++) {
671 logln("### Elapsed time by patterns ###");
672 for (int32_t i = 0; i < NUM_PATTERNS; i++) {
673 logln(UnicodeString("") + data.times[i] + "ms (" + PATTERNS[i] + ")");
674 total += data.times[i];
676 logln((UnicodeString) "Total: " + total + "ms");
677 logln((UnicodeString) "Iteration: " + data.testCounts);
687 UTimeZoneFormatStyle style;
689 const char* expected;
691 UTimeZoneFormatTimeType timeType;
695 TimeZoneFormatTest::TestParse(void) {
696 const ParseTestData DATA[] = {
697 // text inPos locale style parseAll expected outPos timeType
698 {"Z", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, false, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN},
699 {"Z", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, false, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN},
700 {"Zambia time", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, true, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN},
701 {"Zambia time", 0, "en_US", UTZFMT_STYLE_GENERIC_LOCATION, false, "Africa/Lusaka", 11, UTZFMT_TIME_TYPE_UNKNOWN},
702 {"Zambia time", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, true, "Africa/Lusaka", 11, UTZFMT_TIME_TYPE_UNKNOWN},
703 {"+00:00", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, false, "Etc/GMT", 6, UTZFMT_TIME_TYPE_UNKNOWN},
704 {"-01:30:45", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, false, "GMT-01:30:45", 9, UTZFMT_TIME_TYPE_UNKNOWN},
705 {"-7", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, false, "GMT-07:00", 2, UTZFMT_TIME_TYPE_UNKNOWN},
706 {"-2222", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, false, "GMT-22:22", 5, UTZFMT_TIME_TYPE_UNKNOWN},
707 {"-3333", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, false, "GMT-03:33", 4, UTZFMT_TIME_TYPE_UNKNOWN},
708 {"XXX+01:30YYY", 3, "en_US", UTZFMT_STYLE_LOCALIZED_GMT, false, "GMT+01:30", 9, UTZFMT_TIME_TYPE_UNKNOWN},
709 {"GMT0", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "Etc/GMT", 3, UTZFMT_TIME_TYPE_UNKNOWN},
710 {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD},
711 {"ESTx", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD},
712 {"EDTx", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/New_York", 3, UTZFMT_TIME_TYPE_DAYLIGHT},
713 {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, false, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN},
714 {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, true, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD},
715 {"EST", 0, "en_CA", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/Toronto", 3, UTZFMT_TIME_TYPE_STANDARD},
716 {NULL, 0, NULL, UTZFMT_STYLE_GENERIC_LOCATION, false, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN}
719 for (int32_t i = 0; DATA[i].text; i++) {
720 UErrorCode status = U_ZERO_ERROR;
721 LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(Locale(DATA[i].locale), status));
722 if (U_FAILURE(status)) {
723 dataerrln("Fail TimeZoneFormat::createInstance: %s", u_errorName(status));
726 UTimeZoneFormatTimeType ttype = UTZFMT_TIME_TYPE_UNKNOWN;
727 ParsePosition pos(DATA[i].inPos);
728 int32_t parseOptions = DATA[i].parseAll ? UTZFMT_PARSE_OPTION_ALL_STYLES : UTZFMT_PARSE_OPTION_NONE;
729 TimeZone* tz = tzfmt->parse(DATA[i].style, DATA[i].text, pos, parseOptions, &ttype);
731 UnicodeString errMsg;
735 if (outID != UnicodeString(DATA[i].expected)) {
736 errMsg = (UnicodeString)"Time zone ID: " + outID + " - expected: " + DATA[i].expected;
737 } else if (pos.getIndex() != DATA[i].outPos) {
738 errMsg = (UnicodeString)"Parsed pos: " + pos.getIndex() + " - expected: " + DATA[i].outPos;
739 } else if (ttype != DATA[i].timeType) {
740 errMsg = (UnicodeString)"Time type: " + ttype + " - expected: " + DATA[i].timeType;
744 if (DATA[i].expected) {
745 errln((UnicodeString)"Fail: Parse failure - expected: " + DATA[i].expected);
748 if (errMsg.length() > 0) {
749 errln((UnicodeString)"Fail: " + errMsg + " [text=" + DATA[i].text + ", pos=" + DATA[i].inPos + ", style=" + DATA[i].style + "]");
755 TimeZoneFormatTest::TestISOFormat(void) {
756 const int32_t OFFSET[] = {
761 -77777, // -1m 17.777s
765 -37800000, // -10h 30m
766 -37845000, // -10h 30m 45s
770 const char* ISO_STR[][11] = {
773 "Z", "Z", "Z", "Z", "Z",
774 "+00", "+0000", "+00:00", "+0000", "+00:00",
779 "Z", "Z", "Z", "Z", "Z",
780 "+00", "+0000", "+00:00", "+0000", "+00:00",
785 "Z", "Z", "Z", "-000059", "-00:00:59",
786 "+00", "+0000", "+00:00", "-000059", "-00:00:59",
791 "+0001", "+0001", "+00:01", "+0001", "+00:01",
792 "+0001", "+0001", "+00:01", "+0001", "+00:01",
797 "-0001", "-0001", "-00:01", "-000117", "-00:01:17",
798 "-0001", "-0001", "-00:01", "-000117", "-00:01:17",
803 "+0030", "+0030", "+00:30", "+0030", "+00:30",
804 "+0030", "+0030", "+00:30", "+0030", "+00:30",
809 "-01", "-0100", "-01:00", "-0100", "-01:00",
810 "-01", "-0100", "-01:00", "-0100", "-01:00",
815 "+10", "+1000", "+10:00", "+1000", "+10:00",
816 "+10", "+1000", "+10:00", "+1000", "+10:00",
821 "-1030", "-1030", "-10:30", "-1030", "-10:30",
822 "-1030", "-1030", "-10:30", "-1030", "-10:30",
827 "-1030", "-1030", "-10:30", "-103045", "-10:30:45",
828 "-1030", "-1030", "-10:30", "-103045", "-10:30:45",
839 const char* PATTERN[] = {
840 "X", "XX", "XXX", "XXXX", "XXXXX",
841 "x", "xx", "xxx", "xxxx", "xxxxx",
842 "Z", // equivalent to "xxxx"
846 const int32_t MIN_OFFSET_UNIT[] = {
847 60000, 60000, 60000, 1000, 1000,
848 60000, 60000, 60000, 1000, 1000,
853 UErrorCode status = U_ZERO_ERROR;
854 LocalPointer<SimpleDateFormat> sdf(new SimpleDateFormat(status));
855 if (U_FAILURE(status)) {
856 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status));
859 UDate d = Calendar::getNow();
861 for (uint32_t i = 0; i < sizeof(OFFSET)/sizeof(OFFSET[0]); i++) {
862 SimpleTimeZone* tz = new SimpleTimeZone(OFFSET[i], UnicodeString("Zone Offset:") + OFFSET[i] + "ms");
863 sdf->adoptTimeZone(tz);
864 for (int32_t j = 0; PATTERN[j] != 0; j++) {
865 sdf->applyPattern(UnicodeString(PATTERN[j]));
866 UnicodeString result;
867 sdf->format(d, result);
870 if (result != UnicodeString(ISO_STR[i][j])) {
871 errln((UnicodeString)"FAIL: pattern=" + PATTERN[j] + ", offset=" + OFFSET[i] + " -> "
872 + result + " (expected: " + ISO_STR[i][j] + ")");
875 // Offset out of range
876 // Note: for now, there is no way to propagate the error status through
877 // the SimpleDateFormat::format above.
878 if (result.length() > 0) {
879 errln((UnicodeString)"FAIL: Non-Empty result for pattern=" + PATTERN[j] + ", offset=" + OFFSET[i]
880 + " (expected: empty result)");
887 LocalPointer<Calendar> outcal(Calendar::createInstance(status));
888 if (U_FAILURE(status)) {
889 dataerrln("Fail new Calendar: %s", u_errorName(status));
892 for (int32_t i = 0; ISO_STR[i][0] != NULL; i++) {
893 for (int32_t j = 0; PATTERN[j] != 0; j++) {
894 if (ISO_STR[i][j] == 0) {
897 ParsePosition pos(0);
898 SimpleTimeZone* bogusTZ = new SimpleTimeZone(-1, UnicodeString("Zone Offset: -1ms"));
899 outcal->adoptTimeZone(bogusTZ);
900 sdf->applyPattern(PATTERN[j]);
902 sdf->parse(UnicodeString(ISO_STR[i][j]), *(outcal.getAlias()), pos);
904 if (pos.getIndex() != (int32_t)uprv_strlen(ISO_STR[i][j])) {
905 errln((UnicodeString)"FAIL: Failed to parse the entire input string: " + ISO_STR[i][j]);
908 const TimeZone& outtz = outcal->getTimeZone();
909 int32_t outOffset = outtz.getRawOffset();
910 int32_t adjustedOffset = OFFSET[i] / MIN_OFFSET_UNIT[j] * MIN_OFFSET_UNIT[j];
911 if (outOffset != adjustedOffset) {
912 errln((UnicodeString)"FAIL: Incorrect offset:" + outOffset + "ms for input string: " + ISO_STR[i][j]
913 + " (expected:" + adjustedOffset + "ms)");
924 UTimeZoneFormatStyle style;
925 const char* expected;
926 UTimeZoneFormatTimeType timeType;
930 TimeZoneFormatTest::TestFormat(void) {
931 UDate dateJan = 1358208000000.0; // 2013-01-15T00:00:00Z
932 UDate dateJul = 1373846400000.0; // 2013-07-15T00:00:00Z
934 const FormatTestData DATA[] = {
937 "America/Los_Angeles",
939 UTZFMT_STYLE_GENERIC_LOCATION,
941 UTZFMT_TIME_TYPE_UNKNOWN
945 "America/Los_Angeles",
947 UTZFMT_STYLE_GENERIC_LONG,
949 UTZFMT_TIME_TYPE_UNKNOWN
953 "America/Los_Angeles",
955 UTZFMT_STYLE_SPECIFIC_LONG,
956 "Pacific Standard Time",
957 UTZFMT_TIME_TYPE_STANDARD
961 "America/Los_Angeles",
963 UTZFMT_STYLE_SPECIFIC_LONG,
964 "Pacific Daylight Time",
965 UTZFMT_TIME_TYPE_DAYLIGHT
969 "America/Los_Angeles",
971 UTZFMT_STYLE_ZONE_ID,
972 "America/Los_Angeles",
973 UTZFMT_TIME_TYPE_UNKNOWN
977 "America/Los_Angeles",
979 UTZFMT_STYLE_ZONE_ID_SHORT,
981 UTZFMT_TIME_TYPE_UNKNOWN
985 "America/Los_Angeles",
987 UTZFMT_STYLE_EXEMPLAR_LOCATION,
989 UTZFMT_TIME_TYPE_UNKNOWN
996 UTZFMT_STYLE_GENERIC_LONG,
997 "\\u65E5\\u672C\\u6A19\\u6E96\\u6642",
998 UTZFMT_TIME_TYPE_UNKNOWN
1001 {0, 0, 0.0, UTZFMT_STYLE_GENERIC_LOCATION, 0, UTZFMT_TIME_TYPE_UNKNOWN}
1004 for (int32_t i = 0; DATA[i].locale; i++) {
1005 UErrorCode status = U_ZERO_ERROR;
1006 LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(Locale(DATA[i].locale), status));
1007 if (U_FAILURE(status)) {
1008 dataerrln("Fail TimeZoneFormat::createInstance: %s", u_errorName(status));
1012 LocalPointer<TimeZone> tz(TimeZone::createTimeZone(DATA[i].tzid));
1014 UTimeZoneFormatTimeType timeType;
1016 tzfmt->format(DATA[i].style, *(tz.getAlias()), DATA[i].date, out, &timeType);
1017 UnicodeString expected(DATA[i].expected, -1, US_INV);
1018 expected = expected.unescape();
1020 assertEquals(UnicodeString("Format result for ") + DATA[i].tzid + " (Test Case " + i + ")", expected, out);
1021 if (DATA[i].timeType != timeType) {
1022 dataerrln(UnicodeString("Formatted time zone type (Test Case ") + i + "), returned="
1023 + timeType + ", expected=" + DATA[i].timeType);
1028 #endif /* #if !UCONFIG_NO_FORMATTING */