+ StringEnumeration* s1 = TimeZone::createEnumeration("US");
+ StringEnumeration* s2 = TimeZone::createEnumeration("US");
+ for(i=0;i<n;++i){
+ const UnicodeString* id1 = s1->snext(ec);
+ if(id1==NULL || U_FAILURE(ec)){
+ errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n,i);
+ }
+ TimeZone* tz1 = TimeZone::createTimeZone(*id1);
+ for(int j=0; j<n;++j){
+ const UnicodeString* id2 = s2->snext(ec);
+ if(id2==NULL || U_FAILURE(ec)){
+ errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n,i);
+ }
+ TimeZone* tz2 = TimeZone::createTimeZone(*id2);
+ if(tz1->hasSameRules(*tz2)){
+ logln("ID1 : " + *id1+" == ID2 : " +*id2);
+ }
+ delete tz2;
+ }
+ delete tz1;
+ }
+ delete s1;
+ delete s2;
+ delete s;
+}
+
+void TimeZoneTest::TestHistorical() {
+ const int32_t H = U_MILLIS_PER_HOUR;
+ struct {
+ const char* id;
+ int32_t time; // epoch seconds
+ int32_t offset; // total offset (millis)
+ } DATA[] = {
+ // Add transition points (before/after) as desired to test historical
+ // behavior.
+ {"America/Los_Angeles", 638963999, -8*H}, // Sun Apr 01 01:59:59 GMT-08:00 1990
+ {"America/Los_Angeles", 638964000, -7*H}, // Sun Apr 01 03:00:00 GMT-07:00 1990
+ {"America/Los_Angeles", 657104399, -7*H}, // Sun Oct 28 01:59:59 GMT-07:00 1990
+ {"America/Los_Angeles", 657104400, -8*H}, // Sun Oct 28 01:00:00 GMT-08:00 1990
+ {"America/Goose_Bay", -116445601, -4*H}, // Sun Apr 24 01:59:59 GMT-04:00 1966
+ {"America/Goose_Bay", -116445600, -3*H}, // Sun Apr 24 03:00:00 GMT-03:00 1966
+ {"America/Goose_Bay", -100119601, -3*H}, // Sun Oct 30 01:59:59 GMT-03:00 1966
+ {"America/Goose_Bay", -100119600, -4*H}, // Sun Oct 30 01:00:00 GMT-04:00 1966
+ {"America/Goose_Bay", -84391201, -4*H}, // Sun Apr 30 01:59:59 GMT-04:00 1967
+ {"America/Goose_Bay", -84391200, -3*H}, // Sun Apr 30 03:00:00 GMT-03:00 1967
+ {"America/Goose_Bay", -68670001, -3*H}, // Sun Oct 29 01:59:59 GMT-03:00 1967
+ {"America/Goose_Bay", -68670000, -4*H}, // Sun Oct 29 01:00:00 GMT-04:00 1967
+ {0, 0, 0}
+ };
+
+ for (int32_t i=0; DATA[i].id!=0; ++i) {
+ const char* id = DATA[i].id;
+ TimeZone *tz = TimeZone::createTimeZone(id);
+ UnicodeString s;
+ if (tz == 0) {
+ errln("FAIL: Cannot create %s", id);
+ } else if (tz->getID(s) != UnicodeString(id)) {
+ dataerrln((UnicodeString)"FAIL: createTimeZone(" + id + ") => " + s);
+ } else {
+ UErrorCode ec = U_ZERO_ERROR;
+ int32_t raw, dst;
+ UDate when = (double) DATA[i].time * U_MILLIS_PER_SECOND;
+ tz->getOffset(when, FALSE, raw, dst, ec);
+ if (U_FAILURE(ec)) {
+ errln("FAIL: getOffset");
+ } else if ((raw+dst) != DATA[i].offset) {
+ errln((UnicodeString)"FAIL: " + DATA[i].id + ".getOffset(" +
+ //when + " = " +
+ dateToString(when) + ") => " +
+ raw + ", " + dst);
+ } else {
+ logln((UnicodeString)"Ok: " + DATA[i].id + ".getOffset(" +
+ //when + " = " +
+ dateToString(when) + ") => " +
+ raw + ", " + dst);
+ }
+ }
+ delete tz;
+ }
+}
+
+void TimeZoneTest::TestEquivalentIDs() {
+ int32_t n = TimeZone::countEquivalentIDs("PST");
+ if (n < 2) {
+ dataerrln((UnicodeString)"FAIL: countEquivalentIDs(PST) = " + n);
+ } else {
+ UBool sawLA = FALSE;
+ for (int32_t i=0; i<n; ++i) {
+ UnicodeString id = TimeZone::getEquivalentID("PST", i);
+ logln((UnicodeString)"" + i + " : " + id);
+ if (id == UnicodeString("America/Los_Angeles")) {
+ sawLA = TRUE;
+ }
+ }
+ if (!sawLA) {
+ errln("FAIL: America/Los_Angeles should be in the list");
+ }
+ }
+}
+
+// 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 < UPRV_LENGTHOF(timezones); ++t) {
+ tz = timezones[t];
+ for (i = 0; i < UPRV_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/Antigua", "America/Port_of_Spain"},
+ {"America/Anguilla", "America/Port_of_Spain"},
+ {"America/Curacao", "America/Aruba"},
+ {"America/Dominica", "America/Port_of_Spain"},
+ {"America/Grenada", "America/Port_of_Spain"},
+ {"America/Guadeloupe", "America/Port_of_Spain"},
+ {"America/Kralendijk", "America/Aruba"},
+ {"America/Lower_Princes", "America/Aruba"},
+ {"America/Marigot", "America/Port_of_Spain"},
+ {"America/Montserrat", "America/Port_of_Spain"},
+ {"America/Panama", "America/Cayman"},
+ {"America/Shiprock", "America/Denver"},
+ {"America/St_Barthelemy", "America/Port_of_Spain"},
+ {"America/St_Kitts", "America/Port_of_Spain"},
+ {"America/St_Lucia", "America/Port_of_Spain"},
+ {"America/St_Thomas", "America/Port_of_Spain"},
+ {"America/St_Vincent", "America/Port_of_Spain"},
+ {"America/Toronto", "America/Montreal"},
+ {"America/Tortola", "America/Port_of_Spain"},
+ {"America/Virgin", "America/Port_of_Spain"},
+ {"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"},
+ {"Pacific/Midway", "Pacific/Pago_Pago"},
+ {"Pacific/Saipan", "Pacific/Guam"},
+ {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);
+ }
+ }