]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/tztest.cpp
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / tztest.cpp
CommitLineData
374ca955 1/***********************************************************************
b75a7d8f 2 * COPYRIGHT:
4388f060 3 * Copyright (c) 1997-2012, International Business Machines Corporation
374ca955
A
4 * and others. All Rights Reserved.
5 ***********************************************************************/
b75a7d8f
A
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_FORMATTING
10
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"
17#include "tztest.h"
18#include "cmemory.h"
374ca955 19#include "putilimp.h"
46f4442e
A
20#include "cstring.h"
21#include "olsontz.h"
22
23#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
b75a7d8f
A
24
25#define CASE(id,test) case id: \
26 name = #test; \
27 if (exec) { \
28 logln(#test "---"); logln(""); \
29 test(); \
30 } \
31 break
32
33// *****************************************************************************
34// class TimeZoneTest
35// *****************************************************************************
36
46f4442e 37// TODO: We should probably read following data at runtime, so we can update
729e4ab9 38// these values every release with necessary data changes.
46f4442e
A
39const int32_t TimeZoneTest::REFERENCE_YEAR = 2009;
40const char * TimeZoneTest::REFERENCE_DATA_VERSION = "2009d";
41
b75a7d8f
A
42void TimeZoneTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
43{
4388f060
A
44 if (exec) {
45 logln("TestSuite TestTimeZone");
b75a7d8f 46 }
4388f060
A
47 TESTCASE_AUTO_BEGIN;
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);
69 TESTCASE_AUTO_END;
b75a7d8f
A
70}
71
72const int32_t TimeZoneTest::millisPerHour = 3600000;
73
74// ---------------------------------------------------------------------------------
75
76/**
77 * Generic API testing for API coverage.
78 */
79void
80TimeZoneTest::TestGenericAPI()
81{
82 UnicodeString id("NewGMT");
83 int32_t offset = 12345;
84
85 SimpleTimeZone *zone = new SimpleTimeZone(offset, id);
86 if (zone->useDaylightTime()) errln("FAIL: useDaylightTime should return FALSE");
87
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");
92 delete zoneclone;
93
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");
98
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");
103
104 TimeZone* saveDefault = TimeZone::createDefault();
374ca955 105 logln((UnicodeString)"TimeZone::createDefault() => " + saveDefault->getID(id));
46f4442e 106 TimeZone* pstZone = TimeZone::createTimeZone("America/Los_Angeles");
b75a7d8f 107
374ca955 108 logln("call uprv_timezone() which uses the host");
b75a7d8f
A
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)");
111
112 int32_t tzoffset = uprv_timezone();
374ca955 113 logln(UnicodeString("Value returned from uprv_timezone = ") + tzoffset);
b75a7d8f
A
114 // Invert sign because UNIX semantics are backwards
115 if (tzoffset < 0)
116 tzoffset = -tzoffset;
46f4442e
A
117 if ((*saveDefault == *pstZone) && (tzoffset != 28800)) {
118 errln("FAIL: t_timezone may be incorrect. It is not 28800");
b75a7d8f 119 }
46f4442e
A
120
121 if ((tzoffset % 900) != 0) {
729e4ab9
A
122 /*
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.
128 */
129 infoln("WARNING: t_timezone may be incorrect. It is not a multiple of 15min.", tzoffset);
b75a7d8f
A
130 }
131
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);
138 delete defaultzone;
139 delete zoneclone;
46f4442e
A
140 delete pstZone;
141
142 UErrorCode status = U_ZERO_ERROR;
143 const char* tzver = TimeZone::getTZDataVersion(status);
144 if (U_FAILURE(status)) {
729e4ab9 145 errcheckln(status, "FAIL: getTZDataVersion failed - %s", u_errorName(status));
46f4442e
A
146 } else if (uprv_strlen(tzver) != 5 /* 4 digits + 1 letter */) {
147 errln((UnicodeString)"FAIL: getTZDataVersion returned " + tzver);
148 } else {
149 logln((UnicodeString)"tzdata version: " + tzver);
150 }
b75a7d8f
A
151}
152
153// ---------------------------------------------------------------------------------
154
155/**
156 * Test the setStartRule/setEndRule API calls.
157 */
158void
159TimeZoneTest::TestRuleAPI()
160{
161 UErrorCode status = U_ZERO_ERROR;
162
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");
166
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);
729e4ab9 170 if (failure(status, "new GregorianCalendar", TRUE)) return;
b75a7d8f
A
171 gc->clear();
172 gc->set(1990, UCAL_MARCH, 1);
173 UDate marchOneStd = gc->getTime(status); // Local Std time midnight
174 gc->clear();
175 gc->set(1990, UCAL_JULY, 1);
176 UDate julyOneStd = gc->getTime(status); // Local Std time midnight
177 if (failure(status, "GregorianCalendar::getTime")) return;
178
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);
182
183 zone->setStartRule(UCAL_MARCH, 1, 0, startHour, status);
184 zone->setEndRule (UCAL_JULY, 1, 0, endHour, status);
185
186 delete gc;
187 gc = new GregorianCalendar(*zone, status);
188 if (failure(status, "new GregorianCalendar")) return;
189
190 UDate marchOne = marchOneStd + startHour;
191 UDate julyOne = julyOneStd + endHour - 3600000; // Adjust from wall to Std time
192
193 UDate expMarchOne = 636251400000.0;
194 if (marchOne != expMarchOne)
195 {
196 errln((UnicodeString)"FAIL: Expected start computed as " + marchOne +
197 " = " + dateToString(marchOne));
198 logln((UnicodeString)" Should be " + expMarchOne +
199 " = " + dateToString(expMarchOne));
200 }
201
202 UDate expJulyOne = 646793100000.0;
203 if (julyOne != expJulyOne)
204 {
205 errln((UnicodeString)"FAIL: Expected start computed as " + julyOne +
206 " = " + dateToString(julyOne));
207 logln((UnicodeString)" Should be " + expJulyOne +
208 " = " + dateToString(expJulyOne));
209 }
210
374ca955
A
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);
b75a7d8f
A
213
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");
220
221 zone->setStartYear(1991);
222 if (zone->inDaylightTime(marchOne, status) ||
223 zone->inDaylightTime(julyOne - 1000, status))
224 errln("FAIL: Start year broken");
225
226 failure(status, "TestRuleAPI");
227 delete gc;
228 delete zone;
229}
230
231void
374ca955
A
232TimeZoneTest::findTransition(const TimeZone& tz,
233 UDate min, UDate max) {
234 UErrorCode ec = U_ZERO_ERROR;
235 UnicodeString id,s;
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"));
241 return;
242 }
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) {
247 min = mid;
248 } else {
249 max = mid;
250 }
251 if (failure(ec, "TimeZone::inDaylightTime")) return;
252 }
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));
259}
260
261void
262TimeZoneTest::testUsingBinarySearch(const TimeZone& tz,
263 UDate min, UDate max,
264 UDate expectedBoundary)
b75a7d8f
A
265{
266 UErrorCode status = U_ZERO_ERROR;
374ca955
A
267 UBool startsInDST = tz.inDaylightTime(min, status);
268 if (failure(status, "TimeZone::inDaylightTime")) return;
269 if (tz.inDaylightTime(max, status) == startsInDST) {
b75a7d8f
A
270 logln("Error: inDaylightTime(" + dateToString(max) + ") != " + ((!startsInDST)?"TRUE":"FALSE"));
271 return;
272 }
374ca955 273 if (failure(status, "TimeZone::inDaylightTime")) return;
b75a7d8f
A
274 while ((max - min) > INTERVAL) {
275 UDate mid = (min + max) / 2;
374ca955 276 if (tz.inDaylightTime(mid, status) == startsInDST) {
b75a7d8f 277 min = mid;
374ca955 278 } else {
b75a7d8f
A
279 max = mid;
280 }
374ca955 281 if (failure(status, "TimeZone::inDaylightTime")) return;
b75a7d8f
A
282 }
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;
287 if (mindelta >= 0 &&
288 mindelta <= INTERVAL &&
289 maxdelta >= 0 &&
290 maxdelta <= INTERVAL)
291 logln(UnicodeString("PASS: Expected bdry: ") + expectedBoundary + " = " + dateToString(expectedBoundary));
292 else
293 errln(UnicodeString("FAIL: Expected bdry: ") + expectedBoundary + " = " + dateToString(expectedBoundary));
294}
295
296const UDate TimeZoneTest::INTERVAL = 100;
297
298// ---------------------------------------------------------------------------------
299
300// -------------------------------------
301
302/**
303 * Test the offset of the PRT timezone.
304 */
305void
306TimeZoneTest::TestPRTOffset()
307{
308 TimeZone* tz = TimeZone::createTimeZone("PRT");
309 if (tz == 0) {
310 errln("FAIL: TimeZone(PRT) is null");
311 }
312 else {
374ca955
A
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) {
729e4ab9 318 dataerrln("FAIL: Offset for PRT should be %d, found %d", expectedHour, foundHour);
374ca955
A
319 } else {
320 logln("PASS: Offset for PRT should be %d, found %d", expectedHour, foundHour);
321 }
b75a7d8f
A
322 }
323 delete tz;
324}
325
326// -------------------------------------
327
328/**
329 * Regress a specific bug with a sequence of API calls.
330 */
331void
332TimeZoneTest::TestVariousAPI518()
333{
334 UErrorCode status = U_ZERO_ERROR;
335 TimeZone* time_zone = TimeZone::createTimeZone("PST");
336 UDate d = date(97, UCAL_APRIL, 30);
337 UnicodeString str;
338 logln("The timezone is " + time_zone->getID(str));
729e4ab9
A
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");
b75a7d8f
A
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)
729e4ab9 349 dataerrln("FAIL: getOffset returned wrong value");
b75a7d8f
A
350 if (U_FAILURE(status)) { errln("FAIL: GregorianCalendar::set failed"); return; }
351 delete gc;
352 delete time_zone;
353}
354
355// -------------------------------------
356
357/**
358 * Test the call which retrieves the available IDs.
359 */
360void
361TimeZoneTest::TestGetAvailableIDs913()
362{
363 UErrorCode ec = U_ZERO_ERROR;
374ca955
A
364 int32_t i;
365
366#ifdef U_USE_TIMEZONE_OBSOLETE_2_8
367 // Test legacy API -- remove these tests when the corresponding API goes away (duh)
368 int32_t numIDs = -1;
369 const UnicodeString** ids = TimeZone::createAvailableIDs(numIDs);
370 if (ids == 0 || numIDs < 1) {
371 errln("FAIL: createAvailableIDs()");
372 } else {
373 UnicodeString buf("TimeZone::createAvailableIDs() = { ");
374 for(i=0; i<numIDs; ++i) {
375 if (i) buf.append(", ");
376 buf.append(*ids[i]);
377 }
378 buf.append(" } ");
379 logln(buf + numIDs);
380 // we own the array; the caller owns the contained strings (yuck)
381 uprv_free(ids);
382 }
383
384 numIDs = -1;
385 ids = TimeZone::createAvailableIDs(-8*U_MILLIS_PER_HOUR, numIDs);
386 if (ids == 0 || numIDs < 1) {
387 errln("FAIL: createAvailableIDs(-8:00)");
388 } else {
389 UnicodeString buf("TimeZone::createAvailableIDs(-8:00) = { ");
390 for(i=0; i<numIDs; ++i) {
391 if (i) buf.append(", ");
392 buf.append(*ids[i]);
393 }
394 buf.append(" } ");
395 logln(buf + numIDs);
396 // we own the array; the caller owns the contained strings (yuck)
397 uprv_free(ids);
398 }
399 numIDs = -1;
400 ids = TimeZone::createAvailableIDs("US", numIDs);
401 if (ids == 0 || numIDs < 1) {
402 errln("FAIL: createAvailableIDs(US) ids=%d, numIDs=%d", ids, numIDs);
403 } else {
404 UnicodeString buf("TimeZone::createAvailableIDs(US) = { ");
405 for(i=0; i<numIDs; ++i) {
406 if (i) buf.append(", ");
407 buf.append(*ids[i]);
408 }
409 buf.append(" } ");
410 logln(buf + numIDs);
411 // we own the array; the caller owns the contained strings (yuck)
412 uprv_free(ids);
413 }
414#endif
415
b75a7d8f
A
416 UnicodeString str;
417 UnicodeString *buf = new UnicodeString("TimeZone::createEnumeration() = { ");
418 int32_t s_length;
419 StringEnumeration* s = TimeZone::createEnumeration();
4388f060
A
420 if (s == NULL) {
421 dataerrln("Unable to create TimeZone enumeration");
422 return;
423 }
b75a7d8f 424 s_length = s->count(ec);
b75a7d8f
A
425 for (i = 0; i < s_length;++i) {
426 if (i > 0) *buf += ", ";
374ca955
A
427 if ((i & 1) == 0) {
428 *buf += *s->snext(ec);
429 } else {
430 *buf += UnicodeString(s->next(NULL, ec), "");
431 }
432
433 if((i % 5) == 4) {
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");
438 } else {
439 delete s;
440 s = s2;
441 }
442 }
b75a7d8f
A
443 }
444 *buf += " };";
445 logln(*buf);
446
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.
450 */
451 s->reset(ec);
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);
457 if (z == 0) {
458 errln(UnicodeString("FAIL: createTimeZone(") +
459 *id + ") -> 0");
460 } else if (z->getID(str) != *id) {
461 errln(UnicodeString("FAIL: createTimeZone(") +
462 *id + ") -> zone " + str);
463 } else {
464 logln(UnicodeString("OK: createTimeZone(") +
465 *id + ") succeeded");
466 }
467 delete z;
468 }
469 }
470 delete s;
471
472 buf->truncate(0);
374ca955
A
473 *buf += "TimeZone::createEnumeration(GMT+01:00) = { ";
474
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);
480 }
481 delete s;
482 *buf += " };";
483 logln(*buf);
484
485
486 buf->truncate(0);
487 *buf += "TimeZone::createEnumeration(US) = { ";
b75a7d8f 488
374ca955 489 s = TimeZone::createEnumeration("US");
b75a7d8f
A
490 s_length = s->count(ec);
491 for (i = 0; i < s_length;++i) {
492 if (i > 0) *buf += ", ";
493 *buf += *s->snext(ec);
494 }
495 *buf += " };";
496 logln(*buf);
374ca955 497
b75a7d8f
A
498 TimeZone *tz = TimeZone::createTimeZone("PST");
499 if (tz != 0) logln("getTimeZone(PST) = " + tz->getID(str));
500 else errln("FAIL: getTimeZone(PST) = null");
501 delete tz;
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");
505 delete tz;
506
507 // @bug 4096694
508 tz = TimeZone::createTimeZone("NON_EXISTENT");
509 UnicodeString temp;
510 if (tz == 0)
511 errln("FAIL: getTimeZone(NON_EXISTENT) = null");
4388f060 512 else if (tz->getID(temp) != UCAL_UNKNOWN_ZONE_ID)
b75a7d8f
A
513 errln("FAIL: getTimeZone(NON_EXISTENT) = " + temp);
514 delete tz;
515
516 delete buf;
517 delete s;
518}
519
4388f060
A
520void
521TimeZoneTest::TestGetAvailableIDsNew()
522{
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;
528 int32_t rawOffset;
529 const UnicodeString *id1, *id2;
530 UnicodeString canonicalID;
531 UBool isSystemID;
532 char region[4];
533 int32_t zoneCount;
534
535 any = canonical = canonicalLoc = any_US = canonical_US = canonicalLoc_US = any_W5 = any_CA_W5 = any_US_E14 = NULL;
536
537 any = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, NULL, NULL, ec);
538 if (U_FAILURE(ec)) {
539 dataerrln("Failed to create enumration for ANY");
540 goto cleanup;
541 }
542
543 canonical = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, ec);
544 if (U_FAILURE(ec)) {
545 errln("Failed to create enumration for CANONICAL");
546 goto cleanup;
547 }
548
549 canonicalLoc = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, NULL, NULL, ec);
550 if (U_FAILURE(ec)) {
551 errln("Failed to create enumration for CANONICALLOC");
552 goto cleanup;
553 }
554
555 any_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", NULL, ec);
556 if (U_FAILURE(ec)) {
557 errln("Failed to create enumration for ANY_US");
558 goto cleanup;
559 }
560
561 canonical_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, "US", NULL, ec);
562 if (U_FAILURE(ec)) {
563 errln("Failed to create enumration for CANONICAL_US");
564 goto cleanup;
565 }
566
567 canonicalLoc_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, "US", NULL, ec);
568 if (U_FAILURE(ec)) {
569 errln("Failed to create enumration for CANONICALLOC_US");
570 goto cleanup;
571 }
572
573 rawOffset = (-5)*60*60*1000;
574 any_W5 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, NULL, &rawOffset, ec);
575 if (U_FAILURE(ec)) {
576 errln("Failed to create enumration for ANY_W5");
577 goto cleanup;
578 }
579
580 any_CA_W5 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "CA", &rawOffset, ec);
581 if (U_FAILURE(ec)) {
582 errln("Failed to create enumration for ANY_CA_W5");
583 goto cleanup;
584 }
585
586 rawOffset = 14*60*60*1000;
587 any_US_E14 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", &rawOffset, ec);
588 if (U_FAILURE(ec)) {
589 errln("Failed to create enumration for ANY_US_E14");
590 goto cleanup;
591 }
592
593 checkContainsAll(any, "ANY", canonical, "CANONICAL");
594 checkContainsAll(canonical, "CANONICAL", canonicalLoc, "CANONICALLOC");
595
596 checkContainsAll(any, "ANY", any_US, "ANY_US");
597 checkContainsAll(canonical, "CANONICAL", canonical_US, "CANONICAL_US");
598 checkContainsAll(canonicalLoc, "CANONICALLOC", canonicalLoc_US, "CANONICALLOC_US");
599
600 checkContainsAll(any_US, "ANY_US", canonical_US, "CANONICAL_US");
601 checkContainsAll(canonical_US, "CANONICAL_US", canonicalLoc_US, "CANONICALLOC_US");
602
603 checkContainsAll(any, "ANY", any_W5, "ANY_W5");
604 checkContainsAll(any_W5, "ANY_W5", any_CA_W5, "ANY_CA_W5");
605
606 // And ID in any set, but not in canonical set must not be a canonical ID
607 any->reset(ec);
608 while ((id1 = any->snext(ec)) != NULL) {
609 UBool found = FALSE;
610 canonical->reset(ec);
611 while ((id2 = canonical->snext(ec)) != NULL) {
612 if (*id1 == *id2) {
613 found = TRUE;
614 break;
615 }
616 }
617 if (U_FAILURE(ec)) {
618 break;
619 }
620 if (!found) {
621 TimeZone::getCanonicalID(*id1, canonicalID, isSystemID, ec);
622 if (U_FAILURE(ec)) {
623 break;
624 }
625 if (*id1 == canonicalID) {
626 errln((UnicodeString)"FAIL: canonicalID [" + *id1 + "] is not in CANONICAL");
627 }
628 if (!isSystemID) {
629 errln((UnicodeString)"FAIL: ANY contains non-system ID: " + *id1);
630 }
631 }
632 }
633 if (U_FAILURE(ec)) {
634 errln("Error checking IDs in ANY, but not in CANONICAL");
635 ec = U_ZERO_ERROR;
636 }
637
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);
642 if (U_FAILURE(ec)) {
643 break;
644 }
645 if (*id1 != canonicalID) {
646 errln((UnicodeString)"FAIL: CANONICAL contains non-canonical ID: " + *id1);
647 }
648 if (!isSystemID) {
649 errln((UnicodeString)"FAILE: CANONICAL contains non-system ID: " + *id1);
650 }
651 }
652 if (U_FAILURE(ec)) {
653 errln("Error checking IDs in CANONICAL");
654 ec = U_ZERO_ERROR;
655 }
656
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);
661 if (U_FAILURE(ec)) {
662 break;
663 }
664 if (uprv_strcmp(region, "001") == 0) {
665 errln((UnicodeString)"FAIL: CANONICALLOC contains non location zone: " + *id1);
666 }
667 }
668 if (U_FAILURE(ec)) {
669 errln("Error checking IDs in CANONICALLOC");
670 ec = U_ZERO_ERROR;
671 }
672
673 // any_US must contain only US zones
674 any_US->reset(ec);
675 while ((id1 = any_US->snext(ec)) != NULL) {
676 TimeZone::getRegion(*id1, region, sizeof(region), ec);
677 if (U_FAILURE(ec)) {
678 break;
679 }
680 if (uprv_strcmp(region, "US") != 0) {
681 errln((UnicodeString)"FAIL: ANY_US contains non-US zone ID: " + *id1);
682 }
683 }
684 if (U_FAILURE(ec)) {
685 errln("Error checking IDs in ANY_US");
686 ec = U_ZERO_ERROR;
687 }
688
689 // any_W5 must contain only GMT-05:00 zones
690 any_W5->reset(ec);
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);
695 }
696 delete tz;
697 }
698 if (U_FAILURE(ec)) {
699 errln("Error checking IDs in ANY_W5");
700 ec = U_ZERO_ERROR;
701 }
702
703 // No US zone swith GMT+14:00
704 zoneCount = any_US_E14->count(ec);
705 if (U_FAILURE(ec)) {
706 errln("Error checking IDs in ANY_US_E14");
707 ec = U_ZERO_ERROR;
708 } else if (zoneCount != 0) {
709 errln("FAIL: ANY_US_E14 must be empty");
710 }
711
712cleanup:
713 delete any;
714 delete canonical;
715 delete canonicalLoc;
716 delete any_US;
717 delete canonical_US;
718 delete canonicalLoc_US;
719 delete any_W5;
720 delete any_CA_W5;
721 delete any_US_E14;
722}
723
724void
725TimeZoneTest::checkContainsAll(StringEnumeration *s1, const char *name1,
726 StringEnumeration *s2, const char *name2)
727{
728 UErrorCode ec = U_ZERO_ERROR;
729 const UnicodeString *id1, *id2;
730
731 s2->reset(ec);
732
733 while ((id2 = s2->snext(ec)) != NULL) {
734 UBool found = FALSE;
735 s1->reset(ec);
736 while ((id1 = s1->snext(ec)) != NULL) {
737 if (*id1 == *id2) {
738 found = TRUE;
739 break;
740 }
741 }
742 if (!found) {
743 errln((UnicodeString)"FAIL: " + name1 + "does not contain "
744 + *id2 + " in " + name2);
745 }
746 }
747
748 if (U_FAILURE(ec)) {
749 errln((UnicodeString)"Error checkContainsAll for " + name1 + " - " + name2);
750 }
751}
b75a7d8f
A
752
753/**
374ca955
A
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]
757 *
b75a7d8f
A
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.
772 * 12/3/99 aliu
773 *
4162bf98
A
774 * Added tests for additional zones and aliases from the icuzones file.
775 * Markus Scherer 2006-nov-06
776 *
b75a7d8f
A
777 * [srl - from java - 7/5/1998]
778 * @bug 4130885
779 * Certain short zone IDs, used since 1.1.x, are incorrect.
780 *
781 * The worst of these is:
782 *
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.
786 *
787 * Other wrong zone IDs:
788 *
789 * ECT (European Central Time) GMT+1:00: ECT is Ecuador Time,
790 * GMT-5:00. European Central time is abbreviated CEST.
791 *
792 * SST (Solomon Island Time) GMT+11:00. SST is actually Samoa Standard Time,
793 * GMT-11:00. Solomon Island time is SBT.
794 *
795 * NST (New Zealand Time) GMT+12:00. NST is the abbreviation for
796 * Newfoundland Standard Time, GMT-3:30. New Zealanders use NZST.
797 *
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,
800 * GMT-4:00.
801 *
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.
805 *
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.
808 *
809 * FAKE zones:
810 *
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) +
827 *
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).
831 */
832void TimeZoneTest::TestShortZoneIDs()
833{
46f4442e
A
834 UErrorCode status = U_ZERO_ERROR;
835
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.");
848 }
849
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
854 // fail.
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
858
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.");
863 }
864
b75a7d8f
A
865 int32_t i;
866 // Create a small struct to hold the array
867 struct
868 {
869 const char *id;
870 int32_t offset;
871 UBool daylight;
872 }
873 kReferenceList [] =
874 {
875 {"MIT", -660, FALSE},
876 {"HST", -600, FALSE},
877 {"AST", -540, TRUE},
878 {"PST", -480, TRUE},
879 {"PNT", -420, FALSE},
374ca955 880 {"MST", -420, FALSE}, // updated Aug 2003 aliu
b75a7d8f 881 {"CST", -360, TRUE},
73c04bcf 882 {"IET", -300, TRUE}, // updated Jan 2006 srl
374ca955 883 {"EST", -300, FALSE}, // updated Aug 2003 aliu
b75a7d8f
A
884 {"PRT", -240, FALSE},
885 {"CNT", -210, TRUE},
4162bf98 886 {"AGT", -180, TRUE}, // updated by tzdata2007k
b75a7d8f 887 {"BET", -180, TRUE},
b75a7d8f
A
888 {"GMT", 0, FALSE},
889 {"UTC", 0, FALSE}, // ** srl: seems broken in C++
890 {"ECT", 60, TRUE},
46f4442e 891 {"MET", 60, TRUE}, // updated 12/3/99 aliu
b75a7d8f
A
892 {"ART", 120, TRUE},
893 {"EET", 120, TRUE},
46f4442e 894 {"CAT", 120, FALSE}, // Africa/Harare
b75a7d8f 895 {"EAT", 180, FALSE},
b75a7d8f 896 {"NET", 240, TRUE}, // updated 12/3/99 aliu
46f4442e 897 {"PLT", 300, FALSE}, // updated by 2008c - no DST after 2008
b75a7d8f
A
898 {"IST", 330, FALSE},
899 {"BST", 360, FALSE},
900 {"VST", 420, FALSE},
374ca955 901 {"CTT", 480, FALSE}, // updated Aug 2003 aliu
b75a7d8f 902 {"JST", 540, FALSE},
374ca955 903 {"ACT", 570, FALSE}, // updated Aug 2003 aliu
b75a7d8f
A
904 {"AET", 600, TRUE},
905 {"SST", 660, FALSE},
b75a7d8f 906 {"NST", 720, TRUE}, // Pacific/Auckland
46f4442e
A
907
908 // From icuzones:
909 {"Etc/Unknown", 0, FALSE},
910
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},
924
b75a7d8f
A
925 {"",0,FALSE}
926 };
927
b75a7d8f
A
928 for(i=0;kReferenceList[i].id[0];i++) {
929 UnicodeString itsID(kReferenceList[i].id);
930 UBool ok = TRUE;
931 // Check existence.
932 TimeZone *tz = TimeZone::createTimeZone(itsID);
46f4442e 933 if (!tz || (kReferenceList[i].offset != 0 && *tz == *TimeZone::getGMT())) {
b75a7d8f
A
934 errln("FAIL: Time Zone " + itsID + " does not exist!");
935 continue;
936 }
937
938 // Check daylight usage.
939 UBool usesDaylight = tz->useDaylightTime();
940 if (usesDaylight != kReferenceList[i].daylight) {
46f4442e
A
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"));
946 } else {
947 errln("FAIL: Time Zone " + itsID + " use daylight is " +
948 (usesDaylight?"TRUE":"FALSE") +
949 " but it should be " +
950 ((kReferenceList[i].daylight)?"TRUE":"FALSE"));
951 }
b75a7d8f
A
952 ok = FALSE;
953 }
954
955 // Check offset
956 int32_t offsetInMinutes = tz->getRawOffset()/60000;
957 if (offsetInMinutes != kReferenceList[i].offset) {
46f4442e
A
958 if (isNonReferenceTzdataVersion || isDateBeforeReferenceYear) {
959 logln("FAIL: Time Zone " + itsID + " raw offset is " +
960 offsetInMinutes +
961 " but it should be " + kReferenceList[i].offset);
962 } else {
963 errln("FAIL: Time Zone " + itsID + " raw offset is " +
964 offsetInMinutes +
965 " but it should be " + kReferenceList[i].offset);
966 }
b75a7d8f
A
967 ok = FALSE;
968 }
969
970 if (ok) {
971 logln("OK: " + itsID +
972 " useDaylightTime() & getRawOffset() as expected");
973 }
974 delete tz;
975 }
976
977
978 // OK now test compat
979 logln("Testing for compatibility zones");
980
981 const char* compatibilityMap[] = {
982 // This list is copied from tz.alias. If tz.alias
4162bf98 983 // changes, this list must be updated. Current as of Mar 2007
b75a7d8f
A
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",
4162bf98 990 "BST", "Asia/Dhaka", // # spelling changed in 2000h; was Asia/Dacca
b75a7d8f
A
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
4162bf98
A
998 // "EST", "America/New_York", # Defined as -05:00
999 // "HST", "Pacific/Honolulu", # Defined as -10:00
b75a7d8f
A
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",
4162bf98 1005 // "MST", "America/Denver", # Defined as -07:00
b75a7d8f
A
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",
1013 "UTC", "Etc/GMT",
1014 "VST", "Asia/Saigon",
1015 "","",""
1016 };
1017
1018 for (i=0;*compatibilityMap[i];i+=2) {
1019 UnicodeString itsID;
1020
1021 const char *zone1 = compatibilityMap[i];
1022 const char *zone2 = compatibilityMap[i+1];
1023
1024 TimeZone *tz1 = TimeZone::createTimeZone(zone1);
1025 TimeZone *tz2 = TimeZone::createTimeZone(zone2);
1026
1027 if (!tz1) {
1028 errln(UnicodeString("FAIL: Could not find short ID zone ") + zone1);
1029 }
1030 if (!tz2) {
1031 errln(UnicodeString("FAIL: Could not find long ID zone ") + zone2);
1032 }
1033
1034 if (tz1 && tz2) {
1035 // make NAME same so comparison will only look at the rest
1036 tz2->setID(tz1->getID(itsID));
1037
1038 if (*tz1 != *tz2) {
1039 errln("FAIL: " + UnicodeString(zone1) +
1040 " != " + UnicodeString(zone2));
1041 } else {
1042 logln("OK: " + UnicodeString(zone1) +
1043 " == " + UnicodeString(zone2));
1044 }
1045 }
1046
1047 delete tz1;
1048 delete tz2;
1049 }
1050}
1051
46f4442e 1052
b75a7d8f
A
1053/**
1054 * Utility function for TestCustomParse
1055 */
46f4442e
A
1056UnicodeString& TimeZoneTest::formatOffset(int32_t offset, UnicodeString &rv) {
1057 rv.remove();
1058 UChar sign = 0x002B;
1059 if (offset < 0) {
1060 sign = 0x002D;
1061 offset = -offset;
1062 }
1063
1064 int32_t s = offset % 60;
1065 offset /= 60;
1066 int32_t m = offset % 60;
1067 int32_t h = offset / 60;
1068
1069 rv += (UChar)(sign);
1070 if (h >= 10) {
1071 rv += (UChar)(0x0030 + (h/10));
1072 } else {
1073 rv += (UChar)0x0030;
1074 }
1075 rv += (UChar)(0x0030 + (h%10));
1076
1077 rv += (UChar)0x003A; /* ':' */
1078 if (m >= 10) {
1079 rv += (UChar)(0x0030 + (m/10));
1080 } else {
1081 rv += (UChar)0x0030;
1082 }
1083 rv += (UChar)(0x0030 + (m%10));
b75a7d8f 1084
46f4442e
A
1085 if (s) {
1086 rv += (UChar)0x003A; /* ':' */
1087 if (s >= 10) {
1088 rv += (UChar)(0x0030 + (s/10));
1089 } else {
1090 rv += (UChar)0x0030;
1091 }
1092 rv += (UChar)(0x0030 + (s%10));
1093 }
1094 return rv;
1095}
b75a7d8f 1096
46f4442e
A
1097/**
1098 * Utility function for TestCustomParse, generating time zone ID
1099 * string for the give offset.
1100 */
1101UnicodeString& TimeZoneTest::formatTZID(int32_t offset, UnicodeString &rv) {
1102 rv.remove();
1103 UChar sign = 0x002B;
1104 if (offset < 0) {
1105 sign = 0x002D;
1106 offset = -offset;
1107 }
b75a7d8f 1108
46f4442e
A
1109 int32_t s = offset % 60;
1110 offset /= 60;
1111 int32_t m = offset % 60;
1112 int32_t h = offset / 60;
b75a7d8f 1113
46f4442e
A
1114 rv += "GMT";
1115 rv += (UChar)(sign);
1116 if (h >= 10) {
1117 rv += (UChar)(0x0030 + (h/10));
1118 } else {
1119 rv += (UChar)0x0030;
1120 }
1121 rv += (UChar)(0x0030 + (h%10));
729e4ab9 1122 rv += (UChar)0x003A;
46f4442e
A
1123 if (m >= 10) {
1124 rv += (UChar)(0x0030 + (m/10));
1125 } else {
1126 rv += (UChar)0x0030;
1127 }
1128 rv += (UChar)(0x0030 + (m%10));
b75a7d8f 1129
46f4442e 1130 if (s) {
729e4ab9 1131 rv += (UChar)0x003A;
46f4442e
A
1132 if (s >= 10) {
1133 rv += (UChar)(0x0030 + (s/10));
1134 } else {
1135 rv += (UChar)0x0030;
1136 }
1137 rv += (UChar)(0x0030 + (s%10));
1138 }
1139 return rv;
b75a7d8f
A
1140}
1141
b75a7d8f
A
1142/**
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.
1147 *
1148 * @bug 4044013
1149 */
1150void TimeZoneTest::TestCustomParse()
1151{
1152 int32_t i;
1153 const int32_t kUnparseable = 604800; // the number of seconds in a week. More than any offset should be.
b75a7d8f
A
1154
1155 struct
1156 {
1157 const char *customId;
1158 int32_t expectedOffset;
1159 }
1160 kData[] =
1161 {
46f4442e
A
1162 // ID Expected offset in seconds
1163 {"GMT", kUnparseable}, //Isn't custom. [returns normal GMT]
b75a7d8f 1164 {"GMT-YOUR.AD.HERE", kUnparseable},
46f4442e
A
1165 {"GMT0", kUnparseable},
1166 {"GMT+0", (0)},
1167 {"GMT+1", (1*60*60)},
1168 {"GMT-0030", (-30*60)},
1169 {"GMT+15:99", kUnparseable},
b75a7d8f
A
1170 {"GMT+", kUnparseable},
1171 {"GMT-", kUnparseable},
1172 {"GMT+0:", kUnparseable},
1173 {"GMT-:", kUnparseable},
46f4442e
A
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},
b75a7d8f
A
1187 {0, 0}
1188 };
1189
46f4442e 1190 for (i=0; kData[i].customId != 0; i++) {
b75a7d8f
A
1191 UnicodeString id(kData[i].customId);
1192 int32_t exp = kData[i].expectedOffset;
b75a7d8f
A
1193 TimeZone *zone = TimeZone::createTimeZone(id);
1194 UnicodeString itsID, temp;
1195
729e4ab9 1196 if (dynamic_cast<OlsonTimeZone *>(zone) != NULL) {
46f4442e
A
1197 logln(id + " -> Olson time zone");
1198 } else {
b75a7d8f 1199 zone->getID(itsID);
46f4442e
A
1200 int32_t ioffset = zone->getRawOffset()/1000;
1201 UnicodeString offset, expectedID;
1202 formatOffset(ioffset, offset);
1203 formatTZID(ioffset, expectedID);
1204 logln(id + " -> " + itsID + " " + offset);
4388f060 1205 if (exp == kUnparseable && itsID != UCAL_UNKNOWN_ZONE_ID) {
46f4442e
A
1206 errln("Expected parse failure for " + id +
1207 ", got offset of " + offset +
1208 ", id " + itsID);
b75a7d8f 1209 }
46f4442e
A
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"
729e4ab9 1212 // ICU creates custom zones with IDs of the form "GMT+02:00"
46f4442e 1213 else if (exp != kUnparseable && (ioffset != exp || itsID != expectedID)) {
729e4ab9 1214 dataerrln("Expected offset of " + formatOffset(exp, temp) +
46f4442e
A
1215 ", id " + expectedID +
1216 ", for " + id +
1217 ", got offset of " + offset +
1218 ", id " + itsID);
b75a7d8f
A
1219 }
1220 }
1221 delete zone;
1222 }
1223}
1224
73c04bcf
A
1225void
1226TimeZoneTest::TestAliasedNames()
1227{
1228 struct {
1229 const char *from;
1230 const char *to;
1231 } kData[] = {
1232 /* Generated by org.unicode.cldr.tool.CountItems */
1233
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"},
1302 {"GMT", "Etc/GMT"},
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"},
1330 {"UCT", "Etc/GMT"},
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"},
1344 {"UTC", "Etc/GMT"},
1345 {"Universal", "Etc/GMT"},
1346 {"W-SU", "Europe/Moscow"},
1347 {"Zulu", "Etc/GMT"},
1348 /* Total: 113 */
1349
1350 };
1351
1352 TimeZone::EDisplayType styles[] = { TimeZone::SHORT, TimeZone::LONG };
1353 UBool useDst[] = { FALSE, TRUE };
1354 int32_t noLoc = uloc_countAvailable();
1355
73c04bcf
A
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);
1364 }
46f4442e 1365 if(!quick) {
73c04bcf
A
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++) {
1370 fromName.remove();
1371 toName.remove();
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);
1379 }
1380 }
1381 }
1382 }
1383 } else {
1384 fromName.remove();
1385 toName.remove();
1386 from->getDisplayName(fromName);
1387 to->getDisplayName(toName);
1388 if(fromName.compare(toName) != 0) {
1389 errln("Fail: Expected "+toName+" but got " + fromName);
1390 }
1391 }
1392 delete from;
1393 delete to;
1394 }
1395}
1396
b75a7d8f
A
1397/**
1398 * Test the basic functionality of the getDisplayName() API.
1399 *
1400 * @bug 4112869
1401 * @bug 4028006
1402 *
1403 * See also API change request A41.
1404 *
1405 * 4/21/98 - make smarter, so the test works if the ext resources
1406 * are present or not.
1407 */
1408void
1409TimeZoneTest::TestDisplayName()
1410{
1411 UErrorCode status = U_ZERO_ERROR;
1412 int32_t i;
1413 TimeZone *zone = TimeZone::createTimeZone("PST");
1414 UnicodeString name;
1415 zone->getDisplayName(Locale::getEnglish(), name);
1416 logln("PST->" + name);
1417 if (name.compare("Pacific Standard Time") != 0)
729e4ab9 1418 dataerrln("Fail: Expected \"Pacific Standard Time\" but got " + name);
b75a7d8f
A
1419
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 //*****************************************************************
1425 struct
1426 {
1427 UBool useDst;
1428 TimeZone::EDisplayType style;
1429 const char *expect;
1430 } kData[] = {
1431 {FALSE, TimeZone::SHORT, "PST"},
1432 {TRUE, TimeZone::SHORT, "PDT"},
1433 {FALSE, TimeZone::LONG, "Pacific Standard Time"},
1434 {TRUE, TimeZone::LONG, "Pacific Daylight Time"},
1435
729e4ab9
A
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"},
1440
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"},
1445
1446 {FALSE, TimeZone::SHORT_COMMONLY_USED, "PST"},
1447 {TRUE, TimeZone::SHORT_COMMONLY_USED, "PDT"},
4388f060
A
1448 {FALSE, TimeZone::GENERIC_LOCATION, "United States Time (Los Angeles)"},
1449 {TRUE, TimeZone::GENERIC_LOCATION, "United States Time (Los Angeles)"},
729e4ab9 1450
b75a7d8f
A
1451 {FALSE, TimeZone::LONG, ""}
1452 };
1453
1454 for (i=0; kData[i].expect[0] != '\0'; i++)
1455 {
1456 name.remove();
1457 name = zone->getDisplayName(kData[i].useDst,
1458 kData[i].style,
1459 Locale::getEnglish(), name);
1460 if (name.compare(kData[i].expect) != 0)
729e4ab9 1461 dataerrln("Fail: Expected " + UnicodeString(kData[i].expect) + "; got " + name);
b75a7d8f
A
1462 logln("PST [with options]->" + name);
1463 }
374ca955
A
1464 for (i=0; kData[i].expect[0] != '\0'; i++)
1465 {
b75a7d8f
A
1466 name.remove();
1467 name = zone->getDisplayName(kData[i].useDst,
1468 kData[i].style, name);
1469 if (name.compare(kData[i].expect) != 0)
729e4ab9 1470 dataerrln("Fail: Expected " + UnicodeString(kData[i].expect) + "; got " + name);
b75a7d8f
A
1471 logln("PST [with options]->" + name);
1472 }
1473
1474
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");
1478
1479 zone2->setStartRule(UCAL_JANUARY, 1, 0, 0, status);
1480 zone2->setEndRule(UCAL_DECEMBER, 31, 0, 0, status);
1481
374ca955
A
1482 UnicodeString inDaylight;
1483 if (zone2->inDaylightTime(UDate(0), status)) {
1484 inDaylight = UnicodeString("TRUE");
1485 } else {
1486 inDaylight = UnicodeString("FALSE");
1487 }
b75a7d8f
A
1488 logln(UnicodeString("Modified PST inDaylightTime->") + inDaylight );
1489 if(U_FAILURE(status))
1490 {
729e4ab9 1491 dataerrln("Some sort of error..." + UnicodeString(u_errorName(status))); // REVISIT
b75a7d8f
A
1492 }
1493 name.remove();
1494 name = zone2->getDisplayName(Locale::getEnglish(),name);
1495 logln("Modified PST->" + name);
1496 if (name.compare("Pacific Standard Time") != 0)
729e4ab9 1497 dataerrln("Fail: Expected \"Pacific Standard Time\"");
b75a7d8f
A
1498
1499 // Make sure we get the default display format for Locales
1500 // with no display name data.
374ca955 1501 Locale mt_MT("mt_MT");
b75a7d8f 1502 name.remove();
374ca955 1503 name = zone->getDisplayName(mt_MT,name);
b75a7d8f
A
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 //*****************************************************************
374ca955 1509 logln("PST(mt_MT)->" + name);
b75a7d8f
A
1510
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.
374ca955 1514 ResourceBundle enRB(NULL,
b75a7d8f
A
1515 Locale::getEnglish(), status);
1516 if(U_FAILURE(status))
729e4ab9 1517 dataerrln("Couldn't get ResourceBundle for en - %s", u_errorName(status));
b75a7d8f 1518
374ca955
A
1519 ResourceBundle mtRB(NULL,
1520 mt_MT, status);
b75a7d8f 1521 //if(U_FAILURE(status))
374ca955 1522 // errln("Couldn't get ResourceBundle for mt_MT");
b75a7d8f
A
1523
1524 UBool noZH = U_FAILURE(status);
1525
1526 if (noZH) {
374ca955 1527 logln("Warning: Not testing the mt_MT behavior because resource is absent");
b75a7d8f 1528 if (name != "Pacific Standard Time")
729e4ab9 1529 dataerrln("Fail: Expected Pacific Standard Time");
b75a7d8f
A
1530 }
1531
1532
1533 if (name.compare("GMT-08:00") &&
1534 name.compare("GMT-8:00") &&
1535 name.compare("GMT-0800") &&
1536 name.compare("GMT-800")) {
729e4ab9
A
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("************************************************************");
b75a7d8f
A
1541 }
1542
1543 // Now try a non-existent zone
1544 delete zone2;
1545 zone2 = new SimpleTimeZone(90*60*1000, "xyzzy");
1546 name.remove();
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"))
729e4ab9 1553 dataerrln("Fail: Expected GMT+01:30 or something similar");
374ca955 1554 name.truncate(0);
b75a7d8f
A
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"))
729e4ab9 1561 dataerrln("Fail: Expected GMT+01:30 or something similar");
b75a7d8f
A
1562 // clean up
1563 delete zone;
1564 delete zone2;
1565}
1566
1567/**
1568 * @bug 4107276
1569 */
1570void
1571TimeZoneTest::TestDSTSavings()
1572{
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");
1582
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.");
1591
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.");
1597
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.");
1603
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.");
1610
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.");
1616
1617 delete tz;
1618}
1619
1620/**
1621 * @bug 4107570
1622 */
1623void
1624TimeZoneTest::TestAlternateRules()
1625{
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.
1628 // --rtg 1/28/98
1629
1630 SimpleTimeZone tz(-5 * U_MILLIS_PER_HOUR, "alternateRuleTest");
1631
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");
1640
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.");
1646
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.");
1652
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.");
1657
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.");
1663
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");
1671
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.");
1677
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.");
1683
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.");
1689
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.");
1695}
1696
73c04bcf
A
1697void 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();
1701 UnicodeString id;
1702 int32_t expected = 1800000;
1703 if (expected != dst_icu) {
729e4ab9 1704 dataerrln(UnicodeString("java reports dst savings of ") + expected +
73c04bcf
A
1705 " but icu reports " + dst_icu +
1706 " for tz " + tz_icu->getID(id));
1707 } else {
1708 logln(UnicodeString("both java and icu report dst savings of ") + expected + " for tz " + tz_icu->getID(id));
1709 }
1710 delete tz_icu;
1711}
1712
b75a7d8f
A
1713/**
1714 * Test country code support. Jitterbug 776.
1715 */
1716void 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;
1720 int32_t n;
1721 StringEnumeration* s = TimeZone::createEnumeration("US");
4388f060
A
1722 if (s == NULL) {
1723 dataerrln("Unable to create TimeZone enumeration for US");
1724 return;
1725 }
b75a7d8f
A
1726 n = s->count(ec);
1727 UBool la = FALSE, tokyo = FALSE;
1728 UnicodeString laZone("America/Los_Angeles", "");
1729 UnicodeString tokyoZone("Asia/Tokyo", "");
1730 int32_t i;
1731
1732 if (s == NULL || n <= 0) {
729e4ab9 1733 dataerrln("FAIL: TimeZone::createEnumeration() returned nothing");
b75a7d8f
A
1734 return;
1735 }
1736 for (i=0; i<n; ++i) {
1737 const UnicodeString* id = s->snext(ec);
1738 if (*id == (laZone)) {
1739 la = TRUE;
1740 }
1741 if (*id == (tokyoZone)) {
1742 tokyo = TRUE;
1743 }
1744 }
1745 if (!la || tokyo) {
1746 errln("FAIL: " + laZone + " in US = " + la);
1747 errln("FAIL: " + tokyoZone + " in US = " + tokyo);
1748 }
1749 delete s;
1750
1751 s = TimeZone::createEnumeration("JP");
1752 n = s->count(ec);
1753 la = FALSE; tokyo = FALSE;
1754
1755 for (i=0; i<n; ++i) {
1756 const UnicodeString* id = s->snext(ec);
1757 if (*id == (laZone)) {
1758 la = TRUE;
1759 }
1760 if (*id == (tokyoZone)) {
1761 tokyo = TRUE;
1762 }
1763 }
1764 if (la || !tokyo) {
1765 errln("FAIL: " + laZone + " in JP = " + la);
1766 errln("FAIL: " + tokyoZone + " in JP = " + tokyo);
1767 }
374ca955
A
1768 StringEnumeration* s1 = TimeZone::createEnumeration("US");
1769 StringEnumeration* s2 = TimeZone::createEnumeration("US");
1770 for(i=0;i<n;++i){
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);
1774 }
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);
1780 }
1781 TimeZone* tz2 = TimeZone::createTimeZone(*id2);
1782 if(tz1->hasSameRules(*tz2)){
1783 logln("ID1 : " + *id1+" == ID2 : " +*id2);
1784 }
1785 delete tz2;
1786 }
1787 delete tz1;
1788 }
1789 delete s1;
1790 delete s2;
b75a7d8f
A
1791 delete s;
1792}
1793
374ca955
A
1794void TimeZoneTest::TestHistorical() {
1795 const int32_t H = U_MILLIS_PER_HOUR;
1796 struct {
1797 const char* id;
1798 int32_t time; // epoch seconds
1799 int32_t offset; // total offset (millis)
1800 } DATA[] = {
1801 // Add transition points (before/after) as desired to test historical
1802 // behavior.
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
1815 {0, 0, 0}
1816 };
1817
1818 for (int32_t i=0; DATA[i].id!=0; ++i) {
1819 const char* id = DATA[i].id;
1820 TimeZone *tz = TimeZone::createTimeZone(id);
1821 UnicodeString s;
1822 if (tz == 0) {
1823 errln("FAIL: Cannot create %s", id);
1824 } else if (tz->getID(s) != UnicodeString(id)) {
729e4ab9 1825 dataerrln((UnicodeString)"FAIL: createTimeZone(" + id + ") => " + s);
374ca955
A
1826 } else {
1827 UErrorCode ec = U_ZERO_ERROR;
1828 int32_t raw, dst;
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(" +
1835 //when + " = " +
1836 dateToString(when) + ") => " +
1837 raw + ", " + dst);
1838 } else {
1839 logln((UnicodeString)"Ok: " + DATA[i].id + ".getOffset(" +
1840 //when + " = " +
1841 dateToString(when) + ") => " +
1842 raw + ", " + dst);
1843 }
1844 }
1845 delete tz;
1846 }
1847}
1848
1849void TimeZoneTest::TestEquivalentIDs() {
1850 int32_t n = TimeZone::countEquivalentIDs("PST");
1851 if (n < 2) {
729e4ab9 1852 dataerrln((UnicodeString)"FAIL: countEquivalentIDs(PST) = " + n);
374ca955
A
1853 } else {
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")) {
1859 sawLA = TRUE;
1860 }
1861 }
1862 if (!sawLA) {
1863 errln("FAIL: America/Los_Angeles should be in the list");
1864 }
1865 }
1866}
1867
46f4442e
A
1868// Test that a transition at the end of February is handled correctly.
1869void TimeZoneTest::TestFebruary() {
1870 UErrorCode status = U_ZERO_ERROR;
1871
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.
1875 //
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
1884 status);
1885 if (U_FAILURE(status)) {
1886 errln("Unable to create the SimpleTimeZone(nov-feb): %s", u_errorName(status));
1887 return;
1888 }
1889
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
1899 status);
1900 if (U_FAILURE(status)) {
1901 errln("Unable to create the SimpleTimeZone(nov-feb2): %s", u_errorName(status));
1902 return;
1903 }
1904
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)) {
729e4ab9 1908 dataerrln("Unable to create the UTC calendar: %s", u_errorName(status));
46f4442e
A
1909 return;
1910 }
1911
1912 struct {
1913 // UTC time.
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;
1917 } data[] = {
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 },
1922
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 },
1927
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 },
1932
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 }
1937 };
1938
1939 TimeZone *timezones[] = { &tz1, &tz2 };
1940
1941 TimeZone *tz;
1942 UDate dt;
1943 int32_t t, i, raw, dst;
1944 for (t = 0; t < LENGTHOF(timezones); ++t) {
1945 tz = 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",
1952 t, i,
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;
1956 continue;
1957 }
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",
1961 t, i,
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",
1968 t, i,
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);
1972 }
1973 }
1974 }
1975}
1976void TimeZoneTest::TestCanonicalID() {
1977
1978 // Some canonical IDs in CLDR are defined as "Link"
1979 // in Olson tzdata.
1980 static const struct {
1981 const char *alias;
1982 const char *zone;
1983 } excluded1[] = {
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"},
8421dd3e
A
1987 {"America/Lower_Princes", "America/Curacao"},
1988 {"America/Kralendijk", "America/Curacao"},
46f4442e
A
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"},
2004 {0, 0}
2005 };
2006
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
2012 // below.
2013 static const char* excluded2[] = {
2014 "Etc/UCT", "UCT",
2015 "Etc/UTC", "UTC",
2016 "Etc/Universal", "Universal",
2017 "Etc/Zulu", "Zulu", 0
2018 };
2019
2020 // Walk through equivalency groups
2021 UErrorCode ec = U_ZERO_ERROR;
2022 int32_t s_length, i, j, k;
2023 StringEnumeration* s = TimeZone::createEnumeration();
4388f060
A
2024 if (s == NULL) {
2025 dataerrln("Unable to create TimeZone enumeration");
2026 return;
2027 }
46f4442e
A
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);
2033 if (nEquiv == 0) {
2034 continue;
2035 }
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.");
2046 ec = U_ZERO_ERROR;
2047 continue;
2048 }
2049 // Some exceptional cases
2050 for (k = 0; excluded1[k].alias != 0; k++) {
2051 if (tmpCanonical == excluded1[k].alias) {
2052 tmpCanonical = excluded1[k].zone;
2053 break;
2054 }
2055 }
2056 if (j == 0) {
2057 canonicalID = tmpCanonical;
2058 } else if (canonicalID != tmpCanonical) {
2059 errln("FAIL: getCanonicalID(" + tmp + ") returned " + tmpCanonical + " expected:" + canonicalID);
2060 }
2061
2062 if (canonicalID == tmp) {
2063 bFoundCanonical = TRUE;
2064 }
2065 }
2066 // At least one ID in an equvalency group must match the
2067 // canonicalID
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])) {
2073 isExcluded = TRUE;
2074 break;
2075 }
2076 }
2077 if (isExcluded) {
2078 continue;
2079 }
2080 errln((UnicodeString)"FAIL: No timezone ids match the canonical ID " + canonicalID);
2081 }
2082 }
2083 delete s;
2084
2085 // Testing some special cases
2086 static const struct {
2087 const char *id;
2088 const char *expected;
2089 UBool isSystem;
2090 } data[] = {
729e4ab9
A
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},
46f4442e
A
2097 {"GMT+1:90", 0, FALSE},
2098 {"America/Argentina/Buenos_Aires", "America/Buenos_Aires", TRUE},
4388f060 2099 {"Etc/Unknown", "Etc/Unknown", FALSE},
46f4442e
A
2100 {"bogus", 0, FALSE},
2101 {"", 0, FALSE},
4388f060
A
2102 {"America/Marigot", "America/Marigot", TRUE}, // Olson link, but CLDR canonical (#8953)
2103 {"Europe/Bratislava", "Europe/Bratislava", TRUE}, // Same as above
46f4442e
A
2104 {0, 0, FALSE}
2105 };
2106
2107 UBool isSystemID;
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");
2114 }
2115 ec = U_ZERO_ERROR;
2116 continue;
2117 }
2118 if (canonicalID != data[i].expected) {
729e4ab9 2119 dataerrln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
46f4442e
A
2120 + "\") returned " + canonicalID + " - expected: " + data[i].expected);
2121 }
2122 if (isSystemID != data[i].isSystem) {
729e4ab9 2123 dataerrln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
46f4442e
A
2124 + "\") set " + isSystemID + " to isSystemID");
2125 }
2126 }
2127}
2128
2129//
2130// Test Display Names, choosing zones and lcoales where there are multiple
2131// meta-zones defined.
2132//
2133static struct {
2134 const char *zoneName;
2135 const char *localeName;
2136 UBool summerTime;
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"},
2145
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"},
2150
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"},
2156
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"},
2161
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"},
2167
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
2174 };
2175
2176void TimeZoneTest::TestDisplayNamesMeta() {
2177 UErrorCode status = U_ZERO_ERROR;
2178 GregorianCalendar cal(*TimeZone::getGMT(), status);
729e4ab9 2179 if (failure(status, "GregorianCalendar", TRUE)) return;
46f4442e
A
2180
2181 UBool isReferenceYear = TRUE;
2182 if (cal.get(UCAL_YEAR, status) != TimeZoneTest::REFERENCE_YEAR) {
2183 isReferenceYear = FALSE;
2184 }
2185
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,
2193 locale,
2194 displayName);
2195 if (displayName != zoneDisplayTestData[testNum].expectedDisplayName) {
2196 char name[100];
2197 UErrorCode status = U_ZERO_ERROR;
2198 displayName.extract(name, 100, NULL, status);
2199 if (isReferenceYear) {
2200 sawAnError = TRUE;
729e4ab9 2201 dataerrln("Incorrect time zone display name. zone = \"%s\",\n"
46f4442e
A
2202 " locale = \"%s\", style = %s, Summertime = %d\n"
2203 " Expected \"%s\", "
729e4ab9 2204 " Got \"%s\"\n Error: %s", zoneDisplayTestData[testNum].zoneName,
46f4442e
A
2205 zoneDisplayTestData[testNum].localeName,
2206 zoneDisplayTestData[testNum].style==TimeZone::SHORT ?
2207 "SHORT" : "LONG",
2208 zoneDisplayTestData[testNum].summerTime,
2209 zoneDisplayTestData[testNum].expectedDisplayName,
729e4ab9
A
2210 name,
2211 u_errorName(status));
46f4442e
A
2212 } else {
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 ?
2219 "SHORT" : "LONG",
2220 zoneDisplayTestData[testNum].summerTime,
2221 zoneDisplayTestData[testNum].expectedDisplayName,
2222 name);
2223 }
2224 }
2225 delete zone;
2226 }
2227 if (sawAnError) {
729e4ab9 2228 dataerrln("***Note: Errors could be the result of changes to zoneStrings locale data");
46f4442e
A
2229 }
2230}
2231
4388f060
A
2232void TimeZoneTest::TestGetRegion()
2233{
2234 static const struct {
2235 const char *id;
2236 const char *region;
2237 } data[] = {
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
2242 {"Etc/UTC", "001"},
2243 {"EST5EDT", "001"},
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
2252 {0, 0}
2253 };
2254
2255 int32_t i;
2256 char region[4];
2257 UErrorCode sts;
2258 for (i = 0; data[i].id; i++) {
2259 sts = U_ZERO_ERROR;
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 + "]");
2268 }
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 + "]");
2274 }
2275 } else {
2276 errln((UnicodeString)"Fail: getRegion(\"" + data[i].id
2277 + "\") returns an unexpected error status");
2278 }
2279 }
2280
2281 // Extra test cases for short buffer
2282 int32_t len;
2283 char region2[2];
2284 sts = U_ZERO_ERROR;
2285
2286 len = TimeZone::getRegion("America/New_York", region2, sizeof(region2), sts);
2287 if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
2288 dataerrln("Error calling TimeZone::getRegion");
2289 } else {
2290 if (sts != U_STRING_NOT_TERMINATED_WARNING) {
2291 errln("Expected U_STRING_NOT_TERMINATED_WARNING");
2292 }
2293 if (len != 2) { // length of "US"
2294 errln("Incorrect result length");
2295 }
2296 if (uprv_strncmp(region2, "US", 2) != 0) {
2297 errln("Incorrect result");
2298 }
2299 }
2300
2301 char region1[1];
2302 sts = U_ZERO_ERROR;
2303
2304 len = TimeZone::getRegion("America/Chicago", region1, sizeof(region1), sts);
2305 if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
2306 dataerrln("Error calling TimeZone::getRegion");
2307 } else {
2308 if (sts != U_BUFFER_OVERFLOW_ERROR) {
2309 errln("Expected U_BUFFER_OVERFLOW_ERROR");
2310 }
2311 if (len != 2) { // length of "US"
2312 errln("Incorrect result length");
2313 }
2314 }
2315}
2316
2317void TimeZoneTest::TestGetUnknown() {
2318 const TimeZone &unknown = TimeZone::getUnknown();
2319 UnicodeString expectedID = UNICODE_STRING_SIMPLE("Etc/Unknown");
2320 UnicodeString id;
2321 assertEquals("getUnknown() wrong ID", expectedID, unknown.getID(id));
2322 assertTrue("getUnknown() wrong offset", 0 == unknown.getRawOffset());
2323 assertFalse("getUnknown() uses DST", unknown.useDaylightTime());
2324}
2325
b75a7d8f 2326#endif /* #if !UCONFIG_NO_FORMATTING */