+// Test that a transition at the end of February is handled correctly.
+void TimeZoneTest::TestFebruary() {
+ UErrorCode status = U_ZERO_ERROR;
+
+ // Time zone with daylight savings time from the first Sunday in November
+ // to the last Sunday in February.
+ // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
+ //
+ // Note: In tzdata2007h, the rule had changed, so no actual zones uses
+ // lastSun in Feb anymore.
+ SimpleTimeZone tz1(-3 * U_MILLIS_PER_HOUR, // raw offset: 3h before (west of) GMT
+ UNICODE_STRING("nov-feb", 7),
+ UCAL_NOVEMBER, 1, UCAL_SUNDAY, // start: November, first, Sunday
+ 0, // midnight wall time
+ UCAL_FEBRUARY, -1, UCAL_SUNDAY, // end: February, last, Sunday
+ 0, // midnight wall time
+ status);
+ if (U_FAILURE(status)) {
+ errln("Unable to create the SimpleTimeZone(nov-feb): %s", u_errorName(status));
+ return;
+ }
+
+ // Now hardcode the same rules as for Brazil in tzdata 2006n, so that
+ // we cover the intended code even when in the future zoneinfo hardcodes
+ // these transition dates.
+ SimpleTimeZone tz2(-3 * U_MILLIS_PER_HOUR, // raw offset: 3h before (west of) GMT
+ UNICODE_STRING("nov-feb2", 8),
+ UCAL_NOVEMBER, 1, -UCAL_SUNDAY, // start: November, 1 or after, Sunday
+ 0, // midnight wall time
+ UCAL_FEBRUARY, -29, -UCAL_SUNDAY,// end: February, 29 or before, Sunday
+ 0, // midnight wall time
+ status);
+ if (U_FAILURE(status)) {
+ errln("Unable to create the SimpleTimeZone(nov-feb2): %s", u_errorName(status));
+ return;
+ }
+
+ // Gregorian calendar with the UTC time zone for getting sample test date/times.
+ GregorianCalendar gc(*TimeZone::getGMT(), status);
+ if (U_FAILURE(status)) {
+ dataerrln("Unable to create the UTC calendar: %s", u_errorName(status));
+ return;
+ }
+
+ struct {
+ // UTC time.
+ int32_t year, month, day, hour, minute, second;
+ // Expected time zone offset in hours after GMT (negative=before GMT).
+ int32_t offsetHours;
+ } data[] = {
+ { 2006, UCAL_NOVEMBER, 5, 02, 59, 59, -3 },
+ { 2006, UCAL_NOVEMBER, 5, 03, 00, 00, -2 },
+ { 2007, UCAL_FEBRUARY, 25, 01, 59, 59, -2 },
+ { 2007, UCAL_FEBRUARY, 25, 02, 00, 00, -3 },
+
+ { 2007, UCAL_NOVEMBER, 4, 02, 59, 59, -3 },
+ { 2007, UCAL_NOVEMBER, 4, 03, 00, 00, -2 },
+ { 2008, UCAL_FEBRUARY, 24, 01, 59, 59, -2 },
+ { 2008, UCAL_FEBRUARY, 24, 02, 00, 00, -3 },
+
+ { 2008, UCAL_NOVEMBER, 2, 02, 59, 59, -3 },
+ { 2008, UCAL_NOVEMBER, 2, 03, 00, 00, -2 },
+ { 2009, UCAL_FEBRUARY, 22, 01, 59, 59, -2 },
+ { 2009, UCAL_FEBRUARY, 22, 02, 00, 00, -3 },
+
+ { 2009, UCAL_NOVEMBER, 1, 02, 59, 59, -3 },
+ { 2009, UCAL_NOVEMBER, 1, 03, 00, 00, -2 },
+ { 2010, UCAL_FEBRUARY, 28, 01, 59, 59, -2 },
+ { 2010, UCAL_FEBRUARY, 28, 02, 00, 00, -3 }
+ };
+
+ TimeZone *timezones[] = { &tz1, &tz2 };
+
+ TimeZone *tz;
+ UDate dt;
+ int32_t t, i, raw, dst;
+ for (t = 0; t < LENGTHOF(timezones); ++t) {
+ tz = timezones[t];
+ for (i = 0; i < LENGTHOF(data); ++i) {
+ gc.set(data[i].year, data[i].month, data[i].day,
+ data[i].hour, data[i].minute, data[i].second);
+ dt = gc.getTime(status);
+ if (U_FAILURE(status)) {
+ errln("test case %d.%d: bad date/time %04d-%02d-%02d %02d:%02d:%02d",
+ t, i,
+ data[i].year, data[i].month + 1, data[i].day,
+ data[i].hour, data[i].minute, data[i].second);
+ status = U_ZERO_ERROR;
+ continue;
+ }
+ tz->getOffset(dt, FALSE, raw, dst, status);
+ if (U_FAILURE(status)) {
+ errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) fails: %s",
+ t, i,
+ data[i].year, data[i].month + 1, data[i].day,
+ data[i].hour, data[i].minute, data[i].second,
+ u_errorName(status));
+ status = U_ZERO_ERROR;
+ } else if ((raw + dst) != data[i].offsetHours * U_MILLIS_PER_HOUR) {
+ errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) returns %d+%d != %d",
+ t, i,
+ data[i].year, data[i].month + 1, data[i].day,
+ data[i].hour, data[i].minute, data[i].second,
+ raw, dst, data[i].offsetHours * U_MILLIS_PER_HOUR);
+ }
+ }
+ }
+}
+
+void TimeZoneTest::TestCanonicalIDAPI() {
+ // Bogus input string.
+ UnicodeString bogus;
+ bogus.setToBogus();
+ UnicodeString canonicalID;
+ UErrorCode ec = U_ZERO_ERROR;
+ UnicodeString *pResult = &TimeZone::getCanonicalID(bogus, canonicalID, ec);
+ assertEquals("TimeZone::getCanonicalID(bogus) should fail", U_ILLEGAL_ARGUMENT_ERROR, ec);
+ assertTrue("TimeZone::getCanonicalID(bogus) should return the dest string", pResult == &canonicalID);
+
+ // U_FAILURE on input.
+ UnicodeString berlin("Europe/Berlin");
+ ec = U_MEMORY_ALLOCATION_ERROR;
+ pResult = &TimeZone::getCanonicalID(berlin, canonicalID, ec);
+ assertEquals("TimeZone::getCanonicalID(failure) should fail", U_MEMORY_ALLOCATION_ERROR, ec);
+ assertTrue("TimeZone::getCanonicalID(failure) should return the dest string", pResult == &canonicalID);
+
+ // Valid input should un-bogus the dest string.
+ canonicalID.setToBogus();
+ ec = U_ZERO_ERROR;
+ pResult = &TimeZone::getCanonicalID(berlin, canonicalID, ec);
+ assertSuccess("TimeZone::getCanonicalID(bogus dest) should succeed", ec, TRUE);
+ assertTrue("TimeZone::getCanonicalID(bogus dest) should return the dest string", pResult == &canonicalID);
+ assertFalse("TimeZone::getCanonicalID(bogus dest) should un-bogus the dest string", canonicalID.isBogus());
+ assertEquals("TimeZone::getCanonicalID(bogus dest) unexpected result", canonicalID, berlin, TRUE);
+}
+
+void TimeZoneTest::TestCanonicalID() {
+
+ // Some canonical IDs in CLDR are defined as "Link"
+ // in Olson tzdata.
+ static const struct {
+ const char *alias;
+ const char *zone;
+ } excluded1[] = {
+ {"Africa/Addis_Ababa", "Africa/Nairobi"},
+ {"Africa/Asmera", "Africa/Nairobi"},
+ {"Africa/Bamako", "Africa/Abidjan"},
+ {"Africa/Bangui", "Africa/Lagos"},
+ {"Africa/Banjul", "Africa/Abidjan"},
+ {"Africa/Blantyre", "Africa/Maputo"},
+ {"Africa/Brazzaville", "Africa/Lagos"},
+ {"Africa/Bujumbura", "Africa/Maputo"},
+ {"Africa/Conakry", "Africa/Abidjan"},
+ {"Africa/Dakar", "Africa/Abidjan"},
+ {"Africa/Dar_es_Salaam", "Africa/Nairobi"},
+ {"Africa/Djibouti", "Africa/Nairobi"},
+ {"Africa/Douala", "Africa/Lagos"},
+ {"Africa/Freetown", "Africa/Abidjan"},
+ {"Africa/Gaborone", "Africa/Maputo"},
+ {"Africa/Harare", "Africa/Maputo"},
+ {"Africa/Kampala", "Africa/Nairobi"},
+ {"Africa/Khartoum", "Africa/Juba"},
+ {"Africa/Kigali", "Africa/Maputo"},
+ {"Africa/Kinshasa", "Africa/Lagos"},
+ {"Africa/Libreville", "Africa/Lagos"},
+ {"Africa/Lome", "Africa/Abidjan"},
+ {"Africa/Luanda", "Africa/Lagos"},
+ {"Africa/Lubumbashi", "Africa/Maputo"},
+ {"Africa/Lusaka", "Africa/Maputo"},
+ {"Africa/Maseru", "Africa/Johannesburg"},
+ {"Africa/Malabo", "Africa/Lagos"},
+ {"Africa/Mbabane", "Africa/Johannesburg"},
+ {"Africa/Mogadishu", "Africa/Nairobi"},
+ {"Africa/Niamey", "Africa/Lagos"},
+ {"Africa/Nouakchott", "Africa/Abidjan"},
+ {"Africa/Ouagadougou", "Africa/Abidjan"},
+ {"Africa/Porto-Novo", "Africa/Lagos"},
+ {"Africa/Sao_Tome", "Africa/Abidjan"},
+ {"America/Curacao", "America/Aruba"},
+ {"America/Dominica", "America/Anguilla"},
+ {"America/Grenada", "America/Anguilla"},
+ {"America/Guadeloupe", "America/Anguilla"},
+ {"America/Kralendijk", "America/Aruba"},
+ {"America/Lower_Princes", "America/Aruba"},
+ {"America/Marigot", "America/Anguilla"},
+ {"America/Montserrat", "America/Anguilla"},
+ {"America/Port_of_Spain", "America/Anguilla"},
+ {"America/Shiprock", "America/Denver"}, // America/Shiprock is defined as a Link to America/Denver in tzdata
+ {"America/St_Barthelemy", "America/Anguilla"},
+ {"America/St_Kitts", "America/Anguilla"},
+ {"America/St_Lucia", "America/Anguilla"},
+ {"America/St_Thomas", "America/Anguilla"},
+ {"America/St_Vincent", "America/Anguilla"},
+ {"America/Tortola", "America/Anguilla"},
+ {"America/Virgin", "America/Anguilla"},
+ {"Antarctica/South_Pole", "Antarctica/McMurdo"},
+ {"Arctic/Longyearbyen", "Europe/Oslo"},
+ {"Asia/Kuwait", "Asia/Aden"},
+ {"Asia/Muscat", "Asia/Dubai"},
+ {"Asia/Phnom_Penh", "Asia/Bangkok"},
+ {"Asia/Qatar", "Asia/Bahrain"},
+ {"Asia/Riyadh", "Asia/Aden"},
+ {"Asia/Vientiane", "Asia/Bangkok"},
+ {"Atlantic/Jan_Mayen", "Europe/Oslo"},
+ {"Atlantic/St_Helena", "Africa/Abidjan"},
+ {"Europe/Bratislava", "Europe/Prague"},
+ {"Europe/Busingen", "Europe/Zurich"},
+ {"Europe/Guernsey", "Europe/London"},
+ {"Europe/Isle_of_Man", "Europe/London"},
+ {"Europe/Jersey", "Europe/London"},
+ {"Europe/Ljubljana", "Europe/Belgrade"},
+ {"Europe/Mariehamn", "Europe/Helsinki"},
+ {"Europe/Podgorica", "Europe/Belgrade"},
+ {"Europe/San_Marino", "Europe/Rome"},
+ {"Europe/Sarajevo", "Europe/Belgrade"},
+ {"Europe/Skopje", "Europe/Belgrade"},
+ {"Europe/Vaduz", "Europe/Zurich"},
+ {"Europe/Vatican", "Europe/Rome"},
+ {"Europe/Zagreb", "Europe/Belgrade"},
+ {"Indian/Antananarivo", "Africa/Nairobi"},
+ {"Indian/Comoro", "Africa/Nairobi"},
+ {"Indian/Mayotte", "Africa/Nairobi"},
+ {"Pacific/Auckland", "Antarctica/McMurdo"},
+ {"Pacific/Johnston", "Pacific/Honolulu"},
+ {0, 0}
+ };
+
+ // Following IDs are aliases of Etc/GMT in CLDR,
+ // but Olson tzdata has 3 independent definitions
+ // for Etc/GMT, Etc/UTC, Etc/UCT.
+ // Until we merge them into one equivalent group
+ // in zoneinfo.res, we exclude them in the test
+ // below.
+ static const char* excluded2[] = {
+ "Etc/UCT", "UCT",
+ "Etc/UTC", "UTC",
+ "Etc/Universal", "Universal",
+ "Etc/Zulu", "Zulu", 0
+ };
+
+ // Walk through equivalency groups
+ UErrorCode ec = U_ZERO_ERROR;
+ int32_t s_length, i, j, k;
+ StringEnumeration* s = TimeZone::createEnumeration();
+ if (s == NULL) {
+ dataerrln("Unable to create TimeZone enumeration");
+ return;
+ }
+ UnicodeString canonicalID, tmpCanonical;
+ s_length = s->count(ec);
+ for (i = 0; i < s_length;++i) {
+ const UnicodeString *tzid = s->snext(ec);
+ int32_t nEquiv = TimeZone::countEquivalentIDs(*tzid);
+ if (nEquiv == 0) {
+ continue;
+ }
+ UBool bFoundCanonical = FALSE;
+ // Make sure getCanonicalID returns the exact same result
+ // for all entries within a same equivalency group with some
+ // exceptions listed in exluded1.
+ // Also, one of them must be canonical id.
+ for (j = 0; j < nEquiv; j++) {
+ UnicodeString tmp = TimeZone::getEquivalentID(*tzid, j);
+ TimeZone::getCanonicalID(tmp, tmpCanonical, ec);
+ if (U_FAILURE(ec)) {
+ errln((UnicodeString)"FAIL: getCanonicalID(" + tmp + ") failed.");
+ ec = U_ZERO_ERROR;
+ continue;
+ }
+ // Some exceptional cases
+ for (k = 0; excluded1[k].alias != 0; k++) {
+ if (tmpCanonical == excluded1[k].alias) {
+ tmpCanonical = excluded1[k].zone;
+ break;
+ }
+ }
+ if (j == 0) {
+ canonicalID = tmpCanonical;
+ } else if (canonicalID != tmpCanonical) {
+ errln("FAIL: getCanonicalID(" + tmp + ") returned " + tmpCanonical + " expected:" + canonicalID);
+ }
+
+ if (canonicalID == tmp) {
+ bFoundCanonical = TRUE;
+ }
+ }
+ // At least one ID in an equvalency group must match the
+ // canonicalID
+ if (bFoundCanonical == FALSE) {
+ // test exclusion because of differences between Olson tzdata and CLDR
+ UBool isExcluded = FALSE;
+ for (k = 0; excluded2[k] != 0; k++) {
+ if (*tzid == UnicodeString(excluded2[k])) {
+ isExcluded = TRUE;
+ break;
+ }
+ }
+ if (isExcluded) {
+ continue;
+ }
+ errln((UnicodeString)"FAIL: No timezone ids match the canonical ID " + canonicalID);
+ }
+ }
+ delete s;
+
+ // Testing some special cases
+ static const struct {
+ const char *id;
+ const char *expected;
+ UBool isSystem;
+ } data[] = {
+ {"GMT-03", "GMT-03:00", FALSE},
+ {"GMT+4", "GMT+04:00", FALSE},
+ {"GMT-055", "GMT-00:55", FALSE},
+ {"GMT+430", "GMT+04:30", FALSE},
+ {"GMT-12:15", "GMT-12:15", FALSE},
+ {"GMT-091015", "GMT-09:10:15", FALSE},
+ {"GMT+1:90", 0, FALSE},
+ {"America/Argentina/Buenos_Aires", "America/Buenos_Aires", TRUE},
+ {"Etc/Unknown", "Etc/Unknown", FALSE},
+ {"bogus", 0, FALSE},
+ {"", 0, FALSE},
+ {"America/Marigot", "America/Marigot", TRUE}, // Olson link, but CLDR canonical (#8953)
+ {"Europe/Bratislava", "Europe/Bratislava", TRUE}, // Same as above
+ {0, 0, FALSE}
+ };
+
+ UBool isSystemID;
+ for (i = 0; data[i].id != 0; i++) {
+ TimeZone::getCanonicalID(UnicodeString(data[i].id), canonicalID, isSystemID, ec);
+ if (U_FAILURE(ec)) {
+ if (ec != U_ILLEGAL_ARGUMENT_ERROR || data[i].expected != 0) {
+ errln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
+ + "\") returned status U_ILLEGAL_ARGUMENT_ERROR");
+ }
+ ec = U_ZERO_ERROR;
+ continue;
+ }
+ if (canonicalID != data[i].expected) {
+ dataerrln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
+ + "\") returned " + canonicalID + " - expected: " + data[i].expected);
+ }
+ if (isSystemID != data[i].isSystem) {
+ dataerrln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
+ + "\") set " + isSystemID + " to isSystemID");
+ }
+ }
+}
+
+//
+// Test Display Names, choosing zones and lcoales where there are multiple
+// meta-zones defined.
+//
+static struct {
+ const char *zoneName;
+ const char *localeName;
+ UBool summerTime;
+ TimeZone::EDisplayType style;
+ const char *expectedDisplayName; }
+ zoneDisplayTestData [] = {
+ // zone id locale summer format expected display name
+ {"Europe/London", "en", FALSE, TimeZone::SHORT, "GMT"},
+ {"Europe/London", "en", FALSE, TimeZone::LONG, "Greenwich Mean Time"},
+ {"Europe/London", "en", TRUE, TimeZone::SHORT, "GMT+1" /*"BST"*/},
+ {"Europe/London", "en", TRUE, TimeZone::LONG, "British Summer Time"},
+
+ {"America/Anchorage", "en", FALSE, TimeZone::SHORT, "AKST"},
+ {"America/Anchorage", "en", FALSE, TimeZone::LONG, "Alaska Standard Time"},
+ {"America/Anchorage", "en", TRUE, TimeZone::SHORT, "AKDT"},
+ {"America/Anchorage", "en", TRUE, TimeZone::LONG, "Alaska Daylight Time"},
+
+ // Southern Hemisphere, all data from meta:Australia_Western
+ {"Australia/Perth", "en", FALSE, TimeZone::SHORT, "GMT+8"/*"AWST"*/},
+ {"Australia/Perth", "en", FALSE, TimeZone::LONG, "Australian Western Standard Time"},
+ // Note: Perth does not observe DST currently. When display name is missing,
+ // the localized GMT format with the current offset is used even daylight name was
+ // requested. See #9350.
+ {"Australia/Perth", "en", TRUE, TimeZone::SHORT, "GMT+8"/*"AWDT"*/},
+ {"Australia/Perth", "en", TRUE, TimeZone::LONG, "Australian Western Daylight Time"},
+
+ {"America/Sao_Paulo", "en", FALSE, TimeZone::SHORT, "GMT-3"/*"BRT"*/},
+ {"America/Sao_Paulo", "en", FALSE, TimeZone::LONG, "Brasilia Standard Time"},
+ {"America/Sao_Paulo", "en", TRUE, TimeZone::SHORT, "GMT-2"/*"BRST"*/},
+ {"America/Sao_Paulo", "en", TRUE, TimeZone::LONG, "Brasilia Summer Time"},
+
+ // No Summer Time, but had it before 1983.
+ {"Pacific/Honolulu", "en", FALSE, TimeZone::SHORT, "HST"},
+ {"Pacific/Honolulu", "en", FALSE, TimeZone::LONG, "Hawaii-Aleutian Standard Time"},
+ {"Pacific/Honolulu", "en", TRUE, TimeZone::SHORT, "HDT"},
+ {"Pacific/Honolulu", "en", TRUE, TimeZone::LONG, "Hawaii-Aleutian Daylight Time"},
+
+ // Northern, has Summer, not commonly used.
+ {"Europe/Helsinki", "en", FALSE, TimeZone::SHORT, "GMT+2"/*"EET"*/},
+ {"Europe/Helsinki", "en", FALSE, TimeZone::LONG, "Eastern European Standard Time"},
+ {"Europe/Helsinki", "en", TRUE, TimeZone::SHORT, "GMT+3"/*"EEST"*/},
+ {"Europe/Helsinki", "en", TRUE, TimeZone::LONG, "Eastern European Summer Time"},
+
+ // Repeating the test data for DST. The test data below trigger the problem reported
+ // by Ticket#6644
+ {"Europe/London", "en", TRUE, TimeZone::SHORT, "GMT+1" /*"BST"*/},
+ {"Europe/London", "en", TRUE, TimeZone::LONG, "British Summer Time"},
+
+ {NULL, NULL, FALSE, TimeZone::SHORT, NULL} // NULL values terminate list
+ };
+
+void TimeZoneTest::TestDisplayNamesMeta() {
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar cal(*TimeZone::getGMT(), status);
+ if (failure(status, "GregorianCalendar", TRUE)) return;
+
+ UBool sawAnError = FALSE;
+ for (int testNum = 0; zoneDisplayTestData[testNum].zoneName != NULL; testNum++) {
+ Locale locale = Locale::createFromName(zoneDisplayTestData[testNum].localeName);
+ TimeZone *zone = TimeZone::createTimeZone(zoneDisplayTestData[testNum].zoneName);
+ UnicodeString displayName;
+ zone->getDisplayName(zoneDisplayTestData[testNum].summerTime,
+ zoneDisplayTestData[testNum].style,
+ locale,
+ displayName);
+ if (displayName != zoneDisplayTestData[testNum].expectedDisplayName) {
+ char name[100];
+ UErrorCode status = U_ZERO_ERROR;
+ displayName.extract(name, 100, NULL, status);
+ if (isDevelopmentBuild) {
+ sawAnError = TRUE;
+ dataerrln("Incorrect time zone display name. zone = \"%s\",\n"
+ " locale = \"%s\", style = %s, Summertime = %d\n"
+ " Expected \"%s\", "
+ " Got \"%s\"\n Error: %s", zoneDisplayTestData[testNum].zoneName,
+ zoneDisplayTestData[testNum].localeName,
+ zoneDisplayTestData[testNum].style==TimeZone::SHORT ?
+ "SHORT" : "LONG",
+ zoneDisplayTestData[testNum].summerTime,
+ zoneDisplayTestData[testNum].expectedDisplayName,
+ name,
+ u_errorName(status));
+ } else {
+ logln("Incorrect time zone display name. zone = \"%s\",\n"
+ " locale = \"%s\", style = %s, Summertime = %d\n"
+ " Expected \"%s\", "
+ " Got \"%s\"\n", zoneDisplayTestData[testNum].zoneName,
+ zoneDisplayTestData[testNum].localeName,
+ zoneDisplayTestData[testNum].style==TimeZone::SHORT ?
+ "SHORT" : "LONG",
+ zoneDisplayTestData[testNum].summerTime,
+ zoneDisplayTestData[testNum].expectedDisplayName,
+ name);
+ }
+ }
+ delete zone;
+ }
+ if (sawAnError) {
+ dataerrln("***Note: Errors could be the result of changes to zoneStrings locale data");
+ }
+}
+
+void TimeZoneTest::TestGetRegion()
+{
+ static const struct {
+ const char *id;
+ const char *region;
+ } data[] = {
+ {"America/Los_Angeles", "US"},
+ {"America/Indianapolis", "US"}, // CLDR canonical, Olson backward
+ {"America/Indiana/Indianapolis", "US"}, // CLDR alias
+ {"Mexico/General", "MX"}, // Link America/Mexico_City, Olson backward
+ {"Etc/UTC", "001"},
+ {"EST5EDT", "001"},
+ {"PST", "US"}, // Link America/Los_Angeles
+ {"Europe/Helsinki", "FI"},
+ {"Europe/Mariehamn", "AX"}, // Link Europe/Helsinki, but in zone.tab
+ {"Asia/Riyadh", "SA"},
+ // tz file solar87 was removed from tzdata2013i
+ // {"Asia/Riyadh87", "001"}, // this should be "SA" actually, but not in zone.tab
+ {"Etc/Unknown", 0}, // CLDR canonical, but not a sysmte zone ID
+ {"bogus", 0}, // bogus
+ {"GMT+08:00", 0}, // a custom ID, not a system zone ID
+ {0, 0}
+ };
+
+ int32_t i;
+ char region[4];
+ UErrorCode sts;
+ for (i = 0; data[i].id; i++) {
+ sts = U_ZERO_ERROR;
+ TimeZone::getRegion(data[i].id, region, sizeof(region), sts);
+ if (U_SUCCESS(sts)) {
+ if (data[i].region == 0) {
+ errln((UnicodeString)"Fail: getRegion(\"" + data[i].id + "\") returns "
+ + region + " [expected: U_ILLEGAL_ARGUMENT_ERROR]");
+ } else if (uprv_strcmp(region, data[i].region) != 0) {
+ errln((UnicodeString)"Fail: getRegion(\"" + data[i].id + "\") returns "
+ + region + " [expected: " + data[i].region + "]");
+ }
+ } else if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
+ if (data[i].region != 0) {
+ dataerrln((UnicodeString)"Fail: getRegion(\"" + data[i].id
+ + "\") returns error status U_ILLEGAL_ARGUMENT_ERROR [expected: "
+ + data[i].region + "]");
+ }
+ } else {
+ errln((UnicodeString)"Fail: getRegion(\"" + data[i].id
+ + "\") returns an unexpected error status");
+ }
+ }
+
+ // Extra test cases for short buffer
+ int32_t len;
+ char region2[2];
+ sts = U_ZERO_ERROR;
+
+ len = TimeZone::getRegion("America/New_York", region2, sizeof(region2), sts);
+ if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
+ dataerrln("Error calling TimeZone::getRegion");
+ } else {
+ if (sts != U_STRING_NOT_TERMINATED_WARNING) {
+ errln("Expected U_STRING_NOT_TERMINATED_WARNING");
+ }
+ if (len != 2) { // length of "US"
+ errln("Incorrect result length");
+ }
+ if (uprv_strncmp(region2, "US", 2) != 0) {
+ errln("Incorrect result");
+ }
+ }
+
+ char region1[1];
+ sts = U_ZERO_ERROR;
+
+ len = TimeZone::getRegion("America/Chicago", region1, sizeof(region1), sts);
+ if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
+ dataerrln("Error calling TimeZone::getRegion");
+ } else {
+ if (sts != U_BUFFER_OVERFLOW_ERROR) {
+ errln("Expected U_BUFFER_OVERFLOW_ERROR");
+ }
+ if (len != 2) { // length of "US"
+ errln("Incorrect result length");
+ }
+ }
+}
+
+void TimeZoneTest::TestGetUnknown() {
+ const TimeZone &unknown = TimeZone::getUnknown();
+ UnicodeString expectedID = UNICODE_STRING_SIMPLE("Etc/Unknown");
+ UnicodeString id;
+ assertEquals("getUnknown() wrong ID", expectedID, unknown.getID(id));
+ assertTrue("getUnknown() wrong offset", 0 == unknown.getRawOffset());
+ assertFalse("getUnknown() uses DST", unknown.useDaylightTime());
+}
+
+void TimeZoneTest::TestGetWindowsID(void) {
+ static const struct {
+ const char *id;
+ const char *winid;
+ } TESTDATA[] = {
+ {"America/New_York", "Eastern Standard Time"},
+ {"America/Montreal", "Eastern Standard Time"},
+ {"America/Los_Angeles", "Pacific Standard Time"},
+ {"America/Vancouver", "Pacific Standard Time"},
+ {"Asia/Shanghai", "China Standard Time"},
+ {"Asia/Chongqing", "China Standard Time"},
+ {"America/Indianapolis", "US Eastern Standard Time"}, // CLDR canonical name
+ {"America/Indiana/Indianapolis", "US Eastern Standard Time"}, // tzdb canonical name
+ {"Asia/Khandyga", "Yakutsk Standard Time"},
+ {"Australia/Eucla", ""}, // No Windows ID mapping
+ {"Bogus", ""},
+ {0, 0},
+ };
+
+ for (int32_t i = 0; TESTDATA[i].id != 0; i++) {
+ UErrorCode sts = U_ZERO_ERROR;
+ UnicodeString windowsID;
+
+ TimeZone::getWindowsID(UnicodeString(TESTDATA[i].id), windowsID, sts);
+ assertSuccess(TESTDATA[i].id, sts);
+ assertEquals(TESTDATA[i].id, UnicodeString(TESTDATA[i].winid), windowsID, TRUE);
+ }
+}
+
+void TimeZoneTest::TestGetIDForWindowsID(void) {
+ static const struct {
+ const char *winid;
+ const char *region;
+ const char *id;
+ } TESTDATA[] = {
+ {"Eastern Standard Time", 0, "America/New_York"},
+ {"Eastern Standard Time", "US", "America/New_York"},
+ {"Eastern Standard Time", "CA", "America/Toronto"},
+ {"Eastern Standard Time", "CN", "America/New_York"},
+ {"China Standard Time", 0, "Asia/Shanghai"},
+ {"China Standard Time", "CN", "Asia/Shanghai"},
+ {"China Standard Time", "HK", "Asia/Hong_Kong"},
+ {"Mid-Atlantic Standard Time", 0, ""}, // No tz database mapping
+ {"Bogus", 0, ""},
+ {0, 0, 0},
+ };
+
+ for (int32_t i = 0; TESTDATA[i].winid != 0; i++) {
+ UErrorCode sts = U_ZERO_ERROR;
+ UnicodeString id;
+
+ TimeZone::getIDForWindowsID(UnicodeString(TESTDATA[i].winid), TESTDATA[i].region,
+ id, sts);
+ assertSuccess(UnicodeString(TESTDATA[i].winid) + "/" + TESTDATA[i].region, sts);
+ assertEquals(UnicodeString(TESTDATA[i].winid) + "/" + TESTDATA[i].region, TESTDATA[i].id, id, TRUE);
+ }
+}
+