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