1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /***********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 1997-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***********************************************************************/
8
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_FORMATTING
12
13 #include "unicode/timezone.h"
14 #include "unicode/simpletz.h"
15 #include "unicode/calendar.h"
16 #include "unicode/gregocal.h"
17 #include "unicode/localpointer.h"
18 #include "unicode/resbund.h"
19 #include "unicode/strenum.h"
20 #include "unicode/uversion.h"
21 #include "tztest.h"
22 #include "cmemory.h"
23 #include "putilimp.h"
24 #include "cstring.h"
25 #include "olsontz.h"
26
27 #define CASE(id,test) case id: \
28 name = #test; \
29 if (exec) { \
30 logln(#test "---"); logln(""); \
31 test(); \
32 } \
33 break
34
35 // *****************************************************************************
36 // class TimeZoneTest
37 // *****************************************************************************
38
39 // Some test case data is current date/tzdata version sensitive and producing errors
40 // when year/rule are changed. Although we want to keep our eyes on test failures
41 // caused by tzdata changes while development, keep maintaining test data in maintenance
42 // stream is a little bit hassle. ICU 49 or later versions are using minor version field
43 // to indicate a development build (0) or official release build (others). For development
44 // builds, a test failure triggers an error, while release builds only report them in
45 // verbose mode with logln.
46 static UBool isDevelopmentBuild = (U_ICU_VERSION_MINOR_NUM == 0);
47
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)48 void TimeZoneTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
49 {
50 if (exec) {
51 logln("TestSuite TestTimeZone");
52 }
53 TESTCASE_AUTO_BEGIN;
54 TESTCASE_AUTO(TestPRTOffset);
55 TESTCASE_AUTO(TestVariousAPI518);
56 TESTCASE_AUTO(TestGetAvailableIDs913);
57 TESTCASE_AUTO(TestGenericAPI);
58 TESTCASE_AUTO(TestRuleAPI);
59 TESTCASE_AUTO(TestShortZoneIDs);
60 TESTCASE_AUTO(TestCustomParse);
61 TESTCASE_AUTO(TestDisplayName);
62 TESTCASE_AUTO(TestDSTSavings);
63 TESTCASE_AUTO(TestAlternateRules);
64 TESTCASE_AUTO(TestCountries);
65 TESTCASE_AUTO(TestHistorical);
66 TESTCASE_AUTO(TestEquivalentIDs);
67 TESTCASE_AUTO(TestAliasedNames);
68 TESTCASE_AUTO(TestFractionalDST);
69 TESTCASE_AUTO(TestFebruary);
70 TESTCASE_AUTO(TestCanonicalIDAPI);
71 TESTCASE_AUTO(TestCanonicalID);
72 TESTCASE_AUTO(TestDisplayNamesMeta);
73 TESTCASE_AUTO(TestGetRegion);
74 TESTCASE_AUTO(TestGetAvailableIDsNew);
75 TESTCASE_AUTO(TestGetUnknown);
76 TESTCASE_AUTO(TestGetGMT);
77 TESTCASE_AUTO(TestGetWindowsID);
78 TESTCASE_AUTO(TestGetIDForWindowsID);
79 TESTCASE_AUTO(TestCasablancaNameAndOffset22041);
80 TESTCASE_AUTO(TestRawOffsetAndOffsetConsistency22041);
81 TESTCASE_AUTO_END;
82 }
83
84 const int32_t TimeZoneTest::millisPerHour = 3600000;
85
86 // ---------------------------------------------------------------------------------
87
88 /**
89 * Generic API testing for API coverage.
90 */
91 void
TestGenericAPI()92 TimeZoneTest::TestGenericAPI()
93 {
94 UnicodeString id("NewGMT");
95 int32_t offset = 12345;
96
97 SimpleTimeZone *zone = new SimpleTimeZone(offset, id);
98 if (zone->useDaylightTime()) errln("FAIL: useDaylightTime should return false");
99
100 TimeZone* zoneclone = zone->clone();
101 if (!(*zoneclone == *zone)) errln("FAIL: clone or operator== failed");
102 zoneclone->setID("abc");
103 if (!(*zoneclone != *zone)) errln("FAIL: clone or operator!= failed");
104 delete zoneclone;
105
106 zoneclone = zone->clone();
107 if (!(*zoneclone == *zone)) errln("FAIL: clone or operator== failed");
108 zoneclone->setRawOffset(45678);
109 if (!(*zoneclone != *zone)) errln("FAIL: clone or operator!= failed");
110
111 SimpleTimeZone copy(*zone);
112 if (!(copy == *zone)) errln("FAIL: copy constructor or operator== failed");
113 copy = *dynamic_cast<SimpleTimeZone*>(zoneclone);
114 if (!(copy == *zoneclone)) errln("FAIL: assignment operator or operator== failed");
115
116 TimeZone* saveDefault = TimeZone::createDefault();
117 logln((UnicodeString)"TimeZone::createDefault() => " + saveDefault->getID(id));
118
119 TimeZone::adoptDefault(zone);
120 TimeZone* defaultzone = TimeZone::createDefault();
121 if (defaultzone == zone ||
122 !(*defaultzone == *zone))
123 errln("FAIL: createDefault failed");
124 TimeZone::adoptDefault(saveDefault);
125 delete defaultzone;
126 delete zoneclone;
127
128 logln("call uprv_timezone() which uses the host");
129 logln("to get the difference in seconds between coordinated universal");
130 logln("time and local time. E.g., -28,800 for PST (GMT-8hrs)");
131
132 int32_t tzoffset = uprv_timezone();
133 if ((tzoffset % 900) != 0) {
134 /*
135 * Ticket#6364 and #7648
136 * A few time zones are using GMT offests not a multiple of 15 minutes.
137 * Therefore, we should not interpret such case as an error.
138 * We downgrade this from errln to infoln. When we see this message,
139 * we should examine if it is ignorable or not.
140 */
141 infoln("WARNING: t_timezone may be incorrect. It is not a multiple of 15min.", tzoffset);
142 }
143
144 TimeZone* hostZone = TimeZone::detectHostTimeZone();
145 int32_t hostZoneRawOffset = hostZone->getRawOffset();
146 logln("hostZone->getRawOffset() = %d , tzoffset = %d", hostZoneRawOffset, tzoffset * (-1000));
147
148 /* Host time zone's offset should match the offset returned by uprv_timezone() */
149 if (hostZoneRawOffset != tzoffset * (-1000)) {
150 UnicodeString id;
151 hostZone->getID(id);
152 // Known issues in ICU-22274 we have issues in time zone
153 // Africa/Casablanca Europe/Dublin America/Godthab America/Nuuk
154 if (id == u"Africa/Casablanca" || id == u"Europe/Dublin" ||
155 id == u"America/Godthab" || id == u"America/Nuuk" ||
156 id == u"Africa/El_Aaiun") {
157 logKnownIssue( "ICU-22274", "detectHostTimeZone()'s raw offset != host timezone's offset in TimeZone " + id);
158 } else {
159 errln("FAIL: detectHostTimeZone()'s raw offset != host timezone's offset");
160 }
161 }
162 delete hostZone;
163
164 UErrorCode status = U_ZERO_ERROR;
165 const char* tzver = TimeZone::getTZDataVersion(status);
166 if (U_FAILURE(status)) {
167 errcheckln(status, "FAIL: getTZDataVersion failed - %s", u_errorName(status));
168 } else {
169 int32_t tzverLen = uprv_strlen(tzver);
170 if (tzverLen == 5 || tzverLen == 6 /* 4 digits + 1 or 2 letters */) {
171 logln((UnicodeString)"tzdata version: " + tzver);
172 } else {
173 errln((UnicodeString)"FAIL: getTZDataVersion returned " + tzver);
174 }
175 }
176 }
177
178 // ---------------------------------------------------------------------------------
179
180 /**
181 * Test the setStartRule/setEndRule API calls.
182 */
183 void
TestRuleAPI()184 TimeZoneTest::TestRuleAPI()
185 {
186 UErrorCode status = U_ZERO_ERROR;
187
188 UDate offset = 60*60*1000*1.75; // Pick a weird offset
189 SimpleTimeZone *zone = new SimpleTimeZone((int32_t)offset, "TestZone");
190 if (zone->useDaylightTime()) errln("FAIL: useDaylightTime should return false");
191
192 // Establish our expected transition times. Do this with a non-DST
193 // calendar with the (above) declared local offset.
194 GregorianCalendar *gc = new GregorianCalendar(*zone, status);
195 if (failure(status, "new GregorianCalendar", true)) return;
196 gc->clear();
197 gc->set(1990, UCAL_MARCH, 1);
198 UDate marchOneStd = gc->getTime(status); // Local Std time midnight
199 gc->clear();
200 gc->set(1990, UCAL_JULY, 1);
201 UDate julyOneStd = gc->getTime(status); // Local Std time midnight
202 if (failure(status, "GregorianCalendar::getTime")) return;
203
204 // Starting and ending hours, WALL TIME
205 int32_t startHour = (int32_t)(2.25 * 3600000);
206 int32_t endHour = (int32_t)(3.5 * 3600000);
207
208 zone->setStartRule(UCAL_MARCH, 1, 0, startHour, status);
209 zone->setEndRule (UCAL_JULY, 1, 0, endHour, status);
210
211 delete gc;
212 gc = new GregorianCalendar(*zone, status);
213 if (failure(status, "new GregorianCalendar")) return;
214
215 UDate marchOne = marchOneStd + startHour;
216 UDate julyOne = julyOneStd + endHour - 3600000; // Adjust from wall to Std time
217
218 UDate expMarchOne = 636251400000.0;
219 if (marchOne != expMarchOne)
220 {
221 errln((UnicodeString)"FAIL: Expected start computed as " + marchOne +
222 " = " + dateToString(marchOne));
223 logln((UnicodeString)" Should be " + expMarchOne +
224 " = " + dateToString(expMarchOne));
225 }
226
227 UDate expJulyOne = 646793100000.0;
228 if (julyOne != expJulyOne)
229 {
230 errln((UnicodeString)"FAIL: Expected start computed as " + julyOne +
231 " = " + dateToString(julyOne));
232 logln((UnicodeString)" Should be " + expJulyOne +
233 " = " + dateToString(expJulyOne));
234 }
235
236 testUsingBinarySearch(*zone, date(90, UCAL_JANUARY, 1), date(90, UCAL_JUNE, 15), marchOne);
237 testUsingBinarySearch(*zone, date(90, UCAL_JUNE, 1), date(90, UCAL_DECEMBER, 31), julyOne);
238
239 if (zone->inDaylightTime(marchOne - 1000, status) ||
240 !zone->inDaylightTime(marchOne, status))
241 errln("FAIL: Start rule broken");
242 if (!zone->inDaylightTime(julyOne - 1000, status) ||
243 zone->inDaylightTime(julyOne, status))
244 errln("FAIL: End rule broken");
245
246 zone->setStartYear(1991);
247 if (zone->inDaylightTime(marchOne, status) ||
248 zone->inDaylightTime(julyOne - 1000, status))
249 errln("FAIL: Start year broken");
250
251 failure(status, "TestRuleAPI");
252 delete gc;
253 delete zone;
254 }
255
256 void
findTransition(const TimeZone & tz,UDate min,UDate max)257 TimeZoneTest::findTransition(const TimeZone& tz,
258 UDate min, UDate max) {
259 UErrorCode ec = U_ZERO_ERROR;
260 UnicodeString id,s;
261 UBool startsInDST = tz.inDaylightTime(min, ec);
262 if (failure(ec, "TimeZone::inDaylightTime")) return;
263 if (tz.inDaylightTime(max, ec) == startsInDST) {
264 logln("Error: " + tz.getID(id) + ".inDaylightTime(" + dateToString(min) + ") = " + (startsInDST?"true":"false") +
265 ", inDaylightTime(" + dateToString(max) + ") = " + (startsInDST?"true":"false"));
266 return;
267 }
268 if (failure(ec, "TimeZone::inDaylightTime")) return;
269 while ((max - min) > INTERVAL) {
270 UDate mid = (min + max) / 2;
271 if (tz.inDaylightTime(mid, ec) == startsInDST) {
272 min = mid;
273 } else {
274 max = mid;
275 }
276 if (failure(ec, "TimeZone::inDaylightTime")) return;
277 }
278 min = 1000.0 * uprv_floor(min/1000.0);
279 max = 1000.0 * uprv_floor(max/1000.0);
280 logln(tz.getID(id) + " Before: " + min/1000 + " = " +
281 dateToString(min,s,tz));
282 logln(tz.getID(id) + " After: " + max/1000 + " = " +
283 dateToString(max,s,tz));
284 }
285
286 void
testUsingBinarySearch(const TimeZone & tz,UDate min,UDate max,UDate expectedBoundary)287 TimeZoneTest::testUsingBinarySearch(const TimeZone& tz,
288 UDate min, UDate max,
289 UDate expectedBoundary)
290 {
291 UErrorCode status = U_ZERO_ERROR;
292 UBool startsInDST = tz.inDaylightTime(min, status);
293 if (failure(status, "TimeZone::inDaylightTime")) return;
294 if (tz.inDaylightTime(max, status) == startsInDST) {
295 logln("Error: inDaylightTime(" + dateToString(max) + ") != " + ((!startsInDST)?"true":"false"));
296 return;
297 }
298 if (failure(status, "TimeZone::inDaylightTime")) return;
299 while ((max - min) > INTERVAL) {
300 UDate mid = (min + max) / 2;
301 if (tz.inDaylightTime(mid, status) == startsInDST) {
302 min = mid;
303 } else {
304 max = mid;
305 }
306 if (failure(status, "TimeZone::inDaylightTime")) return;
307 }
308 logln(UnicodeString("Binary Search Before: ") + uprv_floor(0.5 + min) + " = " + dateToString(min));
309 logln(UnicodeString("Binary Search After: ") + uprv_floor(0.5 + max) + " = " + dateToString(max));
310 UDate mindelta = expectedBoundary - min;
311 UDate maxdelta = max - expectedBoundary;
312 if (mindelta >= 0 &&
313 mindelta <= INTERVAL &&
314 maxdelta >= 0 &&
315 maxdelta <= INTERVAL)
316 logln(UnicodeString("PASS: Expected bdry: ") + expectedBoundary + " = " + dateToString(expectedBoundary));
317 else
318 errln(UnicodeString("FAIL: Expected bdry: ") + expectedBoundary + " = " + dateToString(expectedBoundary));
319 }
320
321 const UDate TimeZoneTest::INTERVAL = 100;
322
323 // ---------------------------------------------------------------------------------
324
325 // -------------------------------------
326
327 /**
328 * Test the offset of the PRT timezone.
329 */
330 void
TestPRTOffset()331 TimeZoneTest::TestPRTOffset()
332 {
333 TimeZone* tz = TimeZone::createTimeZone("PRT");
334 if (tz == 0) {
335 errln("FAIL: TimeZone(PRT) is null");
336 }
337 else {
338 int32_t expectedHour = -4;
339 double expectedOffset = (((double)expectedHour) * millisPerHour);
340 double foundOffset = tz->getRawOffset();
341 int32_t foundHour = (int32_t)foundOffset / millisPerHour;
342 if (expectedOffset != foundOffset) {
343 dataerrln("FAIL: Offset for PRT should be %d, found %d", expectedHour, foundHour);
344 } else {
345 logln("PASS: Offset for PRT should be %d, found %d", expectedHour, foundHour);
346 }
347 }
348 delete tz;
349 }
350
351 // -------------------------------------
352
353 /**
354 * Regress a specific bug with a sequence of API calls.
355 */
356 void
TestVariousAPI518()357 TimeZoneTest::TestVariousAPI518()
358 {
359 UErrorCode status = U_ZERO_ERROR;
360 TimeZone* time_zone = TimeZone::createTimeZone("PST");
361 UDate d = date(97, UCAL_APRIL, 30);
362 UnicodeString str;
363 logln("The timezone is " + time_zone->getID(str));
364 if (!time_zone->inDaylightTime(d, status)) dataerrln("FAIL: inDaylightTime returned false");
365 if (failure(status, "TimeZone::inDaylightTime", true)) return;
366 if (!time_zone->useDaylightTime()) dataerrln("FAIL: useDaylightTime returned false");
367 if (time_zone->getRawOffset() != - 8 * millisPerHour) dataerrln("FAIL: getRawOffset returned wrong value");
368 GregorianCalendar *gc = new GregorianCalendar(status);
369 if (U_FAILURE(status)) { errln("FAIL: Couldn't create GregorianCalendar"); return; }
370 gc->setTime(d, status);
371 if (U_FAILURE(status)) { errln("FAIL: GregorianCalendar::setTime failed"); return; }
372 if (time_zone->getOffset(gc->AD, gc->get(UCAL_YEAR, status), gc->get(UCAL_MONTH, status),
373 gc->get(UCAL_DATE, status), (uint8_t)gc->get(UCAL_DAY_OF_WEEK, status), 0, status) != - 7 * millisPerHour)
374 dataerrln("FAIL: getOffset returned wrong value");
375 if (U_FAILURE(status)) { errln("FAIL: GregorianCalendar::set failed"); return; }
376 delete gc;
377 delete time_zone;
378 }
379
380 // -------------------------------------
381
382 /**
383 * Test the call which retrieves the available IDs.
384 */
385 void
TestGetAvailableIDs913()386 TimeZoneTest::TestGetAvailableIDs913()
387 {
388 UErrorCode ec = U_ZERO_ERROR;
389 int32_t i;
390
391 #ifdef U_USE_TIMEZONE_OBSOLETE_2_8
392 // Test legacy API -- remove these tests when the corresponding API goes away (duh)
393 int32_t numIDs = -1;
394 const UnicodeString** ids = TimeZone::createAvailableIDs(numIDs);
395 if (ids == 0 || numIDs < 1) {
396 errln("FAIL: createAvailableIDs()");
397 } else {
398 UnicodeString buf("TimeZone::createAvailableIDs() = { ");
399 for(i=0; i<numIDs; ++i) {
400 if (i) buf.append(", ");
401 buf.append(*ids[i]);
402 }
403 buf.append(" } ");
404 logln(buf + numIDs);
405 // we own the array; the caller owns the contained strings (yuck)
406 uprv_free(ids);
407 }
408
409 numIDs = -1;
410 ids = TimeZone::createAvailableIDs(-8*U_MILLIS_PER_HOUR, numIDs);
411 if (ids == 0 || numIDs < 1) {
412 errln("FAIL: createAvailableIDs(-8:00)");
413 } else {
414 UnicodeString buf("TimeZone::createAvailableIDs(-8:00) = { ");
415 for(i=0; i<numIDs; ++i) {
416 if (i) buf.append(", ");
417 buf.append(*ids[i]);
418 }
419 buf.append(" } ");
420 logln(buf + numIDs);
421 // we own the array; the caller owns the contained strings (yuck)
422 uprv_free(ids);
423 }
424 numIDs = -1;
425 ids = TimeZone::createAvailableIDs("US", numIDs);
426 if (ids == 0 || numIDs < 1) {
427 errln("FAIL: createAvailableIDs(US) ids=%d, numIDs=%d", ids, numIDs);
428 } else {
429 UnicodeString buf("TimeZone::createAvailableIDs(US) = { ");
430 for(i=0; i<numIDs; ++i) {
431 if (i) buf.append(", ");
432 buf.append(*ids[i]);
433 }
434 buf.append(" } ");
435 logln(buf + numIDs);
436 // we own the array; the caller owns the contained strings (yuck)
437 uprv_free(ids);
438 }
439 #endif
440
441 UnicodeString str;
442 UnicodeString buf(u"TimeZone::createEnumeration() = { ");
443 int32_t s_length;
444 StringEnumeration* s = TimeZone::createEnumeration(ec);
445 LocalPointer<StringEnumeration> tmp1(TimeZone::createEnumeration(), ec);
446 if (U_FAILURE(ec) || s == nullptr) {
447 dataerrln("Unable to create TimeZone enumeration");
448 return;
449 }
450 s_length = s->count(ec);
451 if (s_length != tmp1->count(ec)) {
452 errln("TimeZone::createEnumeration() with no status args returns a different count.");
453 }
454 for (i = 0; i < s_length;++i) {
455 if (i > 0) buf += ", ";
456 if ((i & 1) == 0) {
457 buf += *s->snext(ec);
458 } else {
459 buf += UnicodeString(s->next(nullptr, ec), "");
460 }
461
462 if((i % 5) == 4) {
463 // replace s with a clone of itself
464 StringEnumeration *s2 = s->clone();
465 if(s2 == nullptr || s_length != s2->count(ec)) {
466 errln("TimezoneEnumeration.clone() failed");
467 } else {
468 delete s;
469 s = s2;
470 }
471 }
472 }
473 buf += " };";
474 logln(buf);
475
476 /* Confirm that the following zones can be retrieved: The first
477 * zone, the last zone, and one in-between. This tests the binary
478 * search through the system zone data.
479 */
480 s->reset(ec);
481 int32_t middle = s_length/2;
482 for (i=0; i<s_length; ++i) {
483 const UnicodeString* id = s->snext(ec);
484 if (i==0 || i==middle || i==(s_length-1)) {
485 TimeZone *z = TimeZone::createTimeZone(*id);
486 if (z == 0) {
487 errln(UnicodeString("FAIL: createTimeZone(") +
488 *id + ") -> 0");
489 } else if (z->getID(str) != *id) {
490 errln(UnicodeString("FAIL: createTimeZone(") +
491 *id + ") -> zone " + str);
492 } else {
493 logln(UnicodeString("OK: createTimeZone(") +
494 *id + ") succeeded");
495 }
496 delete z;
497 }
498 }
499 delete s;
500
501 buf.truncate(0);
502 buf += "TimeZone::createEnumeration(GMT+01:00) = { ";
503
504 s = TimeZone::createEnumerationForRawOffset(1 * U_MILLIS_PER_HOUR, ec);
505 LocalPointer<StringEnumeration> tmp2(TimeZone::createEnumeration(1 * U_MILLIS_PER_HOUR), ec);
506 if (U_FAILURE(ec)) {
507 dataerrln("Unable to create TimeZone enumeration for GMT+1");
508 return;
509 }
510 s_length = s->count(ec);
511 if (s_length != tmp2->count(ec)) {
512 errln("TimeZone::createEnumeration(GMT+01:00) with no status args returns a different count.");
513 }
514 for (i = 0; i < s_length;++i) {
515 if (i > 0) buf += ", ";
516 buf += *s->snext(ec);
517 }
518 delete s;
519 buf += " };";
520 logln(buf);
521
522
523 buf.truncate(0);
524 buf += "TimeZone::createEnumeration(US) = { ";
525
526 s = TimeZone::createEnumerationForRegion("US", ec);
527 LocalPointer<StringEnumeration> tmp3(TimeZone::createEnumeration("US"), ec);
528 if (U_FAILURE(ec)) {
529 dataerrln("Unable to create TimeZone enumeration for US");
530 return;
531 }
532 s_length = s->count(ec);
533 if (s_length != tmp3->count(ec)) {
534 errln("TimeZone::createEnumeration(\"US\") with no status args returns a different count.");
535 }
536 for (i = 0; i < s_length; ++i) {
537 if (i > 0) buf += ", ";
538 buf += *s->snext(ec);
539 }
540 buf += " };";
541 logln(buf);
542
543 TimeZone *tz = TimeZone::createTimeZone("PST");
544 if (tz != 0) logln("getTimeZone(PST) = " + tz->getID(str));
545 else errln("FAIL: getTimeZone(PST) = null");
546 delete tz;
547 tz = TimeZone::createTimeZone("America/Los_Angeles");
548 if (tz != 0) logln("getTimeZone(America/Los_Angeles) = " + tz->getID(str));
549 else errln("FAIL: getTimeZone(PST) = null");
550 delete tz;
551
552 // @bug 4096694
553 tz = TimeZone::createTimeZone("NON_EXISTENT");
554 UnicodeString temp;
555 if (tz == 0)
556 errln("FAIL: getTimeZone(NON_EXISTENT) = null");
557 else if (tz->getID(temp) != UCAL_UNKNOWN_ZONE_ID)
558 errln("FAIL: getTimeZone(NON_EXISTENT) = " + temp);
559 delete tz;
560
561 delete s;
562 }
563
564 void
TestGetAvailableIDsNew()565 TimeZoneTest::TestGetAvailableIDsNew()
566 {
567 UErrorCode ec = U_ZERO_ERROR;
568 StringEnumeration *any, *canonical, *canonicalLoc;
569 StringEnumeration *any_US, *canonical_US, *canonicalLoc_US;
570 StringEnumeration *any_W5, *any_CA_W5;
571 StringEnumeration *any_US_E14;
572 int32_t rawOffset;
573 const UnicodeString *id1, *id2;
574 UnicodeString canonicalID;
575 UBool isSystemID;
576 char region[4] = {0};
577 int32_t zoneCount;
578
579 any = canonical = canonicalLoc = any_US = canonical_US = canonicalLoc_US = any_W5 = any_CA_W5 = any_US_E14 = nullptr;
580
581 any = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, nullptr, nullptr, ec);
582 if (U_FAILURE(ec)) {
583 dataerrln("Failed to create enumeration for ANY");
584 goto cleanup;
585 }
586
587 canonical = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, nullptr, nullptr, ec);
588 if (U_FAILURE(ec)) {
589 errln("Failed to create enumeration for CANONICAL");
590 goto cleanup;
591 }
592
593 canonicalLoc = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, nullptr, nullptr, ec);
594 if (U_FAILURE(ec)) {
595 errln("Failed to create enumeration for CANONICALLOC");
596 goto cleanup;
597 }
598
599 any_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", nullptr, ec);
600 if (U_FAILURE(ec)) {
601 errln("Failed to create enumeration for ANY_US");
602 goto cleanup;
603 }
604
605 canonical_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, "US", nullptr, ec);
606 if (U_FAILURE(ec)) {
607 errln("Failed to create enumeration for CANONICAL_US");
608 goto cleanup;
609 }
610
611 canonicalLoc_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, "US", nullptr, ec);
612 if (U_FAILURE(ec)) {
613 errln("Failed to create enumeration for CANONICALLOC_US");
614 goto cleanup;
615 }
616
617 rawOffset = (-5)*60*60*1000;
618 any_W5 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, nullptr, &rawOffset, ec);
619 if (U_FAILURE(ec)) {
620 errln("Failed to create enumeration for ANY_W5");
621 goto cleanup;
622 }
623
624 any_CA_W5 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "CA", &rawOffset, ec);
625 if (U_FAILURE(ec)) {
626 errln("Failed to create enumeration for ANY_CA_W5");
627 goto cleanup;
628 }
629
630 rawOffset = 14*60*60*1000;
631 any_US_E14 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", &rawOffset, ec);
632 if (U_FAILURE(ec)) {
633 errln("Failed to create enumeration for ANY_US_E14");
634 goto cleanup;
635 }
636
637 checkContainsAll(any, "ANY", canonical, "CANONICAL");
638 checkContainsAll(canonical, "CANONICAL", canonicalLoc, "CANONICALLOC");
639
640 checkContainsAll(any, "ANY", any_US, "ANY_US");
641 checkContainsAll(canonical, "CANONICAL", canonical_US, "CANONICAL_US");
642 checkContainsAll(canonicalLoc, "CANONICALLOC", canonicalLoc_US, "CANONICALLOC_US");
643
644 checkContainsAll(any_US, "ANY_US", canonical_US, "CANONICAL_US");
645 checkContainsAll(canonical_US, "CANONICAL_US", canonicalLoc_US, "CANONICALLOC_US");
646
647 checkContainsAll(any, "ANY", any_W5, "ANY_W5");
648 checkContainsAll(any_W5, "ANY_W5", any_CA_W5, "ANY_CA_W5");
649
650 // And ID in any set, but not in canonical set must not be a canonical ID
651 any->reset(ec);
652 while ((id1 = any->snext(ec)) != nullptr) {
653 UBool found = false;
654 canonical->reset(ec);
655 while ((id2 = canonical->snext(ec)) != nullptr) {
656 if (*id1 == *id2) {
657 found = true;
658 break;
659 }
660 }
661 if (U_FAILURE(ec)) {
662 break;
663 }
664 if (!found) {
665 TimeZone::getCanonicalID(*id1, canonicalID, isSystemID, ec);
666 if (U_FAILURE(ec)) {
667 break;
668 }
669 if (*id1 == canonicalID) {
670 errln((UnicodeString)"FAIL: canonicalID [" + *id1 + "] is not in CANONICAL");
671 }
672 if (!isSystemID) {
673 errln((UnicodeString)"FAIL: ANY contains non-system ID: " + *id1);
674 }
675 }
676 }
677 if (U_FAILURE(ec)) {
678 errln("Error checking IDs in ANY, but not in CANONICAL");
679 ec = U_ZERO_ERROR;
680 }
681
682 // canonical set must contains only canonical IDs
683 canonical->reset(ec);
684 while ((id1 = canonical->snext(ec)) != nullptr) {
685 TimeZone::getCanonicalID(*id1, canonicalID, isSystemID, ec);
686 if (U_FAILURE(ec)) {
687 break;
688 }
689 if (*id1 != canonicalID) {
690 errln((UnicodeString)"FAIL: CANONICAL contains non-canonical ID: " + *id1);
691 }
692 if (!isSystemID) {
693 errln((UnicodeString)"FAILE: CANONICAL contains non-system ID: " + *id1);
694 }
695 }
696 if (U_FAILURE(ec)) {
697 errln("Error checking IDs in CANONICAL");
698 ec = U_ZERO_ERROR;
699 }
700
701 // canonicalLoc set must contain only canonical location IDs
702 canonicalLoc->reset(ec);
703 while ((id1 = canonicalLoc->snext(ec)) != nullptr) {
704 TimeZone::getRegion(*id1, region, sizeof(region), ec);
705 if (U_FAILURE(ec)) {
706 break;
707 }
708 if (uprv_strcmp(region, "001") == 0) {
709 errln((UnicodeString)"FAIL: CANONICALLOC contains non location zone: " + *id1);
710 }
711 }
712 if (U_FAILURE(ec)) {
713 errln("Error checking IDs in CANONICALLOC");
714 ec = U_ZERO_ERROR;
715 }
716
717 // any_US must contain only US zones
718 any_US->reset(ec);
719 while ((id1 = any_US->snext(ec)) != nullptr) {
720 TimeZone::getRegion(*id1, region, sizeof(region), ec);
721 if (U_FAILURE(ec)) {
722 break;
723 }
724 if (uprv_strcmp(region, "US") != 0) {
725 errln((UnicodeString)"FAIL: ANY_US contains non-US zone ID: " + *id1);
726 }
727 }
728 if (U_FAILURE(ec)) {
729 errln("Error checking IDs in ANY_US");
730 ec = U_ZERO_ERROR;
731 }
732
733 // any_W5 must contain only GMT-05:00 zones
734 any_W5->reset(ec);
735 while ((id1 = any_W5->snext(ec)) != nullptr) {
736 TimeZone *tz = TimeZone::createTimeZone(*id1);
737 if (tz->getRawOffset() != (-5)*60*60*1000) {
738 errln((UnicodeString)"FAIL: ANY_W5 contains a zone whose offset is not -05:00: " + *id1);
739 }
740 delete tz;
741 }
742 if (U_FAILURE(ec)) {
743 errln("Error checking IDs in ANY_W5");
744 ec = U_ZERO_ERROR;
745 }
746
747 // No US zone swith GMT+14:00
748 zoneCount = any_US_E14->count(ec);
749 if (U_FAILURE(ec)) {
750 errln("Error checking IDs in ANY_US_E14");
751 ec = U_ZERO_ERROR;
752 } else if (zoneCount != 0) {
753 errln("FAIL: ANY_US_E14 must be empty");
754 }
755
756 cleanup:
757 delete any;
758 delete canonical;
759 delete canonicalLoc;
760 delete any_US;
761 delete canonical_US;
762 delete canonicalLoc_US;
763 delete any_W5;
764 delete any_CA_W5;
765 delete any_US_E14;
766 }
767
768 void
checkContainsAll(StringEnumeration * s1,const char * name1,StringEnumeration * s2,const char * name2)769 TimeZoneTest::checkContainsAll(StringEnumeration *s1, const char *name1,
770 StringEnumeration *s2, const char *name2)
771 {
772 UErrorCode ec = U_ZERO_ERROR;
773 const UnicodeString *id1, *id2;
774
775 s2->reset(ec);
776
777 while ((id2 = s2->snext(ec)) != nullptr) {
778 UBool found = false;
779 s1->reset(ec);
780 while ((id1 = s1->snext(ec)) != nullptr) {
781 if (*id1 == *id2) {
782 found = true;
783 break;
784 }
785 }
786 if (!found) {
787 errln((UnicodeString)"FAIL: " + name1 + "does not contain "
788 + *id2 + " in " + name2);
789 }
790 }
791
792 if (U_FAILURE(ec)) {
793 errln((UnicodeString)"Error checkContainsAll for " + name1 + " - " + name2);
794 }
795 }
796
797 /**
798 * NOTE: As of ICU 2.8, this test confirms that the "tz.alias"
799 * file, used to build ICU alias zones, is working. It also
800 * looks at some genuine Olson compatibility IDs. [aliu]
801 *
802 * This test is problematic. It should really just confirm that
803 * the list of compatibility zone IDs exist and are somewhat
804 * meaningful (that is, they aren't all aliases of GMT). It goes a
805 * bit further -- it hard-codes expectations about zone behavior,
806 * when in fact zones are redefined quite frequently. ICU's build
807 * process means that it is easy to update ICU to contain the
808 * latest Olson zone data, but if a zone tested here changes, then
809 * this test will fail. I have updated the test for 1999j data,
810 * but further updates will probably be required. Note that some
811 * of the concerts listed below no longer apply -- in particular,
812 * we do NOT overwrite real UNIX zones with 3-letter IDs. There
813 * are two points of overlap as of 1999j: MET and EET. These are
814 * both real UNIX zones, so we just use the official
815 * definition. This test has been updated to reflect this.
816 * 12/3/99 aliu
817 *
818 * Added tests for additional zones and aliases from the icuzones file.
819 * Markus Scherer 2006-nov-06
820 *
821 * [srl - from java - 7/5/1998]
822 * @bug 4130885
823 * Certain short zone IDs, used since 1.1.x, are incorrect.
824 *
825 * The worst of these is:
826 *
827 * "CAT" (Central African Time) should be GMT+2:00, but instead returns a
828 * zone at GMT-1:00. The zone at GMT-1:00 should be called EGT, CVT, EGST,
829 * or AZOST, depending on which zone is meant, but in no case is it CAT.
830 *
831 * Other wrong zone IDs:
832 *
833 * ECT (European Central Time) GMT+1:00: ECT is Ecuador Time,
834 * GMT-5:00. European Central time is abbreviated CEST.
835 *
836 * SST (Solomon Island Time) GMT+11:00. SST is actually Samoa Standard Time,
837 * GMT-11:00. Solomon Island time is SBT.
838 *
839 * NST (New Zealand Time) GMT+12:00. NST is the abbreviation for
840 * Newfoundland Standard Time, GMT-3:30. New Zealanders use NZST.
841 *
842 * AST (Alaska Standard Time) GMT-9:00. [This has already been noted in
843 * another bug.] It should be "AKST". AST is Atlantic Standard Time,
844 * GMT-4:00.
845 *
846 * PNT (Phoenix Time) GMT-7:00. PNT usually means Pitcairn Time,
847 * GMT-8:30. There is no standard abbreviation for Phoenix time, as distinct
848 * from MST with daylight savings.
849 *
850 * In addition to these problems, a number of zones are FAKE. That is, they
851 * don't match what people use in the real world.
852 *
853 * FAKE zones:
854 *
855 * EET (should be EEST)
856 * ART (should be EEST)
857 * MET (should be IRST)
858 * NET (should be AMST)
859 * PLT (should be PKT)
860 * BST (should be BDT)
861 * VST (should be ICT)
862 * CTT (should be CST) +
863 * ACT (should be CST) +
864 * AET (should be EST) +
865 * MIT (should be WST) +
866 * IET (should be EST) +
867 * PRT (should be AST) +
868 * CNT (should be NST)
869 * AGT (should be ARST)
870 * BET (should be EST) +
871 *
872 * + A zone with the correct name already exists and means something
873 * else. E.g., EST usually indicates the US Eastern zone, so it cannot be
874 * used for Brazil (BET).
875 */
TestShortZoneIDs()876 void TimeZoneTest::TestShortZoneIDs()
877 {
878 int32_t i;
879 // Create a small struct to hold the array
880 struct
881 {
882 const char *id;
883 int32_t offset;
884 UBool daylight;
885 }
886 kReferenceList [] =
887 {
888 {"HST", -600, false}, // Olson northamerica -10:00
889 {"AST", -540, true}, // ICU Link - America/Anchorage
890 {"PST", -480, true}, // ICU Link - America/Los_Angeles
891 {"PNT", -420, false}, // ICU Link - America/Phoenix
892 {"MST", -420, false}, // updated Aug 2003 aliu
893 {"CST", -360, true}, // Olson northamerica -7:00
894 {"IET", -300, true}, // ICU Link - America/Indiana/Indianapolis
895 {"EST", -300, false}, // Olson northamerica -5:00
896 {"PRT", -240, false}, // ICU Link - America/Puerto_Rico
897 {"CNT", -210, true}, // ICU Link - America/St_Johns
898 {"AGT", -180, false}, // ICU Link - America/Argentina/Buenos_Aires
899 // Per https://mm.icann.org/pipermail/tz-announce/2019-July/000056.html
900 // Brazil has canceled DST and will stay on standard time indefinitely.
901 {"BET", -180, false}, // ICU Link - America/Sao_Paulo
902 {"GMT", 0, false}, // Olson etcetera Link - Etc/GMT
903 {"UTC", 0, false}, // Olson etcetera 0
904 {"ECT", 60, true}, // ICU Link - Europe/Paris
905 {"MET", 60, true}, // Olson europe 1:00 C-Eur
906 {"CAT", 120, false}, // ICU Link - Africa/Maputo
907 {"ART", 120, false}, // ICU Link - Africa/Cairo
908 {"EET", 120, true}, // Olson europe 2:00 EU
909 {"EAT", 180, false}, // ICU Link - Africa/Addis_Ababa
910 {"NET", 240, false}, // ICU Link - Asia/Yerevan
911 {"PLT", 300, false}, // ICU Link - Asia/Karachi
912 {"IST", 330, false}, // ICU Link - Asia/Kolkata
913 {"BST", 360, false}, // ICU Link - Asia/Dhaka
914 {"VST", 420, false}, // ICU Link - Asia/Ho_Chi_Minh
915 {"CTT", 480, false}, // ICU Link - Asia/Shanghai
916 {"JST", 540, false}, // ICU Link - Asia/Tokyo
917 {"ACT", 570, false}, // ICU Link - Australia/Darwin
918 {"AET", 600, true}, // ICU Link - Australia/Sydney
919 {"SST", 660, false}, // ICU Link - Pacific/Guadalcanal
920 {"NST", 720, true}, // ICU Link - Pacific/Auckland
921 {"MIT", 780, false}, // ICU Link - Pacific/Apia
922
923 {"Etc/Unknown", 0, false}, // CLDR
924
925 {"SystemV/AST4ADT", -240, true},
926 {"SystemV/EST5EDT", -300, true},
927 {"SystemV/CST6CDT", -360, true},
928 {"SystemV/MST7MDT", -420, true},
929 {"SystemV/PST8PDT", -480, true},
930 {"SystemV/YST9YDT", -540, true},
931 {"SystemV/AST4", -240, false},
932 {"SystemV/EST5", -300, false},
933 {"SystemV/CST6", -360, false},
934 {"SystemV/MST7", -420, false},
935 {"SystemV/PST8", -480, false},
936 {"SystemV/YST9", -540, false},
937 {"SystemV/HST10", -600, false},
938
939 {"",0,false}
940 };
941
942 for(i=0;kReferenceList[i].id[0];i++) {
943 UnicodeString itsID(kReferenceList[i].id);
944 UBool ok = true;
945 // Check existence.
946 TimeZone *tz = TimeZone::createTimeZone(itsID);
947 if (!tz || (kReferenceList[i].offset != 0 && *tz == *TimeZone::getGMT())) {
948 errln("FAIL: Time Zone " + itsID + " does not exist!");
949 continue;
950 }
951
952 // Check daylight usage.
953 UBool usesDaylight = tz->useDaylightTime();
954 if (usesDaylight != kReferenceList[i].daylight) {
955 if (!isDevelopmentBuild) {
956 logln("Warning: Time Zone " + itsID + " use daylight is " +
957 (usesDaylight?"true":"false") +
958 " but it should be " +
959 ((kReferenceList[i].daylight)?"true":"false"));
960 } else {
961 dataerrln("FAIL: Time Zone " + itsID + " use daylight is " +
962 (usesDaylight?"true":"false") +
963 " but it should be " +
964 ((kReferenceList[i].daylight)?"true":"false"));
965 }
966 ok = false;
967 }
968
969 // Check offset
970 int32_t offsetInMinutes = tz->getRawOffset()/60000;
971 if (offsetInMinutes != kReferenceList[i].offset) {
972 if (!isDevelopmentBuild) {
973 logln("FAIL: Time Zone " + itsID + " raw offset is " +
974 offsetInMinutes +
975 " but it should be " + kReferenceList[i].offset);
976 } else {
977 dataerrln("FAIL: Time Zone " + itsID + " raw offset is " +
978 offsetInMinutes +
979 " but it should be " + kReferenceList[i].offset);
980 }
981 ok = false;
982 }
983
984 if (ok) {
985 logln("OK: " + itsID +
986 " useDaylightTime() & getRawOffset() as expected");
987 }
988 delete tz;
989 }
990
991
992 // OK now test compat
993 logln("Testing for compatibility zones");
994
995 const char* compatibilityMap[] = {
996 // This list is copied from tz.alias. If tz.alias
997 // changes, this list must be updated. Current as of Mar 2007
998 "ACT", "Australia/Darwin",
999 "AET", "Australia/Sydney",
1000 "AGT", "America/Buenos_Aires",
1001 "ART", "Africa/Cairo",
1002 "AST", "America/Anchorage",
1003 "BET", "America/Sao_Paulo",
1004 "BST", "Asia/Dhaka", // # spelling changed in 2000h; was Asia/Dacca
1005 "CAT", "Africa/Maputo",
1006 "CNT", "America/St_Johns",
1007 "CST", "America/Chicago",
1008 "CTT", "Asia/Shanghai",
1009 "EAT", "Africa/Addis_Ababa",
1010 "ECT", "Europe/Paris",
1011 // EET Europe/Istanbul # EET is a standard UNIX zone
1012 // "EST", "America/New_York", # Defined as -05:00
1013 // "HST", "Pacific/Honolulu", # Defined as -10:00
1014 "IET", "America/Indianapolis",
1015 "IST", "Asia/Calcutta",
1016 "JST", "Asia/Tokyo",
1017 // MET Asia/Tehran # MET is a standard UNIX zone
1018 "MIT", "Pacific/Apia",
1019 // "MST", "America/Denver", # Defined as -07:00
1020 "NET", "Asia/Yerevan",
1021 "NST", "Pacific/Auckland",
1022 "PLT", "Asia/Karachi",
1023 "PNT", "America/Phoenix",
1024 "PRT", "America/Puerto_Rico",
1025 "PST", "America/Los_Angeles",
1026 "SST", "Pacific/Guadalcanal",
1027 "UTC", "Etc/GMT",
1028 "VST", "Asia/Saigon",
1029 "","",""
1030 };
1031
1032 for (i=0;*compatibilityMap[i];i+=2) {
1033 UnicodeString itsID;
1034
1035 const char *zone1 = compatibilityMap[i];
1036 const char *zone2 = compatibilityMap[i+1];
1037
1038 TimeZone *tz1 = TimeZone::createTimeZone(zone1);
1039 TimeZone *tz2 = TimeZone::createTimeZone(zone2);
1040
1041 if (!tz1) {
1042 errln(UnicodeString("FAIL: Could not find short ID zone ") + zone1);
1043 }
1044 if (!tz2) {
1045 errln(UnicodeString("FAIL: Could not find long ID zone ") + zone2);
1046 }
1047
1048 if (tz1 && tz2) {
1049 // make NAME same so comparison will only look at the rest
1050 tz2->setID(tz1->getID(itsID));
1051
1052 if (*tz1 != *tz2) {
1053 errln("FAIL: " + UnicodeString(zone1) +
1054 " != " + UnicodeString(zone2));
1055 } else {
1056 logln("OK: " + UnicodeString(zone1) +
1057 " == " + UnicodeString(zone2));
1058 }
1059 }
1060
1061 delete tz1;
1062 delete tz2;
1063 }
1064 }
1065
1066
1067 /**
1068 * Utility function for TestCustomParse
1069 */
formatOffset(int32_t offset,UnicodeString & rv)1070 UnicodeString& TimeZoneTest::formatOffset(int32_t offset, UnicodeString &rv) {
1071 rv.remove();
1072 char16_t sign = 0x002B;
1073 if (offset < 0) {
1074 sign = 0x002D;
1075 offset = -offset;
1076 }
1077
1078 int32_t s = offset % 60;
1079 offset /= 60;
1080 int32_t m = offset % 60;
1081 int32_t h = offset / 60;
1082
1083 rv += (char16_t)(sign);
1084 if (h >= 10) {
1085 rv += (char16_t)(0x0030 + (h/10));
1086 } else {
1087 rv += (char16_t)0x0030;
1088 }
1089 rv += (char16_t)(0x0030 + (h%10));
1090
1091 rv += (char16_t)0x003A; /* ':' */
1092 if (m >= 10) {
1093 rv += (char16_t)(0x0030 + (m/10));
1094 } else {
1095 rv += (char16_t)0x0030;
1096 }
1097 rv += (char16_t)(0x0030 + (m%10));
1098
1099 if (s) {
1100 rv += (char16_t)0x003A; /* ':' */
1101 if (s >= 10) {
1102 rv += (char16_t)(0x0030 + (s/10));
1103 } else {
1104 rv += (char16_t)0x0030;
1105 }
1106 rv += (char16_t)(0x0030 + (s%10));
1107 }
1108 return rv;
1109 }
1110
1111 /**
1112 * Utility function for TestCustomParse, generating time zone ID
1113 * string for the give offset.
1114 */
formatTZID(int32_t offset,UnicodeString & rv)1115 UnicodeString& TimeZoneTest::formatTZID(int32_t offset, UnicodeString &rv) {
1116 rv.remove();
1117 char16_t sign = 0x002B;
1118 if (offset < 0) {
1119 sign = 0x002D;
1120 offset = -offset;
1121 }
1122
1123 int32_t s = offset % 60;
1124 offset /= 60;
1125 int32_t m = offset % 60;
1126 int32_t h = offset / 60;
1127
1128 rv += "GMT";
1129 rv += (char16_t)(sign);
1130 if (h >= 10) {
1131 rv += (char16_t)(0x0030 + (h/10));
1132 } else {
1133 rv += (char16_t)0x0030;
1134 }
1135 rv += (char16_t)(0x0030 + (h%10));
1136 rv += (char16_t)0x003A;
1137 if (m >= 10) {
1138 rv += (char16_t)(0x0030 + (m/10));
1139 } else {
1140 rv += (char16_t)0x0030;
1141 }
1142 rv += (char16_t)(0x0030 + (m%10));
1143
1144 if (s) {
1145 rv += (char16_t)0x003A;
1146 if (s >= 10) {
1147 rv += (char16_t)(0x0030 + (s/10));
1148 } else {
1149 rv += (char16_t)0x0030;
1150 }
1151 rv += (char16_t)(0x0030 + (s%10));
1152 }
1153 return rv;
1154 }
1155
1156 /**
1157 * As part of the VM fix (see CCC approved RFE 4028006, bug
1158 * 4044013), TimeZone.getTimeZone() has been modified to recognize
1159 * generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
1160 * GMT[+-]hh. Test this behavior here.
1161 *
1162 * @bug 4044013
1163 */
TestCustomParse()1164 void TimeZoneTest::TestCustomParse()
1165 {
1166 int32_t i;
1167 const int32_t kUnparseable = 604800; // the number of seconds in a week. More than any offset should be.
1168
1169 struct
1170 {
1171 const char *customId;
1172 int32_t expectedOffset;
1173 }
1174 kData[] =
1175 {
1176 // ID Expected offset in seconds
1177 {"GMT", kUnparseable}, //Isn't custom. [returns normal GMT]
1178 {"GMT-YOUR.AD.HERE", kUnparseable},
1179 {"GMT0", kUnparseable},
1180 {"GMT+0", (0)},
1181 {"GMT+1", (1*60*60)},
1182 {"GMT-0030", (-30*60)},
1183 {"GMT+15:99", kUnparseable},
1184 {"GMT+", kUnparseable},
1185 {"GMT-", kUnparseable},
1186 {"GMT+0:", kUnparseable},
1187 {"GMT-:", kUnparseable},
1188 {"GMT-YOUR.AD.HERE", kUnparseable},
1189 {"GMT+0010", (10*60)}, // Interpret this as 00:10
1190 {"GMT-10", (-10*60*60)},
1191 {"GMT+30", kUnparseable},
1192 {"GMT-3:30", (-(3*60+30)*60)},
1193 {"GMT-230", (-(2*60+30)*60)},
1194 {"GMT+05:13:05", ((5*60+13)*60+5)},
1195 {"GMT-71023", (-((7*60+10)*60+23))},
1196 {"GMT+01:23:45:67", kUnparseable},
1197 {"GMT+01:234", kUnparseable},
1198 {"GMT-2:31:123", kUnparseable},
1199 {"GMT+3:75", kUnparseable},
1200 {"GMT-01010101", kUnparseable},
1201 {0, 0}
1202 };
1203
1204 for (i=0; kData[i].customId != 0; i++) {
1205 UnicodeString id(kData[i].customId);
1206 int32_t exp = kData[i].expectedOffset;
1207 TimeZone *zone = TimeZone::createTimeZone(id);
1208 UnicodeString itsID, temp;
1209
1210 OlsonTimeZone *ozone = dynamic_cast<OlsonTimeZone *>(zone);
1211 if (ozone != nullptr) {
1212 logln(id + " -> Olson time zone");
1213 ozone->operator=(*ozone); // self-assignment should be a no-op
1214 } else {
1215 zone->getID(itsID);
1216 int32_t ioffset = zone->getRawOffset()/1000;
1217 UnicodeString offset, expectedID;
1218 formatOffset(ioffset, offset);
1219 formatTZID(ioffset, expectedID);
1220 logln(id + " -> " + itsID + " " + offset);
1221 if (exp == kUnparseable && itsID != UCAL_UNKNOWN_ZONE_ID) {
1222 errln("Expected parse failure for " + id +
1223 ", got offset of " + offset +
1224 ", id " + itsID);
1225 }
1226 // JDK 1.3 creates custom zones with the ID "Custom"
1227 // JDK 1.4 creates custom zones with IDs of the form "GMT+02:00"
1228 // ICU creates custom zones with IDs of the form "GMT+02:00"
1229 else if (exp != kUnparseable && (ioffset != exp || itsID != expectedID)) {
1230 dataerrln("Expected offset of " + formatOffset(exp, temp) +
1231 ", id " + expectedID +
1232 ", for " + id +
1233 ", got offset of " + offset +
1234 ", id " + itsID);
1235 }
1236 }
1237 delete zone;
1238 }
1239 }
1240
1241 void
TestAliasedNames()1242 TimeZoneTest::TestAliasedNames()
1243 {
1244 struct {
1245 const char *from;
1246 const char *to;
1247 } kData[] = {
1248 /* Generated by org.unicode.cldr.tool.CountItems */
1249
1250 /* zoneID, canonical zoneID */
1251 {"Africa/Asmara", "Africa/Addis_Ababa"},
1252 {"Africa/Timbuktu", "Africa/Abidjan"},
1253 {"America/Argentina/Buenos_Aires", "America/Buenos_Aires"},
1254 {"America/Argentina/Catamarca", "America/Catamarca"},
1255 {"America/Argentina/ComodRivadavia", "America/Catamarca"},
1256 {"America/Argentina/Cordoba", "America/Cordoba"},
1257 {"America/Argentina/Jujuy", "America/Jujuy"},
1258 {"America/Argentina/Mendoza", "America/Mendoza"},
1259 {"America/Atka", "America/Adak"},
1260 {"America/Ensenada", "America/Tijuana"},
1261 {"America/Fort_Wayne", "America/Indianapolis"},
1262 {"America/Indiana/Indianapolis", "America/Indianapolis"},
1263 {"America/Kentucky/Louisville", "America/Louisville"},
1264 {"America/Knox_IN", "America/Indiana/Knox"},
1265 {"America/Porto_Acre", "America/Rio_Branco"},
1266 {"America/Rosario", "America/Cordoba"},
1267 {"America/Shiprock", "America/Denver"},
1268 {"America/Virgin", "America/Anguilla"},
1269 {"Antarctica/South_Pole", "Antarctica/McMurdo"},
1270 {"Asia/Ashkhabad", "Asia/Ashgabat"},
1271 {"Asia/Chongqing", "Asia/Shanghai"},
1272 {"Asia/Chungking", "Asia/Shanghai"},
1273 {"Asia/Dacca", "Asia/Dhaka"},
1274 {"Asia/Harbin", "Asia/Shanghai"},
1275 {"Asia/Ho_Chi_Minh", "Asia/Saigon"},
1276 {"Asia/Istanbul", "Europe/Istanbul"},
1277 {"Asia/Kashgar", "Asia/Urumqi"},
1278 {"Asia/Kathmandu", "Asia/Katmandu"},
1279 {"Asia/Kolkata", "Asia/Calcutta"},
1280 {"Asia/Macao", "Asia/Macau"},
1281 {"Asia/Tel_Aviv", "Asia/Jerusalem"},
1282 {"Asia/Thimbu", "Asia/Thimphu"},
1283 {"Asia/Ujung_Pandang", "Asia/Makassar"},
1284 {"Asia/Ulan_Bator", "Asia/Ulaanbaatar"},
1285 {"Atlantic/Faroe", "Atlantic/Faeroe"},
1286 {"Atlantic/Jan_Mayen", "Arctic/Longyearbyen"},
1287 {"Australia/ACT", "Australia/Sydney"},
1288 {"Australia/Canberra", "Australia/Sydney"},
1289 {"Australia/LHI", "Australia/Lord_Howe"},
1290 {"Australia/NSW", "Australia/Sydney"},
1291 {"Australia/North", "Australia/Darwin"},
1292 {"Australia/Queensland", "Australia/Brisbane"},
1293 {"Australia/South", "Australia/Adelaide"},
1294 {"Australia/Tasmania", "Australia/Hobart"},
1295 {"Australia/Victoria", "Australia/Melbourne"},
1296 {"Australia/West", "Australia/Perth"},
1297 {"Australia/Yancowinna", "Australia/Broken_Hill"},
1298 {"Brazil/Acre", "America/Rio_Branco"},
1299 {"Brazil/DeNoronha", "America/Noronha"},
1300 {"Brazil/East", "America/Sao_Paulo"},
1301 {"Brazil/West", "America/Manaus"},
1302 {"Canada/Atlantic", "America/Halifax"},
1303 {"Canada/Central", "America/Winnipeg"},
1304 {"Canada/East-Saskatchewan", "America/Regina"},
1305 {"Canada/Eastern", "America/Toronto"},
1306 {"Canada/Mountain", "America/Edmonton"},
1307 {"Canada/Newfoundland", "America/St_Johns"},
1308 {"Canada/Pacific", "America/Vancouver"},
1309 {"Canada/Saskatchewan", "America/Regina"},
1310 {"Canada/Yukon", "America/Whitehorse"},
1311 {"Chile/Continental", "America/Santiago"},
1312 {"Chile/EasterIsland", "Pacific/Easter"},
1313 {"Cuba", "America/Havana"},
1314 {"EST", "Etc/GMT+5"},
1315 {"Egypt", "Africa/Cairo"},
1316 {"Eire", "Europe/Dublin"},
1317 {"Etc/GMT+0", "Etc/GMT"},
1318 {"Etc/GMT-0", "Etc/GMT"},
1319 {"Etc/GMT0", "Etc/GMT"},
1320 {"Etc/Greenwich", "Etc/GMT"},
1321 {"Etc/UCT", "Etc/UTC"},
1322 {"Etc/Universal", "Etc/UTC"},
1323 {"Etc/Zulu", "Etc/UTC"},
1324 {"Europe/Belfast", "Europe/London"},
1325 {"Europe/Nicosia", "Asia/Nicosia"},
1326 {"Europe/Tiraspol", "Europe/Chisinau"},
1327 {"GB", "Europe/London"},
1328 {"GB-Eire", "Europe/London"},
1329 {"GMT", "Etc/GMT"},
1330 {"GMT+0", "Etc/GMT"},
1331 {"GMT-0", "Etc/GMT"},
1332 {"GMT0", "Etc/GMT"},
1333 {"Greenwich", "Etc/GMT"},
1334 {"HST", "Etc/GMT+10"},
1335 {"Hongkong", "Asia/Hong_Kong"},
1336 {"Iceland", "Atlantic/Reykjavik"},
1337 {"Iran", "Asia/Tehran"},
1338 {"Israel", "Asia/Jerusalem"},
1339 {"Jamaica", "America/Jamaica"},
1340 {"Japan", "Asia/Tokyo"},
1341 {"Kwajalein", "Pacific/Kwajalein"},
1342 {"Libya", "Africa/Tripoli"},
1343 {"MST", "Etc/GMT+7"},
1344 {"Mexico/BajaNorte", "America/Tijuana"},
1345 {"Mexico/BajaSur", "America/Mazatlan"},
1346 {"Mexico/General", "America/Mexico_City"},
1347 {"NZ", "Antarctica/McMurdo"},
1348 {"NZ-CHAT", "Pacific/Chatham"},
1349 {"Navajo", "America/Denver"},
1350 {"PRC", "Asia/Shanghai"},
1351 {"Pacific/Chuuk", "Pacific/Truk"},
1352 {"Pacific/Pohnpei", "Pacific/Ponape"},
1353 {"Pacific/Samoa", "Pacific/Midway"},
1354 {"Pacific/Yap", "Pacific/Truk"},
1355 {"Poland", "Europe/Warsaw"},
1356 {"Portugal", "Europe/Lisbon"},
1357 {"ROC", "Asia/Taipei"},
1358 {"ROK", "Asia/Seoul"},
1359 {"Singapore", "Asia/Singapore"},
1360 {"SystemV/AST4", "Etc/GMT+4"},
1361 {"SystemV/CST6", "Etc/GMT+6"},
1362 {"SystemV/EST5", "Etc/GMT+5"},
1363 {"SystemV/HST10", "Etc/GMT+10"},
1364 {"SystemV/MST7", "Etc/GMT+7"},
1365 {"SystemV/PST8", "Etc/GMT+8"},
1366 {"SystemV/YST9", "Etc/GMT+9"},
1367 {"Turkey", "Europe/Istanbul"},
1368 {"UCT", "Etc/UTC"},
1369 {"US/Alaska", "America/Anchorage"},
1370 {"US/Aleutian", "America/Adak"},
1371 {"US/Arizona", "America/Phoenix"},
1372 {"US/Central", "America/Chicago"},
1373 {"US/East-Indiana", "America/Indianapolis"},
1374 {"US/Eastern", "America/New_York"},
1375 {"US/Hawaii", "Pacific/Honolulu"},
1376 {"US/Indiana-Starke", "America/Indiana/Knox"},
1377 {"US/Michigan", "America/Detroit"},
1378 {"US/Mountain", "America/Denver"},
1379 {"US/Pacific", "America/Los_Angeles"},
1380 {"US/Pacific-New", "America/Los_Angeles"},
1381 {"US/Samoa", "Pacific/Midway"},
1382 {"UTC", "Etc/UTC"},
1383 {"Universal", "Etc/UTC"},
1384 {"W-SU", "Europe/Moscow"},
1385 {"Zulu", "Etc/UTC"},
1386 /* Total: 136 */
1387 };
1388
1389 TimeZone::EDisplayType styles[] = { TimeZone::SHORT, TimeZone::LONG };
1390 UBool useDst[] = { false, true };
1391 int32_t noLoc = uloc_countAvailable();
1392
1393 int32_t i, j, k, loc;
1394 UnicodeString fromName, toName;
1395 TimeZone *from = nullptr, *to = nullptr;
1396 for(i = 0; i < UPRV_LENGTHOF(kData); i++) {
1397 from = TimeZone::createTimeZone(kData[i].from);
1398 to = TimeZone::createTimeZone(kData[i].to);
1399 if(!from->hasSameRules(*to)) {
1400 errln("different at %i\n", i);
1401 }
1402 if(!quick) {
1403 for(loc = 0; loc < noLoc; loc++) {
1404 const char* locale = uloc_getAvailable(loc);
1405 for(j = 0; j < UPRV_LENGTHOF(styles); j++) {
1406 for(k = 0; k < UPRV_LENGTHOF(useDst); k++) {
1407 fromName.remove();
1408 toName.remove();
1409 from->getDisplayName(useDst[k], styles[j],locale, fromName);
1410 to->getDisplayName(useDst[k], styles[j], locale, toName);
1411 if(fromName.compare(toName) != 0) {
1412 errln("Fail: Expected "+toName+" but got " + prettify(fromName)
1413 + " for locale: " + locale + " index: "+ loc
1414 + " to id "+ kData[i].to
1415 + " from id " + kData[i].from);
1416 }
1417 }
1418 }
1419 }
1420 } else {
1421 fromName.remove();
1422 toName.remove();
1423 from->getDisplayName(fromName);
1424 to->getDisplayName(toName);
1425 if(fromName.compare(toName) != 0) {
1426 errln("Fail: Expected "+toName+" but got " + fromName);
1427 }
1428 }
1429 delete from;
1430 delete to;
1431 }
1432 }
1433
1434 /**
1435 * Test the basic functionality of the getDisplayName() API.
1436 *
1437 * @bug 4112869
1438 * @bug 4028006
1439 *
1440 * See also API change request A41.
1441 *
1442 * 4/21/98 - make smarter, so the test works if the ext resources
1443 * are present or not.
1444 */
1445 void
TestDisplayName()1446 TimeZoneTest::TestDisplayName()
1447 {
1448 UErrorCode status = U_ZERO_ERROR;
1449 int32_t i;
1450 TimeZone *zone = TimeZone::createTimeZone("PST");
1451 UnicodeString name;
1452 zone->getDisplayName(Locale::getEnglish(), name);
1453 logln("PST->" + name);
1454 if (name.compare("Pacific Standard Time") != 0)
1455 dataerrln("Fail: Expected \"Pacific Standard Time\" but got " + name);
1456
1457 //*****************************************************************
1458 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1459 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1460 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1461 //*****************************************************************
1462 struct
1463 {
1464 UBool useDst;
1465 TimeZone::EDisplayType style;
1466 const char *expect;
1467 } kData[] = {
1468 {false, TimeZone::SHORT, "PST"},
1469 {true, TimeZone::SHORT, "PDT"},
1470 {false, TimeZone::LONG, "Pacific Standard Time"},
1471 {true, TimeZone::LONG, "Pacific Daylight Time"},
1472
1473 {false, TimeZone::SHORT_GENERIC, "PT"},
1474 {true, TimeZone::SHORT_GENERIC, "PT"},
1475 {false, TimeZone::LONG_GENERIC, "Pacific Time"},
1476 {true, TimeZone::LONG_GENERIC, "Pacific Time"},
1477
1478 {false, TimeZone::SHORT_GMT, "-0800"},
1479 {true, TimeZone::SHORT_GMT, "-0700"},
1480 {false, TimeZone::LONG_GMT, "GMT-08:00"},
1481 {true, TimeZone::LONG_GMT, "GMT-07:00"},
1482
1483 {false, TimeZone::SHORT_COMMONLY_USED, "PST"},
1484 {true, TimeZone::SHORT_COMMONLY_USED, "PDT"},
1485 {false, TimeZone::GENERIC_LOCATION, "Los Angeles Time"},
1486 {true, TimeZone::GENERIC_LOCATION, "Los Angeles Time"},
1487
1488 {false, TimeZone::LONG, ""}
1489 };
1490
1491 for (i=0; kData[i].expect[0] != '\0'; i++)
1492 {
1493 name.remove();
1494 name = zone->getDisplayName(kData[i].useDst,
1495 kData[i].style,
1496 Locale::getEnglish(), name);
1497 if (name.compare(kData[i].expect) != 0)
1498 dataerrln("Fail: Expected " + UnicodeString(kData[i].expect) + "; got " + name);
1499 logln("PST [with options]->" + name);
1500 }
1501 for (i=0; kData[i].expect[0] != '\0'; i++)
1502 {
1503 name.remove();
1504 name = zone->getDisplayName(kData[i].useDst,
1505 kData[i].style, name);
1506 if (name.compare(kData[i].expect) != 0)
1507 dataerrln("Fail: Expected " + UnicodeString(kData[i].expect) + "; got " + name);
1508 logln("PST [with options]->" + name);
1509 }
1510
1511
1512 // Make sure that we don't display the DST name by constructing a fake
1513 // PST zone that has DST all year long.
1514 SimpleTimeZone *zone2 = new SimpleTimeZone(0, "PST");
1515
1516 zone2->setStartRule(UCAL_JANUARY, 1, 0, 0, status);
1517 zone2->setEndRule(UCAL_DECEMBER, 31, 0, 0, status);
1518
1519 UnicodeString inDaylight;
1520 if (zone2->inDaylightTime(UDate(0), status)) {
1521 inDaylight = UnicodeString("true");
1522 } else {
1523 inDaylight = UnicodeString("false");
1524 }
1525 logln(UnicodeString("Modified PST inDaylightTime->") + inDaylight );
1526 if(U_FAILURE(status))
1527 {
1528 dataerrln("Some sort of error..." + UnicodeString(u_errorName(status))); // REVISIT
1529 }
1530 name.remove();
1531 name = zone2->getDisplayName(Locale::getEnglish(),name);
1532 logln("Modified PST->" + name);
1533 if (name.compare("Pacific Standard Time") != 0)
1534 dataerrln("Fail: Expected \"Pacific Standard Time\"");
1535
1536 // Make sure we get the default display format for Locales
1537 // with no display name data.
1538 Locale mt_MT("mt_MT");
1539 name.remove();
1540 name = zone->getDisplayName(mt_MT,name);
1541 //*****************************************************************
1542 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1543 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1544 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1545 //*****************************************************************
1546 logln("PST(mt_MT)->" + name);
1547
1548 // *** REVISIT SRL how in the world do I check this? looks java specific.
1549 // Now be smart -- check to see if zh resource is even present.
1550 // If not, we expect the en fallback behavior.
1551 ResourceBundle enRB(nullptr,
1552 Locale::getEnglish(), status);
1553 if(U_FAILURE(status))
1554 dataerrln("Couldn't get ResourceBundle for en - %s", u_errorName(status));
1555
1556 ResourceBundle mtRB(nullptr,
1557 mt_MT, status);
1558 //if(U_FAILURE(status))
1559 // errln("Couldn't get ResourceBundle for mt_MT");
1560
1561 UBool noZH = U_FAILURE(status);
1562
1563 if (noZH) {
1564 logln("Warning: Not testing the mt_MT behavior because resource is absent");
1565 if (name != "Pacific Standard Time")
1566 dataerrln("Fail: Expected Pacific Standard Time");
1567 }
1568
1569
1570 if (name.compare("GMT-08:00") &&
1571 name.compare("GMT-8:00") &&
1572 name.compare("GMT-0800") &&
1573 name.compare("GMT-800")) {
1574 dataerrln(UnicodeString("Fail: Expected GMT-08:00 or something similar for PST in mt_MT but got ") + name );
1575 dataerrln("************************************************************");
1576 dataerrln("THE ABOVE FAILURE MAY JUST MEAN THE LOCALE DATA HAS CHANGED");
1577 dataerrln("************************************************************");
1578 }
1579
1580 // Now try a non-existent zone
1581 delete zone2;
1582 zone2 = new SimpleTimeZone(90*60*1000, "xyzzy");
1583 name.remove();
1584 name = zone2->getDisplayName(Locale::getEnglish(),name);
1585 logln("GMT+90min->" + name);
1586 if (name.compare("GMT+01:30") &&
1587 name.compare("GMT+1:30") &&
1588 name.compare("GMT+0130") &&
1589 name.compare("GMT+130"))
1590 dataerrln("Fail: Expected GMT+01:30 or something similar");
1591 name.truncate(0);
1592 zone2->getDisplayName(name);
1593 logln("GMT+90min->" + name);
1594 if (name.compare("GMT+01:30") &&
1595 name.compare("GMT+1:30") &&
1596 name.compare("GMT+0130") &&
1597 name.compare("GMT+130"))
1598 dataerrln("Fail: Expected GMT+01:30 or something similar");
1599 // clean up
1600 delete zone;
1601 delete zone2;
1602 }
1603
1604 /**
1605 * @bug 4107276
1606 */
1607 void
TestDSTSavings()1608 TimeZoneTest::TestDSTSavings()
1609 {
1610 UErrorCode status = U_ZERO_ERROR;
1611 // It might be better to find a way to integrate this test into the main TimeZone
1612 // tests above, but I don't have time to figure out how to do this (or if it's
1613 // even really a good idea). Let's consider that a future. --rtg 1/27/98
1614 SimpleTimeZone *tz = new SimpleTimeZone(-5 * U_MILLIS_PER_HOUR, "dstSavingsTest",
1615 UCAL_MARCH, 1, 0, 0, UCAL_SEPTEMBER, 1, 0, 0,
1616 (int32_t)(0.5 * U_MILLIS_PER_HOUR), status);
1617 if(U_FAILURE(status))
1618 errln("couldn't create TimeZone");
1619
1620 if (tz->getRawOffset() != -5 * U_MILLIS_PER_HOUR)
1621 errln(UnicodeString("Got back a raw offset of ") + (tz->getRawOffset() / U_MILLIS_PER_HOUR) +
1622 " hours instead of -5 hours.");
1623 if (!tz->useDaylightTime())
1624 errln("Test time zone should use DST but claims it doesn't.");
1625 if (tz->getDSTSavings() != 0.5 * U_MILLIS_PER_HOUR)
1626 errln(UnicodeString("Set DST offset to 0.5 hour, but got back ") + (tz->getDSTSavings() /
1627 U_MILLIS_PER_HOUR) + " hours instead.");
1628
1629 int32_t offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JANUARY, 1,
1630 UCAL_THURSDAY, 10 * U_MILLIS_PER_HOUR,status);
1631 if (offset != -5 * U_MILLIS_PER_HOUR)
1632 errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
1633 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1634
1635 offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JUNE, 1, UCAL_MONDAY,
1636 10 * U_MILLIS_PER_HOUR,status);
1637 if (offset != -4.5 * U_MILLIS_PER_HOUR)
1638 errln(UnicodeString("The offset for 10 AM, 6/1/98 should have been -4.5 hours, but we got ")
1639 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1640
1641 tz->setDSTSavings(U_MILLIS_PER_HOUR, status);
1642 offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JANUARY, 1,
1643 UCAL_THURSDAY, 10 * U_MILLIS_PER_HOUR,status);
1644 if (offset != -5 * U_MILLIS_PER_HOUR)
1645 errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
1646 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1647
1648 offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JUNE, 1, UCAL_MONDAY,
1649 10 * U_MILLIS_PER_HOUR,status);
1650 if (offset != -4 * U_MILLIS_PER_HOUR)
1651 errln(UnicodeString("The offset for 10 AM, 6/1/98 (with a 1-hour DST offset) should have been -4 hours, but we got ")
1652 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1653
1654 delete tz;
1655 }
1656
1657 /**
1658 * @bug 4107570
1659 */
1660 void
TestAlternateRules()1661 TimeZoneTest::TestAlternateRules()
1662 {
1663 // Like TestDSTSavings, this test should probably be integrated somehow with the main
1664 // test at the top of this class, but I didn't have time to figure out how to do that.
1665 // --rtg 1/28/98
1666
1667 SimpleTimeZone tz(-5 * U_MILLIS_PER_HOUR, "alternateRuleTest");
1668
1669 // test the day-of-month API
1670 UErrorCode status = U_ZERO_ERROR;
1671 tz.setStartRule(UCAL_MARCH, 10, 12 * U_MILLIS_PER_HOUR, status);
1672 if(U_FAILURE(status))
1673 errln("tz.setStartRule failed");
1674 tz.setEndRule(UCAL_OCTOBER, 20, 12 * U_MILLIS_PER_HOUR, status);
1675 if(U_FAILURE(status))
1676 errln("tz.setStartRule failed");
1677
1678 int32_t offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 5,
1679 UCAL_THURSDAY, 10 * U_MILLIS_PER_HOUR,status);
1680 if (offset != -5 * U_MILLIS_PER_HOUR)
1681 errln(UnicodeString("The offset for 10AM, 3/5/98 should have been -5 hours, but we got ")
1682 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1683
1684 offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 15,
1685 UCAL_SUNDAY, 10 * millisPerHour,status);
1686 if (offset != -4 * U_MILLIS_PER_HOUR)
1687 errln(UnicodeString("The offset for 10AM, 3/15/98 should have been -4 hours, but we got ")
1688 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1689
1690 offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 15,
1691 UCAL_THURSDAY, 10 * millisPerHour,status);
1692 if (offset != -4 * U_MILLIS_PER_HOUR)
1693 errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ") + (offset / U_MILLIS_PER_HOUR) + " hours.");
1694
1695 offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 25,
1696 UCAL_SUNDAY, 10 * millisPerHour,status);
1697 if (offset != -5 * U_MILLIS_PER_HOUR)
1698 errln(UnicodeString("The offset for 10AM, 10/25/98 should have been -5 hours, but we got ")
1699 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1700
1701 // test the day-of-week-after-day-in-month API
1702 tz.setStartRule(UCAL_MARCH, 10, UCAL_FRIDAY, 12 * millisPerHour, true, status);
1703 if(U_FAILURE(status))
1704 errln("tz.setStartRule failed");
1705 tz.setEndRule(UCAL_OCTOBER, 20, UCAL_FRIDAY, 12 * millisPerHour, false, status);
1706 if(U_FAILURE(status))
1707 errln("tz.setStartRule failed");
1708
1709 offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 11,
1710 UCAL_WEDNESDAY, 10 * millisPerHour,status);
1711 if (offset != -5 * U_MILLIS_PER_HOUR)
1712 errln(UnicodeString("The offset for 10AM, 3/11/98 should have been -5 hours, but we got ")
1713 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1714
1715 offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 14,
1716 UCAL_SATURDAY, 10 * millisPerHour,status);
1717 if (offset != -4 * U_MILLIS_PER_HOUR)
1718 errln(UnicodeString("The offset for 10AM, 3/14/98 should have been -4 hours, but we got ")
1719 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1720
1721 offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 15,
1722 UCAL_THURSDAY, 10 * millisPerHour,status);
1723 if (offset != -4 * U_MILLIS_PER_HOUR)
1724 errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ")
1725 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1726
1727 offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 17,
1728 UCAL_SATURDAY, 10 * millisPerHour,status);
1729 if (offset != -5 * U_MILLIS_PER_HOUR)
1730 errln(UnicodeString("The offset for 10AM, 10/17/98 should have been -5 hours, but we got ")
1731 + (offset / U_MILLIS_PER_HOUR) + " hours.");
1732 }
1733
TestFractionalDST()1734 void TimeZoneTest::TestFractionalDST() {
1735 const char* tzName = "Australia/Lord_Howe"; // 30 min offset
1736 TimeZone* tz_icu = TimeZone::createTimeZone(tzName);
1737 int dst_icu = tz_icu->getDSTSavings();
1738 UnicodeString id;
1739 int32_t expected = 1800000;
1740 if (expected != dst_icu) {
1741 dataerrln(UnicodeString("java reports dst savings of ") + expected +
1742 " but icu reports " + dst_icu +
1743 " for tz " + tz_icu->getID(id));
1744 } else {
1745 logln(UnicodeString("both java and icu report dst savings of ") + expected + " for tz " + tz_icu->getID(id));
1746 }
1747 delete tz_icu;
1748 }
1749
1750 /**
1751 * Test country code support. Jitterbug 776.
1752 */
TestCountries()1753 void TimeZoneTest::TestCountries() {
1754 // Make sure America/Los_Angeles is in the "US" group, and
1755 // Asia/Tokyo isn't. Vice versa for the "JP" group.
1756 UErrorCode ec = U_ZERO_ERROR;
1757 int32_t n;
1758 StringEnumeration* s = TimeZone::createEnumerationForRegion("US", ec);
1759 if (U_FAILURE(ec)) {
1760 dataerrln("Unable to create TimeZone enumeration for US");
1761 return;
1762 }
1763 n = s->count(ec);
1764 UBool la = false, tokyo = false;
1765 UnicodeString laZone("America/Los_Angeles", "");
1766 UnicodeString tokyoZone("Asia/Tokyo", "");
1767 int32_t i;
1768
1769 if (n <= 0) {
1770 dataerrln("FAIL: TimeZone::createEnumeration() returned nothing");
1771 return;
1772 }
1773 for (i=0; i<n; ++i) {
1774 const UnicodeString* id = s->snext(ec);
1775 if (*id == (laZone)) {
1776 la = true;
1777 }
1778 if (*id == (tokyoZone)) {
1779 tokyo = true;
1780 }
1781 }
1782 if (!la || tokyo) {
1783 errln("FAIL: " + laZone + " in US = " + la);
1784 errln("FAIL: " + tokyoZone + " in US = " + tokyo);
1785 }
1786 delete s;
1787
1788 s = TimeZone::createEnumerationForRegion("JP", ec);
1789 if (U_FAILURE(ec)) {
1790 dataerrln("Unable to create TimeZone enumeration for JP");
1791 return;
1792 }
1793 n = s->count(ec);
1794 la = false; tokyo = false;
1795
1796 for (i=0; i<n; ++i) {
1797 const UnicodeString* id = s->snext(ec);
1798 if (*id == (laZone)) {
1799 la = true;
1800 }
1801 if (*id == (tokyoZone)) {
1802 tokyo = true;
1803 }
1804 }
1805 if (la || !tokyo) {
1806 errln("FAIL: " + laZone + " in JP = " + la);
1807 errln("FAIL: " + tokyoZone + " in JP = " + tokyo);
1808 }
1809 StringEnumeration* s1 = TimeZone::createEnumerationForRegion("US", ec);
1810 StringEnumeration* s2 = TimeZone::createEnumerationForRegion("US", ec);
1811 if (U_FAILURE(ec)) {
1812 dataerrln("Unable to create TimeZone enumeration for US");
1813 return;
1814 }
1815 for(i=0;i<n;++i){
1816 const UnicodeString* id1 = s1->snext(ec);
1817 if(id1==nullptr || U_FAILURE(ec)){
1818 errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n,i);
1819 }
1820 TimeZone* tz1 = TimeZone::createTimeZone(*id1);
1821 for(int j=0; j<n;++j){
1822 const UnicodeString* id2 = s2->snext(ec);
1823 if(id2==nullptr || U_FAILURE(ec)){
1824 errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n,i);
1825 }
1826 TimeZone* tz2 = TimeZone::createTimeZone(*id2);
1827 if(tz1->hasSameRules(*tz2)){
1828 logln("ID1 : " + *id1+" == ID2 : " +*id2);
1829 }
1830 delete tz2;
1831 }
1832 delete tz1;
1833 }
1834 delete s1;
1835 delete s2;
1836 delete s;
1837 }
1838
TestHistorical()1839 void TimeZoneTest::TestHistorical() {
1840 const int32_t H = U_MILLIS_PER_HOUR;
1841 struct {
1842 const char* id;
1843 int32_t time; // epoch seconds
1844 int32_t offset; // total offset (millis)
1845 } DATA[] = {
1846 // Add transition points (before/after) as desired to test historical
1847 // behavior.
1848 {"America/Los_Angeles", 638963999, -8*H}, // Sun Apr 01 01:59:59 GMT-08:00 1990
1849 {"America/Los_Angeles", 638964000, -7*H}, // Sun Apr 01 03:00:00 GMT-07:00 1990
1850 {"America/Los_Angeles", 657104399, -7*H}, // Sun Oct 28 01:59:59 GMT-07:00 1990
1851 {"America/Los_Angeles", 657104400, -8*H}, // Sun Oct 28 01:00:00 GMT-08:00 1990
1852 {"America/Goose_Bay", -116445601, -4*H}, // Sun Apr 24 01:59:59 GMT-04:00 1966
1853 {"America/Goose_Bay", -116445600, -3*H}, // Sun Apr 24 03:00:00 GMT-03:00 1966
1854 {"America/Goose_Bay", -100119601, -3*H}, // Sun Oct 30 01:59:59 GMT-03:00 1966
1855 {"America/Goose_Bay", -100119600, -4*H}, // Sun Oct 30 01:00:00 GMT-04:00 1966
1856 {"America/Goose_Bay", -84391201, -4*H}, // Sun Apr 30 01:59:59 GMT-04:00 1967
1857 {"America/Goose_Bay", -84391200, -3*H}, // Sun Apr 30 03:00:00 GMT-03:00 1967
1858 {"America/Goose_Bay", -68670001, -3*H}, // Sun Oct 29 01:59:59 GMT-03:00 1967
1859 {"America/Goose_Bay", -68670000, -4*H}, // Sun Oct 29 01:00:00 GMT-04:00 1967
1860 {0, 0, 0}
1861 };
1862
1863 for (int32_t i=0; DATA[i].id!=0; ++i) {
1864 const char* id = DATA[i].id;
1865 TimeZone *tz = TimeZone::createTimeZone(id);
1866 UnicodeString s;
1867 if (tz == 0) {
1868 errln("FAIL: Cannot create %s", id);
1869 } else if (tz->getID(s) != UnicodeString(id)) {
1870 dataerrln((UnicodeString)"FAIL: createTimeZone(" + id + ") => " + s);
1871 } else {
1872 UErrorCode ec = U_ZERO_ERROR;
1873 int32_t raw, dst;
1874 UDate when = (double) DATA[i].time * U_MILLIS_PER_SECOND;
1875 tz->getOffset(when, false, raw, dst, ec);
1876 if (U_FAILURE(ec)) {
1877 errln("FAIL: getOffset");
1878 } else if ((raw+dst) != DATA[i].offset) {
1879 errln((UnicodeString)"FAIL: " + DATA[i].id + ".getOffset(" +
1880 //when + " = " +
1881 dateToString(when) + ") => " +
1882 raw + ", " + dst);
1883 } else {
1884 logln((UnicodeString)"Ok: " + DATA[i].id + ".getOffset(" +
1885 //when + " = " +
1886 dateToString(when) + ") => " +
1887 raw + ", " + dst);
1888 }
1889 }
1890 delete tz;
1891 }
1892 }
1893
TestEquivalentIDs()1894 void TimeZoneTest::TestEquivalentIDs() {
1895 int32_t n = TimeZone::countEquivalentIDs("PST");
1896 if (n < 2) {
1897 dataerrln((UnicodeString)"FAIL: countEquivalentIDs(PST) = " + n);
1898 } else {
1899 UBool sawLA = false;
1900 for (int32_t i=0; i<n; ++i) {
1901 UnicodeString id = TimeZone::getEquivalentID("PST", i);
1902 logln((UnicodeString)"" + i + " : " + id);
1903 if (id == UnicodeString("America/Los_Angeles")) {
1904 sawLA = true;
1905 }
1906 }
1907 if (!sawLA) {
1908 errln("FAIL: America/Los_Angeles should be in the list");
1909 }
1910 }
1911 }
1912
1913 // Test that a transition at the end of February is handled correctly.
TestFebruary()1914 void TimeZoneTest::TestFebruary() {
1915 UErrorCode status = U_ZERO_ERROR;
1916
1917 // Time zone with daylight savings time from the first Sunday in November
1918 // to the last Sunday in February.
1919 // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
1920 //
1921 // Note: In tzdata2007h, the rule had changed, so no actual zones uses
1922 // lastSun in Feb anymore.
1923 SimpleTimeZone tz1(-3 * U_MILLIS_PER_HOUR, // raw offset: 3h before (west of) GMT
1924 UNICODE_STRING("nov-feb", 7),
1925 UCAL_NOVEMBER, 1, UCAL_SUNDAY, // start: November, first, Sunday
1926 0, // midnight wall time
1927 UCAL_FEBRUARY, -1, UCAL_SUNDAY, // end: February, last, Sunday
1928 0, // midnight wall time
1929 status);
1930 if (U_FAILURE(status)) {
1931 errln("Unable to create the SimpleTimeZone(nov-feb): %s", u_errorName(status));
1932 return;
1933 }
1934
1935 // Now hardcode the same rules as for Brazil in tzdata 2006n, so that
1936 // we cover the intended code even when in the future zoneinfo hardcodes
1937 // these transition dates.
1938 SimpleTimeZone tz2(-3 * U_MILLIS_PER_HOUR, // raw offset: 3h before (west of) GMT
1939 UNICODE_STRING("nov-feb2", 8),
1940 UCAL_NOVEMBER, 1, -UCAL_SUNDAY, // start: November, 1 or after, Sunday
1941 0, // midnight wall time
1942 UCAL_FEBRUARY, -29, -UCAL_SUNDAY,// end: February, 29 or before, Sunday
1943 0, // midnight wall time
1944 status);
1945 if (U_FAILURE(status)) {
1946 errln("Unable to create the SimpleTimeZone(nov-feb2): %s", u_errorName(status));
1947 return;
1948 }
1949
1950 // Gregorian calendar with the UTC time zone for getting sample test date/times.
1951 GregorianCalendar gc(*TimeZone::getGMT(), status);
1952 if (U_FAILURE(status)) {
1953 dataerrln("Unable to create the UTC calendar: %s", u_errorName(status));
1954 return;
1955 }
1956
1957 struct {
1958 // UTC time.
1959 int32_t year, month, day, hour, minute, second;
1960 // Expected time zone offset in hours after GMT (negative=before GMT).
1961 int32_t offsetHours;
1962 } data[] = {
1963 { 2006, UCAL_NOVEMBER, 5, 02, 59, 59, -3 },
1964 { 2006, UCAL_NOVEMBER, 5, 03, 00, 00, -2 },
1965 { 2007, UCAL_FEBRUARY, 25, 01, 59, 59, -2 },
1966 { 2007, UCAL_FEBRUARY, 25, 02, 00, 00, -3 },
1967
1968 { 2007, UCAL_NOVEMBER, 4, 02, 59, 59, -3 },
1969 { 2007, UCAL_NOVEMBER, 4, 03, 00, 00, -2 },
1970 { 2008, UCAL_FEBRUARY, 24, 01, 59, 59, -2 },
1971 { 2008, UCAL_FEBRUARY, 24, 02, 00, 00, -3 },
1972
1973 { 2008, UCAL_NOVEMBER, 2, 02, 59, 59, -3 },
1974 { 2008, UCAL_NOVEMBER, 2, 03, 00, 00, -2 },
1975 { 2009, UCAL_FEBRUARY, 22, 01, 59, 59, -2 },
1976 { 2009, UCAL_FEBRUARY, 22, 02, 00, 00, -3 },
1977
1978 { 2009, UCAL_NOVEMBER, 1, 02, 59, 59, -3 },
1979 { 2009, UCAL_NOVEMBER, 1, 03, 00, 00, -2 },
1980 { 2010, UCAL_FEBRUARY, 28, 01, 59, 59, -2 },
1981 { 2010, UCAL_FEBRUARY, 28, 02, 00, 00, -3 }
1982 };
1983
1984 TimeZone *timezones[] = { &tz1, &tz2 };
1985
1986 TimeZone *tz;
1987 UDate dt;
1988 int32_t t, i, raw, dst;
1989 for (t = 0; t < UPRV_LENGTHOF(timezones); ++t) {
1990 tz = timezones[t];
1991 for (i = 0; i < UPRV_LENGTHOF(data); ++i) {
1992 gc.set(data[i].year, data[i].month, data[i].day,
1993 data[i].hour, data[i].minute, data[i].second);
1994 dt = gc.getTime(status);
1995 if (U_FAILURE(status)) {
1996 errln("test case %d.%d: bad date/time %04d-%02d-%02d %02d:%02d:%02d",
1997 t, i,
1998 data[i].year, data[i].month + 1, data[i].day,
1999 data[i].hour, data[i].minute, data[i].second);
2000 status = U_ZERO_ERROR;
2001 continue;
2002 }
2003 tz->getOffset(dt, false, raw, dst, status);
2004 if (U_FAILURE(status)) {
2005 errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) fails: %s",
2006 t, i,
2007 data[i].year, data[i].month + 1, data[i].day,
2008 data[i].hour, data[i].minute, data[i].second,
2009 u_errorName(status));
2010 status = U_ZERO_ERROR;
2011 } else if ((raw + dst) != data[i].offsetHours * U_MILLIS_PER_HOUR) {
2012 errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) returns %d+%d != %d",
2013 t, i,
2014 data[i].year, data[i].month + 1, data[i].day,
2015 data[i].hour, data[i].minute, data[i].second,
2016 raw, dst, data[i].offsetHours * U_MILLIS_PER_HOUR);
2017 }
2018 }
2019 }
2020 }
2021
TestCanonicalIDAPI()2022 void TimeZoneTest::TestCanonicalIDAPI() {
2023 // Bogus input string.
2024 UnicodeString bogus;
2025 bogus.setToBogus();
2026 UnicodeString canonicalID;
2027 UErrorCode ec = U_ZERO_ERROR;
2028 UnicodeString *pResult = &TimeZone::getCanonicalID(bogus, canonicalID, ec);
2029 assertEquals("TimeZone::getCanonicalID(bogus) should fail", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, ec);
2030 assertTrue("TimeZone::getCanonicalID(bogus) should return the dest string", pResult == &canonicalID);
2031
2032 // U_FAILURE on input.
2033 UnicodeString berlin("Europe/Berlin");
2034 ec = U_MEMORY_ALLOCATION_ERROR;
2035 pResult = &TimeZone::getCanonicalID(berlin, canonicalID, ec);
2036 assertEquals("TimeZone::getCanonicalID(failure) should fail", (int32_t)U_MEMORY_ALLOCATION_ERROR, ec);
2037 assertTrue("TimeZone::getCanonicalID(failure) should return the dest string", pResult == &canonicalID);
2038
2039 // Valid input should un-bogus the dest string.
2040 canonicalID.setToBogus();
2041 ec = U_ZERO_ERROR;
2042 pResult = &TimeZone::getCanonicalID(berlin, canonicalID, ec);
2043 assertSuccess("TimeZone::getCanonicalID(bogus dest) should succeed", ec, true);
2044 assertTrue("TimeZone::getCanonicalID(bogus dest) should return the dest string", pResult == &canonicalID);
2045 assertFalse("TimeZone::getCanonicalID(bogus dest) should un-bogus the dest string", canonicalID.isBogus());
2046 assertEquals("TimeZone::getCanonicalID(bogus dest) unexpected result", canonicalID, berlin, true);
2047 }
2048
TestCanonicalID()2049 void TimeZoneTest::TestCanonicalID() {
2050
2051 // Olson (IANA) tzdata used to have very few "Link"s long time ago.
2052 // This test case was written when most of CLDR canonical time zones are
2053 // defined as independent "Zone" in the TZ database.
2054 // Since then, the TZ maintainer found some historic rules in mid 20th century
2055 // were not really reliable, and many zones are now sharing rules.
2056 // As of TZ database release 2022a, there are quite a lot of zones defined
2057 // by "Link" to another zone, so the exception table below becomes really
2058 // big. It might be still useful to make sure CLDR zone aliases are consistent
2059 // with zone rules.
2060 static const struct {
2061 const char *alias; // link-from
2062 const char *zone; // link-to (A zone ID with "Zone" rule)
2063 } excluded1[] = {
2064 {"Africa/Accra", "Africa/Abidjan"},
2065 {"Africa/Addis_Ababa", "Africa/Nairobi"},
2066 {"Africa/Asmera", "Africa/Nairobi"},
2067 {"Africa/Bamako", "Africa/Abidjan"},
2068 {"Africa/Bangui", "Africa/Lagos"},
2069 {"Africa/Banjul", "Africa/Abidjan"},
2070 {"Africa/Blantyre", "Africa/Maputo"},
2071 {"Africa/Brazzaville", "Africa/Lagos"},
2072 {"Africa/Bujumbura", "Africa/Maputo"},
2073 {"Africa/Conakry", "Africa/Abidjan"},
2074 {"Africa/Dakar", "Africa/Abidjan"},
2075 {"Africa/Dar_es_Salaam", "Africa/Nairobi"},
2076 {"Africa/Djibouti", "Africa/Nairobi"},
2077 {"Africa/Douala", "Africa/Lagos"},
2078 {"Africa/Freetown", "Africa/Abidjan"},
2079 {"Africa/Gaborone", "Africa/Maputo"},
2080 {"Africa/Harare", "Africa/Maputo"},
2081 {"Africa/Kampala", "Africa/Nairobi"},
2082 {"Africa/Khartoum", "Africa/Juba"},
2083 {"Africa/Kigali", "Africa/Maputo"},
2084 {"Africa/Kinshasa", "Africa/Lagos"},
2085 {"Africa/Libreville", "Africa/Lagos"},
2086 {"Africa/Lome", "Africa/Abidjan"},
2087 {"Africa/Luanda", "Africa/Lagos"},
2088 {"Africa/Lubumbashi", "Africa/Maputo"},
2089 {"Africa/Lusaka", "Africa/Maputo"},
2090 {"Africa/Maseru", "Africa/Johannesburg"},
2091 {"Africa/Malabo", "Africa/Lagos"},
2092 {"Africa/Mbabane", "Africa/Johannesburg"},
2093 {"Africa/Mogadishu", "Africa/Nairobi"},
2094 {"Africa/Niamey", "Africa/Lagos"},
2095 {"Africa/Nouakchott", "Africa/Abidjan"},
2096 {"Africa/Ouagadougou", "Africa/Abidjan"},
2097 {"Africa/Porto-Novo", "Africa/Lagos"},
2098 {"Africa/Sao_Tome", "Africa/Abidjan"},
2099 {"America/Antigua", "America/Puerto_Rico"},
2100 {"America/Anguilla", "America/Puerto_Rico"},
2101 {"America/Aruba", "America/Puerto_Rico"},
2102 {"America/Atikokan", "America/Panama"},
2103 {"America/Blanc-Sablon", "America/Puerto_Rico"},
2104 {"America/Cayman", "America/Panama"},
2105 {"America/Coral_Harbour", "America/Panama"},
2106 {"America/Creston", "America/Phoenix"},
2107 {"America/Curacao", "America/Puerto_Rico"},
2108 {"America/Dominica", "America/Puerto_Rico"},
2109 {"America/Grenada", "America/Puerto_Rico"},
2110 {"America/Guadeloupe", "America/Puerto_Rico"},
2111 {"America/Kralendijk", "America/Puerto_Rico"},
2112 {"America/Lower_Princes", "America/Puerto_Rico"},
2113 {"America/Marigot", "America/Puerto_Rico"},
2114 {"America/Montreal", "America/Toronto"},
2115 {"America/Montserrat", "America/Puerto_Rico"},
2116 {"America/Nassau", "America/Toronto"},
2117 {"America/Nipigon", "America/Toronto"},
2118 {"America/Pangnirtung", "America/Iqaluit"},
2119 {"America/Port_of_Spain", "America/Puerto_Rico"},
2120 {"America/Rainy_River", "America/Winnipeg"},
2121 {"America/Santa_Isabel", "America/Tijuana"},
2122 {"America/Shiprock", "America/Denver"},
2123 {"America/St_Barthelemy", "America/Puerto_Rico"},
2124 {"America/St_Kitts", "America/Puerto_Rico"},
2125 {"America/St_Lucia", "America/Puerto_Rico"},
2126 {"America/St_Thomas", "America/Puerto_Rico"},
2127 {"America/St_Vincent", "America/Puerto_Rico"},
2128 {"America/Thunder_Bay", "America/Toronto"},
2129 {"America/Tortola", "America/Puerto_Rico"},
2130 {"America/Virgin", "America/Puerto_Rico"},
2131 {"America/Yellowknife", "America/Edmonton"},
2132 {"Antarctica/DumontDUrville", "Pacific/Port_Moresby"},
2133 {"Antarctica/South_Pole", "Antarctica/McMurdo"},
2134 {"Antarctica/Syowa", "Asia/Riyadh"},
2135 {"Arctic/Longyearbyen", "Europe/Berlin"},
2136 {"Asia/Aden", "Asia/Riyadh"},
2137 {"Asia/Brunei", "Asia/Kuching"},
2138 {"Asia/Kuala_Lumpur", "Asia/Singapore"},
2139 {"Asia/Kuwait", "Asia/Riyadh"},
2140 {"Asia/Muscat", "Asia/Dubai"},
2141 {"Asia/Phnom_Penh", "Asia/Bangkok"},
2142 {"Asia/Qatar", "Asia/Bahrain"},
2143 {"Asia/Urumqi", "Antarctica/Vostok"},
2144 {"Asia/Vientiane", "Asia/Bangkok"},
2145 {"Atlantic/Jan_Mayen", "Europe/Berlin"},
2146 {"Atlantic/Reykjavik", "Africa/Abidjan"},
2147 {"Atlantic/St_Helena", "Africa/Abidjan"},
2148 {"Australia/Currie", "Australia/Hobart"},
2149 {"Australia/Tasmania", "Australia/Hobart"},
2150 {"Europe/Bratislava", "Europe/Prague"},
2151 {"Europe/Brussels", "Europe/Amsterdam"},
2152 {"Europe/Busingen", "Europe/Zurich"},
2153 {"Europe/Copenhagen", "Europe/Berlin"},
2154 {"Europe/Guernsey", "Europe/London"},
2155 {"Europe/Isle_of_Man", "Europe/London"},
2156 {"Europe/Jersey", "Europe/London"},
2157 {"Europe/Ljubljana", "Europe/Belgrade"},
2158 {"Europe/Luxembourg", "Europe/Amsterdam"},
2159 {"Europe/Mariehamn", "Europe/Helsinki"},
2160 {"Europe/Monaco", "Europe/Paris"},
2161 {"Europe/Oslo", "Europe/Berlin"},
2162 {"Europe/Podgorica", "Europe/Belgrade"},
2163 {"Europe/San_Marino", "Europe/Rome"},
2164 {"Europe/Sarajevo", "Europe/Belgrade"},
2165 {"Europe/Skopje", "Europe/Belgrade"},
2166 {"Europe/Stockholm", "Europe/Berlin"},
2167 {"Europe/Uzhgorod", "Europe/Kiev"},
2168 {"Europe/Vaduz", "Europe/Zurich"},
2169 {"Europe/Vatican", "Europe/Rome"},
2170 {"Europe/Zagreb", "Europe/Belgrade"},
2171 {"Europe/Zaporozhye", "Europe/Kiev"},
2172 {"Indian/Antananarivo", "Africa/Nairobi"},
2173 {"Indian/Christmas", "Asia/Bangkok"},
2174 {"Indian/Cocos", "Asia/Rangoon"},
2175 {"Indian/Comoro", "Africa/Nairobi"},
2176 {"Indian/Mahe", "Asia/Dubai"},
2177 {"Indian/Maldives", "Indian/Kerguelen"},
2178 {"Indian/Mayotte", "Africa/Nairobi"},
2179 {"Indian/Reunion", "Asia/Dubai"},
2180 {"Pacific/Auckland", "Antarctica/McMurdo"},
2181 {"Pacific/Johnston", "Pacific/Honolulu"},
2182 {"Pacific/Majuro", "Pacific/Funafuti"},
2183 {"Pacific/Midway", "Pacific/Pago_Pago"},
2184 {"Pacific/Ponape", "Pacific/Guadalcanal"},
2185 {"Pacific/Saipan", "Pacific/Guam"},
2186 {"Pacific/Tarawa", "Pacific/Funafuti"},
2187 {"Pacific/Truk", "Pacific/Port_Moresby"},
2188 {"Pacific/Wake", "Pacific/Funafuti"},
2189 {"Pacific/Wallis", "Pacific/Funafuti"},
2190 {0, 0}
2191 };
2192
2193 // Following IDs are aliases of Etc/GMT in CLDR,
2194 // but Olson tzdata has 3 independent definitions
2195 // for Etc/GMT, Etc/UTC, Etc/UCT.
2196 // Until we merge them into one equivalent group
2197 // in zoneinfo.res, we exclude them in the test
2198 // below.
2199 static const char* excluded2[] = {
2200 "Etc/UCT", "UCT",
2201 "Etc/UTC", "UTC",
2202 "Etc/Universal", "Universal",
2203 "Etc/Zulu", "Zulu", 0
2204 };
2205
2206 // Walk through equivalency groups
2207 UErrorCode ec = U_ZERO_ERROR;
2208 int32_t s_length, i, j, k;
2209 StringEnumeration* s = TimeZone::createEnumeration(ec);
2210 if (U_FAILURE(ec)) {
2211 dataerrln("Unable to create TimeZone enumeration");
2212 return;
2213 }
2214 UnicodeString canonicalID, tmpCanonical;
2215 s_length = s->count(ec);
2216 for (i = 0; i < s_length;++i) {
2217 const UnicodeString *tzid = s->snext(ec);
2218 int32_t nEquiv = TimeZone::countEquivalentIDs(*tzid);
2219 if (nEquiv == 0) {
2220 continue;
2221 }
2222 UBool bFoundCanonical = false;
2223 // Make sure getCanonicalID returns the exact same result
2224 // for all entries within a same equivalency group with some
2225 // exceptions listed in exluded1.
2226 // Also, one of them must be canonical id.
2227 for (j = 0; j < nEquiv; j++) {
2228 UnicodeString tmp = TimeZone::getEquivalentID(*tzid, j);
2229 TimeZone::getCanonicalID(tmp, tmpCanonical, ec);
2230 if (U_FAILURE(ec)) {
2231 errln((UnicodeString)"FAIL: getCanonicalID(" + tmp + ") failed.");
2232 ec = U_ZERO_ERROR;
2233 continue;
2234 }
2235 // Some exceptional cases
2236 for (k = 0; excluded1[k].alias != 0; k++) {
2237 if (tmpCanonical == excluded1[k].alias) {
2238 tmpCanonical = excluded1[k].zone;
2239 break;
2240 }
2241 }
2242 if (j == 0) {
2243 canonicalID = tmpCanonical;
2244 } else if (canonicalID != tmpCanonical) {
2245 errln("FAIL: getCanonicalID(" + tmp + ") returned " + tmpCanonical + " expected:" + canonicalID);
2246 }
2247
2248 if (canonicalID == tmp) {
2249 bFoundCanonical = true;
2250 }
2251 }
2252 // At least one ID in an equvalency group must match the
2253 // canonicalID
2254 if (bFoundCanonical == false) {
2255 // test exclusion because of differences between Olson tzdata and CLDR
2256 UBool isExcluded = false;
2257 for (k = 0; excluded2[k] != 0; k++) {
2258 if (*tzid == UnicodeString(excluded2[k])) {
2259 isExcluded = true;
2260 break;
2261 }
2262 }
2263 if (isExcluded) {
2264 continue;
2265 }
2266 errln((UnicodeString)"FAIL: No timezone ids match the canonical ID " + canonicalID);
2267 }
2268 }
2269 delete s;
2270
2271 // Testing some special cases
2272 static const struct {
2273 const char *id;
2274 const char *expected;
2275 UBool isSystem;
2276 } data[] = {
2277 {"GMT-03", "GMT-03:00", false},
2278 {"GMT+4", "GMT+04:00", false},
2279 {"GMT-055", "GMT-00:55", false},
2280 {"GMT+430", "GMT+04:30", false},
2281 {"GMT-12:15", "GMT-12:15", false},
2282 {"GMT-091015", "GMT-09:10:15", false},
2283 {"GMT+1:90", 0, false},
2284 {"America/Argentina/Buenos_Aires", "America/Buenos_Aires", true},
2285 {"Etc/Unknown", "Etc/Unknown", false},
2286 {"bogus", 0, false},
2287 {"", 0, false},
2288 {"America/Marigot", "America/Marigot", true}, // Olson link, but CLDR canonical (#8953)
2289 {"Europe/Bratislava", "Europe/Bratislava", true}, // Same as above
2290 {0, 0, false}
2291 };
2292
2293 UBool isSystemID;
2294 for (i = 0; data[i].id != 0; i++) {
2295 TimeZone::getCanonicalID(UnicodeString(data[i].id), canonicalID, isSystemID, ec);
2296 if (U_FAILURE(ec)) {
2297 if (ec != U_ILLEGAL_ARGUMENT_ERROR || data[i].expected != 0) {
2298 errln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
2299 + "\") returned status U_ILLEGAL_ARGUMENT_ERROR");
2300 }
2301 ec = U_ZERO_ERROR;
2302 continue;
2303 }
2304 if (canonicalID != data[i].expected) {
2305 dataerrln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
2306 + "\") returned " + canonicalID + " - expected: " + data[i].expected);
2307 }
2308 if (isSystemID != data[i].isSystem) {
2309 dataerrln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
2310 + "\") set " + isSystemID + " to isSystemID");
2311 }
2312 }
2313 }
2314
2315 //
2316 // Test Display Names, choosing zones and lcoales where there are multiple
2317 // meta-zones defined.
2318 //
2319 static struct {
2320 const char *zoneName;
2321 const char *localeName;
2322 UBool summerTime;
2323 TimeZone::EDisplayType style;
2324 const char *expectedDisplayName; }
2325 zoneDisplayTestData [] = {
2326 // zone id locale summer format expected display name
2327 {"Europe/London", "en", false, TimeZone::SHORT, "GMT"},
2328 {"Europe/London", "en", false, TimeZone::LONG, "Greenwich Mean Time"},
2329 {"Europe/London", "en", true, TimeZone::SHORT, "GMT+1" /*"BST"*/},
2330 {"Europe/London", "en", true, TimeZone::LONG, "British Summer Time"},
2331
2332 {"America/Anchorage", "en", false, TimeZone::SHORT, "AKST"},
2333 {"America/Anchorage", "en", false, TimeZone::LONG, "Alaska Standard Time"},
2334 {"America/Anchorage", "en", true, TimeZone::SHORT, "AKDT"},
2335 {"America/Anchorage", "en", true, TimeZone::LONG, "Alaska Daylight Time"},
2336
2337 // Southern Hemisphere, all data from meta:Australia_Western
2338 {"Australia/Perth", "en", false, TimeZone::SHORT, "GMT+8"/*"AWST"*/},
2339 {"Australia/Perth", "en", false, TimeZone::LONG, "Australian Western Standard Time"},
2340 // Note: Perth does not observe DST currently. When display name is missing,
2341 // the localized GMT format with the current offset is used even daylight name was
2342 // requested. See #9350.
2343 {"Australia/Perth", "en", true, TimeZone::SHORT, "GMT+8"/*"AWDT"*/},
2344 {"Australia/Perth", "en", true, TimeZone::LONG, "Australian Western Daylight Time"},
2345
2346 {"America/Sao_Paulo", "en", false, TimeZone::SHORT, "GMT-3"/*"BRT"*/},
2347 {"America/Sao_Paulo", "en", false, TimeZone::LONG, "Brasilia Standard Time"},
2348
2349 // Per https://mm.icann.org/pipermail/tz-announce/2019-July/000056.html
2350 // Brazil has canceled DST and will stay on standard time indefinitely.
2351 // {"America/Sao_Paulo", "en", true, TimeZone::SHORT, "GMT-2"/*"BRST"*/},
2352 // {"America/Sao_Paulo", "en", true, TimeZone::LONG, "Brasilia Summer Time"},
2353
2354 // No Summer Time, but had it before 1983.
2355 {"Pacific/Honolulu", "en", false, TimeZone::SHORT, "HST"},
2356 {"Pacific/Honolulu", "en", false, TimeZone::LONG, "Hawaii-Aleutian Standard Time"},
2357 {"Pacific/Honolulu", "en", true, TimeZone::SHORT, "HDT"},
2358 {"Pacific/Honolulu", "en", true, TimeZone::LONG, "Hawaii-Aleutian Daylight Time"},
2359
2360 // Northern, has Summer, not commonly used.
2361 {"Europe/Helsinki", "en", false, TimeZone::SHORT, "GMT+2"/*"EET"*/},
2362 {"Europe/Helsinki", "en", false, TimeZone::LONG, "Eastern European Standard Time"},
2363 {"Europe/Helsinki", "en", true, TimeZone::SHORT, "GMT+3"/*"EEST"*/},
2364 {"Europe/Helsinki", "en", true, TimeZone::LONG, "Eastern European Summer Time"},
2365
2366 // Repeating the test data for DST. The test data below trigger the problem reported
2367 // by Ticket#6644
2368 {"Europe/London", "en", true, TimeZone::SHORT, "GMT+1" /*"BST"*/},
2369 {"Europe/London", "en", true, TimeZone::LONG, "British Summer Time"},
2370
2371 {nullptr, nullptr, false, TimeZone::SHORT, nullptr} // nullptr values terminate list
2372 };
2373
TestDisplayNamesMeta()2374 void TimeZoneTest::TestDisplayNamesMeta() {
2375 UErrorCode status = U_ZERO_ERROR;
2376 GregorianCalendar cal(*TimeZone::getGMT(), status);
2377 if (failure(status, "GregorianCalendar", true)) return;
2378
2379 UBool sawAnError = false;
2380 for (int testNum = 0; zoneDisplayTestData[testNum].zoneName != nullptr; testNum++) {
2381 Locale locale = Locale::createFromName(zoneDisplayTestData[testNum].localeName);
2382 TimeZone *zone = TimeZone::createTimeZone(zoneDisplayTestData[testNum].zoneName);
2383 UnicodeString displayName;
2384 zone->getDisplayName(zoneDisplayTestData[testNum].summerTime,
2385 zoneDisplayTestData[testNum].style,
2386 locale,
2387 displayName);
2388 if (displayName != zoneDisplayTestData[testNum].expectedDisplayName) {
2389 char name[100];
2390 UErrorCode status = U_ZERO_ERROR;
2391 displayName.extract(name, 100, nullptr, status);
2392 if (isDevelopmentBuild) {
2393 sawAnError = true;
2394 dataerrln("Incorrect time zone display name. zone = \"%s\",\n"
2395 " locale = \"%s\", style = %s, Summertime = %d\n"
2396 " Expected \"%s\", "
2397 " Got \"%s\"\n Error: %s", zoneDisplayTestData[testNum].zoneName,
2398 zoneDisplayTestData[testNum].localeName,
2399 zoneDisplayTestData[testNum].style==TimeZone::SHORT ?
2400 "SHORT" : "LONG",
2401 zoneDisplayTestData[testNum].summerTime,
2402 zoneDisplayTestData[testNum].expectedDisplayName,
2403 name,
2404 u_errorName(status));
2405 } else {
2406 logln("Incorrect time zone display name. zone = \"%s\",\n"
2407 " locale = \"%s\", style = %s, Summertime = %d\n"
2408 " Expected \"%s\", "
2409 " Got \"%s\"\n", zoneDisplayTestData[testNum].zoneName,
2410 zoneDisplayTestData[testNum].localeName,
2411 zoneDisplayTestData[testNum].style==TimeZone::SHORT ?
2412 "SHORT" : "LONG",
2413 zoneDisplayTestData[testNum].summerTime,
2414 zoneDisplayTestData[testNum].expectedDisplayName,
2415 name);
2416 }
2417 }
2418 delete zone;
2419 }
2420 if (sawAnError) {
2421 dataerrln("***Note: Errors could be the result of changes to zoneStrings locale data");
2422 }
2423 }
2424
TestGetRegion()2425 void TimeZoneTest::TestGetRegion()
2426 {
2427 static const struct {
2428 const char *id;
2429 const char *region;
2430 } data[] = {
2431 {"America/Los_Angeles", "US"},
2432 {"America/Indianapolis", "US"}, // CLDR canonical, Olson backward
2433 {"America/Indiana/Indianapolis", "US"}, // CLDR alias
2434 {"Mexico/General", "MX"}, // Link America/Mexico_City, Olson backward
2435 {"Etc/UTC", "001"},
2436 {"EST5EDT", "001"},
2437 {"PST", "US"}, // Link America/Los_Angeles
2438 {"Europe/Helsinki", "FI"},
2439 {"Europe/Mariehamn", "AX"}, // Link Europe/Helsinki, but in zone.tab
2440 {"Asia/Riyadh", "SA"},
2441 // tz file solar87 was removed from tzdata2013i
2442 // {"Asia/Riyadh87", "001"}, // this should be "SA" actually, but not in zone.tab
2443 {"Atlantic/Jan_Mayen", "SJ"},
2444 {"Pacific/Truk", "FM"},
2445 {"Etc/Unknown", 0}, // CLDR canonical, but not a sysmte zone ID
2446 {"bogus", 0}, // bogus
2447 {"GMT+08:00", 0}, // a custom ID, not a system zone ID
2448 {0, 0}
2449 };
2450
2451 int32_t i;
2452 char region[4];
2453 UErrorCode sts;
2454 for (i = 0; data[i].id; i++) {
2455 sts = U_ZERO_ERROR;
2456 TimeZone::getRegion(data[i].id, region, sizeof(region), sts);
2457 if (U_SUCCESS(sts)) {
2458 if (data[i].region == 0) {
2459 errln((UnicodeString)"Fail: getRegion(\"" + data[i].id + "\") returns "
2460 + region + " [expected: U_ILLEGAL_ARGUMENT_ERROR]");
2461 } else if (uprv_strcmp(region, data[i].region) != 0) {
2462 errln((UnicodeString)"Fail: getRegion(\"" + data[i].id + "\") returns "
2463 + region + " [expected: " + data[i].region + "]");
2464 }
2465 } else if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
2466 if (data[i].region != 0) {
2467 dataerrln((UnicodeString)"Fail: getRegion(\"" + data[i].id
2468 + "\") returns error status U_ILLEGAL_ARGUMENT_ERROR [expected: "
2469 + data[i].region + "]");
2470 }
2471 } else {
2472 errln((UnicodeString)"Fail: getRegion(\"" + data[i].id
2473 + "\") returns an unexpected error status");
2474 }
2475 }
2476
2477 // Extra test cases for short buffer
2478 int32_t len;
2479 char region2[2];
2480 sts = U_ZERO_ERROR;
2481
2482 len = TimeZone::getRegion("America/New_York", region2, sizeof(region2), sts);
2483 if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
2484 dataerrln("Error calling TimeZone::getRegion");
2485 } else {
2486 if (sts != U_STRING_NOT_TERMINATED_WARNING) {
2487 errln("Expected U_STRING_NOT_TERMINATED_WARNING");
2488 }
2489 if (len != 2) { // length of "US"
2490 errln("Incorrect result length");
2491 }
2492 if (uprv_strncmp(region2, "US", 2) != 0) {
2493 errln("Incorrect result");
2494 }
2495 }
2496
2497 char region1[1];
2498 sts = U_ZERO_ERROR;
2499
2500 len = TimeZone::getRegion("America/Chicago", region1, sizeof(region1), sts);
2501 if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
2502 dataerrln("Error calling TimeZone::getRegion");
2503 } else {
2504 if (sts != U_BUFFER_OVERFLOW_ERROR) {
2505 errln("Expected U_BUFFER_OVERFLOW_ERROR");
2506 }
2507 if (len != 2) { // length of "US"
2508 errln("Incorrect result length");
2509 }
2510 }
2511 }
2512
TestGetUnknown()2513 void TimeZoneTest::TestGetUnknown() {
2514 const TimeZone &unknown = TimeZone::getUnknown();
2515 UnicodeString expectedID = UNICODE_STRING_SIMPLE("Etc/Unknown");
2516 UnicodeString id;
2517 assertEquals("getUnknown() wrong ID", expectedID, unknown.getID(id));
2518 assertTrue("getUnknown() wrong offset", 0 == unknown.getRawOffset());
2519 assertFalse("getUnknown() uses DST", unknown.useDaylightTime());
2520 }
2521
TestGetGMT()2522 void TimeZoneTest::TestGetGMT() {
2523 const TimeZone *gmt = TimeZone::getGMT();
2524 UnicodeString expectedID = UNICODE_STRING_SIMPLE("GMT");
2525 UnicodeString id;
2526 assertEquals("getGMT() wrong ID", expectedID, gmt->getID(id));
2527 assertTrue("getGMT() wrong offset", 0 == gmt->getRawOffset());
2528 assertFalse("getGMT() uses DST", gmt->useDaylightTime());
2529 }
2530
TestGetWindowsID()2531 void TimeZoneTest::TestGetWindowsID() {
2532 static const struct {
2533 const char *id;
2534 const char *winid;
2535 } TESTDATA[] = {
2536 {"America/New_York", "Eastern Standard Time"},
2537 {"America/Montreal", "Eastern Standard Time"},
2538 {"America/Los_Angeles", "Pacific Standard Time"},
2539 {"America/Vancouver", "Pacific Standard Time"},
2540 {"Asia/Shanghai", "China Standard Time"},
2541 {"Asia/Chongqing", "China Standard Time"},
2542 {"America/Indianapolis", "US Eastern Standard Time"}, // CLDR canonical name
2543 {"America/Indiana/Indianapolis", "US Eastern Standard Time"}, // tzdb canonical name
2544 {"Asia/Khandyga", "Yakutsk Standard Time"},
2545 {"Australia/Eucla", "Aus Central W. Standard Time"}, // formerly no Windows ID mapping, now has one
2546 {"Bogus", ""},
2547 {0, 0},
2548 };
2549
2550 for (int32_t i = 0; TESTDATA[i].id != 0; i++) {
2551 UErrorCode sts = U_ZERO_ERROR;
2552 UnicodeString windowsID;
2553
2554 TimeZone::getWindowsID(UnicodeString(TESTDATA[i].id), windowsID, sts);
2555 assertSuccess(TESTDATA[i].id, sts);
2556 assertEquals(TESTDATA[i].id, UnicodeString(TESTDATA[i].winid), windowsID, true);
2557 }
2558 }
2559
TestGetIDForWindowsID()2560 void TimeZoneTest::TestGetIDForWindowsID() {
2561 static const struct {
2562 const char *winid;
2563 const char *region;
2564 const char *id;
2565 } TESTDATA[] = {
2566 {"Eastern Standard Time", 0, "America/New_York"},
2567 {"Eastern Standard Time", "US", "America/New_York"},
2568 {"Eastern Standard Time", "CA", "America/Toronto"},
2569 {"Eastern Standard Time", "CN", "America/New_York"},
2570 {"China Standard Time", 0, "Asia/Shanghai"},
2571 {"China Standard Time", "CN", "Asia/Shanghai"},
2572 {"China Standard Time", "HK", "Asia/Hong_Kong"},
2573 {"Mid-Atlantic Standard Time", 0, ""}, // No tz database mapping
2574 {"Bogus", 0, ""},
2575 {0, 0, 0},
2576 };
2577
2578 for (int32_t i = 0; TESTDATA[i].winid != 0; i++) {
2579 UErrorCode sts = U_ZERO_ERROR;
2580 UnicodeString id;
2581
2582 TimeZone::getIDForWindowsID(UnicodeString(TESTDATA[i].winid), TESTDATA[i].region,
2583 id, sts);
2584 assertSuccess(UnicodeString(TESTDATA[i].winid) + "/" + TESTDATA[i].region, sts);
2585 assertEquals(UnicodeString(TESTDATA[i].winid) + "/" + TESTDATA[i].region, TESTDATA[i].id, id, true);
2586 }
2587 }
2588
TestCasablancaNameAndOffset22041()2589 void TimeZoneTest::TestCasablancaNameAndOffset22041() {
2590 std::unique_ptr<TimeZone> zone(TimeZone::createTimeZone("Africa/Casablanca"));
2591 UnicodeString standardName, summerName;
2592 zone->getDisplayName(false, TimeZone::LONG, Locale::getEnglish(), standardName);
2593 zone->getDisplayName(true, TimeZone::LONG, Locale::getEnglish(), summerName);
2594 int32_t raw, dst;
2595 UErrorCode status = U_ZERO_ERROR;
2596 zone->getOffset(Calendar::getNow(), false, raw, dst, status);
2597 assertEquals(u"TimeZone name for Africa/Casablanca should not contain '+02' since it is located in UTC, but got "
2598 + standardName, -1, standardName.indexOf("+02"));
2599 assertEquals(u"TimeZone name for Africa/Casablanca should not contain '+02' since it is located in UTC, but got "
2600 + summerName, -1, summerName.indexOf("+02"));
2601 assertEquals("getRawOffset() and the raw from getOffset(now, false, raw, dst, status) should not be different but got",
2602 zone->getRawOffset(), raw);
2603 }
2604
TestRawOffsetAndOffsetConsistency22041()2605 void TimeZoneTest::TestRawOffsetAndOffsetConsistency22041() {
2606 UErrorCode status = U_ZERO_ERROR;
2607 LocalPointer<StringEnumeration> s(TimeZone::createEnumeration(status));
2608 if (U_FAILURE(status)) {
2609 dataerrln("Unable to create TimeZone enumeration");
2610 return;
2611 }
2612 const char* tz;
2613 UDate now = Calendar::getNow();
2614 while ((tz = s->next(nullptr, status)) != nullptr && U_SUCCESS(status)) {
2615 std::unique_ptr<TimeZone> zone(TimeZone::createTimeZone(tz));
2616 int32_t raw, dst;
2617 zone->getOffset(now, false, raw, dst, status);
2618 if (U_FAILURE(status)) {
2619 errln("TimeZone '%s' getOffset() return error", tz);
2620 }
2621 assertEquals(u"TimeZone '" + UnicodeString(tz) +
2622 u"' getRawOffset() and the raw from getOffset(now, false, raw, dst, status) should not be different but got",
2623 zone->getRawOffset(), raw);
2624 }
2625 }
2626
TestGMTMinus24ICU22526()2627 void TimeZoneTest::TestGMTMinus24ICU22526() {
2628 UErrorCode status = U_ZERO_ERROR;
2629 LocalPointer<TimeZone> tz(TimeZone::createTimeZone("GMT-23:59"), status);
2630 U_ASSERT(U_SUCCESS(status));
2631 GregorianCalendar gc(tz.orphan(), status);
2632 gc.setTime(123456789, status);
2633 gc.get(UCAL_MONTH, status);
2634 }
2635 #endif /* #if !UCONFIG_NO_FORMATTING */
2636