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