1 /***********************************************************************
3 * Copyright (c) 1997-2012, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/timezone.h"
12 #include "unicode/simpletz.h"
13 #include "unicode/calendar.h"
14 #include "unicode/gregocal.h"
15 #include "unicode/resbund.h"
16 #include "unicode/strenum.h"
23 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
25 #define CASE(id,test) case id: \
28 logln(#test "---"); logln(""); \
33 // *****************************************************************************
35 // *****************************************************************************
37 // TODO: We should probably read following data at runtime, so we can update
38 // these values every release with necessary data changes.
39 const int32_t TimeZoneTest::REFERENCE_YEAR
= 2009;
40 const char * TimeZoneTest::REFERENCE_DATA_VERSION
= "2009d";
42 void TimeZoneTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
45 logln("TestSuite TestTimeZone");
48 TESTCASE_AUTO(TestPRTOffset
);
49 TESTCASE_AUTO(TestVariousAPI518
);
50 TESTCASE_AUTO(TestGetAvailableIDs913
);
51 TESTCASE_AUTO(TestGenericAPI
);
52 TESTCASE_AUTO(TestRuleAPI
);
53 TESTCASE_AUTO(TestShortZoneIDs
);
54 TESTCASE_AUTO(TestCustomParse
);
55 TESTCASE_AUTO(TestDisplayName
);
56 TESTCASE_AUTO(TestDSTSavings
);
57 TESTCASE_AUTO(TestAlternateRules
);
58 TESTCASE_AUTO(TestCountries
);
59 TESTCASE_AUTO(TestHistorical
);
60 TESTCASE_AUTO(TestEquivalentIDs
);
61 TESTCASE_AUTO(TestAliasedNames
);
62 TESTCASE_AUTO(TestFractionalDST
);
63 TESTCASE_AUTO(TestFebruary
);
64 TESTCASE_AUTO(TestCanonicalID
);
65 TESTCASE_AUTO(TestDisplayNamesMeta
);
66 TESTCASE_AUTO(TestGetRegion
);
67 TESTCASE_AUTO(TestGetAvailableIDsNew
);
68 TESTCASE_AUTO(TestGetUnknown
);
72 const int32_t TimeZoneTest::millisPerHour
= 3600000;
74 // ---------------------------------------------------------------------------------
77 * Generic API testing for API coverage.
80 TimeZoneTest::TestGenericAPI()
82 UnicodeString
id("NewGMT");
83 int32_t offset
= 12345;
85 SimpleTimeZone
*zone
= new SimpleTimeZone(offset
, id
);
86 if (zone
->useDaylightTime()) errln("FAIL: useDaylightTime should return FALSE");
88 TimeZone
* zoneclone
= zone
->clone();
89 if (!(*zoneclone
== *zone
)) errln("FAIL: clone or operator== failed");
90 zoneclone
->setID("abc");
91 if (!(*zoneclone
!= *zone
)) errln("FAIL: clone or operator!= failed");
94 zoneclone
= zone
->clone();
95 if (!(*zoneclone
== *zone
)) errln("FAIL: clone or operator== failed");
96 zoneclone
->setRawOffset(45678);
97 if (!(*zoneclone
!= *zone
)) errln("FAIL: clone or operator!= failed");
99 SimpleTimeZone
copy(*zone
);
100 if (!(copy
== *zone
)) errln("FAIL: copy constructor or operator== failed");
101 copy
= *(SimpleTimeZone
*)zoneclone
;
102 if (!(copy
== *zoneclone
)) errln("FAIL: assignment operator or operator== failed");
104 TimeZone
* saveDefault
= TimeZone::createDefault();
105 logln((UnicodeString
)"TimeZone::createDefault() => " + saveDefault
->getID(id
));
106 TimeZone
* pstZone
= TimeZone::createTimeZone("America/Los_Angeles");
108 logln("call uprv_timezone() which uses the host");
109 logln("to get the difference in seconds between coordinated universal");
110 logln("time and local time. E.g., -28,800 for PST (GMT-8hrs)");
112 int32_t tzoffset
= uprv_timezone();
113 logln(UnicodeString("Value returned from uprv_timezone = ") + tzoffset
);
114 // Invert sign because UNIX semantics are backwards
116 tzoffset
= -tzoffset
;
117 if ((*saveDefault
== *pstZone
) && (tzoffset
!= 28800)) {
118 errln("FAIL: t_timezone may be incorrect. It is not 28800");
121 if ((tzoffset
% 900) != 0) {
123 * Ticket#6364 and #7648
124 * A few time zones are using GMT offests not a multiple of 15 minutes.
125 * Therefore, we should not interpret such case as an error.
126 * We downgrade this from errln to infoln. When we see this message,
127 * we should examine if it is ignorable or not.
129 infoln("WARNING: t_timezone may be incorrect. It is not a multiple of 15min.", tzoffset
);
132 TimeZone::adoptDefault(zone
);
133 TimeZone
* defaultzone
= TimeZone::createDefault();
134 if (defaultzone
== zone
||
135 !(*defaultzone
== *zone
))
136 errln("FAIL: createDefault failed");
137 TimeZone::adoptDefault(saveDefault
);
142 UErrorCode status
= U_ZERO_ERROR
;
143 const char* tzver
= TimeZone::getTZDataVersion(status
);
144 if (U_FAILURE(status
)) {
145 errcheckln(status
, "FAIL: getTZDataVersion failed - %s", u_errorName(status
));
146 } else if (uprv_strlen(tzver
) != 5 /* 4 digits + 1 letter */) {
147 errln((UnicodeString
)"FAIL: getTZDataVersion returned " + tzver
);
149 logln((UnicodeString
)"tzdata version: " + tzver
);
153 // ---------------------------------------------------------------------------------
156 * Test the setStartRule/setEndRule API calls.
159 TimeZoneTest::TestRuleAPI()
161 UErrorCode status
= U_ZERO_ERROR
;
163 UDate offset
= 60*60*1000*1.75; // Pick a weird offset
164 SimpleTimeZone
*zone
= new SimpleTimeZone((int32_t)offset
, "TestZone");
165 if (zone
->useDaylightTime()) errln("FAIL: useDaylightTime should return FALSE");
167 // Establish our expected transition times. Do this with a non-DST
168 // calendar with the (above) declared local offset.
169 GregorianCalendar
*gc
= new GregorianCalendar(*zone
, status
);
170 if (failure(status
, "new GregorianCalendar", TRUE
)) return;
172 gc
->set(1990, UCAL_MARCH
, 1);
173 UDate marchOneStd
= gc
->getTime(status
); // Local Std time midnight
175 gc
->set(1990, UCAL_JULY
, 1);
176 UDate julyOneStd
= gc
->getTime(status
); // Local Std time midnight
177 if (failure(status
, "GregorianCalendar::getTime")) return;
179 // Starting and ending hours, WALL TIME
180 int32_t startHour
= (int32_t)(2.25 * 3600000);
181 int32_t endHour
= (int32_t)(3.5 * 3600000);
183 zone
->setStartRule(UCAL_MARCH
, 1, 0, startHour
, status
);
184 zone
->setEndRule (UCAL_JULY
, 1, 0, endHour
, status
);
187 gc
= new GregorianCalendar(*zone
, status
);
188 if (failure(status
, "new GregorianCalendar")) return;
190 UDate marchOne
= marchOneStd
+ startHour
;
191 UDate julyOne
= julyOneStd
+ endHour
- 3600000; // Adjust from wall to Std time
193 UDate expMarchOne
= 636251400000.0;
194 if (marchOne
!= expMarchOne
)
196 errln((UnicodeString
)"FAIL: Expected start computed as " + marchOne
+
197 " = " + dateToString(marchOne
));
198 logln((UnicodeString
)" Should be " + expMarchOne
+
199 " = " + dateToString(expMarchOne
));
202 UDate expJulyOne
= 646793100000.0;
203 if (julyOne
!= expJulyOne
)
205 errln((UnicodeString
)"FAIL: Expected start computed as " + julyOne
+
206 " = " + dateToString(julyOne
));
207 logln((UnicodeString
)" Should be " + expJulyOne
+
208 " = " + dateToString(expJulyOne
));
211 testUsingBinarySearch(*zone
, date(90, UCAL_JANUARY
, 1), date(90, UCAL_JUNE
, 15), marchOne
);
212 testUsingBinarySearch(*zone
, date(90, UCAL_JUNE
, 1), date(90, UCAL_DECEMBER
, 31), julyOne
);
214 if (zone
->inDaylightTime(marchOne
- 1000, status
) ||
215 !zone
->inDaylightTime(marchOne
, status
))
216 errln("FAIL: Start rule broken");
217 if (!zone
->inDaylightTime(julyOne
- 1000, status
) ||
218 zone
->inDaylightTime(julyOne
, status
))
219 errln("FAIL: End rule broken");
221 zone
->setStartYear(1991);
222 if (zone
->inDaylightTime(marchOne
, status
) ||
223 zone
->inDaylightTime(julyOne
- 1000, status
))
224 errln("FAIL: Start year broken");
226 failure(status
, "TestRuleAPI");
232 TimeZoneTest::findTransition(const TimeZone
& tz
,
233 UDate min
, UDate max
) {
234 UErrorCode ec
= U_ZERO_ERROR
;
236 UBool startsInDST
= tz
.inDaylightTime(min
, ec
);
237 if (failure(ec
, "TimeZone::inDaylightTime")) return;
238 if (tz
.inDaylightTime(max
, ec
) == startsInDST
) {
239 logln("Error: " + tz
.getID(id
) + ".inDaylightTime(" + dateToString(min
) + ") = " + (startsInDST
?"TRUE":"FALSE") +
240 ", inDaylightTime(" + dateToString(max
) + ") = " + (startsInDST
?"TRUE":"FALSE"));
243 if (failure(ec
, "TimeZone::inDaylightTime")) return;
244 while ((max
- min
) > INTERVAL
) {
245 UDate mid
= (min
+ max
) / 2;
246 if (tz
.inDaylightTime(mid
, ec
) == startsInDST
) {
251 if (failure(ec
, "TimeZone::inDaylightTime")) return;
253 min
= 1000.0 * uprv_floor(min
/1000.0);
254 max
= 1000.0 * uprv_floor(max
/1000.0);
255 logln(tz
.getID(id
) + " Before: " + min
/1000 + " = " +
256 dateToString(min
,s
,tz
));
257 logln(tz
.getID(id
) + " After: " + max
/1000 + " = " +
258 dateToString(max
,s
,tz
));
262 TimeZoneTest::testUsingBinarySearch(const TimeZone
& tz
,
263 UDate min
, UDate max
,
264 UDate expectedBoundary
)
266 UErrorCode status
= U_ZERO_ERROR
;
267 UBool startsInDST
= tz
.inDaylightTime(min
, status
);
268 if (failure(status
, "TimeZone::inDaylightTime")) return;
269 if (tz
.inDaylightTime(max
, status
) == startsInDST
) {
270 logln("Error: inDaylightTime(" + dateToString(max
) + ") != " + ((!startsInDST
)?"TRUE":"FALSE"));
273 if (failure(status
, "TimeZone::inDaylightTime")) return;
274 while ((max
- min
) > INTERVAL
) {
275 UDate mid
= (min
+ max
) / 2;
276 if (tz
.inDaylightTime(mid
, status
) == startsInDST
) {
281 if (failure(status
, "TimeZone::inDaylightTime")) return;
283 logln(UnicodeString("Binary Search Before: ") + uprv_floor(0.5 + min
) + " = " + dateToString(min
));
284 logln(UnicodeString("Binary Search After: ") + uprv_floor(0.5 + max
) + " = " + dateToString(max
));
285 UDate mindelta
= expectedBoundary
- min
;
286 UDate maxdelta
= max
- expectedBoundary
;
288 mindelta
<= INTERVAL
&&
290 maxdelta
<= INTERVAL
)
291 logln(UnicodeString("PASS: Expected bdry: ") + expectedBoundary
+ " = " + dateToString(expectedBoundary
));
293 errln(UnicodeString("FAIL: Expected bdry: ") + expectedBoundary
+ " = " + dateToString(expectedBoundary
));
296 const UDate
TimeZoneTest::INTERVAL
= 100;
298 // ---------------------------------------------------------------------------------
300 // -------------------------------------
303 * Test the offset of the PRT timezone.
306 TimeZoneTest::TestPRTOffset()
308 TimeZone
* tz
= TimeZone::createTimeZone("PRT");
310 errln("FAIL: TimeZone(PRT) is null");
313 int32_t expectedHour
= -4;
314 double expectedOffset
= (((double)expectedHour
) * millisPerHour
);
315 double foundOffset
= tz
->getRawOffset();
316 int32_t foundHour
= (int32_t)foundOffset
/ millisPerHour
;
317 if (expectedOffset
!= foundOffset
) {
318 dataerrln("FAIL: Offset for PRT should be %d, found %d", expectedHour
, foundHour
);
320 logln("PASS: Offset for PRT should be %d, found %d", expectedHour
, foundHour
);
326 // -------------------------------------
329 * Regress a specific bug with a sequence of API calls.
332 TimeZoneTest::TestVariousAPI518()
334 UErrorCode status
= U_ZERO_ERROR
;
335 TimeZone
* time_zone
= TimeZone::createTimeZone("PST");
336 UDate d
= date(97, UCAL_APRIL
, 30);
338 logln("The timezone is " + time_zone
->getID(str
));
339 if (!time_zone
->inDaylightTime(d
, status
)) dataerrln("FAIL: inDaylightTime returned FALSE");
340 if (failure(status
, "TimeZone::inDaylightTime", TRUE
)) return;
341 if (!time_zone
->useDaylightTime()) dataerrln("FAIL: useDaylightTime returned FALSE");
342 if (time_zone
->getRawOffset() != - 8 * millisPerHour
) dataerrln("FAIL: getRawOffset returned wrong value");
343 GregorianCalendar
*gc
= new GregorianCalendar(status
);
344 if (U_FAILURE(status
)) { errln("FAIL: Couldn't create GregorianCalendar"); return; }
345 gc
->setTime(d
, status
);
346 if (U_FAILURE(status
)) { errln("FAIL: GregorianCalendar::setTime failed"); return; }
347 if (time_zone
->getOffset(gc
->AD
, gc
->get(UCAL_YEAR
, status
), gc
->get(UCAL_MONTH
, status
),
348 gc
->get(UCAL_DATE
, status
), (uint8_t)gc
->get(UCAL_DAY_OF_WEEK
, status
), 0, status
) != - 7 * millisPerHour
)
349 dataerrln("FAIL: getOffset returned wrong value");
350 if (U_FAILURE(status
)) { errln("FAIL: GregorianCalendar::set failed"); return; }
355 // -------------------------------------
358 * Test the call which retrieves the available IDs.
361 TimeZoneTest::TestGetAvailableIDs913()
363 UErrorCode ec
= U_ZERO_ERROR
;
366 #ifdef U_USE_TIMEZONE_OBSOLETE_2_8
367 // Test legacy API -- remove these tests when the corresponding API goes away (duh)
369 const UnicodeString
** ids
= TimeZone::createAvailableIDs(numIDs
);
370 if (ids
== 0 || numIDs
< 1) {
371 errln("FAIL: createAvailableIDs()");
373 UnicodeString
buf("TimeZone::createAvailableIDs() = { ");
374 for(i
=0; i
<numIDs
; ++i
) {
375 if (i
) buf
.append(", ");
380 // we own the array; the caller owns the contained strings (yuck)
385 ids
= TimeZone::createAvailableIDs(-8*U_MILLIS_PER_HOUR
, numIDs
);
386 if (ids
== 0 || numIDs
< 1) {
387 errln("FAIL: createAvailableIDs(-8:00)");
389 UnicodeString
buf("TimeZone::createAvailableIDs(-8:00) = { ");
390 for(i
=0; i
<numIDs
; ++i
) {
391 if (i
) buf
.append(", ");
396 // we own the array; the caller owns the contained strings (yuck)
400 ids
= TimeZone::createAvailableIDs("US", numIDs
);
401 if (ids
== 0 || numIDs
< 1) {
402 errln("FAIL: createAvailableIDs(US) ids=%d, numIDs=%d", ids
, numIDs
);
404 UnicodeString
buf("TimeZone::createAvailableIDs(US) = { ");
405 for(i
=0; i
<numIDs
; ++i
) {
406 if (i
) buf
.append(", ");
411 // we own the array; the caller owns the contained strings (yuck)
417 UnicodeString
*buf
= new UnicodeString("TimeZone::createEnumeration() = { ");
419 StringEnumeration
* s
= TimeZone::createEnumeration();
421 dataerrln("Unable to create TimeZone enumeration");
424 s_length
= s
->count(ec
);
425 for (i
= 0; i
< s_length
;++i
) {
426 if (i
> 0) *buf
+= ", ";
428 *buf
+= *s
->snext(ec
);
430 *buf
+= UnicodeString(s
->next(NULL
, ec
), "");
434 // replace s with a clone of itself
435 StringEnumeration
*s2
= s
->clone();
436 if(s2
== NULL
|| s_length
!= s2
->count(ec
)) {
437 errln("TimezoneEnumeration.clone() failed");
447 /* Confirm that the following zones can be retrieved: The first
448 * zone, the last zone, and one in-between. This tests the binary
449 * search through the system zone data.
452 int32_t middle
= s_length
/2;
453 for (i
=0; i
<s_length
; ++i
) {
454 const UnicodeString
* id
= s
->snext(ec
);
455 if (i
==0 || i
==middle
|| i
==(s_length
-1)) {
456 TimeZone
*z
= TimeZone::createTimeZone(*id
);
458 errln(UnicodeString("FAIL: createTimeZone(") +
460 } else if (z
->getID(str
) != *id
) {
461 errln(UnicodeString("FAIL: createTimeZone(") +
462 *id
+ ") -> zone " + str
);
464 logln(UnicodeString("OK: createTimeZone(") +
465 *id
+ ") succeeded");
473 *buf
+= "TimeZone::createEnumeration(GMT+01:00) = { ";
475 s
= TimeZone::createEnumeration(1 * U_MILLIS_PER_HOUR
);
476 s_length
= s
->count(ec
);
477 for (i
= 0; i
< s_length
;++i
) {
478 if (i
> 0) *buf
+= ", ";
479 *buf
+= *s
->snext(ec
);
487 *buf
+= "TimeZone::createEnumeration(US) = { ";
489 s
= TimeZone::createEnumeration("US");
490 s_length
= s
->count(ec
);
491 for (i
= 0; i
< s_length
;++i
) {
492 if (i
> 0) *buf
+= ", ";
493 *buf
+= *s
->snext(ec
);
498 TimeZone
*tz
= TimeZone::createTimeZone("PST");
499 if (tz
!= 0) logln("getTimeZone(PST) = " + tz
->getID(str
));
500 else errln("FAIL: getTimeZone(PST) = null");
502 tz
= TimeZone::createTimeZone("America/Los_Angeles");
503 if (tz
!= 0) logln("getTimeZone(America/Los_Angeles) = " + tz
->getID(str
));
504 else errln("FAIL: getTimeZone(PST) = null");
508 tz
= TimeZone::createTimeZone("NON_EXISTENT");
511 errln("FAIL: getTimeZone(NON_EXISTENT) = null");
512 else if (tz
->getID(temp
) != UCAL_UNKNOWN_ZONE_ID
)
513 errln("FAIL: getTimeZone(NON_EXISTENT) = " + temp
);
521 TimeZoneTest::TestGetAvailableIDsNew()
523 UErrorCode ec
= U_ZERO_ERROR
;
524 StringEnumeration
*any
, *canonical
, *canonicalLoc
;
525 StringEnumeration
*any_US
, *canonical_US
, *canonicalLoc_US
;
526 StringEnumeration
*any_W5
, *any_CA_W5
;
527 StringEnumeration
*any_US_E14
;
529 const UnicodeString
*id1
, *id2
;
530 UnicodeString canonicalID
;
535 any
= canonical
= canonicalLoc
= any_US
= canonical_US
= canonicalLoc_US
= any_W5
= any_CA_W5
= any_US_E14
= NULL
;
537 any
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, NULL
, NULL
, ec
);
539 dataerrln("Failed to create enumration for ANY");
543 canonical
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL
, NULL
, NULL
, ec
);
545 errln("Failed to create enumration for CANONICAL");
549 canonicalLoc
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION
, NULL
, NULL
, ec
);
551 errln("Failed to create enumration for CANONICALLOC");
555 any_US
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, "US", NULL
, ec
);
557 errln("Failed to create enumration for ANY_US");
561 canonical_US
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL
, "US", NULL
, ec
);
563 errln("Failed to create enumration for CANONICAL_US");
567 canonicalLoc_US
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION
, "US", NULL
, ec
);
569 errln("Failed to create enumration for CANONICALLOC_US");
573 rawOffset
= (-5)*60*60*1000;
574 any_W5
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, NULL
, &rawOffset
, ec
);
576 errln("Failed to create enumration for ANY_W5");
580 any_CA_W5
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, "CA", &rawOffset
, ec
);
582 errln("Failed to create enumration for ANY_CA_W5");
586 rawOffset
= 14*60*60*1000;
587 any_US_E14
= TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY
, "US", &rawOffset
, ec
);
589 errln("Failed to create enumration for ANY_US_E14");
593 checkContainsAll(any
, "ANY", canonical
, "CANONICAL");
594 checkContainsAll(canonical
, "CANONICAL", canonicalLoc
, "CANONICALLOC");
596 checkContainsAll(any
, "ANY", any_US
, "ANY_US");
597 checkContainsAll(canonical
, "CANONICAL", canonical_US
, "CANONICAL_US");
598 checkContainsAll(canonicalLoc
, "CANONICALLOC", canonicalLoc_US
, "CANONICALLOC_US");
600 checkContainsAll(any_US
, "ANY_US", canonical_US
, "CANONICAL_US");
601 checkContainsAll(canonical_US
, "CANONICAL_US", canonicalLoc_US
, "CANONICALLOC_US");
603 checkContainsAll(any
, "ANY", any_W5
, "ANY_W5");
604 checkContainsAll(any_W5
, "ANY_W5", any_CA_W5
, "ANY_CA_W5");
606 // And ID in any set, but not in canonical set must not be a canonical ID
608 while ((id1
= any
->snext(ec
)) != NULL
) {
610 canonical
->reset(ec
);
611 while ((id2
= canonical
->snext(ec
)) != NULL
) {
621 TimeZone::getCanonicalID(*id1
, canonicalID
, isSystemID
, ec
);
625 if (*id1
== canonicalID
) {
626 errln((UnicodeString
)"FAIL: canonicalID [" + *id1
+ "] is not in CANONICAL");
629 errln((UnicodeString
)"FAIL: ANY contains non-system ID: " + *id1
);
634 errln("Error checking IDs in ANY, but not in CANONICAL");
638 // canonical set must contains only canonical IDs
639 canonical
->reset(ec
);
640 while ((id1
= canonical
->snext(ec
)) != NULL
) {
641 TimeZone::getCanonicalID(*id1
, canonicalID
, isSystemID
, ec
);
645 if (*id1
!= canonicalID
) {
646 errln((UnicodeString
)"FAIL: CANONICAL contains non-canonical ID: " + *id1
);
649 errln((UnicodeString
)"FAILE: CANONICAL contains non-system ID: " + *id1
);
653 errln("Error checking IDs in CANONICAL");
657 // canonicalLoc set must contain only canonical location IDs
658 canonicalLoc
->reset(ec
);
659 while ((id1
= canonicalLoc
->snext(ec
)) != NULL
) {
660 TimeZone::getRegion(*id1
, region
, sizeof(region
), ec
);
664 if (uprv_strcmp(region
, "001") == 0) {
665 errln((UnicodeString
)"FAIL: CANONICALLOC contains non location zone: " + *id1
);
669 errln("Error checking IDs in CANONICALLOC");
673 // any_US must contain only US zones
675 while ((id1
= any_US
->snext(ec
)) != NULL
) {
676 TimeZone::getRegion(*id1
, region
, sizeof(region
), ec
);
680 if (uprv_strcmp(region
, "US") != 0) {
681 errln((UnicodeString
)"FAIL: ANY_US contains non-US zone ID: " + *id1
);
685 errln("Error checking IDs in ANY_US");
689 // any_W5 must contain only GMT-05:00 zones
691 while ((id1
= any_W5
->snext(ec
)) != NULL
) {
692 TimeZone
*tz
= TimeZone::createTimeZone(*id1
);
693 if (tz
->getRawOffset() != (-5)*60*60*1000) {
694 errln((UnicodeString
)"FAIL: ANY_W5 contains a zone whose offset is not -05:00: " + *id1
);
699 errln("Error checking IDs in ANY_W5");
703 // No US zone swith GMT+14:00
704 zoneCount
= any_US_E14
->count(ec
);
706 errln("Error checking IDs in ANY_US_E14");
708 } else if (zoneCount
!= 0) {
709 errln("FAIL: ANY_US_E14 must be empty");
718 delete canonicalLoc_US
;
725 TimeZoneTest::checkContainsAll(StringEnumeration
*s1
, const char *name1
,
726 StringEnumeration
*s2
, const char *name2
)
728 UErrorCode ec
= U_ZERO_ERROR
;
729 const UnicodeString
*id1
, *id2
;
733 while ((id2
= s2
->snext(ec
)) != NULL
) {
736 while ((id1
= s1
->snext(ec
)) != NULL
) {
743 errln((UnicodeString
)"FAIL: " + name1
+ "does not contain "
744 + *id2
+ " in " + name2
);
749 errln((UnicodeString
)"Error checkContainsAll for " + name1
+ " - " + name2
);
754 * NOTE: As of ICU 2.8, this test confirms that the "tz.alias"
755 * file, used to build ICU alias zones, is working. It also
756 * looks at some genuine Olson compatibility IDs. [aliu]
758 * This test is problematic. It should really just confirm that
759 * the list of compatibility zone IDs exist and are somewhat
760 * meaningful (that is, they aren't all aliases of GMT). It goes a
761 * bit further -- it hard-codes expectations about zone behavior,
762 * when in fact zones are redefined quite frequently. ICU's build
763 * process means that it is easy to update ICU to contain the
764 * latest Olson zone data, but if a zone tested here changes, then
765 * this test will fail. I have updated the test for 1999j data,
766 * but further updates will probably be required. Note that some
767 * of the concerts listed below no longer apply -- in particular,
768 * we do NOT overwrite real UNIX zones with 3-letter IDs. There
769 * are two points of overlap as of 1999j: MET and EET. These are
770 * both real UNIX zones, so we just use the official
771 * definition. This test has been updated to reflect this.
774 * Added tests for additional zones and aliases from the icuzones file.
775 * Markus Scherer 2006-nov-06
777 * [srl - from java - 7/5/1998]
779 * Certain short zone IDs, used since 1.1.x, are incorrect.
781 * The worst of these is:
783 * "CAT" (Central African Time) should be GMT+2:00, but instead returns a
784 * zone at GMT-1:00. The zone at GMT-1:00 should be called EGT, CVT, EGST,
785 * or AZOST, depending on which zone is meant, but in no case is it CAT.
787 * Other wrong zone IDs:
789 * ECT (European Central Time) GMT+1:00: ECT is Ecuador Time,
790 * GMT-5:00. European Central time is abbreviated CEST.
792 * SST (Solomon Island Time) GMT+11:00. SST is actually Samoa Standard Time,
793 * GMT-11:00. Solomon Island time is SBT.
795 * NST (New Zealand Time) GMT+12:00. NST is the abbreviation for
796 * Newfoundland Standard Time, GMT-3:30. New Zealanders use NZST.
798 * AST (Alaska Standard Time) GMT-9:00. [This has already been noted in
799 * another bug.] It should be "AKST". AST is Atlantic Standard Time,
802 * PNT (Phoenix Time) GMT-7:00. PNT usually means Pitcairn Time,
803 * GMT-8:30. There is no standard abbreviation for Phoenix time, as distinct
804 * from MST with daylight savings.
806 * In addition to these problems, a number of zones are FAKE. That is, they
807 * don't match what people use in the real world.
811 * EET (should be EEST)
812 * ART (should be EEST)
813 * MET (should be IRST)
814 * NET (should be AMST)
815 * PLT (should be PKT)
816 * BST (should be BDT)
817 * VST (should be ICT)
818 * CTT (should be CST) +
819 * ACT (should be CST) +
820 * AET (should be EST) +
821 * MIT (should be WST) +
822 * IET (should be EST) +
823 * PRT (should be AST) +
824 * CNT (should be NST)
825 * AGT (should be ARST)
826 * BET (should be EST) +
828 * + A zone with the correct name already exists and means something
829 * else. E.g., EST usually indicates the US Eastern zone, so it cannot be
830 * used for Brazil (BET).
832 void TimeZoneTest::TestShortZoneIDs()
834 UErrorCode status
= U_ZERO_ERROR
;
836 // This test case is tzdata version sensitive.
837 UBool isNonReferenceTzdataVersion
= FALSE
;
838 const char *tzdataVer
= TimeZone::getTZDataVersion(status
);
839 if (failure(status
, "getTZDataVersion")) return;
840 if (uprv_strcmp(tzdataVer
, TimeZoneTest::REFERENCE_DATA_VERSION
) != 0) {
841 // Note: We want to display a warning message here if
842 // REFERENCE_DATA_VERSION is out of date - so we
843 // do not forget to update the value before GA.
844 isNonReferenceTzdataVersion
= TRUE
;
845 logln(UnicodeString("Warning: Active tzdata version (") + tzdataVer
+
846 ") does not match the reference tzdata version ("
847 + REFERENCE_DATA_VERSION
+ ") for this test case data.");
850 // Note: useDaylightTime returns true if DST is observed
851 // in the time zone in the current calendar year. The test
852 // data is valid for the date after the reference year below.
853 // If system clock is before the year, some test cases may
855 GregorianCalendar
cal(*TimeZone::getGMT(), status
);
856 if (failure(status
, "GregorianCalendar")) return;
857 cal
.set(TimeZoneTest::REFERENCE_YEAR
, UCAL_JANUARY
, 2); // day 2 in GMT
859 UBool isDateBeforeReferenceYear
= ucal_getNow() < cal
.getTime(status
);
860 if (failure(status
, "Calendar::getTime")) return;
861 if (isDateBeforeReferenceYear
) {
862 logln("Warning: Past time is set to the system clock. Some test cases may not return expected results.");
866 // Create a small struct to hold the array
875 {"MIT", -660, FALSE
},
876 {"HST", -600, FALSE
},
879 {"PNT", -420, FALSE
},
880 {"MST", -420, FALSE
}, // updated Aug 2003 aliu
882 {"IET", -300, TRUE
}, // updated Jan 2006 srl
883 {"EST", -300, FALSE
}, // updated Aug 2003 aliu
884 {"PRT", -240, FALSE
},
886 {"AGT", -180, TRUE
}, // updated by tzdata2007k
889 {"UTC", 0, FALSE
}, // ** srl: seems broken in C++
891 {"MET", 60, TRUE
}, // updated 12/3/99 aliu
894 {"CAT", 120, FALSE
}, // Africa/Harare
896 {"NET", 240, TRUE
}, // updated 12/3/99 aliu
897 {"PLT", 300, FALSE
}, // updated by 2008c - no DST after 2008
901 {"CTT", 480, FALSE
}, // updated Aug 2003 aliu
903 {"ACT", 570, FALSE
}, // updated Aug 2003 aliu
906 {"NST", 720, TRUE
}, // Pacific/Auckland
909 {"Etc/Unknown", 0, FALSE
},
911 {"SystemV/AST4ADT", -240, TRUE
},
912 {"SystemV/EST5EDT", -300, TRUE
},
913 {"SystemV/CST6CDT", -360, TRUE
},
914 {"SystemV/MST7MDT", -420, TRUE
},
915 {"SystemV/PST8PDT", -480, TRUE
},
916 {"SystemV/YST9YDT", -540, TRUE
},
917 {"SystemV/AST4", -240, FALSE
},
918 {"SystemV/EST5", -300, FALSE
},
919 {"SystemV/CST6", -360, FALSE
},
920 {"SystemV/MST7", -420, FALSE
},
921 {"SystemV/PST8", -480, FALSE
},
922 {"SystemV/YST9", -540, FALSE
},
923 {"SystemV/HST10", -600, FALSE
},
928 for(i
=0;kReferenceList
[i
].id
[0];i
++) {
929 UnicodeString
itsID(kReferenceList
[i
].id
);
932 TimeZone
*tz
= TimeZone::createTimeZone(itsID
);
933 if (!tz
|| (kReferenceList
[i
].offset
!= 0 && *tz
== *TimeZone::getGMT())) {
934 errln("FAIL: Time Zone " + itsID
+ " does not exist!");
938 // Check daylight usage.
939 UBool usesDaylight
= tz
->useDaylightTime();
940 if (usesDaylight
!= kReferenceList
[i
].daylight
) {
941 if (isNonReferenceTzdataVersion
|| isDateBeforeReferenceYear
) {
942 logln("Warning: Time Zone " + itsID
+ " use daylight is " +
943 (usesDaylight
?"TRUE":"FALSE") +
944 " but it should be " +
945 ((kReferenceList
[i
].daylight
)?"TRUE":"FALSE"));
947 errln("FAIL: Time Zone " + itsID
+ " use daylight is " +
948 (usesDaylight
?"TRUE":"FALSE") +
949 " but it should be " +
950 ((kReferenceList
[i
].daylight
)?"TRUE":"FALSE"));
956 int32_t offsetInMinutes
= tz
->getRawOffset()/60000;
957 if (offsetInMinutes
!= kReferenceList
[i
].offset
) {
958 if (isNonReferenceTzdataVersion
|| isDateBeforeReferenceYear
) {
959 logln("FAIL: Time Zone " + itsID
+ " raw offset is " +
961 " but it should be " + kReferenceList
[i
].offset
);
963 errln("FAIL: Time Zone " + itsID
+ " raw offset is " +
965 " but it should be " + kReferenceList
[i
].offset
);
971 logln("OK: " + itsID
+
972 " useDaylightTime() & getRawOffset() as expected");
978 // OK now test compat
979 logln("Testing for compatibility zones");
981 const char* compatibilityMap
[] = {
982 // This list is copied from tz.alias. If tz.alias
983 // changes, this list must be updated. Current as of Mar 2007
984 "ACT", "Australia/Darwin",
985 "AET", "Australia/Sydney",
986 "AGT", "America/Buenos_Aires",
987 "ART", "Africa/Cairo",
988 "AST", "America/Anchorage",
989 "BET", "America/Sao_Paulo",
990 "BST", "Asia/Dhaka", // # spelling changed in 2000h; was Asia/Dacca
991 "CAT", "Africa/Harare",
992 "CNT", "America/St_Johns",
993 "CST", "America/Chicago",
994 "CTT", "Asia/Shanghai",
995 "EAT", "Africa/Addis_Ababa",
996 "ECT", "Europe/Paris",
997 // EET Europe/Istanbul # EET is a standard UNIX zone
998 // "EST", "America/New_York", # Defined as -05:00
999 // "HST", "Pacific/Honolulu", # Defined as -10:00
1000 "IET", "America/Indianapolis",
1001 "IST", "Asia/Calcutta",
1002 "JST", "Asia/Tokyo",
1003 // MET Asia/Tehran # MET is a standard UNIX zone
1004 "MIT", "Pacific/Apia",
1005 // "MST", "America/Denver", # Defined as -07:00
1006 "NET", "Asia/Yerevan",
1007 "NST", "Pacific/Auckland",
1008 "PLT", "Asia/Karachi",
1009 "PNT", "America/Phoenix",
1010 "PRT", "America/Puerto_Rico",
1011 "PST", "America/Los_Angeles",
1012 "SST", "Pacific/Guadalcanal",
1014 "VST", "Asia/Saigon",
1018 for (i
=0;*compatibilityMap
[i
];i
+=2) {
1019 UnicodeString itsID
;
1021 const char *zone1
= compatibilityMap
[i
];
1022 const char *zone2
= compatibilityMap
[i
+1];
1024 TimeZone
*tz1
= TimeZone::createTimeZone(zone1
);
1025 TimeZone
*tz2
= TimeZone::createTimeZone(zone2
);
1028 errln(UnicodeString("FAIL: Could not find short ID zone ") + zone1
);
1031 errln(UnicodeString("FAIL: Could not find long ID zone ") + zone2
);
1035 // make NAME same so comparison will only look at the rest
1036 tz2
->setID(tz1
->getID(itsID
));
1039 errln("FAIL: " + UnicodeString(zone1
) +
1040 " != " + UnicodeString(zone2
));
1042 logln("OK: " + UnicodeString(zone1
) +
1043 " == " + UnicodeString(zone2
));
1054 * Utility function for TestCustomParse
1056 UnicodeString
& TimeZoneTest::formatOffset(int32_t offset
, UnicodeString
&rv
) {
1058 UChar sign
= 0x002B;
1064 int32_t s
= offset
% 60;
1066 int32_t m
= offset
% 60;
1067 int32_t h
= offset
/ 60;
1069 rv
+= (UChar
)(sign
);
1071 rv
+= (UChar
)(0x0030 + (h
/10));
1073 rv
+= (UChar
)0x0030;
1075 rv
+= (UChar
)(0x0030 + (h%10
));
1077 rv
+= (UChar
)0x003A; /* ':' */
1079 rv
+= (UChar
)(0x0030 + (m
/10));
1081 rv
+= (UChar
)0x0030;
1083 rv
+= (UChar
)(0x0030 + (m%10
));
1086 rv
+= (UChar
)0x003A; /* ':' */
1088 rv
+= (UChar
)(0x0030 + (s
/10));
1090 rv
+= (UChar
)0x0030;
1092 rv
+= (UChar
)(0x0030 + (s%10
));
1098 * Utility function for TestCustomParse, generating time zone ID
1099 * string for the give offset.
1101 UnicodeString
& TimeZoneTest::formatTZID(int32_t offset
, UnicodeString
&rv
) {
1103 UChar sign
= 0x002B;
1109 int32_t s
= offset
% 60;
1111 int32_t m
= offset
% 60;
1112 int32_t h
= offset
/ 60;
1115 rv
+= (UChar
)(sign
);
1117 rv
+= (UChar
)(0x0030 + (h
/10));
1119 rv
+= (UChar
)0x0030;
1121 rv
+= (UChar
)(0x0030 + (h%10
));
1122 rv
+= (UChar
)0x003A;
1124 rv
+= (UChar
)(0x0030 + (m
/10));
1126 rv
+= (UChar
)0x0030;
1128 rv
+= (UChar
)(0x0030 + (m%10
));
1131 rv
+= (UChar
)0x003A;
1133 rv
+= (UChar
)(0x0030 + (s
/10));
1135 rv
+= (UChar
)0x0030;
1137 rv
+= (UChar
)(0x0030 + (s%10
));
1143 * As part of the VM fix (see CCC approved RFE 4028006, bug
1144 * 4044013), TimeZone.getTimeZone() has been modified to recognize
1145 * generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
1146 * GMT[+-]hh. Test this behavior here.
1150 void TimeZoneTest::TestCustomParse()
1153 const int32_t kUnparseable
= 604800; // the number of seconds in a week. More than any offset should be.
1157 const char *customId
;
1158 int32_t expectedOffset
;
1162 // ID Expected offset in seconds
1163 {"GMT", kUnparseable
}, //Isn't custom. [returns normal GMT]
1164 {"GMT-YOUR.AD.HERE", kUnparseable
},
1165 {"GMT0", kUnparseable
},
1167 {"GMT+1", (1*60*60)},
1168 {"GMT-0030", (-30*60)},
1169 {"GMT+15:99", kUnparseable
},
1170 {"GMT+", kUnparseable
},
1171 {"GMT-", kUnparseable
},
1172 {"GMT+0:", kUnparseable
},
1173 {"GMT-:", kUnparseable
},
1174 {"GMT-YOUR.AD.HERE", kUnparseable
},
1175 {"GMT+0010", (10*60)}, // Interpret this as 00:10
1176 {"GMT-10", (-10*60*60)},
1177 {"GMT+30", kUnparseable
},
1178 {"GMT-3:30", (-(3*60+30)*60)},
1179 {"GMT-230", (-(2*60+30)*60)},
1180 {"GMT+05:13:05", ((5*60+13)*60+5)},
1181 {"GMT-71023", (-((7*60+10)*60+23))},
1182 {"GMT+01:23:45:67", kUnparseable
},
1183 {"GMT+01:234", kUnparseable
},
1184 {"GMT-2:31:123", kUnparseable
},
1185 {"GMT+3:75", kUnparseable
},
1186 {"GMT-01010101", kUnparseable
},
1190 for (i
=0; kData
[i
].customId
!= 0; i
++) {
1191 UnicodeString
id(kData
[i
].customId
);
1192 int32_t exp
= kData
[i
].expectedOffset
;
1193 TimeZone
*zone
= TimeZone::createTimeZone(id
);
1194 UnicodeString itsID
, temp
;
1196 if (dynamic_cast<OlsonTimeZone
*>(zone
) != NULL
) {
1197 logln(id
+ " -> Olson time zone");
1200 int32_t ioffset
= zone
->getRawOffset()/1000;
1201 UnicodeString offset
, expectedID
;
1202 formatOffset(ioffset
, offset
);
1203 formatTZID(ioffset
, expectedID
);
1204 logln(id
+ " -> " + itsID
+ " " + offset
);
1205 if (exp
== kUnparseable
&& itsID
!= UCAL_UNKNOWN_ZONE_ID
) {
1206 errln("Expected parse failure for " + id
+
1207 ", got offset of " + offset
+
1210 // JDK 1.3 creates custom zones with the ID "Custom"
1211 // JDK 1.4 creates custom zones with IDs of the form "GMT+02:00"
1212 // ICU creates custom zones with IDs of the form "GMT+02:00"
1213 else if (exp
!= kUnparseable
&& (ioffset
!= exp
|| itsID
!= expectedID
)) {
1214 dataerrln("Expected offset of " + formatOffset(exp
, temp
) +
1215 ", id " + expectedID
+
1217 ", got offset of " + offset
+
1226 TimeZoneTest::TestAliasedNames()
1232 /* Generated by org.unicode.cldr.tool.CountItems */
1234 /* zoneID, canonical zoneID */
1235 {"Africa/Timbuktu", "Africa/Bamako"},
1236 {"America/Argentina/Buenos_Aires", "America/Buenos_Aires"},
1237 {"America/Argentina/Catamarca", "America/Catamarca"},
1238 {"America/Argentina/ComodRivadavia", "America/Catamarca"},
1239 {"America/Argentina/Cordoba", "America/Cordoba"},
1240 {"America/Argentina/Jujuy", "America/Jujuy"},
1241 {"America/Argentina/Mendoza", "America/Mendoza"},
1242 {"America/Atka", "America/Adak"},
1243 {"America/Ensenada", "America/Tijuana"},
1244 {"America/Fort_Wayne", "America/Indiana/Indianapolis"},
1245 {"America/Indianapolis", "America/Indiana/Indianapolis"},
1246 {"America/Knox_IN", "America/Indiana/Knox"},
1247 {"America/Louisville", "America/Kentucky/Louisville"},
1248 {"America/Porto_Acre", "America/Rio_Branco"},
1249 {"America/Rosario", "America/Cordoba"},
1250 {"America/Virgin", "America/St_Thomas"},
1251 {"Asia/Ashkhabad", "Asia/Ashgabat"},
1252 {"Asia/Chungking", "Asia/Chongqing"},
1253 {"Asia/Dacca", "Asia/Dhaka"},
1254 {"Asia/Istanbul", "Europe/Istanbul"},
1255 {"Asia/Macao", "Asia/Macau"},
1256 {"Asia/Tel_Aviv", "Asia/Jerusalem"},
1257 {"Asia/Thimbu", "Asia/Thimphu"},
1258 {"Asia/Ujung_Pandang", "Asia/Makassar"},
1259 {"Asia/Ulan_Bator", "Asia/Ulaanbaatar"},
1260 {"Australia/ACT", "Australia/Sydney"},
1261 {"Australia/Canberra", "Australia/Sydney"},
1262 {"Australia/LHI", "Australia/Lord_Howe"},
1263 {"Australia/NSW", "Australia/Sydney"},
1264 {"Australia/North", "Australia/Darwin"},
1265 {"Australia/Queensland", "Australia/Brisbane"},
1266 {"Australia/South", "Australia/Adelaide"},
1267 {"Australia/Tasmania", "Australia/Hobart"},
1268 {"Australia/Victoria", "Australia/Melbourne"},
1269 {"Australia/West", "Australia/Perth"},
1270 {"Australia/Yancowinna", "Australia/Broken_Hill"},
1271 {"Brazil/Acre", "America/Rio_Branco"},
1272 {"Brazil/DeNoronha", "America/Noronha"},
1273 {"Brazil/East", "America/Sao_Paulo"},
1274 {"Brazil/West", "America/Manaus"},
1275 {"Canada/Atlantic", "America/Halifax"},
1276 {"Canada/Central", "America/Winnipeg"},
1277 {"Canada/East-Saskatchewan", "America/Regina"},
1278 {"Canada/Eastern", "America/Toronto"},
1279 {"Canada/Mountain", "America/Edmonton"},
1280 {"Canada/Newfoundland", "America/St_Johns"},
1281 {"Canada/Pacific", "America/Vancouver"},
1282 {"Canada/Saskatchewan", "America/Regina"},
1283 {"Canada/Yukon", "America/Whitehorse"},
1284 {"Chile/Continental", "America/Santiago"},
1285 {"Chile/EasterIsland", "Pacific/Easter"},
1286 {"Cuba", "America/Havana"},
1287 {"Egypt", "Africa/Cairo"},
1288 {"Eire", "Europe/Dublin"},
1289 {"Etc/GMT+0", "Etc/GMT"},
1290 {"Etc/GMT-0", "Etc/GMT"},
1291 {"Etc/GMT0", "Etc/GMT"},
1292 {"Etc/Greenwich", "Etc/GMT"},
1293 {"Etc/UCT", "Etc/GMT"},
1294 {"Etc/UTC", "Etc/GMT"},
1295 {"Etc/Universal", "Etc/GMT"},
1296 {"Etc/Zulu", "Etc/GMT"},
1297 {"Europe/Belfast", "Europe/London"},
1298 {"Europe/Nicosia", "Asia/Nicosia"},
1299 {"Europe/Tiraspol", "Europe/Chisinau"},
1300 {"GB", "Europe/London"},
1301 {"GB-Eire", "Europe/London"},
1303 {"GMT+0", "Etc/GMT"},
1304 {"GMT-0", "Etc/GMT"},
1305 {"GMT0", "Etc/GMT"},
1306 {"Greenwich", "Etc/GMT"},
1307 {"Hongkong", "Asia/Hong_Kong"},
1308 {"Iceland", "Atlantic/Reykjavik"},
1309 {"Iran", "Asia/Tehran"},
1310 {"Israel", "Asia/Jerusalem"},
1311 {"Jamaica", "America/Jamaica"},
1312 {"Japan", "Asia/Tokyo"},
1313 {"Kwajalein", "Pacific/Kwajalein"},
1314 {"Libya", "Africa/Tripoli"},
1315 {"Mexico/BajaNorte", "America/Tijuana"},
1316 {"Mexico/BajaSur", "America/Mazatlan"},
1317 {"Mexico/General", "America/Mexico_City"},
1318 {"NZ", "Pacific/Auckland"},
1319 {"NZ-CHAT", "Pacific/Chatham"},
1320 {"Navajo", "America/Shiprock"},
1321 {"PRC", "Asia/Shanghai"},
1322 {"Pacific/Samoa", "Pacific/Pago_Pago"},
1323 {"Pacific/Yap", "Pacific/Truk"},
1324 {"Poland", "Europe/Warsaw"},
1325 {"Portugal", "Europe/Lisbon"},
1326 {"ROC", "Asia/Taipei"},
1327 {"ROK", "Asia/Seoul"},
1328 {"Singapore", "Asia/Singapore"},
1329 {"Turkey", "Europe/Istanbul"},
1331 {"US/Alaska", "America/Anchorage"},
1332 {"US/Aleutian", "America/Adak"},
1333 {"US/Arizona", "America/Phoenix"},
1334 {"US/Central", "America/Chicago"},
1335 {"US/East-Indiana", "America/Indiana/Indianapolis"},
1336 {"US/Eastern", "America/New_York"},
1337 {"US/Hawaii", "Pacific/Honolulu"},
1338 {"US/Indiana-Starke", "America/Indiana/Knox"},
1339 {"US/Michigan", "America/Detroit"},
1340 {"US/Mountain", "America/Denver"},
1341 {"US/Pacific", "America/Los_Angeles"},
1342 {"US/Pacific-New", "America/Los_Angeles"},
1343 {"US/Samoa", "Pacific/Pago_Pago"},
1345 {"Universal", "Etc/GMT"},
1346 {"W-SU", "Europe/Moscow"},
1347 {"Zulu", "Etc/GMT"},
1352 TimeZone::EDisplayType styles
[] = { TimeZone::SHORT
, TimeZone::LONG
};
1353 UBool useDst
[] = { FALSE
, TRUE
};
1354 int32_t noLoc
= uloc_countAvailable();
1356 int32_t i
, j
, k
, loc
;
1357 UnicodeString fromName
, toName
;
1358 TimeZone
*from
= NULL
, *to
= NULL
;
1359 for(i
= 0; i
< (int32_t)(sizeof(kData
)/sizeof(kData
[0])); i
++) {
1360 from
= TimeZone::createTimeZone(kData
[i
].from
);
1361 to
= TimeZone::createTimeZone(kData
[i
].to
);
1362 if(!from
->hasSameRules(*to
)) {
1363 errln("different at %i\n", i
);
1366 for(loc
= 0; loc
< noLoc
; loc
++) {
1367 const char* locale
= uloc_getAvailable(loc
);
1368 for(j
= 0; j
< (int32_t)(sizeof(styles
)/sizeof(styles
[0])); j
++) {
1369 for(k
= 0; k
< (int32_t)(sizeof(useDst
)/sizeof(useDst
[0])); k
++) {
1372 from
->getDisplayName(useDst
[k
], styles
[j
],locale
, fromName
);
1373 to
->getDisplayName(useDst
[k
], styles
[j
], locale
, toName
);
1374 if(fromName
.compare(toName
) != 0) {
1375 errln("Fail: Expected "+toName
+" but got " + prettify(fromName
)
1376 + " for locale: " + locale
+ " index: "+ loc
1377 + " to id "+ kData
[i
].to
1378 + " from id " + kData
[i
].from
);
1386 from
->getDisplayName(fromName
);
1387 to
->getDisplayName(toName
);
1388 if(fromName
.compare(toName
) != 0) {
1389 errln("Fail: Expected "+toName
+" but got " + fromName
);
1398 * Test the basic functionality of the getDisplayName() API.
1403 * See also API change request A41.
1405 * 4/21/98 - make smarter, so the test works if the ext resources
1406 * are present or not.
1409 TimeZoneTest::TestDisplayName()
1411 UErrorCode status
= U_ZERO_ERROR
;
1413 TimeZone
*zone
= TimeZone::createTimeZone("PST");
1415 zone
->getDisplayName(Locale::getEnglish(), name
);
1416 logln("PST->" + name
);
1417 if (name
.compare("Pacific Standard Time") != 0)
1418 dataerrln("Fail: Expected \"Pacific Standard Time\" but got " + name
);
1420 //*****************************************************************
1421 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1422 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1423 // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
1424 //*****************************************************************
1428 TimeZone::EDisplayType style
;
1431 {FALSE
, TimeZone::SHORT
, "PST"},
1432 {TRUE
, TimeZone::SHORT
, "PDT"},
1433 {FALSE
, TimeZone::LONG
, "Pacific Standard Time"},
1434 {TRUE
, TimeZone::LONG
, "Pacific Daylight Time"},
1436 {FALSE
, TimeZone::SHORT_GENERIC
, "PT"},
1437 {TRUE
, TimeZone::SHORT_GENERIC
, "PT"},
1438 {FALSE
, TimeZone::LONG_GENERIC
, "Pacific Time"},
1439 {TRUE
, TimeZone::LONG_GENERIC
, "Pacific Time"},
1441 {FALSE
, TimeZone::SHORT_GMT
, "-0800"},
1442 {TRUE
, TimeZone::SHORT_GMT
, "-0700"},
1443 {FALSE
, TimeZone::LONG_GMT
, "GMT-08:00"},
1444 {TRUE
, TimeZone::LONG_GMT
, "GMT-07:00"},
1446 {FALSE
, TimeZone::SHORT_COMMONLY_USED
, "PST"},
1447 {TRUE
, TimeZone::SHORT_COMMONLY_USED
, "PDT"},
1448 {FALSE
, TimeZone::GENERIC_LOCATION
, "United States Time (Los Angeles)"},
1449 {TRUE
, TimeZone::GENERIC_LOCATION
, "United States Time (Los Angeles)"},
1451 {FALSE
, TimeZone::LONG
, ""}
1454 for (i
=0; kData
[i
].expect
[0] != '\0'; i
++)
1457 name
= zone
->getDisplayName(kData
[i
].useDst
,
1459 Locale::getEnglish(), name
);
1460 if (name
.compare(kData
[i
].expect
) != 0)
1461 dataerrln("Fail: Expected " + UnicodeString(kData
[i
].expect
) + "; got " + name
);
1462 logln("PST [with options]->" + name
);
1464 for (i
=0; kData
[i
].expect
[0] != '\0'; i
++)
1467 name
= zone
->getDisplayName(kData
[i
].useDst
,
1468 kData
[i
].style
, name
);
1469 if (name
.compare(kData
[i
].expect
) != 0)
1470 dataerrln("Fail: Expected " + UnicodeString(kData
[i
].expect
) + "; got " + name
);
1471 logln("PST [with options]->" + name
);
1475 // Make sure that we don't display the DST name by constructing a fake
1476 // PST zone that has DST all year long.
1477 SimpleTimeZone
*zone2
= new SimpleTimeZone(0, "PST");
1479 zone2
->setStartRule(UCAL_JANUARY
, 1, 0, 0, status
);
1480 zone2
->setEndRule(UCAL_DECEMBER
, 31, 0, 0, status
);
1482 UnicodeString inDaylight
;
1483 if (zone2
->inDaylightTime(UDate(0), status
)) {
1484 inDaylight
= UnicodeString("TRUE");
1486 inDaylight
= UnicodeString("FALSE");
1488 logln(UnicodeString("Modified PST inDaylightTime->") + inDaylight
);
1489 if(U_FAILURE(status
))
1491 dataerrln("Some sort of error..." + UnicodeString(u_errorName(status
))); // REVISIT
1494 name
= zone2
->getDisplayName(Locale::getEnglish(),name
);
1495 logln("Modified PST->" + name
);
1496 if (name
.compare("Pacific Standard Time") != 0)
1497 dataerrln("Fail: Expected \"Pacific Standard Time\"");
1499 // Make sure we get the default display format for Locales
1500 // with no display name data.
1501 Locale
mt_MT("mt_MT");
1503 name
= zone
->getDisplayName(mt_MT
,name
);
1504 //*****************************************************************
1505 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1506 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1507 // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
1508 //*****************************************************************
1509 logln("PST(mt_MT)->" + name
);
1511 // *** REVISIT SRL how in the world do I check this? looks java specific.
1512 // Now be smart -- check to see if zh resource is even present.
1513 // If not, we expect the en fallback behavior.
1514 ResourceBundle
enRB(NULL
,
1515 Locale::getEnglish(), status
);
1516 if(U_FAILURE(status
))
1517 dataerrln("Couldn't get ResourceBundle for en - %s", u_errorName(status
));
1519 ResourceBundle
mtRB(NULL
,
1521 //if(U_FAILURE(status))
1522 // errln("Couldn't get ResourceBundle for mt_MT");
1524 UBool noZH
= U_FAILURE(status
);
1527 logln("Warning: Not testing the mt_MT behavior because resource is absent");
1528 if (name
!= "Pacific Standard Time")
1529 dataerrln("Fail: Expected Pacific Standard Time");
1533 if (name
.compare("GMT-08:00") &&
1534 name
.compare("GMT-8:00") &&
1535 name
.compare("GMT-0800") &&
1536 name
.compare("GMT-800")) {
1537 dataerrln(UnicodeString("Fail: Expected GMT-08:00 or something similar for PST in mt_MT but got ") + name
);
1538 dataerrln("************************************************************");
1539 dataerrln("THE ABOVE FAILURE MAY JUST MEAN THE LOCALE DATA HAS CHANGED");
1540 dataerrln("************************************************************");
1543 // Now try a non-existent zone
1545 zone2
= new SimpleTimeZone(90*60*1000, "xyzzy");
1547 name
= zone2
->getDisplayName(Locale::getEnglish(),name
);
1548 logln("GMT+90min->" + name
);
1549 if (name
.compare("GMT+01:30") &&
1550 name
.compare("GMT+1:30") &&
1551 name
.compare("GMT+0130") &&
1552 name
.compare("GMT+130"))
1553 dataerrln("Fail: Expected GMT+01:30 or something similar");
1555 zone2
->getDisplayName(name
);
1556 logln("GMT+90min->" + name
);
1557 if (name
.compare("GMT+01:30") &&
1558 name
.compare("GMT+1:30") &&
1559 name
.compare("GMT+0130") &&
1560 name
.compare("GMT+130"))
1561 dataerrln("Fail: Expected GMT+01:30 or something similar");
1571 TimeZoneTest::TestDSTSavings()
1573 UErrorCode status
= U_ZERO_ERROR
;
1574 // It might be better to find a way to integrate this test into the main TimeZone
1575 // tests above, but I don't have time to figure out how to do this (or if it's
1576 // even really a good idea). Let's consider that a future. --rtg 1/27/98
1577 SimpleTimeZone
*tz
= new SimpleTimeZone(-5 * U_MILLIS_PER_HOUR
, "dstSavingsTest",
1578 UCAL_MARCH
, 1, 0, 0, UCAL_SEPTEMBER
, 1, 0, 0,
1579 (int32_t)(0.5 * U_MILLIS_PER_HOUR
), status
);
1580 if(U_FAILURE(status
))
1581 errln("couldn't create TimeZone");
1583 if (tz
->getRawOffset() != -5 * U_MILLIS_PER_HOUR
)
1584 errln(UnicodeString("Got back a raw offset of ") + (tz
->getRawOffset() / U_MILLIS_PER_HOUR
) +
1585 " hours instead of -5 hours.");
1586 if (!tz
->useDaylightTime())
1587 errln("Test time zone should use DST but claims it doesn't.");
1588 if (tz
->getDSTSavings() != 0.5 * U_MILLIS_PER_HOUR
)
1589 errln(UnicodeString("Set DST offset to 0.5 hour, but got back ") + (tz
->getDSTSavings() /
1590 U_MILLIS_PER_HOUR
) + " hours instead.");
1592 int32_t offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JANUARY
, 1,
1593 UCAL_THURSDAY
, 10 * U_MILLIS_PER_HOUR
,status
);
1594 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1595 errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
1596 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1598 offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JUNE
, 1, UCAL_MONDAY
,
1599 10 * U_MILLIS_PER_HOUR
,status
);
1600 if (offset
!= -4.5 * U_MILLIS_PER_HOUR
)
1601 errln(UnicodeString("The offset for 10 AM, 6/1/98 should have been -4.5 hours, but we got ")
1602 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1604 tz
->setDSTSavings(U_MILLIS_PER_HOUR
, status
);
1605 offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JANUARY
, 1,
1606 UCAL_THURSDAY
, 10 * U_MILLIS_PER_HOUR
,status
);
1607 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1608 errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
1609 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1611 offset
= tz
->getOffset(GregorianCalendar::AD
, 1998, UCAL_JUNE
, 1, UCAL_MONDAY
,
1612 10 * U_MILLIS_PER_HOUR
,status
);
1613 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1614 errln(UnicodeString("The offset for 10 AM, 6/1/98 (with a 1-hour DST offset) should have been -4 hours, but we got ")
1615 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1624 TimeZoneTest::TestAlternateRules()
1626 // Like TestDSTSavings, this test should probably be integrated somehow with the main
1627 // test at the top of this class, but I didn't have time to figure out how to do that.
1630 SimpleTimeZone
tz(-5 * U_MILLIS_PER_HOUR
, "alternateRuleTest");
1632 // test the day-of-month API
1633 UErrorCode status
= U_ZERO_ERROR
;
1634 tz
.setStartRule(UCAL_MARCH
, 10, 12 * U_MILLIS_PER_HOUR
, status
);
1635 if(U_FAILURE(status
))
1636 errln("tz.setStartRule failed");
1637 tz
.setEndRule(UCAL_OCTOBER
, 20, 12 * U_MILLIS_PER_HOUR
, status
);
1638 if(U_FAILURE(status
))
1639 errln("tz.setStartRule failed");
1641 int32_t offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 5,
1642 UCAL_THURSDAY
, 10 * U_MILLIS_PER_HOUR
,status
);
1643 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1644 errln(UnicodeString("The offset for 10AM, 3/5/98 should have been -5 hours, but we got ")
1645 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1647 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 15,
1648 UCAL_SUNDAY
, 10 * millisPerHour
,status
);
1649 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1650 errln(UnicodeString("The offset for 10AM, 3/15/98 should have been -4 hours, but we got ")
1651 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1653 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 15,
1654 UCAL_THURSDAY
, 10 * millisPerHour
,status
);
1655 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1656 errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ") + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1658 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 25,
1659 UCAL_SUNDAY
, 10 * millisPerHour
,status
);
1660 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1661 errln(UnicodeString("The offset for 10AM, 10/25/98 should have been -5 hours, but we got ")
1662 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1664 // test the day-of-week-after-day-in-month API
1665 tz
.setStartRule(UCAL_MARCH
, 10, UCAL_FRIDAY
, 12 * millisPerHour
, TRUE
, status
);
1666 if(U_FAILURE(status
))
1667 errln("tz.setStartRule failed");
1668 tz
.setEndRule(UCAL_OCTOBER
, 20, UCAL_FRIDAY
, 12 * millisPerHour
, FALSE
, status
);
1669 if(U_FAILURE(status
))
1670 errln("tz.setStartRule failed");
1672 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 11,
1673 UCAL_WEDNESDAY
, 10 * millisPerHour
,status
);
1674 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1675 errln(UnicodeString("The offset for 10AM, 3/11/98 should have been -5 hours, but we got ")
1676 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1678 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_MARCH
, 14,
1679 UCAL_SATURDAY
, 10 * millisPerHour
,status
);
1680 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1681 errln(UnicodeString("The offset for 10AM, 3/14/98 should have been -4 hours, but we got ")
1682 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1684 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 15,
1685 UCAL_THURSDAY
, 10 * millisPerHour
,status
);
1686 if (offset
!= -4 * U_MILLIS_PER_HOUR
)
1687 errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ")
1688 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1690 offset
= tz
.getOffset(GregorianCalendar::AD
, 1998, UCAL_OCTOBER
, 17,
1691 UCAL_SATURDAY
, 10 * millisPerHour
,status
);
1692 if (offset
!= -5 * U_MILLIS_PER_HOUR
)
1693 errln(UnicodeString("The offset for 10AM, 10/17/98 should have been -5 hours, but we got ")
1694 + (offset
/ U_MILLIS_PER_HOUR
) + " hours.");
1697 void TimeZoneTest::TestFractionalDST() {
1698 const char* tzName
= "Australia/Lord_Howe"; // 30 min offset
1699 TimeZone
* tz_icu
= TimeZone::createTimeZone(tzName
);
1700 int dst_icu
= tz_icu
->getDSTSavings();
1702 int32_t expected
= 1800000;
1703 if (expected
!= dst_icu
) {
1704 dataerrln(UnicodeString("java reports dst savings of ") + expected
+
1705 " but icu reports " + dst_icu
+
1706 " for tz " + tz_icu
->getID(id
));
1708 logln(UnicodeString("both java and icu report dst savings of ") + expected
+ " for tz " + tz_icu
->getID(id
));
1714 * Test country code support. Jitterbug 776.
1716 void TimeZoneTest::TestCountries() {
1717 // Make sure America/Los_Angeles is in the "US" group, and
1718 // Asia/Tokyo isn't. Vice versa for the "JP" group.
1719 UErrorCode ec
= U_ZERO_ERROR
;
1721 StringEnumeration
* s
= TimeZone::createEnumeration("US");
1723 dataerrln("Unable to create TimeZone enumeration for US");
1727 UBool la
= FALSE
, tokyo
= FALSE
;
1728 UnicodeString
laZone("America/Los_Angeles", "");
1729 UnicodeString
tokyoZone("Asia/Tokyo", "");
1732 if (s
== NULL
|| n
<= 0) {
1733 dataerrln("FAIL: TimeZone::createEnumeration() returned nothing");
1736 for (i
=0; i
<n
; ++i
) {
1737 const UnicodeString
* id
= s
->snext(ec
);
1738 if (*id
== (laZone
)) {
1741 if (*id
== (tokyoZone
)) {
1746 errln("FAIL: " + laZone
+ " in US = " + la
);
1747 errln("FAIL: " + tokyoZone
+ " in US = " + tokyo
);
1751 s
= TimeZone::createEnumeration("JP");
1753 la
= FALSE
; tokyo
= FALSE
;
1755 for (i
=0; i
<n
; ++i
) {
1756 const UnicodeString
* id
= s
->snext(ec
);
1757 if (*id
== (laZone
)) {
1760 if (*id
== (tokyoZone
)) {
1765 errln("FAIL: " + laZone
+ " in JP = " + la
);
1766 errln("FAIL: " + tokyoZone
+ " in JP = " + tokyo
);
1768 StringEnumeration
* s1
= TimeZone::createEnumeration("US");
1769 StringEnumeration
* s2
= TimeZone::createEnumeration("US");
1771 const UnicodeString
* id1
= s1
->snext(ec
);
1772 if(id1
==NULL
|| U_FAILURE(ec
)){
1773 errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n
,i
);
1775 TimeZone
* tz1
= TimeZone::createTimeZone(*id1
);
1776 for(int j
=0; j
<n
;++j
){
1777 const UnicodeString
* id2
= s2
->snext(ec
);
1778 if(id2
==NULL
|| U_FAILURE(ec
)){
1779 errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n
,i
);
1781 TimeZone
* tz2
= TimeZone::createTimeZone(*id2
);
1782 if(tz1
->hasSameRules(*tz2
)){
1783 logln("ID1 : " + *id1
+" == ID2 : " +*id2
);
1794 void TimeZoneTest::TestHistorical() {
1795 const int32_t H
= U_MILLIS_PER_HOUR
;
1798 int32_t time
; // epoch seconds
1799 int32_t offset
; // total offset (millis)
1801 // Add transition points (before/after) as desired to test historical
1803 {"America/Los_Angeles", 638963999, -8*H
}, // Sun Apr 01 01:59:59 GMT-08:00 1990
1804 {"America/Los_Angeles", 638964000, -7*H
}, // Sun Apr 01 03:00:00 GMT-07:00 1990
1805 {"America/Los_Angeles", 657104399, -7*H
}, // Sun Oct 28 01:59:59 GMT-07:00 1990
1806 {"America/Los_Angeles", 657104400, -8*H
}, // Sun Oct 28 01:00:00 GMT-08:00 1990
1807 {"America/Goose_Bay", -116445601, -4*H
}, // Sun Apr 24 01:59:59 GMT-04:00 1966
1808 {"America/Goose_Bay", -116445600, -3*H
}, // Sun Apr 24 03:00:00 GMT-03:00 1966
1809 {"America/Goose_Bay", -100119601, -3*H
}, // Sun Oct 30 01:59:59 GMT-03:00 1966
1810 {"America/Goose_Bay", -100119600, -4*H
}, // Sun Oct 30 01:00:00 GMT-04:00 1966
1811 {"America/Goose_Bay", -84391201, -4*H
}, // Sun Apr 30 01:59:59 GMT-04:00 1967
1812 {"America/Goose_Bay", -84391200, -3*H
}, // Sun Apr 30 03:00:00 GMT-03:00 1967
1813 {"America/Goose_Bay", -68670001, -3*H
}, // Sun Oct 29 01:59:59 GMT-03:00 1967
1814 {"America/Goose_Bay", -68670000, -4*H
}, // Sun Oct 29 01:00:00 GMT-04:00 1967
1818 for (int32_t i
=0; DATA
[i
].id
!=0; ++i
) {
1819 const char* id
= DATA
[i
].id
;
1820 TimeZone
*tz
= TimeZone::createTimeZone(id
);
1823 errln("FAIL: Cannot create %s", id
);
1824 } else if (tz
->getID(s
) != UnicodeString(id
)) {
1825 dataerrln((UnicodeString
)"FAIL: createTimeZone(" + id
+ ") => " + s
);
1827 UErrorCode ec
= U_ZERO_ERROR
;
1829 UDate when
= (double) DATA
[i
].time
* U_MILLIS_PER_SECOND
;
1830 tz
->getOffset(when
, FALSE
, raw
, dst
, ec
);
1831 if (U_FAILURE(ec
)) {
1832 errln("FAIL: getOffset");
1833 } else if ((raw
+dst
) != DATA
[i
].offset
) {
1834 errln((UnicodeString
)"FAIL: " + DATA
[i
].id
+ ".getOffset(" +
1836 dateToString(when
) + ") => " +
1839 logln((UnicodeString
)"Ok: " + DATA
[i
].id
+ ".getOffset(" +
1841 dateToString(when
) + ") => " +
1849 void TimeZoneTest::TestEquivalentIDs() {
1850 int32_t n
= TimeZone::countEquivalentIDs("PST");
1852 dataerrln((UnicodeString
)"FAIL: countEquivalentIDs(PST) = " + n
);
1854 UBool sawLA
= FALSE
;
1855 for (int32_t i
=0; i
<n
; ++i
) {
1856 UnicodeString id
= TimeZone::getEquivalentID("PST", i
);
1857 logln((UnicodeString
)"" + i
+ " : " + id
);
1858 if (id
== UnicodeString("America/Los_Angeles")) {
1863 errln("FAIL: America/Los_Angeles should be in the list");
1868 // Test that a transition at the end of February is handled correctly.
1869 void TimeZoneTest::TestFebruary() {
1870 UErrorCode status
= U_ZERO_ERROR
;
1872 // Time zone with daylight savings time from the first Sunday in November
1873 // to the last Sunday in February.
1874 // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
1876 // Note: In tzdata2007h, the rule had changed, so no actual zones uses
1877 // lastSun in Feb anymore.
1878 SimpleTimeZone
tz1(-3 * U_MILLIS_PER_HOUR
, // raw offset: 3h before (west of) GMT
1879 UNICODE_STRING("nov-feb", 7),
1880 UCAL_NOVEMBER
, 1, UCAL_SUNDAY
, // start: November, first, Sunday
1881 0, // midnight wall time
1882 UCAL_FEBRUARY
, -1, UCAL_SUNDAY
, // end: February, last, Sunday
1883 0, // midnight wall time
1885 if (U_FAILURE(status
)) {
1886 errln("Unable to create the SimpleTimeZone(nov-feb): %s", u_errorName(status
));
1890 // Now hardcode the same rules as for Brazil in tzdata 2006n, so that
1891 // we cover the intended code even when in the future zoneinfo hardcodes
1892 // these transition dates.
1893 SimpleTimeZone
tz2(-3 * U_MILLIS_PER_HOUR
, // raw offset: 3h before (west of) GMT
1894 UNICODE_STRING("nov-feb2", 8),
1895 UCAL_NOVEMBER
, 1, -UCAL_SUNDAY
, // start: November, 1 or after, Sunday
1896 0, // midnight wall time
1897 UCAL_FEBRUARY
, -29, -UCAL_SUNDAY
,// end: February, 29 or before, Sunday
1898 0, // midnight wall time
1900 if (U_FAILURE(status
)) {
1901 errln("Unable to create the SimpleTimeZone(nov-feb2): %s", u_errorName(status
));
1905 // Gregorian calendar with the UTC time zone for getting sample test date/times.
1906 GregorianCalendar
gc(*TimeZone::getGMT(), status
);
1907 if (U_FAILURE(status
)) {
1908 dataerrln("Unable to create the UTC calendar: %s", u_errorName(status
));
1914 int32_t year
, month
, day
, hour
, minute
, second
;
1915 // Expected time zone offset in hours after GMT (negative=before GMT).
1916 int32_t offsetHours
;
1918 { 2006, UCAL_NOVEMBER
, 5, 02, 59, 59, -3 },
1919 { 2006, UCAL_NOVEMBER
, 5, 03, 00, 00, -2 },
1920 { 2007, UCAL_FEBRUARY
, 25, 01, 59, 59, -2 },
1921 { 2007, UCAL_FEBRUARY
, 25, 02, 00, 00, -3 },
1923 { 2007, UCAL_NOVEMBER
, 4, 02, 59, 59, -3 },
1924 { 2007, UCAL_NOVEMBER
, 4, 03, 00, 00, -2 },
1925 { 2008, UCAL_FEBRUARY
, 24, 01, 59, 59, -2 },
1926 { 2008, UCAL_FEBRUARY
, 24, 02, 00, 00, -3 },
1928 { 2008, UCAL_NOVEMBER
, 2, 02, 59, 59, -3 },
1929 { 2008, UCAL_NOVEMBER
, 2, 03, 00, 00, -2 },
1930 { 2009, UCAL_FEBRUARY
, 22, 01, 59, 59, -2 },
1931 { 2009, UCAL_FEBRUARY
, 22, 02, 00, 00, -3 },
1933 { 2009, UCAL_NOVEMBER
, 1, 02, 59, 59, -3 },
1934 { 2009, UCAL_NOVEMBER
, 1, 03, 00, 00, -2 },
1935 { 2010, UCAL_FEBRUARY
, 28, 01, 59, 59, -2 },
1936 { 2010, UCAL_FEBRUARY
, 28, 02, 00, 00, -3 }
1939 TimeZone
*timezones
[] = { &tz1
, &tz2
};
1943 int32_t t
, i
, raw
, dst
;
1944 for (t
= 0; t
< LENGTHOF(timezones
); ++t
) {
1946 for (i
= 0; i
< LENGTHOF(data
); ++i
) {
1947 gc
.set(data
[i
].year
, data
[i
].month
, data
[i
].day
,
1948 data
[i
].hour
, data
[i
].minute
, data
[i
].second
);
1949 dt
= gc
.getTime(status
);
1950 if (U_FAILURE(status
)) {
1951 errln("test case %d.%d: bad date/time %04d-%02d-%02d %02d:%02d:%02d",
1953 data
[i
].year
, data
[i
].month
+ 1, data
[i
].day
,
1954 data
[i
].hour
, data
[i
].minute
, data
[i
].second
);
1955 status
= U_ZERO_ERROR
;
1958 tz
->getOffset(dt
, FALSE
, raw
, dst
, status
);
1959 if (U_FAILURE(status
)) {
1960 errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) fails: %s",
1962 data
[i
].year
, data
[i
].month
+ 1, data
[i
].day
,
1963 data
[i
].hour
, data
[i
].minute
, data
[i
].second
,
1964 u_errorName(status
));
1965 status
= U_ZERO_ERROR
;
1966 } else if ((raw
+ dst
) != data
[i
].offsetHours
* U_MILLIS_PER_HOUR
) {
1967 errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) returns %d+%d != %d",
1969 data
[i
].year
, data
[i
].month
+ 1, data
[i
].day
,
1970 data
[i
].hour
, data
[i
].minute
, data
[i
].second
,
1971 raw
, dst
, data
[i
].offsetHours
* U_MILLIS_PER_HOUR
);
1976 void TimeZoneTest::TestCanonicalID() {
1978 // Some canonical IDs in CLDR are defined as "Link"
1980 static const struct {
1984 {"America/Shiprock", "America/Denver"}, // America/Shiprock is defined as a Link to America/Denver in tzdata
1985 {"America/Marigot", "America/Guadeloupe"},
1986 {"America/St_Barthelemy", "America/Guadeloupe"},
1987 {"America/Lower_Princes", "America/Curacao"},
1988 {"America/Kralendijk", "America/Curacao"},
1989 {"Antarctica/South_Pole", "Antarctica/McMurdo"},
1990 {"Atlantic/Jan_Mayen", "Europe/Oslo"},
1991 {"Arctic/Longyearbyen", "Europe/Oslo"},
1992 {"Europe/Guernsey", "Europe/London"},
1993 {"Europe/Isle_of_Man", "Europe/London"},
1994 {"Europe/Jersey", "Europe/London"},
1995 {"Europe/Ljubljana", "Europe/Belgrade"},
1996 {"Europe/Podgorica", "Europe/Belgrade"},
1997 {"Europe/Sarajevo", "Europe/Belgrade"},
1998 {"Europe/Skopje", "Europe/Belgrade"},
1999 {"Europe/Zagreb", "Europe/Belgrade"},
2000 {"Europe/Bratislava", "Europe/Prague"},
2001 {"Europe/Mariehamn", "Europe/Helsinki"},
2002 {"Europe/San_Marino", "Europe/Rome"},
2003 {"Europe/Vatican", "Europe/Rome"},
2007 // Following IDs are aliases of Etc/GMT in CLDR,
2008 // but Olson tzdata has 3 independent definitions
2009 // for Etc/GMT, Etc/UTC, Etc/UCT.
2010 // Until we merge them into one equivalent group
2011 // in zoneinfo.res, we exclude them in the test
2013 static const char* excluded2
[] = {
2016 "Etc/Universal", "Universal",
2017 "Etc/Zulu", "Zulu", 0
2020 // Walk through equivalency groups
2021 UErrorCode ec
= U_ZERO_ERROR
;
2022 int32_t s_length
, i
, j
, k
;
2023 StringEnumeration
* s
= TimeZone::createEnumeration();
2025 dataerrln("Unable to create TimeZone enumeration");
2028 UnicodeString canonicalID
, tmpCanonical
;
2029 s_length
= s
->count(ec
);
2030 for (i
= 0; i
< s_length
;++i
) {
2031 const UnicodeString
*tzid
= s
->snext(ec
);
2032 int32_t nEquiv
= TimeZone::countEquivalentIDs(*tzid
);
2036 UBool bFoundCanonical
= FALSE
;
2037 // Make sure getCanonicalID returns the exact same result
2038 // for all entries within a same equivalency group with some
2039 // exceptions listed in exluded1.
2040 // Also, one of them must be canonical id.
2041 for (j
= 0; j
< nEquiv
; j
++) {
2042 UnicodeString tmp
= TimeZone::getEquivalentID(*tzid
, j
);
2043 TimeZone::getCanonicalID(tmp
, tmpCanonical
, ec
);
2044 if (U_FAILURE(ec
)) {
2045 errln((UnicodeString
)"FAIL: getCanonicalID(" + tmp
+ ") failed.");
2049 // Some exceptional cases
2050 for (k
= 0; excluded1
[k
].alias
!= 0; k
++) {
2051 if (tmpCanonical
== excluded1
[k
].alias
) {
2052 tmpCanonical
= excluded1
[k
].zone
;
2057 canonicalID
= tmpCanonical
;
2058 } else if (canonicalID
!= tmpCanonical
) {
2059 errln("FAIL: getCanonicalID(" + tmp
+ ") returned " + tmpCanonical
+ " expected:" + canonicalID
);
2062 if (canonicalID
== tmp
) {
2063 bFoundCanonical
= TRUE
;
2066 // At least one ID in an equvalency group must match the
2068 if (bFoundCanonical
== FALSE
) {
2069 // test exclusion because of differences between Olson tzdata and CLDR
2070 UBool isExcluded
= FALSE
;
2071 for (k
= 0; excluded2
[k
] != 0; k
++) {
2072 if (*tzid
== UnicodeString(excluded2
[k
])) {
2080 errln((UnicodeString
)"FAIL: No timezone ids match the canonical ID " + canonicalID
);
2085 // Testing some special cases
2086 static const struct {
2088 const char *expected
;
2091 {"GMT-03", "GMT-03:00", FALSE
},
2092 {"GMT+4", "GMT+04:00", FALSE
},
2093 {"GMT-055", "GMT-00:55", FALSE
},
2094 {"GMT+430", "GMT+04:30", FALSE
},
2095 {"GMT-12:15", "GMT-12:15", FALSE
},
2096 {"GMT-091015", "GMT-09:10:15", FALSE
},
2097 {"GMT+1:90", 0, FALSE
},
2098 {"America/Argentina/Buenos_Aires", "America/Buenos_Aires", TRUE
},
2099 {"Etc/Unknown", "Etc/Unknown", FALSE
},
2100 {"bogus", 0, FALSE
},
2102 {"America/Marigot", "America/Marigot", TRUE
}, // Olson link, but CLDR canonical (#8953)
2103 {"Europe/Bratislava", "Europe/Bratislava", TRUE
}, // Same as above
2108 for (i
= 0; data
[i
].id
!= 0; i
++) {
2109 TimeZone::getCanonicalID(UnicodeString(data
[i
].id
), canonicalID
, isSystemID
, ec
);
2110 if (U_FAILURE(ec
)) {
2111 if (ec
!= U_ILLEGAL_ARGUMENT_ERROR
|| data
[i
].expected
!= 0) {
2112 errln((UnicodeString
)"FAIL: getCanonicalID(\"" + data
[i
].id
2113 + "\") returned status U_ILLEGAL_ARGUMENT_ERROR");
2118 if (canonicalID
!= data
[i
].expected
) {
2119 dataerrln((UnicodeString
)"FAIL: getCanonicalID(\"" + data
[i
].id
2120 + "\") returned " + canonicalID
+ " - expected: " + data
[i
].expected
);
2122 if (isSystemID
!= data
[i
].isSystem
) {
2123 dataerrln((UnicodeString
)"FAIL: getCanonicalID(\"" + data
[i
].id
2124 + "\") set " + isSystemID
+ " to isSystemID");
2130 // Test Display Names, choosing zones and lcoales where there are multiple
2131 // meta-zones defined.
2134 const char *zoneName
;
2135 const char *localeName
;
2137 TimeZone::EDisplayType style
;
2138 const char *expectedDisplayName
; }
2139 zoneDisplayTestData
[] = {
2140 // zone id locale summer format expected display name
2141 {"Europe/London", "en", FALSE
, TimeZone::SHORT
, "GMT"},
2142 {"Europe/London", "en", FALSE
, TimeZone::LONG
, "Greenwich Mean Time"},
2143 {"Europe/London", "en", TRUE
, TimeZone::SHORT
, "GMT+01:00" /*"BST"*/},
2144 {"Europe/London", "en", TRUE
, TimeZone::LONG
, "British Summer Time"},
2146 {"America/Anchorage", "en", FALSE
, TimeZone::SHORT
, "AKST"},
2147 {"America/Anchorage", "en", FALSE
, TimeZone::LONG
, "Alaska Standard Time"},
2148 {"America/Anchorage", "en", TRUE
, TimeZone::SHORT
, "AKDT"},
2149 {"America/Anchorage", "en", TRUE
, TimeZone::LONG
, "Alaska Daylight Time"},
2151 // Southern Hemisphere, all data from meta:Australia_Western
2152 {"Australia/Perth", "en", FALSE
, TimeZone::SHORT
, "GMT+08:00"/*"AWST"*/},
2153 {"Australia/Perth", "en", FALSE
, TimeZone::LONG
, "Australian Western Standard Time"},
2154 {"Australia/Perth", "en", TRUE
, TimeZone::SHORT
, "GMT+09:00"/*"AWDT"*/},
2155 {"Australia/Perth", "en", TRUE
, TimeZone::LONG
, "Australian Western Daylight Time"},
2157 {"America/Sao_Paulo", "en", FALSE
, TimeZone::SHORT
, "GMT-03:00"/*"BRT"*/},
2158 {"America/Sao_Paulo", "en", FALSE
, TimeZone::LONG
, "Brasilia Time"},
2159 {"America/Sao_Paulo", "en", TRUE
, TimeZone::SHORT
, "GMT-02:00"/*"BRST"*/},
2160 {"America/Sao_Paulo", "en", TRUE
, TimeZone::LONG
, "Brasilia Summer Time"},
2162 // No Summer Time, but had it before 1983.
2163 {"Pacific/Honolulu", "en", FALSE
, TimeZone::SHORT
, "HST"},
2164 {"Pacific/Honolulu", "en", FALSE
, TimeZone::LONG
, "Hawaii-Aleutian Standard Time"},
2165 {"Pacific/Honolulu", "en", TRUE
, TimeZone::SHORT
, "HST"},
2166 {"Pacific/Honolulu", "en", TRUE
, TimeZone::LONG
, "Hawaii-Aleutian Standard Time"},
2168 // Northern, has Summer, not commonly used.
2169 {"Europe/Helsinki", "en", FALSE
, TimeZone::SHORT
, "GMT+02:00"/*"EET"*/},
2170 {"Europe/Helsinki", "en", FALSE
, TimeZone::LONG
, "Eastern European Time"},
2171 {"Europe/Helsinki", "en", TRUE
, TimeZone::SHORT
, "GMT+03:00"/*"EEST"*/},
2172 {"Europe/Helsinki", "en", true, TimeZone::LONG
, "Eastern European Summer Time"},
2173 {NULL
, NULL
, FALSE
, TimeZone::SHORT
, NULL
} // NULL values terminate list
2176 void TimeZoneTest::TestDisplayNamesMeta() {
2177 UErrorCode status
= U_ZERO_ERROR
;
2178 GregorianCalendar
cal(*TimeZone::getGMT(), status
);
2179 if (failure(status
, "GregorianCalendar", TRUE
)) return;
2181 UBool isReferenceYear
= TRUE
;
2182 if (cal
.get(UCAL_YEAR
, status
) != TimeZoneTest::REFERENCE_YEAR
) {
2183 isReferenceYear
= FALSE
;
2186 UBool sawAnError
= FALSE
;
2187 for (int testNum
= 0; zoneDisplayTestData
[testNum
].zoneName
!= NULL
; testNum
++) {
2188 Locale locale
= Locale::createFromName(zoneDisplayTestData
[testNum
].localeName
);
2189 TimeZone
*zone
= TimeZone::createTimeZone(zoneDisplayTestData
[testNum
].zoneName
);
2190 UnicodeString displayName
;
2191 zone
->getDisplayName(zoneDisplayTestData
[testNum
].summerTime
,
2192 zoneDisplayTestData
[testNum
].style
,
2195 if (displayName
!= zoneDisplayTestData
[testNum
].expectedDisplayName
) {
2197 UErrorCode status
= U_ZERO_ERROR
;
2198 displayName
.extract(name
, 100, NULL
, status
);
2199 if (isReferenceYear
) {
2201 dataerrln("Incorrect time zone display name. zone = \"%s\",\n"
2202 " locale = \"%s\", style = %s, Summertime = %d\n"
2203 " Expected \"%s\", "
2204 " Got \"%s\"\n Error: %s", zoneDisplayTestData
[testNum
].zoneName
,
2205 zoneDisplayTestData
[testNum
].localeName
,
2206 zoneDisplayTestData
[testNum
].style
==TimeZone::SHORT
?
2208 zoneDisplayTestData
[testNum
].summerTime
,
2209 zoneDisplayTestData
[testNum
].expectedDisplayName
,
2211 u_errorName(status
));
2213 logln("Incorrect time zone display name. zone = \"%s\",\n"
2214 " locale = \"%s\", style = %s, Summertime = %d\n"
2215 " Expected \"%s\", "
2216 " Got \"%s\"\n", zoneDisplayTestData
[testNum
].zoneName
,
2217 zoneDisplayTestData
[testNum
].localeName
,
2218 zoneDisplayTestData
[testNum
].style
==TimeZone::SHORT
?
2220 zoneDisplayTestData
[testNum
].summerTime
,
2221 zoneDisplayTestData
[testNum
].expectedDisplayName
,
2228 dataerrln("***Note: Errors could be the result of changes to zoneStrings locale data");
2232 void TimeZoneTest::TestGetRegion()
2234 static const struct {
2238 {"America/Los_Angeles", "US"},
2239 {"America/Indianapolis", "US"}, // CLDR canonical, Olson backward
2240 {"America/Indiana/Indianapolis", "US"}, // CLDR alias
2241 {"Mexico/General", "MX"}, // Link America/Mexico_City, Olson backward
2244 {"PST", "US"}, // Link America/Los_Angeles
2245 {"Europe/Helsinki", "FI"},
2246 {"Europe/Mariehamn", "AX"}, // Link Europe/Helsinki, but in zone.tab
2247 {"Asia/Riyadh", "SA"},
2248 {"Asia/Riyadh87", "001"}, // this should be "SA" actually, but not in zone.tab
2249 {"Etc/Unknown", 0}, // CLDR canonical, but not a sysmte zone ID
2250 {"bogus", 0}, // bogus
2251 {"GMT+08:00", 0}, // a custom ID, not a system zone ID
2258 for (i
= 0; data
[i
].id
; i
++) {
2260 TimeZone::getRegion(data
[i
].id
, region
, sizeof(region
), sts
);
2261 if (U_SUCCESS(sts
)) {
2262 if (data
[i
].region
== 0) {
2263 errln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
+ "\") returns "
2264 + region
+ " [expected: U_ILLEGAL_ARGUMENT_ERROR]");
2265 } else if (uprv_strcmp(region
, data
[i
].region
) != 0) {
2266 errln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
+ "\") returns "
2267 + region
+ " [expected: " + data
[i
].region
+ "]");
2269 } else if (sts
== U_ILLEGAL_ARGUMENT_ERROR
) {
2270 if (data
[i
].region
!= 0) {
2271 dataerrln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
2272 + "\") returns error status U_ILLEGAL_ARGUMENT_ERROR [expected: "
2273 + data
[i
].region
+ "]");
2276 errln((UnicodeString
)"Fail: getRegion(\"" + data
[i
].id
2277 + "\") returns an unexpected error status");
2281 // Extra test cases for short buffer
2286 len
= TimeZone::getRegion("America/New_York", region2
, sizeof(region2
), sts
);
2287 if (sts
== U_ILLEGAL_ARGUMENT_ERROR
) {
2288 dataerrln("Error calling TimeZone::getRegion");
2290 if (sts
!= U_STRING_NOT_TERMINATED_WARNING
) {
2291 errln("Expected U_STRING_NOT_TERMINATED_WARNING");
2293 if (len
!= 2) { // length of "US"
2294 errln("Incorrect result length");
2296 if (uprv_strncmp(region2
, "US", 2) != 0) {
2297 errln("Incorrect result");
2304 len
= TimeZone::getRegion("America/Chicago", region1
, sizeof(region1
), sts
);
2305 if (sts
== U_ILLEGAL_ARGUMENT_ERROR
) {
2306 dataerrln("Error calling TimeZone::getRegion");
2308 if (sts
!= U_BUFFER_OVERFLOW_ERROR
) {
2309 errln("Expected U_BUFFER_OVERFLOW_ERROR");
2311 if (len
!= 2) { // length of "US"
2312 errln("Incorrect result length");
2317 void TimeZoneTest::TestGetUnknown() {
2318 const TimeZone
&unknown
= TimeZone::getUnknown();
2319 UnicodeString expectedID
= UNICODE_STRING_SIMPLE("Etc/Unknown");
2321 assertEquals("getUnknown() wrong ID", expectedID
, unknown
.getID(id
));
2322 assertTrue("getUnknown() wrong offset", 0 == unknown
.getRawOffset());
2323 assertFalse("getUnknown() uses DST", unknown
.useDaylightTime());
2326 #endif /* #if !UCONFIG_NO_FORMATTING */