* Test equivalency between OlsonTimeZone and custom RBTZ representing the
* equivalent rules in a certain time range
*/
public void TestHistoricalRuleBasedTimeZone() {
// Compare to America/New_York with equivalent RBTZ
TimeZone ny = TimeZone.getTimeZone("America/New_York", TimeZone.TIMEZONE_ICU);
//RBTZ
InitialTimeZoneRule ir = new InitialTimeZoneRule("EST", -5*HOUR, 0);
RuleBasedTimeZone rbtz = new RuleBasedTimeZone("EST5EDT", ir);
DateTimeRule dtr;
AnnualTimeZoneRule tzr;
// Standard time
dtr = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2*HOUR, DateTimeRule.WALL_TIME); // Last Sunday in October, at 2AM wall time
tzr = new AnnualTimeZoneRule("EST", -5*HOUR /* rawOffset */, 0 /* dstSavings */, dtr, 1967, 2006);
rbtz.addTransitionRule(tzr);
dtr = new DateTimeRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY,
true, 2*HOUR, DateTimeRule.WALL_TIME); // SUN>=1 in November, at 2AM wall time
tzr = new AnnualTimeZoneRule("EST", -5*HOUR, 0, dtr, 2007, AnnualTimeZoneRule.MAX_YEAR);
rbtz.addTransitionRule(tzr);
// Daylight saving time
dtr = new DateTimeRule(Calendar.APRIL, -1, Calendar.SUNDAY,
2*HOUR, DateTimeRule.WALL_TIME); // Last Sunday in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1967, 1973);
rbtz.addTransitionRule(tzr);
dtr = new DateTimeRule(Calendar.JANUARY, 6,
2*HOUR, DateTimeRule.WALL_TIME); // January 6, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1974, 1974);
rbtz.addTransitionRule(tzr);
dtr = new DateTimeRule(Calendar.FEBRUARY, 23,
2*HOUR, DateTimeRule.WALL_TIME); // February 23, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1975, 1975);
rbtz.addTransitionRule(tzr);
dtr = new DateTimeRule(Calendar.APRIL, -1, Calendar.SUNDAY,
2*HOUR, DateTimeRule.WALL_TIME); // Last Sunday in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1976, 1986);
rbtz.addTransitionRule(tzr);
dtr = new DateTimeRule(Calendar.APRIL, 1, Calendar.SUNDAY,
true, 2*HOUR, DateTimeRule.WALL_TIME); // SUN>=1 in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1987, 2006);
rbtz.addTransitionRule(tzr);
dtr = new DateTimeRule(Calendar.MARCH, 8, Calendar.SUNDAY,
true, 2*HOUR, DateTimeRule.WALL_TIME); // SUN>=8 in March, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 2007, AnnualTimeZoneRule.MAX_YEAR);
rbtz.addTransitionRule(tzr);
// hasEquivalentTransitions
long jan1_1950 = getUTCMillis(1950, Calendar.JANUARY, 1);
long jan1_1967 = getUTCMillis(1971, Calendar.JANUARY, 1);
long jan1_2010 = getUTCMillis(2010, Calendar.JANUARY, 1);
if (!(((BasicTimeZone)ny).hasEquivalentTransitions(rbtz, jan1_1967, jan1_2010))) {
errln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
}
if (((BasicTimeZone)ny).hasEquivalentTransitions(rbtz, jan1_1950, jan1_2010)) {
errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
}
// Same with above, but calling RBTZ#hasEquivalentTransitions against OlsonTimeZone
if (!rbtz.hasEquivalentTransitions(ny, jan1_1967, jan1_2010)) {
errln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
}
if (rbtz.hasEquivalentTransitions(ny, jan1_1950, jan1_2010)) {
errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
}
// TimeZone APIs
if (ny.hasSameRules(rbtz) || rbtz.hasSameRules(ny)) {
errln("FAIL: hasSameRules must return false");
}
RuleBasedTimeZone rbtzc = (RuleBasedTimeZone)rbtz.clone();
if (!rbtz.hasSameRules(rbtzc) || !rbtz.hasEquivalentTransitions(rbtzc, jan1_1950, jan1_2010)) {
errln("FAIL: hasSameRules/hasEquivalentTransitions must return true for cloned RBTZs");
}
long times[] = {
getUTCMillis(2006, Calendar.MARCH, 15),
getUTCMillis(2006, Calendar.NOVEMBER, 1),
getUTCMillis(2007, Calendar.MARCH, 15),
getUTCMillis(2007, Calendar.NOVEMBER, 1),
getUTCMillis(2008, Calendar.MARCH, 15),
getUTCMillis(2008, Calendar.NOVEMBER, 1)
};
int[] offsets1 = new int[2];
int[] offsets2 = new int[2];
for (int i = 0; i < times.length; i++) {
// Check getOffset - must return the same results for these time data
rbtz.getOffset(times[i], false, offsets1);
ny.getOffset(times[i], false, offsets2);
if (offsets1[0] != offsets2[0] || offsets1[1] != offsets2[1]) {
errln("FAIL: Incompatible time zone offsets for ny and rbtz");
}
// Check inDaylightTime
Date d = new Date(times[i]);
if (rbtz.inDaylightTime(d) != ny.inDaylightTime(d)) {
errln("FAIL: Incompatible daylight saving time for ny and rbtz");
}
}
}