1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /***********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_FORMATTING
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"
26 #define CASE(id,test) case id: \
29 logln(#test "---"); logln(""); \
34 // *****************************************************************************
36 // *****************************************************************************
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);
47 void TimeZoneTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
50 logln("TestSuite TestTimeZone");
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(TestGetWindowsID
);
76 TESTCASE_AUTO(TestGetIDForWindowsID
);
80 const int32_t TimeZoneTest::millisPerHour
= 3600000;
82 // ---------------------------------------------------------------------------------
85 * Generic API testing for API coverage.
88 TimeZoneTest::TestGenericAPI()
90 UnicodeString
id("NewGMT");
91 int32_t offset
= 12345;
93 SimpleTimeZone
*zone
= new SimpleTimeZone(offset
, id
);
94 if (zone
->useDaylightTime()) errln("FAIL: useDaylightTime should return FALSE");
96 TimeZone
* zoneclone
= zone
->clone();
97 if (!(*zoneclone
== *zone
)) errln("FAIL: clone or operator== failed");
98 zoneclone
->setID("abc");
99 if (!(*zoneclone
!= *zone
)) errln("FAIL: clone or operator!= failed");
102 zoneclone
= zone
->clone();
103 if (!(*zoneclone
== *zone
)) errln("FAIL: clone or operator== failed");
104 zoneclone
->setRawOffset(45678);
105 if (!(*zoneclone
!= *zone
)) errln("FAIL: clone or operator!= failed");
107 SimpleTimeZone
copy(*zone
);
108 if (!(copy
== *zone
)) errln("FAIL: copy constructor or operator== failed");
109 copy
= *(SimpleTimeZone
*)zoneclone
;
110 if (!(copy
== *zoneclone
)) errln("FAIL: assignment operator or operator== failed");
112 TimeZone
* saveDefault
= TimeZone::createDefault();
113 logln((UnicodeString
)"TimeZone::createDefault() => " + saveDefault
->getID(id
));
115 TimeZone::adoptDefault(zone
);
116 TimeZone
* defaultzone
= TimeZone::createDefault();
117 if (defaultzone
== zone
||
118 !(*defaultzone
== *zone
))
119 errln("FAIL: createDefault failed");
120 TimeZone::adoptDefault(saveDefault
);
124 logln("call uprv_timezone() which uses the host");
125 logln("to get the difference in seconds between coordinated universal");
126 logln("time and local time. E.g., -28,800 for PST (GMT-8hrs)");
128 int32_t tzoffset
= uprv_timezone();
129 if ((tzoffset
% 900) != 0) {
131 * Ticket#6364 and #7648
132 * A few time zones are using GMT offests not a multiple of 15 minutes.
133 * Therefore, we should not interpret such case as an error.
134 * We downgrade this from errln to infoln. When we see this message,
135 * we should examine if it is ignorable or not.
137 infoln("WARNING: t_timezone may be incorrect. It is not a multiple of 15min.", tzoffset
);
140 TimeZone
* hostZone
= TimeZone::detectHostTimeZone();
141 /* Host time zone's offset should match the offset returned by uprv_timezone() */
142 if (hostZone
->getRawOffset() != tzoffset
* (-1000)) {
143 errln("FAIL: detectHostTimeZone()'s raw offset != host timezone's offset");
147 UErrorCode status
= U_ZERO_ERROR
;
148 const char* tzver
= TimeZone::getTZDataVersion(status
);
149 if (U_FAILURE(status
)) {
150 errcheckln(status
, "FAIL: getTZDataVersion failed - %s", u_errorName(status
));
151 } else if (uprv_strlen(tzver
) != 5 /* 4 digits + 1 letter */) {
152 errln((UnicodeString
)"FAIL: getTZDataVersion returned " + tzver
);
154 logln((UnicodeString
)"tzdata version: " + tzver
);
158 // ---------------------------------------------------------------------------------
161 * Test the setStartRule/setEndRule API calls.
164 TimeZoneTest::TestRuleAPI()
166 UErrorCode status
= U_ZERO_ERROR
;
168 UDate offset
= 60*60*1000*1.75; // Pick a weird offset
169 SimpleTimeZone
*zone
= new SimpleTimeZone((int32_t)offset
, "TestZone");
170 if (zone
->useDaylightTime()) errln("FAIL: useDaylightTime should return FALSE");
172 // Establish our expected transition times. Do this with a non-DST
173 // calendar with the (above) declared local offset.
174 GregorianCalendar
*gc
= new GregorianCalendar(*zone
, status
);
175 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
177 gc
->set(1990, UCAL_MARCH
, 1);
178 UDate marchOneStd
= gc
->getTime(status
); // Local Std time midnight
180 gc
->set(1990, UCAL_JULY
, 1);
181 UDate julyOneStd
= gc
->getTime(status
); // Local Std time midnight
182 if (failure(status
, "GregorianCalendar::getTime")) return;
184 // Starting and ending hours, WALL TIME
185 int32_t startHour
= (int32_t)(2.25 * 3600000);
186 int32_t endHour
= (int32_t)(3.5 * 3600000);
188 zone
->setStartRule(UCAL_MARCH
, 1, 0, startHour
, status
);
189 zone
->setEndRule (UCAL_JULY
, 1, 0, endHour
, status
);
192 gc
= new GregorianCalendar(*zone
, status
);
193 if (failure(status
, "new GregorianCalendar")) return;
195 UDate marchOne
= marchOneStd
+ startHour
;
196 UDate julyOne
= julyOneStd
+ endHour
- 3600000; // Adjust from wall to Std time
198 UDate expMarchOne
= 636251400000.0;
199 if (marchOne
!= expMarchOne
)
201 errln((UnicodeString
)"FAIL: Expected start computed as " + marchOne
+
202 " = " + dateToString(marchOne
));
203 logln((UnicodeString
)" Should be " + expMarchOne
+
204 " = " + dateToString(expMarchOne
));
207 UDate expJulyOne
= 646793100000.0;
208 if (julyOne
!= expJulyOne
)
210 errln((UnicodeString
)"FAIL: Expected start computed as " + julyOne
+
211 " = " + dateToString(julyOne
));
212 logln((UnicodeString
)" Should be " + expJulyOne
+
213 " = " + dateToString(expJulyOne
));
216 testUsingBinarySearch(*zone
, date(90, UCAL_JANUARY
, 1), date(90, UCAL_JUNE
, 15), marchOne
);
217 testUsingBinarySearch(*zone
, date(90, UCAL_JUNE
, 1), date(90, UCAL_DECEMBER
, 31), julyOne
);
219 if (zone
->inDaylightTime(marchOne
- 1000, status
) ||
220 !zone
->inDaylightTime(marchOne
, status
))
221 errln("FAIL: Start rule broken");
222 if (!zone
->inDaylightTime(julyOne
- 1000, status
) ||
223 zone
->inDaylightTime(julyOne
, status
))
224 errln("FAIL: End rule broken");
226 zone
->setStartYear(1991);
227 if (zone
->inDaylightTime(marchOne
, status
) ||
228 zone
->inDaylightTime(julyOne
- 1000, status
))
229 errln("FAIL: Start year broken");
231 failure(status
, "TestRuleAPI");
237 TimeZoneTest::findTransition(const TimeZone
& tz
,
238 UDate min
, UDate max
) {
239 UErrorCode ec
= U_ZERO_ERROR
;
241 UBool startsInDST
= tz
.inDaylightTime(min
, ec
);
242 if (failure(ec
, "TimeZone::inDaylightTime")) return;
243 if (tz
.inDaylightTime(max
, ec
) == startsInDST
) {
244 logln("Error: " + tz
.getID(id
) + ".inDaylightTime(" + dateToString(min
) + ") = " + (startsInDST
?"TRUE":"FALSE") +
245 ", inDaylightTime(" + dateToString(max
) + ") = " + (startsInDST
?"TRUE":"FALSE"));
248 if (failure(ec
, "TimeZone::inDaylightTime")) return;
249 while ((max
- min
) > INTERVAL
) {
250 UDate mid
= (min
+ max
) / 2;
251 if (tz
.inDaylightTime(mid
, ec
) == startsInDST
) {
256 if (failure(ec
, "TimeZone::inDaylightTime")) return;
258 min
= 1000.0 * uprv_floor(min
/1000.0);
259 max
= 1000.0 * uprv_floor(max
/1000.0);
260 logln(tz
.getID(id
) + " Before: " + min
/1000 + " = " +
261 dateToString(min
,s
,tz
));
262 logln(tz
.getID(id
) + " After: " + max
/1000 + " = " +
263 dateToString(max
,s
,tz
));
267 TimeZoneTest::testUsingBinarySearch(const TimeZone
& tz
,
268 UDate min
, UDate max
,
269 UDate expectedBoundary
)
271 UErrorCode status
= U_ZERO_ERROR
;
272 UBool startsInDST
= tz
.inDaylightTime(min
, status
);
273 if (failure(status
, "TimeZone::inDaylightTime")) return;
274 if (tz
.inDaylightTime(max
, status
) == startsInDST
) {
275 logln("Error: inDaylightTime(" + dateToString(max
) + ") != " + ((!startsInDST
)?"TRUE":"FALSE"));
278 if (failure(status
, "TimeZone::inDaylightTime")) return;
279 while ((max
- min
) > INTERVAL
) {
280 UDate mid
= (min
+ max
) / 2;
281 if (tz
.inDaylightTime(mid
, status
) == startsInDST
) {
286 if (failure(status
, "TimeZone::inDaylightTime")) return;
288 logln(UnicodeString("Binary Search Before: ") + uprv_floor(0.5 + min
) + " = " + dateToString(min
));
289 logln(UnicodeString("Binary Search After: ") + uprv_floor(0.5 + max
) + " = " + dateToString(max
));
290 UDate mindelta
= expectedBoundary
- min
;
291 UDate maxdelta
= max
- expectedBoundary
;
293 mindelta
<= INTERVAL
&&
295 maxdelta
<= INTERVAL
)
296 logln(UnicodeString("PASS: Expected bdry: ") + expectedBoundary
+ " = " + dateToString(expectedBoundary
));
298 errln(UnicodeString("FAIL: Expected bdry: ") + expectedBoundary
+ " = " + dateToString(expectedBoundary
));
301 const UDate
TimeZoneTest::INTERVAL
= 100;
303 // ---------------------------------------------------------------------------------
305 // -------------------------------------
308 * Test the offset of the PRT timezone.
311 TimeZoneTest::TestPRTOffset()
313 TimeZone
* tz
= TimeZone::createTimeZone("PRT");
315 errln("FAIL: TimeZone(PRT) is null");
318 int32_t expectedHour
= -4;
319 double expectedOffset
= (((double)expectedHour
) * millisPerHour
);
320 double foundOffset
= tz
->getRawOffset();
321 int32_t foundHour
= (int32_t)foundOffset
/ millisPerHour
;
322 if (expectedOffset
!= foundOffset
) {
323 dataerrln("FAIL: Offset for PRT should be %d, found %d", expectedHour
, foundHour
);
325 logln("PASS: Offset for PRT should be %d, found %d", expectedHour
, foundHour
);
331 // -------------------------------------
334 * Regress a specific bug with a sequence of API calls.
337 TimeZoneTest::TestVariousAPI518()
339 UErrorCode status
= U_ZERO_ERROR
;
340 TimeZone
* time_zone
= TimeZone::createTimeZone("PST");
341 UDate d
= date(97, UCAL_APRIL
, 30);
343 logln("The timezone is " + time_zone
->getID(str
));
344 if (!time_zone
->inDaylightTime(d
, status
)) dataerrln("FAIL: inDaylightTime returned FALSE");
345 if (failure(status
, "TimeZone::inDaylightTime", TRUE
)) return;
346 if (!time_zone
->useDaylightTime()) dataerrln("FAIL: useDaylightTime returned FALSE");
347 if (time_zone
->getRawOffset() != - 8 * millisPerHour
) dataerrln("FAIL: getRawOffset returned wrong value");
348 GregorianCalendar
*gc
= new GregorianCalendar(status
);
349 if (U_FAILURE(status
)) { errln("FAIL: Couldn't create GregorianCalendar"); return; }
350 gc
->setTime(d
, status
);
351 if (U_FAILURE(status
)) { errln("FAIL: GregorianCalendar::setTime failed"); return; }
352 if (time_zone
->getOffset(gc
->AD
, gc
->get(UCAL_YEAR
, status
), gc
->get(UCAL_MONTH
, status
),
353 gc
->get(UCAL_DATE
, status
), (uint8_t)gc
->get(UCAL_DAY_OF_WEEK
, status
), 0, status
) != - 7 * millisPerHour
)
354 dataerrln("FAIL: getOffset returned wrong value");
355 if (U_FAILURE(status
)) { errln("FAIL: GregorianCalendar::set failed"); return; }
360 // -------------------------------------
363 * Test the call which retrieves the available IDs.
366 TimeZoneTest::TestGetAvailableIDs913()
368 UErrorCode ec
= U_ZERO_ERROR
;
371 #ifdef U_USE_TIMEZONE_OBSOLETE_2_8
372 // Test legacy API -- remove these tests when the corresponding API goes away (duh)
374 const UnicodeString
** ids
= TimeZone::createAvailableIDs(numIDs
);
375 if (ids
== 0 || numIDs
< 1) {
376 errln("FAIL: createAvailableIDs()");
378 UnicodeString
buf("TimeZone::createAvailableIDs() = { ");
379 for(i
=0; i
<numIDs
; ++i
) {
380 if (i
) buf
.append(", ");
385 // we own the array; the caller owns the contained strings (yuck)
390 ids
= TimeZone::createAvailableIDs(-8*U_MILLIS_PER_HOUR
, numIDs
);
391 if (ids
== 0 || numIDs
< 1) {
392 errln("FAIL: createAvailableIDs(-8:00)");
394 UnicodeString
buf("TimeZone::createAvailableIDs(-8:00) = { ");
395 for(i
=0; i
<numIDs
; ++i
) {
396 if (i
) buf
.append(", ");
401 // we own the array; the caller owns the contained strings (yuck)
405 ids
= TimeZone::createAvailableIDs("US", numIDs
);
406 if (ids
== 0 || numIDs
< 1) {
407 errln("FAIL: createAvailableIDs(US) ids=%d, numIDs=%d", ids
, numIDs
);
409 UnicodeString
buf("TimeZone::createAvailableIDs(US) = { ");
410 for(i
=0; i
<numIDs
; ++i
) {
411 if (i
) buf
.append(", ");
416 // we own the array; the caller owns the contained strings (yuck)
422 UnicodeString
*buf
= new UnicodeString("TimeZone::createEnumeration() = { ");
424 StringEnumeration
* s
= TimeZone::createEnumeration();
426 dataerrln("Unable to create TimeZone enumeration");
429 s_length
= s
->count(ec
);
430 for (i
= 0; i
< s_length
;++i
) {
431 if (i
> 0) *buf
+= ", ";
433 *buf
+= *s
->snext(ec
);
435 *buf
+= UnicodeString(s
->next(NULL
, ec
), "");
439 // replace s with a clone of itself
440 StringEnumeration
*s2
= s
->clone();
441 if(s2
== NULL
|| s_length
!= s2
->count(ec
)) {
442 errln("TimezoneEnumeration.clone() failed");
452 /* Confirm that the following zones can be retrieved: The first
453 * zone, the last zone, and one in-between. This tests the binary
454 * search through the system zone data.
457 int32_t middle
= s_length
/2;
458 for (i
=0; i
<s_length
; ++i
) {
459 const UnicodeString
* id
= s
->snext(ec
);
460 if (i
==0 || i
==middle
|| i
==(s_length
-1)) {
461 TimeZone
*z
= TimeZone::createTimeZone(*id
);
463 errln(UnicodeString("FAIL: createTimeZone(") +
465 } else if (z
->getID(str
) != *id
) {
466 errln(UnicodeString("FAIL: createTimeZone(") +
467 *id
+ ") -> zone " + str
);
469 logln(UnicodeString("OK: createTimeZone(") +
470 *id
+ ") succeeded");
478 *buf
+= "TimeZone::createEnumeration(GMT+01:00) = { ";
480 s
= TimeZone::createEnumeration(1 * U_MILLIS_PER_HOUR
);
481 s_length
= s
->count(ec
);
482 for (i
= 0; i
< s_length
;++i
) {
483 if (i
> 0) *buf
+= ", ";
484 *buf
+= *s
->snext(ec
);
492 *buf
+= "TimeZone::createEnumeration(US) = { ";
494 s
= TimeZone::createEnumeration("US");
495 s_length
= s
->count(ec
);
496 for (i
= 0; i
< s_length
;++i
) {
497 if (i
> 0) *buf
+= ", ";
498 *buf
+= *s
->snext(ec
);
503 TimeZone
*tz
= TimeZone::createTimeZone("PST");
504 if (tz
!= 0) logln("getTimeZone(PST) = " + tz
->getID(str
));
505 else errln("FAIL: getTimeZone(PST) = null");
507 tz
= TimeZone::createTimeZone("America/Los_Angeles");
508 if (tz
!= 0) logln("getTimeZone(America/Los_Angeles) = " + tz
->getID(str
));
509 else errln("FAIL: getTimeZone(PST) = null");
513 tz
= TimeZone::createTimeZone("NON_EXISTENT");
516 errln("FAIL: getTimeZone(NON_EXISTENT) = null");
517 else if (tz
->getID(temp
) != UCAL_UNKNOWN_ZONE_ID
)
518 errln("FAIL: getTimeZone(NON_EXISTENT) = " + temp
);
526 TimeZoneTest::TestGetAvailableIDsNew()
528 UErrorCode ec
= U_ZERO_ERROR
;
529 StringEnumeration
*any
, *canonical
, *canonicalLoc
;
530 StringEnumeration
*any_US
, *canonical_US
, *canonicalLoc_US
;
531 StringEnumeration
*any_W5
, *any_CA_W5
;
532 StringEnumeration
*any_US_E14
;
534 const UnicodeString
*id1
, *id2
;
535 UnicodeString canonicalID
;
540 any
= canonical
= canonicalLoc
= any_US
= canonical_US
= canonicalLoc_US
= any_W5
= any_CA_W5
= any_US_E14
= NULL
;
542 any
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, NULL
, NULL
, ec
);
544 dataerrln("Failed to create enumration for ANY");
548 canonical
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL
, NULL
, NULL
, ec
);
550 errln("Failed to create enumration for CANONICAL");
554 canonicalLoc
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION
, NULL
, NULL
, ec
);
556 errln("Failed to create enumration for CANONICALLOC");
560 any_US
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, "US", NULL
, ec
);
562 errln("Failed to create enumration for ANY_US");
566 canonical_US
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL
, "US", NULL
, ec
);
568 errln("Failed to create enumration for CANONICAL_US");
572 canonicalLoc_US
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION
, "US", NULL
, ec
);
574 errln("Failed to create enumration for CANONICALLOC_US");
578 rawOffset
= (-5)*60*60*1000;
579 any_W5
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, NULL
, &rawOffset
, ec
);
581 errln("Failed to create enumration for ANY_W5");
585 any_CA_W5
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, "CA", &rawOffset
, ec
);
587 errln("Failed to create enumration for ANY_CA_W5");
591 rawOffset
= 14*60*60*1000;
592 any_US_E14
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, "US", &rawOffset
, ec
);
594 errln("Failed to create enumration for ANY_US_E14");
598 checkContainsAll(any
, "ANY", canonical
, "CANONICAL");
599 checkContainsAll(canonical
, "CANONICAL", canonicalLoc
, "CANONICALLOC");
601 checkContainsAll(any
, "ANY", any_US
, "ANY_US");
602 checkContainsAll(canonical
, "CANONICAL", canonical_US
, "CANONICAL_US");
603 checkContainsAll(canonicalLoc
, "CANONICALLOC", canonicalLoc_US
, "CANONICALLOC_US");
605 checkContainsAll(any_US
, "ANY_US", canonical_US
, "CANONICAL_US");
606 checkContainsAll(canonical_US
, "CANONICAL_US", canonicalLoc_US
, "CANONICALLOC_US");
608 checkContainsAll(any
, "ANY", any_W5
, "ANY_W5");
609 checkContainsAll(any_W5
, "ANY_W5", any_CA_W5
, "ANY_CA_W5");
611 // And ID in any set, but not in canonical set must not be a canonical ID
613 while ((id1
= any
->snext(ec
)) != NULL
) {
615 canonical
->reset(ec
);
616 while ((id2
= canonical
->snext(ec
)) != NULL
) {
626 TimeZone::getCanonicalID(*id1
, canonicalID
, isSystemID
, ec
);
630 if (*id1
== canonicalID
) {
631 errln((UnicodeString
)"FAIL: canonicalID [" + *id1
+ "] is not in CANONICAL");
634 errln((UnicodeString
)"FAIL: ANY contains non-system ID: " + *id1
);
639 errln("Error checking IDs in ANY, but not in CANONICAL");
643 // canonical set must contains only canonical IDs
644 canonical
->reset(ec
);
645 while ((id1
= canonical
->snext(ec
)) != NULL
) {
646 TimeZone::getCanonicalID(*id1
, canonicalID
, isSystemID
, ec
);
650 if (*id1
!= canonicalID
) {
651 errln((UnicodeString
)"FAIL: CANONICAL contains non-canonical ID: " + *id1
);
654 errln((UnicodeString
)"FAILE: CANONICAL contains non-system ID: " + *id1
);
658 errln("Error checking IDs in CANONICAL");
662 // canonicalLoc set must contain only canonical location IDs
663 canonicalLoc
->reset(ec
);
664 while ((id1
= canonicalLoc
->snext(ec
)) != NULL
) {
665 TimeZone::getRegion(*id1
, region
, sizeof(region
), ec
);
669 if (uprv_strcmp(region
, "001") == 0) {
670 errln((UnicodeString
)"FAIL: CANONICALLOC contains non location zone: " + *id1
);
674 errln("Error checking IDs in CANONICALLOC");
678 // any_US must contain only US zones
680 while ((id1
= any_US
->snext(ec
)) != NULL
) {
681 TimeZone::getRegion(*id1
, region
, sizeof(region
), ec
);
685 if (uprv_strcmp(region
, "US") != 0) {
686 errln((UnicodeString
)"FAIL: ANY_US contains non-US zone ID: " + *id1
);
690 errln("Error checking IDs in ANY_US");
694 // any_W5 must contain only GMT-05:00 zones
696 while ((id1
= any_W5
->snext(ec
)) != NULL
) {
697 TimeZone
*tz
= TimeZone::createTimeZone(*id1
);
698 if (tz
->getRawOffset() != (-5)*60*60*1000) {
699 errln((UnicodeString
)"FAIL: ANY_W5 contains a zone whose offset is not -05:00: " + *id1
);
704 errln("Error checking IDs in ANY_W5");
708 // No US zone swith GMT+14:00
709 zoneCount
= any_US_E14
->count(ec
);
711 errln("Error checking IDs in ANY_US_E14");
713 } else if (zoneCount
!= 0) {
714 errln("FAIL: ANY_US_E14 must be empty");
723 delete canonicalLoc_US
;
730 TimeZoneTest::checkContainsAll(StringEnumeration
*s1
, const char *name1
,
731 StringEnumeration
*s2
, const char *name2
)
733 UErrorCode ec
= U_ZERO_ERROR
;
734 const UnicodeString
*id1
, *id2
;
738 while ((id2
= s2
->snext(ec
)) != NULL
) {
741 while ((id1
= s1
->snext(ec
)) != NULL
) {
748 errln((UnicodeString
)"FAIL: " + name1
+ "does not contain "
749 + *id2
+ " in " + name2
);
754 errln((UnicodeString
)"Error checkContainsAll for " + name1
+ " - " + name2
);
759 * NOTE: As of ICU 2.8, this test confirms that the "tz.alias"
760 * file, used to build ICU alias zones, is working. It also
761 * looks at some genuine Olson compatibility IDs. [aliu]
763 * This test is problematic. It should really just confirm that
764 * the list of compatibility zone IDs exist and are somewhat
765 * meaningful (that is, they aren't all aliases of GMT). It goes a
766 * bit further -- it hard-codes expectations about zone behavior,
767 * when in fact zones are redefined quite frequently. ICU's build
768 * process means that it is easy to update ICU to contain the
769 * latest Olson zone data, but if a zone tested here changes, then
770 * this test will fail. I have updated the test for 1999j data,
771 * but further updates will probably be required. Note that some
772 * of the concerts listed below no longer apply -- in particular,
773 * we do NOT overwrite real UNIX zones with 3-letter IDs. There
774 * are two points of overlap as of 1999j: MET and EET. These are
775 * both real UNIX zones, so we just use the official
776 * definition. This test has been updated to reflect this.
779 * Added tests for additional zones and aliases from the icuzones file.
780 * Markus Scherer 2006-nov-06
782 * [srl - from java - 7/5/1998]
784 * Certain short zone IDs, used since 1.1.x, are incorrect.
786 * The worst of these is:
788 * "CAT" (Central African Time) should be GMT+2:00, but instead returns a
789 * zone at GMT-1:00. The zone at GMT-1:00 should be called EGT, CVT, EGST,
790 * or AZOST, depending on which zone is meant, but in no case is it CAT.
792 * Other wrong zone IDs:
794 * ECT (European Central Time) GMT+1:00: ECT is Ecuador Time,
795 * GMT-5:00. European Central time is abbreviated CEST.
797 * SST (Solomon Island Time) GMT+11:00. SST is actually Samoa Standard Time,
798 * GMT-11:00. Solomon Island time is SBT.
800 * NST (New Zealand Time) GMT+12:00. NST is the abbreviation for
801 * Newfoundland Standard Time, GMT-3:30. New Zealanders use NZST.
803 * AST (Alaska Standard Time) GMT-9:00. [This has already been noted in
804 * another bug.] It should be "AKST". AST is Atlantic Standard Time,
807 * PNT (Phoenix Time) GMT-7:00. PNT usually means Pitcairn Time,
808 * GMT-8:30. There is no standard abbreviation for Phoenix time, as distinct
809 * from MST with daylight savings.
811 * In addition to these problems, a number of zones are FAKE. That is, they
812 * don't match what people use in the real world.
816 * EET (should be EEST)
817 * ART (should be EEST)
818 * MET (should be IRST)
819 * NET (should be AMST)
820 * PLT (should be PKT)
821 * BST (should be BDT)
822 * VST (should be ICT)
823 * CTT (should be CST) +
824 * ACT (should be CST) +
825 * AET (should be EST) +
826 * MIT (should be WST) +
827 * IET (should be EST) +
828 * PRT (should be AST) +
829 * CNT (should be NST)
830 * AGT (should be ARST)
831 * BET (should be EST) +
833 * + A zone with the correct name already exists and means something
834 * else. E.g., EST usually indicates the US Eastern zone, so it cannot be
835 * used for Brazil (BET).
837 void TimeZoneTest::TestShortZoneIDs()
840 // Create a small struct to hold the array
849 {"HST", -600, FALSE
}, // Olson northamerica -10:00
850 {"AST", -540, TRUE
}, // ICU Link - America/Anchorage
851 {"PST", -480, TRUE
}, // ICU Link - America/Los_Angeles
852 {"PNT", -420, FALSE
}, // ICU Link - America/Phoenix
853 {"MST", -420, FALSE
}, // updated Aug 2003 aliu
854 {"CST", -360, TRUE
}, // Olson northamerica -7:00
855 {"IET", -300, TRUE
}, // ICU Link - America/Indiana/Indianapolis
856 {"EST", -300, FALSE
}, // Olson northamerica -5:00
857 {"PRT", -240, FALSE
}, // ICU Link - America/Puerto_Rico
858 {"CNT", -210, TRUE
}, // ICU Link - America/St_Johns
859 {"AGT", -180, FALSE
}, // ICU Link - America/Argentina/Buenos_Aires
860 {"BET", -180, TRUE
}, // ICU Link - America/Sao_Paulo
861 {"GMT", 0, FALSE
}, // Olson etcetera Link - Etc/GMT
862 {"UTC", 0, FALSE
}, // Olson etcetera 0
863 {"ECT", 60, TRUE
}, // ICU Link - Europe/Paris
864 {"MET", 60, TRUE
}, // Olson europe 1:00 C-Eur
865 {"CAT", 120, FALSE
}, // ICU Link - Africa/Maputo
866 {"ART", 120, FALSE
}, // ICU Link - Africa/Cairo
867 {"EET", 120, TRUE
}, // Olson europe 2:00 EU
868 {"EAT", 180, FALSE
}, // ICU Link - Africa/Addis_Ababa
869 {"NET", 240, FALSE
}, // ICU Link - Asia/Yerevan
870 {"PLT", 300, FALSE
}, // ICU Link - Asia/Karachi
871 {"IST", 330, FALSE
}, // ICU Link - Asia/Kolkata
872 {"BST", 360, FALSE
}, // ICU Link - Asia/Dhaka
873 {"VST", 420, FALSE
}, // ICU Link - Asia/Ho_Chi_Minh
874 {"CTT", 480, FALSE
}, // ICU Link - Asia/Shanghai
875 {"JST", 540, FALSE
}, // ICU Link - Asia/Tokyo
876 {"ACT", 570, FALSE
}, // ICU Link - Australia/Darwin
877 {"AET", 600, TRUE
}, // ICU Link - Australia/Sydney
878 {"SST", 660, FALSE
}, // ICU Link - Pacific/Guadalcanal
879 {"NST", 720, TRUE
}, // ICU Link - Pacific/Auckland
880 {"MIT", 780, TRUE
}, // ICU Link - Pacific/Apia
882 {"Etc/Unknown", 0, FALSE
}, // CLDR
884 {"SystemV/AST4ADT", -240, TRUE
},
885 {"SystemV/EST5EDT", -300, TRUE
},
886 {"SystemV/CST6CDT", -360, TRUE
},
887 {"SystemV/MST7MDT", -420, TRUE
},
888 {"SystemV/PST8PDT", -480, TRUE
},
889 {"SystemV/YST9YDT", -540, TRUE
},
890 {"SystemV/AST4", -240, FALSE
},
891 {"SystemV/EST5", -300, FALSE
},
892 {"SystemV/CST6", -360, FALSE
},
893 {"SystemV/MST7", -420, FALSE
},
894 {"SystemV/PST8", -480, FALSE
},
895 {"SystemV/YST9", -540, FALSE
},
896 {"SystemV/HST10", -600, FALSE
},
901 for(i
=0;kReferenceList
[i
].id
[0];i
++) {
902 UnicodeString
itsID(kReferenceList
[i
].id
);
905 TimeZone
*tz
= TimeZone::createTimeZone(itsID
);
906 if (!tz
|| (kReferenceList
[i
].offset
!= 0 && *tz
== *TimeZone::getGMT())) {
907 errln("FAIL: Time Zone " + itsID
+ " does not exist!");
911 // Check daylight usage.
912 UBool usesDaylight
= tz
->useDaylightTime();
913 if (usesDaylight
!= kReferenceList
[i
].daylight
) {
914 if (!isDevelopmentBuild
) {
915 logln("Warning: Time Zone " + itsID
+ " use daylight is " +
916 (usesDaylight
?"TRUE":"FALSE") +
917 " but it should be " +
918 ((kReferenceList
[i
].daylight
)?"TRUE":"FALSE"));
920 dataerrln("FAIL: Time Zone " + itsID
+ " use daylight is " +
921 (usesDaylight
?"TRUE":"FALSE") +
922 " but it should be " +
923 ((kReferenceList
[i
].daylight
)?"TRUE":"FALSE"));
929 int32_t offsetInMinutes
= tz
->getRawOffset()/60000;
930 if (offsetInMinutes
!= kReferenceList
[i
].offset
) {
931 if (!isDevelopmentBuild
) {
932 logln("FAIL: Time Zone " + itsID
+ " raw offset is " +
934 " but it should be " + kReferenceList
[i
].offset
);
936 dataerrln("FAIL: Time Zone " + itsID
+ " raw offset is " +
938 " but it should be " + kReferenceList
[i
].offset
);
944 logln("OK: " + itsID
+
945 " useDaylightTime() & getRawOffset() as expected");
951 // OK now test compat
952 logln("Testing for compatibility zones");
954 const char* compatibilityMap
[] = {
955 // This list is copied from tz.alias. If tz.alias
956 // changes, this list must be updated. Current as of Mar 2007
957 "ACT", "Australia/Darwin",
958 "AET", "Australia/Sydney",
959 "AGT", "America/Buenos_Aires",
960 "ART", "Africa/Cairo",
961 "AST", "America/Anchorage",
962 "BET", "America/Sao_Paulo",
963 "BST", "Asia/Dhaka", // # spelling changed in 2000h; was Asia/Dacca
964 "CAT", "Africa/Maputo",
965 "CNT", "America/St_Johns",
966 "CST", "America/Chicago",
967 "CTT", "Asia/Shanghai",
968 "EAT", "Africa/Addis_Ababa",
969 "ECT", "Europe/Paris",
970 // EET Europe/Istanbul # EET is a standard UNIX zone
971 // "EST", "America/New_York", # Defined as -05:00
972 // "HST", "Pacific/Honolulu", # Defined as -10:00
973 "IET", "America/Indianapolis",
974 "IST", "Asia/Calcutta",
976 // MET Asia/Tehran # MET is a standard UNIX zone
977 "MIT", "Pacific/Apia",
978 // "MST", "America/Denver", # Defined as -07:00
979 "NET", "Asia/Yerevan",
980 "NST", "Pacific/Auckland",
981 "PLT", "Asia/Karachi",
982 "PNT", "America/Phoenix",
983 "PRT", "America/Puerto_Rico",
984 "PST", "America/Los_Angeles",
985 "SST", "Pacific/Guadalcanal",
987 "VST", "Asia/Saigon",
991 for (i
=0;*compatibilityMap
[i
];i
+=2) {
994 const char *zone1
= compatibilityMap
[i
];
995 const char *zone2
= compatibilityMap
[i
+1];
997 TimeZone
*tz1
= TimeZone::createTimeZone(zone1
);
998 TimeZone
*tz2
= TimeZone::createTimeZone(zone2
);
1001 errln(UnicodeString("FAIL: Could not find short ID zone ") + zone1
);
1004 errln(UnicodeString("FAIL: Could not find long ID zone ") + zone2
);
1008 // make NAME same so comparison will only look at the rest
1009 tz2
->setID(tz1
->getID(itsID
));
1012 errln("FAIL: " + UnicodeString(zone1
) +
1013 " != " + UnicodeString(zone2
));
1015 logln("OK: " + UnicodeString(zone1
) +
1016 " == " + UnicodeString(zone2
));
1027 * Utility function for TestCustomParse
1029 UnicodeString
& TimeZoneTest::formatOffset(int32_t offset
, UnicodeString
&rv
) {
1031 UChar sign
= 0x002B;
1037 int32_t s
= offset
% 60;
1039 int32_t m
= offset
% 60;
1040 int32_t h
= offset
/ 60;
1042 rv
+= (UChar
)(sign
);
1044 rv
+= (UChar
)(0x0030 + (h
/10));
1046 rv
+= (UChar
)0x0030;
1048 rv
+= (UChar
)(0x0030 + (h%10
));
1050 rv
+= (UChar
)0x003A; /* ':' */
1052 rv
+= (UChar
)(0x0030 + (m
/10));
1054 rv
+= (UChar
)0x0030;
1056 rv
+= (UChar
)(0x0030 + (m%10
));
1059 rv
+= (UChar
)0x003A; /* ':' */
1061 rv
+= (UChar
)(0x0030 + (s
/10));
1063 rv
+= (UChar
)0x0030;
1065 rv
+= (UChar
)(0x0030 + (s%10
));
1071 * Utility function for TestCustomParse, generating time zone ID
1072 * string for the give offset.
1074 UnicodeString
& TimeZoneTest::formatTZID(int32_t offset
, UnicodeString
&rv
) {
1076 UChar sign
= 0x002B;
1082 int32_t s
= offset
% 60;
1084 int32_t m
= offset
% 60;
1085 int32_t h
= offset
/ 60;
1088 rv
+= (UChar
)(sign
);
1090 rv
+= (UChar
)(0x0030 + (h
/10));
1092 rv
+= (UChar
)0x0030;
1094 rv
+= (UChar
)(0x0030 + (h%10
));
1095 rv
+= (UChar
)0x003A;
1097 rv
+= (UChar
)(0x0030 + (m
/10));
1099 rv
+= (UChar
)0x0030;
1101 rv
+= (UChar
)(0x0030 + (m%10
));
1104 rv
+= (UChar
)0x003A;
1106 rv
+= (UChar
)(0x0030 + (s
/10));
1108 rv
+= (UChar
)0x0030;
1110 rv
+= (UChar
)(0x0030 + (s%10
));
1116 * As part of the VM fix (see CCC approved RFE 4028006, bug
1117 * 4044013), TimeZone.getTimeZone() has been modified to recognize
1118 * generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
1119 * GMT[+-]hh. Test this behavior here.
1123 void TimeZoneTest::TestCustomParse()
1126 const int32_t kUnparseable
= 604800; // the number of seconds in a week. More than any offset should be.
1130 const char *customId
;
1131 int32_t expectedOffset
;
1135 // ID Expected offset in seconds
1136 {"GMT", kUnparseable
}, //Isn't custom. [returns normal GMT]
1137 {"GMT-YOUR.AD.HERE", kUnparseable
},
1138 {"GMT0", kUnparseable
},
1140 {"GMT+1", (1*60*60)},
1141 {"GMT-0030", (-30*60)},
1142 {"GMT+15:99", kUnparseable
},
1143 {"GMT+", kUnparseable
},
1144 {"GMT-", kUnparseable
},
1145 {"GMT+0:", kUnparseable
},
1146 {"GMT-:", kUnparseable
},
1147 {"GMT-YOUR.AD.HERE", kUnparseable
},
1148 {"GMT+0010", (10*60)}, // Interpret this as 00:10
1149 {"GMT-10", (-10*60*60)},
1150 {"GMT+30", kUnparseable
},
1151 {"GMT-3:30", (-(3*60+30)*60)},
1152 {"GMT-230", (-(2*60+30)*60)},
1153 {"GMT+05:13:05", ((5*60+13)*60+5)},
1154 {"GMT-71023", (-((7*60+10)*60+23))},
1155 {"GMT+01:23:45:67", kUnparseable
},
1156 {"GMT+01:234", kUnparseable
},
1157 {"GMT-2:31:123", kUnparseable
},
1158 {"GMT+3:75", kUnparseable
},
1159 {"GMT-01010101", kUnparseable
},
1163 for (i
=0; kData
[i
].customId
!= 0; i
++) {
1164 UnicodeString
id(kData
[i
].customId
);
1165 int32_t exp
= kData
[i
].expectedOffset
;
1166 TimeZone
*zone
= TimeZone::createTimeZone(id
);
1167 UnicodeString itsID
, temp
;
1169 if (dynamic_cast<OlsonTimeZone
*>(zone
) != NULL
) {
1170 logln(id
+ " -> Olson time zone");
1173 int32_t ioffset
= zone
->getRawOffset()/1000;
1174 UnicodeString offset
, expectedID
;
1175 formatOffset(ioffset
, offset
);
1176 formatTZID(ioffset
, expectedID
);
1177 logln(id
+ " -> " + itsID
+ " " + offset
);
1178 if (exp
== kUnparseable
&& itsID
!= UCAL_UNKNOWN_ZONE_ID
) {
1179 errln("Expected parse failure for " + id
+
1180 ", got offset of " + offset
+
1183 // JDK 1.3 creates custom zones with the ID "Custom"
1184 // JDK 1.4 creates custom zones with IDs of the form "GMT+02:00"
1185 // ICU creates custom zones with IDs of the form "GMT+02:00"
1186 else if (exp
!= kUnparseable
&& (ioffset
!= exp
|| itsID
!= expectedID
)) {
1187 dataerrln("Expected offset of " + formatOffset(exp
, temp
) +
1188 ", id " + expectedID
+
1190 ", got offset of " + offset
+
1199 TimeZoneTest::TestAliasedNames()
1205 /* Generated by org.unicode.cldr.tool.CountItems */
1207 /* zoneID, canonical zoneID */
1208 {"Africa/Timbuktu", "Africa/Bamako"},
1209 {"America/Argentina/Buenos_Aires", "America/Buenos_Aires"},
1210 {"America/Argentina/Catamarca", "America/Catamarca"},
1211 {"America/Argentina/ComodRivadavia", "America/Catamarca"},
1212 {"America/Argentina/Cordoba", "America/Cordoba"},
1213 {"America/Argentina/Jujuy", "America/Jujuy"},
1214 {"America/Argentina/Mendoza", "America/Mendoza"},
1215 {"America/Atka", "America/Adak"},
1216 {"America/Ensenada", "America/Tijuana"},
1217 {"America/Fort_Wayne", "America/Indiana/Indianapolis"},
1218 {"America/Indianapolis", "America/Indiana/Indianapolis"},
1219 {"America/Knox_IN", "America/Indiana/Knox"},
1220 {"America/Louisville", "America/Kentucky/Louisville"},
1221 {"America/Porto_Acre", "America/Rio_Branco"},
1222 {"America/Rosario", "America/Cordoba"},
1223 {"America/Virgin", "America/St_Thomas"},
1224 {"Asia/Ashkhabad", "Asia/Ashgabat"},
1225 {"Asia/Chungking", "Asia/Chongqing"},
1226 {"Asia/Dacca", "Asia/Dhaka"},
1227 {"Asia/Istanbul", "Europe/Istanbul"},
1228 {"Asia/Macao", "Asia/Macau"},
1229 {"Asia/Tel_Aviv", "Asia/Jerusalem"},
1230 {"Asia/Thimbu", "Asia/Thimphu"},
1231 {"Asia/Ujung_Pandang", "Asia/Makassar"},
1232 {"Asia/Ulan_Bator", "Asia/Ulaanbaatar"},
1233 {"Australia/ACT", "Australia/Sydney"},
1234 {"Australia/Canberra", "Australia/Sydney"},
1235 {"Australia/LHI", "Australia/Lord_Howe"},
1236 {"Australia/NSW", "Australia/Sydney"},
1237 {"Australia/North", "Australia/Darwin"},
1238 {"Australia/Queensland", "Australia/Brisbane"},
1239 {"Australia/South", "Australia/Adelaide"},
1240 {"Australia/Tasmania", "Australia/Hobart"},
1241 {"Australia/Victoria", "Australia/Melbourne"},
1242 {"Australia/West", "Australia/Perth"},
1243 {"Australia/Yancowinna", "Australia/Broken_Hill"},
1244 {"Brazil/Acre", "America/Rio_Branco"},
1245 {"Brazil/DeNoronha", "America/Noronha"},
1246 {"Brazil/East", "America/Sao_Paulo"},
1247 {"Brazil/West", "America/Manaus"},
1248 {"Canada/Atlantic", "America/Halifax"},
1249 {"Canada/Central", "America/Winnipeg"},
1250 {"Canada/East-Saskatchewan", "America/Regina"},
1251 {"Canada/Eastern", "America/Toronto"},
1252 {"Canada/Mountain", "America/Edmonton"},
1253 {"Canada/Newfoundland", "America/St_Johns"},
1254 {"Canada/Pacific", "America/Vancouver"},
1255 {"Canada/Saskatchewan", "America/Regina"},
1256 {"Canada/Yukon", "America/Whitehorse"},
1257 {"Chile/Continental", "America/Santiago"},
1258 {"Chile/EasterIsland", "Pacific/Easter"},
1259 {"Cuba", "America/Havana"},
1260 {"Egypt", "Africa/Cairo"},
1261 {"Eire", "Europe/Dublin"},
1262 {"Etc/GMT+0", "Etc/GMT"},
1263 {"Etc/GMT-0", "Etc/GMT"},
1264 {"Etc/GMT0", "Etc/GMT"},
1265 {"Etc/Greenwich", "Etc/GMT"},
1266 {"Etc/UCT", "Etc/GMT"},
1267 {"Etc/UTC", "Etc/GMT"},
1268 {"Etc/Universal", "Etc/GMT"},
1269 {"Etc/Zulu", "Etc/GMT"},
1270 {"Europe/Belfast", "Europe/London"},
1271 {"Europe/Nicosia", "Asia/Nicosia"},
1272 {"Europe/Tiraspol", "Europe/Chisinau"},
1273 {"GB", "Europe/London"},
1274 {"GB-Eire", "Europe/London"},
1276 {"GMT+0", "Etc/GMT"},
1277 {"GMT-0", "Etc/GMT"},
1278 {"GMT0", "Etc/GMT"},
1279 {"Greenwich", "Etc/GMT"},
1280 {"Hongkong", "Asia/Hong_Kong"},
1281 {"Iceland", "Atlantic/Reykjavik"},
1282 {"Iran", "Asia/Tehran"},
1283 {"Israel", "Asia/Jerusalem"},
1284 {"Jamaica", "America/Jamaica"},
1285 {"Japan", "Asia/Tokyo"},
1286 {"Kwajalein", "Pacific/Kwajalein"},
1287 {"Libya", "Africa/Tripoli"},
1288 {"Mexico/BajaNorte", "America/Tijuana"},
1289 {"Mexico/BajaSur", "America/Mazatlan"},
1290 {"Mexico/General", "America/Mexico_City"},
1291 {"NZ", "Pacific/Auckland"},
1292 {"NZ-CHAT", "Pacific/Chatham"},
1293 {"Navajo", "America/Shiprock"},
1294 {"PRC", "Asia/Shanghai"},
1295 {"Pacific/Samoa", "Pacific/Pago_Pago"},
1296 {"Pacific/Yap", "Pacific/Truk"},
1297 {"Poland", "Europe/Warsaw"},
1298 {"Portugal", "Europe/Lisbon"},
1299 {"ROC", "Asia/Taipei"},
1300 {"ROK", "Asia/Seoul"},
1301 {"Singapore", "Asia/Singapore"},
1302 {"Turkey", "Europe/Istanbul"},
1304 {"US/Alaska", "America/Anchorage"},
1305 {"US/Aleutian", "America/Adak"},
1306 {"US/Arizona", "America/Phoenix"},
1307 {"US/Central", "America/Chicago"},
1308 {"US/East-Indiana", "America/Indiana/Indianapolis"},
1309 {"US/Eastern", "America/New_York"},
1310 {"US/Hawaii", "Pacific/Honolulu"},
1311 {"US/Indiana-Starke", "America/Indiana/Knox"},
1312 {"US/Michigan", "America/Detroit"},
1313 {"US/Mountain", "America/Denver"},
1314 {"US/Pacific", "America/Los_Angeles"},
1315 {"US/Pacific-New", "America/Los_Angeles"},
1316 {"US/Samoa", "Pacific/Pago_Pago"},
1318 {"Universal", "Etc/GMT"},
1319 {"W-SU", "Europe/Moscow"},
1320 {"Zulu", "Etc/GMT"},
1325 TimeZone::EDisplayType styles
[] = { TimeZone::SHORT
, TimeZone::LONG
};
1326 UBool useDst
[] = { FALSE
, TRUE
};
1327 int32_t noLoc
= uloc_countAvailable();
1329 int32_t i
, j
, k
, loc
;
1330 UnicodeString fromName
, toName
;
1331 TimeZone
*from
= NULL
, *to
= NULL
;
1332 for(i
= 0; i
< UPRV_LENGTHOF(kData
); i
++) {
1333 from
= TimeZone::createTimeZone(kData
[i
].from
);
1334 to
= TimeZone::createTimeZone(kData
[i
].to
);
1335 if(!from
->hasSameRules(*to
)) {
1336 errln("different at %i\n", i
);
1339 for(loc
= 0; loc
< noLoc
; loc
++) {
1340 const char* locale
= uloc_getAvailable(loc
);
1341 for(j
= 0; j
< UPRV_LENGTHOF(styles
); j
++) {
1342 for(k
= 0; k
< UPRV_LENGTHOF(useDst
); k
++) {
1345 from
->getDisplayName(useDst
[k
], styles
[j
],locale
, fromName
);
1346 to
->getDisplayName(useDst
[k
], styles
[j
], locale
, toName
);
1347 if(fromName
.compare(toName
) != 0) {
1348 errln("Fail: Expected "+toName
+" but got " + prettify(fromName
)
1349 + " for locale: " + locale
+ " index: "+ loc
1350 + " to id "+ kData
[i
].to
1351 + " from id " + kData
[i
].from
);
1359 from
->getDisplayName(fromName
);
1360 to
->getDisplayName(toName
);
1361 if(fromName
.compare(toName
) != 0) {
1362 errln("Fail: Expected "+toName
+" but got " + fromName
);
1371 * Test the basic functionality of the getDisplayName() API.
1376 * See also API change request A41.
1378 * 4/21/98 - make smarter, so the test works if the ext resources
1379 * are present or not.
1382 TimeZoneTest::TestDisplayName()
1384 UErrorCode status
= U_ZERO_ERROR
;
1386 TimeZone
*zone
= TimeZone::createTimeZone("PST");
1388 zone
->getDisplayName(Locale::getEnglish(), name
);
1389 logln("PST->" + name
);
1390 if (name
.compare("Pacific Standard Time") != 0)
1391 dataerrln("Fail: Expected \"Pacific Standard Time\" but got " + name
);
1393 //*****************************************************************
1394 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1395 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1396 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1397 //*****************************************************************
1401 TimeZone::EDisplayType style
;
1404 {FALSE
, TimeZone::SHORT
, "PST"},
1405 {TRUE
, TimeZone::SHORT
, "PDT"},
1406 {FALSE
, TimeZone::LONG
, "Pacific Standard Time"},
1407 {TRUE
, TimeZone::LONG
, "Pacific Daylight Time"},
1409 {FALSE
, TimeZone::SHORT_GENERIC
, "PT"},
1410 {TRUE
, TimeZone::SHORT_GENERIC
, "PT"},
1411 {FALSE
, TimeZone::LONG_GENERIC
, "Pacific Time"},
1412 {TRUE
, TimeZone::LONG_GENERIC
, "Pacific Time"},
1414 {FALSE
, TimeZone::SHORT_GMT
, "-0800"},
1415 {TRUE
, TimeZone::SHORT_GMT
, "-0700"},
1416 {FALSE
, TimeZone::LONG_GMT
, "GMT-08:00"},
1417 {TRUE
, TimeZone::LONG_GMT
, "GMT-07:00"},
1419 {FALSE
, TimeZone::SHORT_COMMONLY_USED
, "PST"},
1420 {TRUE
, TimeZone::SHORT_COMMONLY_USED
, "PDT"},
1421 {FALSE
, TimeZone::GENERIC_LOCATION
, "Los Angeles Time"},
1422 {TRUE
, TimeZone::GENERIC_LOCATION
, "Los Angeles Time"},
1424 {FALSE
, TimeZone::LONG
, ""}
1427 for (i
=0; kData
[i
].expect
[0] != '\0'; i
++)
1430 name
= zone
->getDisplayName(kData
[i
].useDst
,
1432 Locale::getEnglish(), name
);
1433 if (name
.compare(kData
[i
].expect
) != 0)
1434 dataerrln("Fail: Expected " + UnicodeString(kData
[i
].expect
) + "; got " + name
);
1435 logln("PST [with options]->" + name
);
1437 for (i
=0; kData
[i
].expect
[0] != '\0'; i
++)
1440 name
= zone
->getDisplayName(kData
[i
].useDst
,
1441 kData
[i
].style
, name
);
1442 if (name
.compare(kData
[i
].expect
) != 0)
1443 dataerrln("Fail: Expected " + UnicodeString(kData
[i
].expect
) + "; got " + name
);
1444 logln("PST [with options]->" + name
);
1448 // Make sure that we don't display the DST name by constructing a fake
1449 // PST zone that has DST all year long.
1450 SimpleTimeZone
*zone2
= new SimpleTimeZone(0, "PST");
1452 zone2
->setStartRule(UCAL_JANUARY
, 1, 0, 0, status
);
1453 zone2
->setEndRule(UCAL_DECEMBER
, 31, 0, 0, status
);
1455 UnicodeString inDaylight
;
1456 if (zone2
->inDaylightTime(UDate(0), status
)) {
1457 inDaylight
= UnicodeString("TRUE");
1459 inDaylight
= UnicodeString("FALSE");
1461 logln(UnicodeString("Modified PST inDaylightTime->") + inDaylight
);
1462 if(U_FAILURE(status
))
1464 dataerrln("Some sort of error..." + UnicodeString(u_errorName(status
))); // REVISIT
1467 name
= zone2
->getDisplayName(Locale::getEnglish(),name
);
1468 logln("Modified PST->" + name
);
1469 if (name
.compare("Pacific Standard Time") != 0)
1470 dataerrln("Fail: Expected \"Pacific Standard Time\"");
1472 // Make sure we get the default display format for Locales
1473 // with no display name data.
1474 Locale
mt_MT("mt_MT");
1476 name
= zone
->getDisplayName(mt_MT
,name
);
1477 //*****************************************************************
1478 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1479 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1480 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1481 //*****************************************************************
1482 logln("PST(mt_MT)->" + name
);
1484 // *** REVISIT SRL how in the world do I check this? looks java specific.
1485 // Now be smart -- check to see if zh resource is even present.
1486 // If not, we expect the en fallback behavior.
1487 ResourceBundle
enRB(NULL
,
1488 Locale::getEnglish(), status
);
1489 if(U_FAILURE(status
))
1490 dataerrln("Couldn't get ResourceBundle for en - %s", u_errorName(status
));
1492 ResourceBundle
mtRB(NULL
,
1494 //if(U_FAILURE(status))
1495 // errln("Couldn't get ResourceBundle for mt_MT");
1497 UBool noZH
= U_FAILURE(status
);
1500 logln("Warning: Not testing the mt_MT behavior because resource is absent");
1501 if (name
!= "Pacific Standard Time")
1502 dataerrln("Fail: Expected Pacific Standard Time");
1506 if (name
.compare("GMT-08:00") &&
1507 name
.compare("GMT-8:00") &&
1508 name
.compare("GMT-0800") &&
1509 name
.compare("GMT-800")) {
1510 dataerrln(UnicodeString("Fail: Expected GMT-08:00 or something similar for PST in mt_MT but got ") + name
);
1511 dataerrln("************************************************************");
1512 dataerrln("THE ABOVE FAILURE MAY JUST MEAN THE LOCALE DATA HAS CHANGED");
1513 dataerrln("************************************************************");
1516 // Now try a non-existent zone
1518 zone2
= new SimpleTimeZone(90*60*1000, "xyzzy");
1520 name
= zone2
->getDisplayName(Locale::getEnglish(),name
);
1521 logln("GMT+90min->" + name
);
1522 if (name
.compare("GMT+01:30") &&
1523 name
.compare("GMT+1:30") &&
1524 name
.compare("GMT+0130") &&
1525 name
.compare("GMT+130"))
1526 dataerrln("Fail: Expected GMT+01:30 or something similar");
1528 zone2
->getDisplayName(name
);
1529 logln("GMT+90min->" + name
);
1530 if (name
.compare("GMT+01:30") &&
1531 name
.compare("GMT+1:30") &&
1532 name
.compare("GMT+0130") &&
1533 name
.compare("GMT+130"))
1534 dataerrln("Fail: Expected GMT+01:30 or something similar");
1544 TimeZoneTest::TestDSTSavings()
1546 UErrorCode status
= U_ZERO_ERROR
;
1547 // It might be better to find a way to integrate this test into the main TimeZone
1548 // tests above, but I don't have time to figure out how to do this (or if it's
1549 // even really a good idea). Let's consider that a future. --rtg 1/27/98
1550 SimpleTimeZone
*tz
= new SimpleTimeZone(-5 * U_MILLIS_PER_HOUR
, "dstSavingsTest",
1551 UCAL_MARCH
, 1, 0, 0, UCAL_SEPTEMBER
, 1, 0, 0,
1552 (int32_t)(0.5 * U_MILLIS_PER_HOUR
), status
);
1553 if(U_FAILURE(status
))
1554 errln("couldn't create TimeZone");
1556 if (tz
->getRawOffset() != -5 * U_MILLIS_PER_HOUR
)
1557 errln(UnicodeString("Got back a raw offset of ") + (tz
->getRawOffset() / U_MILLIS_PER_HOUR
) +
1558 " hours instead of -5 hours.");
1559 if (!tz
->useDaylightTime())
1560 errln("Test time zone should use DST but claims it doesn't.");
1561 if (tz
->getDSTSavings() != 0.5 * U_MILLIS_PER_HOUR
)
1562 errln(UnicodeString("Set DST offset to 0.5 hour, but got back ") + (tz
->getDSTSavings() /
1563 U_MILLIS_PER_HOUR
) + " hours instead.");
1565 int32_t offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JANUARY
, 1,
1566 UCAL_THURSDAY
, 10 * U_MILLIS_PER_HOUR
,status
);
1567 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1568 errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
1569 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1571 offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JUNE
, 1, UCAL_MONDAY
,
1572 10 * U_MILLIS_PER_HOUR
,status
);
1573 if (offset
!= -4.5 * U_MILLIS_PER_HOUR
)
1574 errln(UnicodeString("The offset for 10 AM, 6/1/98 should have been -4.5 hours, but we got ")
1575 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1577 tz
->setDSTSavings(U_MILLIS_PER_HOUR
, status
);
1578 offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JANUARY
, 1,
1579 UCAL_THURSDAY
, 10 * U_MILLIS_PER_HOUR
,status
);
1580 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1581 errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
1582 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1584 offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JUNE
, 1, UCAL_MONDAY
,
1585 10 * U_MILLIS_PER_HOUR
,status
);
1586 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1587 errln(UnicodeString("The offset for 10 AM, 6/1/98 (with a 1-hour DST offset) should have been -4 hours, but we got ")
1588 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1597 TimeZoneTest::TestAlternateRules()
1599 // Like TestDSTSavings, this test should probably be integrated somehow with the main
1600 // test at the top of this class, but I didn't have time to figure out how to do that.
1603 SimpleTimeZone
tz(-5 * U_MILLIS_PER_HOUR
, "alternateRuleTest");
1605 // test the day-of-month API
1606 UErrorCode status
= U_ZERO_ERROR
;
1607 tz
.setStartRule(UCAL_MARCH
, 10, 12 * U_MILLIS_PER_HOUR
, status
);
1608 if(U_FAILURE(status
))
1609 errln("tz.setStartRule failed");
1610 tz
.setEndRule(UCAL_OCTOBER
, 20, 12 * U_MILLIS_PER_HOUR
, status
);
1611 if(U_FAILURE(status
))
1612 errln("tz.setStartRule failed");
1614 int32_t offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 5,
1615 UCAL_THURSDAY
, 10 * U_MILLIS_PER_HOUR
,status
);
1616 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1617 errln(UnicodeString("The offset for 10AM, 3/5/98 should have been -5 hours, but we got ")
1618 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1620 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 15,
1621 UCAL_SUNDAY
, 10 * millisPerHour
,status
);
1622 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1623 errln(UnicodeString("The offset for 10AM, 3/15/98 should have been -4 hours, but we got ")
1624 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1626 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 15,
1627 UCAL_THURSDAY
, 10 * millisPerHour
,status
);
1628 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1629 errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ") + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1631 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 25,
1632 UCAL_SUNDAY
, 10 * millisPerHour
,status
);
1633 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1634 errln(UnicodeString("The offset for 10AM, 10/25/98 should have been -5 hours, but we got ")
1635 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1637 // test the day-of-week-after-day-in-month API
1638 tz
.setStartRule(UCAL_MARCH
, 10, UCAL_FRIDAY
, 12 * millisPerHour
, TRUE
, status
);
1639 if(U_FAILURE(status
))
1640 errln("tz.setStartRule failed");
1641 tz
.setEndRule(UCAL_OCTOBER
, 20, UCAL_FRIDAY
, 12 * millisPerHour
, FALSE
, status
);
1642 if(U_FAILURE(status
))
1643 errln("tz.setStartRule failed");
1645 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 11,
1646 UCAL_WEDNESDAY
, 10 * millisPerHour
,status
);
1647 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1648 errln(UnicodeString("The offset for 10AM, 3/11/98 should have been -5 hours, but we got ")
1649 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1651 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 14,
1652 UCAL_SATURDAY
, 10 * millisPerHour
,status
);
1653 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1654 errln(UnicodeString("The offset for 10AM, 3/14/98 should have been -4 hours, but we got ")
1655 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1657 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 15,
1658 UCAL_THURSDAY
, 10 * millisPerHour
,status
);
1659 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1660 errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ")
1661 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1663 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 17,
1664 UCAL_SATURDAY
, 10 * millisPerHour
,status
);
1665 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1666 errln(UnicodeString("The offset for 10AM, 10/17/98 should have been -5 hours, but we got ")
1667 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1670 void TimeZoneTest::TestFractionalDST() {
1671 const char* tzName
= "Australia/Lord_Howe"; // 30 min offset
1672 TimeZone
* tz_icu
= TimeZone::createTimeZone(tzName
);
1673 int dst_icu
= tz_icu
->getDSTSavings();
1675 int32_t expected
= 1800000;
1676 if (expected
!= dst_icu
) {
1677 dataerrln(UnicodeString("java reports dst savings of ") + expected
+
1678 " but icu reports " + dst_icu
+
1679 " for tz " + tz_icu
->getID(id
));
1681 logln(UnicodeString("both java and icu report dst savings of ") + expected
+ " for tz " + tz_icu
->getID(id
));
1687 * Test country code support. Jitterbug 776.
1689 void TimeZoneTest::TestCountries() {
1690 // Make sure America/Los_Angeles is in the "US" group, and
1691 // Asia/Tokyo isn't. Vice versa for the "JP" group.
1692 UErrorCode ec
= U_ZERO_ERROR
;
1694 StringEnumeration
* s
= TimeZone::createEnumeration("US");
1696 dataerrln("Unable to create TimeZone enumeration for US");
1700 UBool la
= FALSE
, tokyo
= FALSE
;
1701 UnicodeString
laZone("America/Los_Angeles", "");
1702 UnicodeString
tokyoZone("Asia/Tokyo", "");
1705 if (s
== NULL
|| n
<= 0) {
1706 dataerrln("FAIL: TimeZone::createEnumeration() returned nothing");
1709 for (i
=0; i
<n
; ++i
) {
1710 const UnicodeString
* id
= s
->snext(ec
);
1711 if (*id
== (laZone
)) {
1714 if (*id
== (tokyoZone
)) {
1719 errln("FAIL: " + laZone
+ " in US = " + la
);
1720 errln("FAIL: " + tokyoZone
+ " in US = " + tokyo
);
1724 s
= TimeZone::createEnumeration("JP");
1726 la
= FALSE
; tokyo
= FALSE
;
1728 for (i
=0; i
<n
; ++i
) {
1729 const UnicodeString
* id
= s
->snext(ec
);
1730 if (*id
== (laZone
)) {
1733 if (*id
== (tokyoZone
)) {
1738 errln("FAIL: " + laZone
+ " in JP = " + la
);
1739 errln("FAIL: " + tokyoZone
+ " in JP = " + tokyo
);
1741 StringEnumeration
* s1
= TimeZone::createEnumeration("US");
1742 StringEnumeration
* s2
= TimeZone::createEnumeration("US");
1744 const UnicodeString
* id1
= s1
->snext(ec
);
1745 if(id1
==NULL
|| U_FAILURE(ec
)){
1746 errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n
,i
);
1748 TimeZone
* tz1
= TimeZone::createTimeZone(*id1
);
1749 for(int j
=0; j
<n
;++j
){
1750 const UnicodeString
* id2
= s2
->snext(ec
);
1751 if(id2
==NULL
|| U_FAILURE(ec
)){
1752 errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n
,i
);
1754 TimeZone
* tz2
= TimeZone::createTimeZone(*id2
);
1755 if(tz1
->hasSameRules(*tz2
)){
1756 logln("ID1 : " + *id1
+" == ID2 : " +*id2
);
1767 void TimeZoneTest::TestHistorical() {
1768 const int32_t H
= U_MILLIS_PER_HOUR
;
1771 int32_t time
; // epoch seconds
1772 int32_t offset
; // total offset (millis)
1774 // Add transition points (before/after) as desired to test historical
1776 {"America/Los_Angeles", 638963999, -8*H
}, // Sun Apr 01 01:59:59 GMT-08:00 1990
1777 {"America/Los_Angeles", 638964000, -7*H
}, // Sun Apr 01 03:00:00 GMT-07:00 1990
1778 {"America/Los_Angeles", 657104399, -7*H
}, // Sun Oct 28 01:59:59 GMT-07:00 1990
1779 {"America/Los_Angeles", 657104400, -8*H
}, // Sun Oct 28 01:00:00 GMT-08:00 1990
1780 {"America/Goose_Bay", -116445601, -4*H
}, // Sun Apr 24 01:59:59 GMT-04:00 1966
1781 {"America/Goose_Bay", -116445600, -3*H
}, // Sun Apr 24 03:00:00 GMT-03:00 1966
1782 {"America/Goose_Bay", -100119601, -3*H
}, // Sun Oct 30 01:59:59 GMT-03:00 1966
1783 {"America/Goose_Bay", -100119600, -4*H
}, // Sun Oct 30 01:00:00 GMT-04:00 1966
1784 {"America/Goose_Bay", -84391201, -4*H
}, // Sun Apr 30 01:59:59 GMT-04:00 1967
1785 {"America/Goose_Bay", -84391200, -3*H
}, // Sun Apr 30 03:00:00 GMT-03:00 1967
1786 {"America/Goose_Bay", -68670001, -3*H
}, // Sun Oct 29 01:59:59 GMT-03:00 1967
1787 {"America/Goose_Bay", -68670000, -4*H
}, // Sun Oct 29 01:00:00 GMT-04:00 1967
1791 for (int32_t i
=0; DATA
[i
].id
!=0; ++i
) {
1792 const char* id
= DATA
[i
].id
;
1793 TimeZone
*tz
= TimeZone::createTimeZone(id
);
1796 errln("FAIL: Cannot create %s", id
);
1797 } else if (tz
->getID(s
) != UnicodeString(id
)) {
1798 dataerrln((UnicodeString
)"FAIL: createTimeZone(" + id
+ ") => " + s
);
1800 UErrorCode ec
= U_ZERO_ERROR
;
1802 UDate when
= (double) DATA
[i
].time
* U_MILLIS_PER_SECOND
;
1803 tz
->getOffset(when
, FALSE
, raw
, dst
, ec
);
1804 if (U_FAILURE(ec
)) {
1805 errln("FAIL: getOffset");
1806 } else if ((raw
+dst
) != DATA
[i
].offset
) {
1807 errln((UnicodeString
)"FAIL: " + DATA
[i
].id
+ ".getOffset(" +
1809 dateToString(when
) + ") => " +
1812 logln((UnicodeString
)"Ok: " + DATA
[i
].id
+ ".getOffset(" +
1814 dateToString(when
) + ") => " +
1822 void TimeZoneTest::TestEquivalentIDs() {
1823 int32_t n
= TimeZone::countEquivalentIDs("PST");
1825 dataerrln((UnicodeString
)"FAIL: countEquivalentIDs(PST) = " + n
);
1827 UBool sawLA
= FALSE
;
1828 for (int32_t i
=0; i
<n
; ++i
) {
1829 UnicodeString id
= TimeZone::getEquivalentID("PST", i
);
1830 logln((UnicodeString
)"" + i
+ " : " + id
);
1831 if (id
== UnicodeString("America/Los_Angeles")) {
1836 errln("FAIL: America/Los_Angeles should be in the list");
1841 // Test that a transition at the end of February is handled correctly.
1842 void TimeZoneTest::TestFebruary() {
1843 UErrorCode status
= U_ZERO_ERROR
;
1845 // Time zone with daylight savings time from the first Sunday in November
1846 // to the last Sunday in February.
1847 // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
1849 // Note: In tzdata2007h, the rule had changed, so no actual zones uses
1850 // lastSun in Feb anymore.
1851 SimpleTimeZone
tz1(-3 * U_MILLIS_PER_HOUR
, // raw offset: 3h before (west of) GMT
1852 UNICODE_STRING("nov-feb", 7),
1853 UCAL_NOVEMBER
, 1, UCAL_SUNDAY
, // start: November, first, Sunday
1854 0, // midnight wall time
1855 UCAL_FEBRUARY
, -1, UCAL_SUNDAY
, // end: February, last, Sunday
1856 0, // midnight wall time
1858 if (U_FAILURE(status
)) {
1859 errln("Unable to create the SimpleTimeZone(nov-feb): %s", u_errorName(status
));
1863 // Now hardcode the same rules as for Brazil in tzdata 2006n, so that
1864 // we cover the intended code even when in the future zoneinfo hardcodes
1865 // these transition dates.
1866 SimpleTimeZone
tz2(-3 * U_MILLIS_PER_HOUR
, // raw offset: 3h before (west of) GMT
1867 UNICODE_STRING("nov-feb2", 8),
1868 UCAL_NOVEMBER
, 1, -UCAL_SUNDAY
, // start: November, 1 or after, Sunday
1869 0, // midnight wall time
1870 UCAL_FEBRUARY
, -29, -UCAL_SUNDAY
,// end: February, 29 or before, Sunday
1871 0, // midnight wall time
1873 if (U_FAILURE(status
)) {
1874 errln("Unable to create the SimpleTimeZone(nov-feb2): %s", u_errorName(status
));
1878 // Gregorian calendar with the UTC time zone for getting sample test date/times.
1879 GregorianCalendar
gc(*TimeZone::getGMT(), status
);
1880 if (U_FAILURE(status
)) {
1881 dataerrln("Unable to create the UTC calendar: %s", u_errorName(status
));
1887 int32_t year
, month
, day
, hour
, minute
, second
;
1888 // Expected time zone offset in hours after GMT (negative=before GMT).
1889 int32_t offsetHours
;
1891 { 2006, UCAL_NOVEMBER
, 5, 02, 59, 59, -3 },
1892 { 2006, UCAL_NOVEMBER
, 5, 03, 00, 00, -2 },
1893 { 2007, UCAL_FEBRUARY
, 25, 01, 59, 59, -2 },
1894 { 2007, UCAL_FEBRUARY
, 25, 02, 00, 00, -3 },
1896 { 2007, UCAL_NOVEMBER
, 4, 02, 59, 59, -3 },
1897 { 2007, UCAL_NOVEMBER
, 4, 03, 00, 00, -2 },
1898 { 2008, UCAL_FEBRUARY
, 24, 01, 59, 59, -2 },
1899 { 2008, UCAL_FEBRUARY
, 24, 02, 00, 00, -3 },
1901 { 2008, UCAL_NOVEMBER
, 2, 02, 59, 59, -3 },
1902 { 2008, UCAL_NOVEMBER
, 2, 03, 00, 00, -2 },
1903 { 2009, UCAL_FEBRUARY
, 22, 01, 59, 59, -2 },
1904 { 2009, UCAL_FEBRUARY
, 22, 02, 00, 00, -3 },
1906 { 2009, UCAL_NOVEMBER
, 1, 02, 59, 59, -3 },
1907 { 2009, UCAL_NOVEMBER
, 1, 03, 00, 00, -2 },
1908 { 2010, UCAL_FEBRUARY
, 28, 01, 59, 59, -2 },
1909 { 2010, UCAL_FEBRUARY
, 28, 02, 00, 00, -3 }
1912 TimeZone
*timezones
[] = { &tz1
, &tz2
};
1916 int32_t t
, i
, raw
, dst
;
1917 for (t
= 0; t
< UPRV_LENGTHOF(timezones
); ++t
) {
1919 for (i
= 0; i
< UPRV_LENGTHOF(data
); ++i
) {
1920 gc
.set(data
[i
].year
, data
[i
].month
, data
[i
].day
,
1921 data
[i
].hour
, data
[i
].minute
, data
[i
].second
);
1922 dt
= gc
.getTime(status
);
1923 if (U_FAILURE(status
)) {
1924 errln("test case %d.%d: bad date/time %04d-%02d-%02d %02d:%02d:%02d",
1926 data
[i
].year
, data
[i
].month
+ 1, data
[i
].day
,
1927 data
[i
].hour
, data
[i
].minute
, data
[i
].second
);
1928 status
= U_ZERO_ERROR
;
1931 tz
->getOffset(dt
, FALSE
, raw
, dst
, status
);
1932 if (U_FAILURE(status
)) {
1933 errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) fails: %s",
1935 data
[i
].year
, data
[i
].month
+ 1, data
[i
].day
,
1936 data
[i
].hour
, data
[i
].minute
, data
[i
].second
,
1937 u_errorName(status
));
1938 status
= U_ZERO_ERROR
;
1939 } else if ((raw
+ dst
) != data
[i
].offsetHours
* U_MILLIS_PER_HOUR
) {
1940 errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) returns %d+%d != %d",
1942 data
[i
].year
, data
[i
].month
+ 1, data
[i
].day
,
1943 data
[i
].hour
, data
[i
].minute
, data
[i
].second
,
1944 raw
, dst
, data
[i
].offsetHours
* U_MILLIS_PER_HOUR
);
1950 void TimeZoneTest::TestCanonicalIDAPI() {
1951 // Bogus input string.
1952 UnicodeString bogus
;
1954 UnicodeString canonicalID
;
1955 UErrorCode ec
= U_ZERO_ERROR
;
1956 UnicodeString
*pResult
= &TimeZone::getCanonicalID(bogus
, canonicalID
, ec
);
1957 assertEquals("TimeZone::getCanonicalID(bogus) should fail", U_ILLEGAL_ARGUMENT_ERROR
, ec
);
1958 assertTrue("TimeZone::getCanonicalID(bogus) should return the dest string", pResult
== &canonicalID
);
1960 // U_FAILURE on input.
1961 UnicodeString
berlin("Europe/Berlin");
1962 ec
= U_MEMORY_ALLOCATION_ERROR
;
1963 pResult
= &TimeZone::getCanonicalID(berlin
, canonicalID
, ec
);
1964 assertEquals("TimeZone::getCanonicalID(failure) should fail", U_MEMORY_ALLOCATION_ERROR
, ec
);
1965 assertTrue("TimeZone::getCanonicalID(failure) should return the dest string", pResult
== &canonicalID
);
1967 // Valid input should un-bogus the dest string.
1968 canonicalID
.setToBogus();
1970 pResult
= &TimeZone::getCanonicalID(berlin
, canonicalID
, ec
);
1971 assertSuccess("TimeZone::getCanonicalID(bogus dest) should succeed", ec
, TRUE
);
1972 assertTrue("TimeZone::getCanonicalID(bogus dest) should return the dest string", pResult
== &canonicalID
);
1973 assertFalse("TimeZone::getCanonicalID(bogus dest) should un-bogus the dest string", canonicalID
.isBogus());
1974 assertEquals("TimeZone::getCanonicalID(bogus dest) unexpected result", canonicalID
, berlin
, TRUE
);
1977 void TimeZoneTest::TestCanonicalID() {
1979 // Some canonical IDs in CLDR are defined as "Link"
1981 static const struct {
1985 {"Africa/Addis_Ababa", "Africa/Nairobi"},
1986 {"Africa/Asmera", "Africa/Nairobi"},
1987 {"Africa/Bamako", "Africa/Abidjan"},
1988 {"Africa/Bangui", "Africa/Lagos"},
1989 {"Africa/Banjul", "Africa/Abidjan"},
1990 {"Africa/Blantyre", "Africa/Maputo"},
1991 {"Africa/Brazzaville", "Africa/Lagos"},
1992 {"Africa/Bujumbura", "Africa/Maputo"},
1993 {"Africa/Conakry", "Africa/Abidjan"},
1994 {"Africa/Dakar", "Africa/Abidjan"},
1995 {"Africa/Dar_es_Salaam", "Africa/Nairobi"},
1996 {"Africa/Djibouti", "Africa/Nairobi"},
1997 {"Africa/Douala", "Africa/Lagos"},
1998 {"Africa/Freetown", "Africa/Abidjan"},
1999 {"Africa/Gaborone", "Africa/Maputo"},
2000 {"Africa/Harare", "Africa/Maputo"},
2001 {"Africa/Kampala", "Africa/Nairobi"},
2002 {"Africa/Khartoum", "Africa/Juba"},
2003 {"Africa/Kigali", "Africa/Maputo"},
2004 {"Africa/Kinshasa", "Africa/Lagos"},
2005 {"Africa/Libreville", "Africa/Lagos"},
2006 {"Africa/Lome", "Africa/Abidjan"},
2007 {"Africa/Luanda", "Africa/Lagos"},
2008 {"Africa/Lubumbashi", "Africa/Maputo"},
2009 {"Africa/Lusaka", "Africa/Maputo"},
2010 {"Africa/Maseru", "Africa/Johannesburg"},
2011 {"Africa/Malabo", "Africa/Lagos"},
2012 {"Africa/Mbabane", "Africa/Johannesburg"},
2013 {"Africa/Mogadishu", "Africa/Nairobi"},
2014 {"Africa/Niamey", "Africa/Lagos"},
2015 {"Africa/Nouakchott", "Africa/Abidjan"},
2016 {"Africa/Ouagadougou", "Africa/Abidjan"},
2017 {"Africa/Porto-Novo", "Africa/Lagos"},
2018 {"Africa/Sao_Tome", "Africa/Abidjan"},
2019 {"America/Antigua", "America/Port_of_Spain"},
2020 {"America/Anguilla", "America/Port_of_Spain"},
2021 {"America/Curacao", "America/Aruba"},
2022 {"America/Dominica", "America/Port_of_Spain"},
2023 {"America/Grenada", "America/Port_of_Spain"},
2024 {"America/Guadeloupe", "America/Port_of_Spain"},
2025 {"America/Kralendijk", "America/Aruba"},
2026 {"America/Lower_Princes", "America/Aruba"},
2027 {"America/Marigot", "America/Port_of_Spain"},
2028 {"America/Montserrat", "America/Port_of_Spain"},
2029 {"America/Panama", "America/Cayman"},
2030 {"America/Santa_Isabel", "America/Tijuana"},
2031 {"America/Shiprock", "America/Denver"},
2032 {"America/St_Barthelemy", "America/Port_of_Spain"},
2033 {"America/St_Kitts", "America/Port_of_Spain"},
2034 {"America/St_Lucia", "America/Port_of_Spain"},
2035 {"America/St_Thomas", "America/Port_of_Spain"},
2036 {"America/St_Vincent", "America/Port_of_Spain"},
2037 {"America/Toronto", "America/Montreal"},
2038 {"America/Tortola", "America/Port_of_Spain"},
2039 {"America/Virgin", "America/Port_of_Spain"},
2040 {"Antarctica/South_Pole", "Antarctica/McMurdo"},
2041 {"Arctic/Longyearbyen", "Europe/Oslo"},
2042 {"Asia/Kuwait", "Asia/Aden"},
2043 {"Asia/Muscat", "Asia/Dubai"},
2044 {"Asia/Phnom_Penh", "Asia/Bangkok"},
2045 {"Asia/Qatar", "Asia/Bahrain"},
2046 {"Asia/Riyadh", "Asia/Aden"},
2047 {"Asia/Vientiane", "Asia/Bangkok"},
2048 {"Atlantic/Jan_Mayen", "Europe/Oslo"},
2049 {"Atlantic/St_Helena", "Africa/Abidjan"},
2050 {"Europe/Bratislava", "Europe/Prague"},
2051 {"Europe/Busingen", "Europe/Zurich"},
2052 {"Europe/Guernsey", "Europe/London"},
2053 {"Europe/Isle_of_Man", "Europe/London"},
2054 {"Europe/Jersey", "Europe/London"},
2055 {"Europe/Ljubljana", "Europe/Belgrade"},
2056 {"Europe/Mariehamn", "Europe/Helsinki"},
2057 {"Europe/Podgorica", "Europe/Belgrade"},
2058 {"Europe/San_Marino", "Europe/Rome"},
2059 {"Europe/Sarajevo", "Europe/Belgrade"},
2060 {"Europe/Skopje", "Europe/Belgrade"},
2061 {"Europe/Vaduz", "Europe/Zurich"},
2062 {"Europe/Vatican", "Europe/Rome"},
2063 {"Europe/Zagreb", "Europe/Belgrade"},
2064 {"Indian/Antananarivo", "Africa/Nairobi"},
2065 {"Indian/Comoro", "Africa/Nairobi"},
2066 {"Indian/Mayotte", "Africa/Nairobi"},
2067 {"Pacific/Auckland", "Antarctica/McMurdo"},
2068 {"Pacific/Johnston", "Pacific/Honolulu"},
2069 {"Pacific/Midway", "Pacific/Pago_Pago"},
2070 {"Pacific/Saipan", "Pacific/Guam"},
2074 // Following IDs are aliases of Etc/GMT in CLDR,
2075 // but Olson tzdata has 3 independent definitions
2076 // for Etc/GMT, Etc/UTC, Etc/UCT.
2077 // Until we merge them into one equivalent group
2078 // in zoneinfo.res, we exclude them in the test
2080 static const char* excluded2
[] = {
2083 "Etc/Universal", "Universal",
2084 "Etc/Zulu", "Zulu", 0
2087 // Walk through equivalency groups
2088 UErrorCode ec
= U_ZERO_ERROR
;
2089 int32_t s_length
, i
, j
, k
;
2090 StringEnumeration
* s
= TimeZone::createEnumeration();
2092 dataerrln("Unable to create TimeZone enumeration");
2095 UnicodeString canonicalID
, tmpCanonical
;
2096 s_length
= s
->count(ec
);
2097 for (i
= 0; i
< s_length
;++i
) {
2098 const UnicodeString
*tzid
= s
->snext(ec
);
2099 int32_t nEquiv
= TimeZone::countEquivalentIDs(*tzid
);
2103 UBool bFoundCanonical
= FALSE
;
2104 // Make sure getCanonicalID returns the exact same result
2105 // for all entries within a same equivalency group with some
2106 // exceptions listed in exluded1.
2107 // Also, one of them must be canonical id.
2108 for (j
= 0; j
< nEquiv
; j
++) {
2109 UnicodeString tmp
= TimeZone::getEquivalentID(*tzid
, j
);
2110 TimeZone::getCanonicalID(tmp
, tmpCanonical
, ec
);
2111 if (U_FAILURE(ec
)) {
2112 errln((UnicodeString
)"FAIL: getCanonicalID(" + tmp
+ ") failed.");
2116 // Some exceptional cases
2117 for (k
= 0; excluded1
[k
].alias
!= 0; k
++) {
2118 if (tmpCanonical
== excluded1
[k
].alias
) {
2119 tmpCanonical
= excluded1
[k
].zone
;
2124 canonicalID
= tmpCanonical
;
2125 } else if (canonicalID
!= tmpCanonical
) {
2126 errln("FAIL: getCanonicalID(" + tmp
+ ") returned " + tmpCanonical
+ " expected:" + canonicalID
);
2129 if (canonicalID
== tmp
) {
2130 bFoundCanonical
= TRUE
;
2133 // At least one ID in an equvalency group must match the
2135 if (bFoundCanonical
== FALSE
) {
2136 // test exclusion because of differences between Olson tzdata and CLDR
2137 UBool isExcluded
= FALSE
;
2138 for (k
= 0; excluded2
[k
] != 0; k
++) {
2139 if (*tzid
== UnicodeString(excluded2
[k
])) {
2147 errln((UnicodeString
)"FAIL: No timezone ids match the canonical ID " + canonicalID
);
2152 // Testing some special cases
2153 static const struct {
2155 const char *expected
;
2158 {"GMT-03", "GMT-03:00", FALSE
},
2159 {"GMT+4", "GMT+04:00", FALSE
},
2160 {"GMT-055", "GMT-00:55", FALSE
},
2161 {"GMT+430", "GMT+04:30", FALSE
},
2162 {"GMT-12:15", "GMT-12:15", FALSE
},
2163 {"GMT-091015", "GMT-09:10:15", FALSE
},
2164 {"GMT+1:90", 0, FALSE
},
2165 {"America/Argentina/Buenos_Aires", "America/Buenos_Aires", TRUE
},
2166 {"Etc/Unknown", "Etc/Unknown", FALSE
},
2167 {"bogus", 0, FALSE
},
2169 {"America/Marigot", "America/Marigot", TRUE
}, // Olson link, but CLDR canonical (#8953)
2170 {"Europe/Bratislava", "Europe/Bratislava", TRUE
}, // Same as above
2175 for (i
= 0; data
[i
].id
!= 0; i
++) {
2176 TimeZone::getCanonicalID(UnicodeString(data
[i
].id
), canonicalID
, isSystemID
, ec
);
2177 if (U_FAILURE(ec
)) {
2178 if (ec
!= U_ILLEGAL_ARGUMENT_ERROR
|| data
[i
].expected
!= 0) {
2179 errln((UnicodeString
)"FAIL: getCanonicalID(\"" + data
[i
].id
2180 + "\") returned status U_ILLEGAL_ARGUMENT_ERROR");
2185 if (canonicalID
!= data
[i
].expected
) {
2186 dataerrln((UnicodeString
)"FAIL: getCanonicalID(\"" + data
[i
].id
2187 + "\") returned " + canonicalID
+ " - expected: " + data
[i
].expected
);
2189 if (isSystemID
!= data
[i
].isSystem
) {
2190 dataerrln((UnicodeString
)"FAIL: getCanonicalID(\"" + data
[i
].id
2191 + "\") set " + isSystemID
+ " to isSystemID");
2197 // Test Display Names, choosing zones and lcoales where there are multiple
2198 // meta-zones defined.
2201 const char *zoneName
;
2202 const char *localeName
;
2204 TimeZone::EDisplayType style
;
2205 const char *expectedDisplayName
; }
2206 zoneDisplayTestData
[] = {
2207 // zone id locale summer format expected display name
2208 {"Europe/London", "en", FALSE
, TimeZone::SHORT
, "GMT"},
2209 {"Europe/London", "en", FALSE
, TimeZone::LONG
, "Greenwich Mean Time"},
2210 {"Europe/London", "en", TRUE
, TimeZone::SHORT
, "GMT+1" /*"BST"*/},
2211 {"Europe/London", "en", TRUE
, TimeZone::LONG
, "British Summer Time"},
2213 {"America/Anchorage", "en", FALSE
, TimeZone::SHORT
, "AKST"},
2214 {"America/Anchorage", "en", FALSE
, TimeZone::LONG
, "Alaska Standard Time"},
2215 {"America/Anchorage", "en", TRUE
, TimeZone::SHORT
, "AKDT"},
2216 {"America/Anchorage", "en", TRUE
, TimeZone::LONG
, "Alaska Daylight Time"},
2218 // Southern Hemisphere, all data from meta:Australia_Western
2219 {"Australia/Perth", "en", FALSE
, TimeZone::SHORT
, "GMT+8"/*"AWST"*/},
2220 {"Australia/Perth", "en", FALSE
, TimeZone::LONG
, "Australian Western Standard Time"},
2221 // Note: Perth does not observe DST currently. When display name is missing,
2222 // the localized GMT format with the current offset is used even daylight name was
2223 // requested. See #9350.
2224 {"Australia/Perth", "en", TRUE
, TimeZone::SHORT
, "GMT+8"/*"AWDT"*/},
2225 {"Australia/Perth", "en", TRUE
, TimeZone::LONG
, "Australian Western Daylight Time"},
2227 {"America/Sao_Paulo", "en", FALSE
, TimeZone::SHORT
, "GMT-3"/*"BRT"*/},
2228 {"America/Sao_Paulo", "en", FALSE
, TimeZone::LONG
, "Brasilia Standard Time"},
2229 {"America/Sao_Paulo", "en", TRUE
, TimeZone::SHORT
, "GMT-2"/*"BRST"*/},
2230 {"America/Sao_Paulo", "en", TRUE
, TimeZone::LONG
, "Brasilia Summer Time"},
2232 // No Summer Time, but had it before 1983.
2233 {"Pacific/Honolulu", "en", FALSE
, TimeZone::SHORT
, "HST"},
2234 {"Pacific/Honolulu", "en", FALSE
, TimeZone::LONG
, "Hawaii-Aleutian Standard Time"},
2235 {"Pacific/Honolulu", "en", TRUE
, TimeZone::SHORT
, "HDT"},
2236 {"Pacific/Honolulu", "en", TRUE
, TimeZone::LONG
, "Hawaii-Aleutian Daylight Time"},
2238 // Northern, has Summer, not commonly used.
2239 {"Europe/Helsinki", "en", FALSE
, TimeZone::SHORT
, "GMT+2"/*"EET"*/},
2240 {"Europe/Helsinki", "en", FALSE
, TimeZone::LONG
, "Eastern European Standard Time"},
2241 {"Europe/Helsinki", "en", TRUE
, TimeZone::SHORT
, "GMT+3"/*"EEST"*/},
2242 {"Europe/Helsinki", "en", TRUE
, TimeZone::LONG
, "Eastern European Summer Time"},
2244 // Repeating the test data for DST. The test data below trigger the problem reported
2246 {"Europe/London", "en", TRUE
, TimeZone::SHORT
, "GMT+1" /*"BST"*/},
2247 {"Europe/London", "en", TRUE
, TimeZone::LONG
, "British Summer Time"},
2249 {NULL
, NULL
, FALSE
, TimeZone::SHORT
, NULL
} // NULL values terminate list
2252 void TimeZoneTest::TestDisplayNamesMeta() {
2253 UErrorCode status
= U_ZERO_ERROR
;
2254 GregorianCalendar
cal(*TimeZone::getGMT(), status
);
2255 if (failure(status
, "GregorianCalendar", TRUE
)) return;
2257 UBool sawAnError
= FALSE
;
2258 for (int testNum
= 0; zoneDisplayTestData
[testNum
].zoneName
!= NULL
; testNum
++) {
2259 Locale locale
= Locale::createFromName(zoneDisplayTestData
[testNum
].localeName
);
2260 TimeZone
*zone
= TimeZone::createTimeZone(zoneDisplayTestData
[testNum
].zoneName
);
2261 UnicodeString displayName
;
2262 zone
->getDisplayName(zoneDisplayTestData
[testNum
].summerTime
,
2263 zoneDisplayTestData
[testNum
].style
,
2266 if (displayName
!= zoneDisplayTestData
[testNum
].expectedDisplayName
) {
2268 UErrorCode status
= U_ZERO_ERROR
;
2269 displayName
.extract(name
, 100, NULL
, status
);
2270 if (isDevelopmentBuild
) {
2272 dataerrln("Incorrect time zone display name. zone = \"%s\",\n"
2273 " locale = \"%s\", style = %s, Summertime = %d\n"
2274 " Expected \"%s\", "
2275 " Got \"%s\"\n Error: %s", zoneDisplayTestData
[testNum
].zoneName
,
2276 zoneDisplayTestData
[testNum
].localeName
,
2277 zoneDisplayTestData
[testNum
].style
==TimeZone::SHORT
?
2279 zoneDisplayTestData
[testNum
].summerTime
,
2280 zoneDisplayTestData
[testNum
].expectedDisplayName
,
2282 u_errorName(status
));
2284 logln("Incorrect time zone display name. zone = \"%s\",\n"
2285 " locale = \"%s\", style = %s, Summertime = %d\n"
2286 " Expected \"%s\", "
2287 " Got \"%s\"\n", zoneDisplayTestData
[testNum
].zoneName
,
2288 zoneDisplayTestData
[testNum
].localeName
,
2289 zoneDisplayTestData
[testNum
].style
==TimeZone::SHORT
?
2291 zoneDisplayTestData
[testNum
].summerTime
,
2292 zoneDisplayTestData
[testNum
].expectedDisplayName
,
2299 dataerrln("***Note: Errors could be the result of changes to zoneStrings locale data");
2303 void TimeZoneTest::TestGetRegion()
2305 static const struct {
2309 {"America/Los_Angeles", "US"},
2310 {"America/Indianapolis", "US"}, // CLDR canonical, Olson backward
2311 {"America/Indiana/Indianapolis", "US"}, // CLDR alias
2312 {"Mexico/General", "MX"}, // Link America/Mexico_City, Olson backward
2315 {"PST", "US"}, // Link America/Los_Angeles
2316 {"Europe/Helsinki", "FI"},
2317 {"Europe/Mariehamn", "AX"}, // Link Europe/Helsinki, but in zone.tab
2318 {"Asia/Riyadh", "SA"},
2319 // tz file solar87 was removed from tzdata2013i
2320 // {"Asia/Riyadh87", "001"}, // this should be "SA" actually, but not in zone.tab
2321 {"Etc/Unknown", 0}, // CLDR canonical, but not a sysmte zone ID
2322 {"bogus", 0}, // bogus
2323 {"GMT+08:00", 0}, // a custom ID, not a system zone ID
2330 for (i
= 0; data
[i
].id
; i
++) {
2332 TimeZone::getRegion(data
[i
].id
, region
, sizeof(region
), sts
);
2333 if (U_SUCCESS(sts
)) {
2334 if (data
[i
].region
== 0) {
2335 errln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
+ "\") returns "
2336 + region
+ " [expected: U_ILLEGAL_ARGUMENT_ERROR]");
2337 } else if (uprv_strcmp(region
, data
[i
].region
) != 0) {
2338 errln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
+ "\") returns "
2339 + region
+ " [expected: " + data
[i
].region
+ "]");
2341 } else if (sts
== U_ILLEGAL_ARGUMENT_ERROR
) {
2342 if (data
[i
].region
!= 0) {
2343 dataerrln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
2344 + "\") returns error status U_ILLEGAL_ARGUMENT_ERROR [expected: "
2345 + data
[i
].region
+ "]");
2348 errln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
2349 + "\") returns an unexpected error status");
2353 // Extra test cases for short buffer
2358 len
= TimeZone::getRegion("America/New_York", region2
, sizeof(region2
), sts
);
2359 if (sts
== U_ILLEGAL_ARGUMENT_ERROR
) {
2360 dataerrln("Error calling TimeZone::getRegion");
2362 if (sts
!= U_STRING_NOT_TERMINATED_WARNING
) {
2363 errln("Expected U_STRING_NOT_TERMINATED_WARNING");
2365 if (len
!= 2) { // length of "US"
2366 errln("Incorrect result length");
2368 if (uprv_strncmp(region2
, "US", 2) != 0) {
2369 errln("Incorrect result");
2376 len
= TimeZone::getRegion("America/Chicago", region1
, sizeof(region1
), sts
);
2377 if (sts
== U_ILLEGAL_ARGUMENT_ERROR
) {
2378 dataerrln("Error calling TimeZone::getRegion");
2380 if (sts
!= U_BUFFER_OVERFLOW_ERROR
) {
2381 errln("Expected U_BUFFER_OVERFLOW_ERROR");
2383 if (len
!= 2) { // length of "US"
2384 errln("Incorrect result length");
2389 void TimeZoneTest::TestGetUnknown() {
2390 const TimeZone
&unknown
= TimeZone::getUnknown();
2391 UnicodeString expectedID
= UNICODE_STRING_SIMPLE("Etc/Unknown");
2393 assertEquals("getUnknown() wrong ID", expectedID
, unknown
.getID(id
));
2394 assertTrue("getUnknown() wrong offset", 0 == unknown
.getRawOffset());
2395 assertFalse("getUnknown() uses DST", unknown
.useDaylightTime());
2398 void TimeZoneTest::TestGetWindowsID(void) {
2399 static const struct {
2403 {"America/New_York", "Eastern Standard Time"},
2404 {"America/Montreal", "Eastern Standard Time"},
2405 {"America/Los_Angeles", "Pacific Standard Time"},
2406 {"America/Vancouver", "Pacific Standard Time"},
2407 {"Asia/Shanghai", "China Standard Time"},
2408 {"Asia/Chongqing", "China Standard Time"},
2409 {"America/Indianapolis", "US Eastern Standard Time"}, // CLDR canonical name
2410 {"America/Indiana/Indianapolis", "US Eastern Standard Time"}, // tzdb canonical name
2411 {"Asia/Khandyga", "Yakutsk Standard Time"},
2412 {"Australia/Eucla", ""}, // No Windows ID mapping
2417 for (int32_t i
= 0; TESTDATA
[i
].id
!= 0; i
++) {
2418 UErrorCode sts
= U_ZERO_ERROR
;
2419 UnicodeString windowsID
;
2421 TimeZone::getWindowsID(UnicodeString(TESTDATA
[i
].id
), windowsID
, sts
);
2422 assertSuccess(TESTDATA
[i
].id
, sts
);
2423 assertEquals(TESTDATA
[i
].id
, UnicodeString(TESTDATA
[i
].winid
), windowsID
, TRUE
);
2427 void TimeZoneTest::TestGetIDForWindowsID(void) {
2428 static const struct {
2433 {"Eastern Standard Time", 0, "America/New_York"},
2434 {"Eastern Standard Time", "US", "America/New_York"},
2435 {"Eastern Standard Time", "CA", "America/Toronto"},
2436 {"Eastern Standard Time", "CN", "America/New_York"},
2437 {"China Standard Time", 0, "Asia/Shanghai"},
2438 {"China Standard Time", "CN", "Asia/Shanghai"},
2439 {"China Standard Time", "HK", "Asia/Hong_Kong"},
2440 {"Mid-Atlantic Standard Time", 0, ""}, // No tz database mapping
2445 for (int32_t i
= 0; TESTDATA
[i
].winid
!= 0; i
++) {
2446 UErrorCode sts
= U_ZERO_ERROR
;
2449 TimeZone::getIDForWindowsID(UnicodeString(TESTDATA
[i
].winid
), TESTDATA
[i
].region
,
2451 assertSuccess(UnicodeString(TESTDATA
[i
].winid
) + "/" + TESTDATA
[i
].region
, sts
);
2452 assertEquals(UnicodeString(TESTDATA
[i
].winid
) + "/" + TESTDATA
[i
].region
, TESTDATA
[i
].id
, id
, TRUE
);
2456 #endif /* #if !UCONFIG_NO_FORMATTING */