use of android.icu.util.AnnualTimeZoneRule in project j2objc by google.
the class TimeZoneRuleTest method TestTimeZoneRuleCoverage.
/*
* API coverage tests for TimeZoneRule
*/
@Test
public void TestTimeZoneRuleCoverage() {
long time1 = getUTCMillis(2005, Calendar.JULY, 4);
long time2 = getUTCMillis(2015, Calendar.JULY, 4);
long time3 = getUTCMillis(1950, Calendar.JULY, 4);
DateTimeRule dtr1 = new DateTimeRule(Calendar.FEBRUARY, 29, Calendar.SUNDAY, false, 3 * HOUR, // Last Sunday on or before Feb 29, at 3 AM, wall time
DateTimeRule.WALL_TIME);
DateTimeRule dtr2 = new DateTimeRule(Calendar.MARCH, 11, 2 * HOUR, // Mar 11, at 2 AM, standard time
DateTimeRule.STANDARD_TIME);
DateTimeRule dtr3 = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SATURDAY, 6 * HOUR, // Last Saturday in Oct, at 6 AM, UTC
DateTimeRule.UTC_TIME);
DateTimeRule dtr4 = new DateTimeRule(Calendar.MARCH, 8, Calendar.SUNDAY, true, 2 * HOUR, // First Sunday on or after Mar 8, at 2 AM, wall time
DateTimeRule.WALL_TIME);
AnnualTimeZoneRule a1 = new AnnualTimeZoneRule("a1", -3 * HOUR, 1 * HOUR, dtr1, 2000, AnnualTimeZoneRule.MAX_YEAR);
AnnualTimeZoneRule a2 = new AnnualTimeZoneRule("a2", -3 * HOUR, 1 * HOUR, dtr1, 2000, AnnualTimeZoneRule.MAX_YEAR);
AnnualTimeZoneRule a3 = new AnnualTimeZoneRule("a3", -3 * HOUR, 1 * HOUR, dtr1, 2000, 2010);
InitialTimeZoneRule i1 = new InitialTimeZoneRule("i1", -3 * HOUR, 0);
InitialTimeZoneRule i2 = new InitialTimeZoneRule("i2", -3 * HOUR, 0);
InitialTimeZoneRule i3 = new InitialTimeZoneRule("i3", -3 * HOUR, 1 * HOUR);
long[] emptytimes = {};
long[] trtimes1 = { 0 };
long[] trtimes2 = { 0, 10000000 };
TimeArrayTimeZoneRule t0 = null;
try {
// Try to construct TimeArrayTimeZoneRule with null transition times
t0 = new TimeArrayTimeZoneRule("nulltimes", -3 * HOUR, 0, null, DateTimeRule.UTC_TIME);
} catch (IllegalArgumentException iae) {
logln("TimeArrayTimeZoneRule constructor throws IllegalArgumentException as expected.");
t0 = null;
}
if (t0 != null) {
errln("FAIL: TimeArrayTimeZoneRule constructor did not throw IllegalArgumentException for null times");
}
try {
// Try to construct TimeArrayTimeZoneRule with empty transition times
t0 = new TimeArrayTimeZoneRule("nulltimes", -3 * HOUR, 0, emptytimes, DateTimeRule.UTC_TIME);
} catch (IllegalArgumentException iae) {
logln("TimeArrayTimeZoneRule constructor throws IllegalArgumentException as expected.");
t0 = null;
}
if (t0 != null) {
errln("FAIL: TimeArrayTimeZoneRule constructor did not throw IllegalArgumentException for empty times");
}
TimeArrayTimeZoneRule t1 = new TimeArrayTimeZoneRule("t1", -3 * HOUR, 0, trtimes1, DateTimeRule.UTC_TIME);
TimeArrayTimeZoneRule t2 = new TimeArrayTimeZoneRule("t2", -3 * HOUR, 0, trtimes1, DateTimeRule.UTC_TIME);
TimeArrayTimeZoneRule t3 = new TimeArrayTimeZoneRule("t3", -3 * HOUR, 0, trtimes2, DateTimeRule.UTC_TIME);
TimeArrayTimeZoneRule t4 = new TimeArrayTimeZoneRule("t4", -3 * HOUR, 0, trtimes1, DateTimeRule.STANDARD_TIME);
TimeArrayTimeZoneRule t5 = new TimeArrayTimeZoneRule("t5", -4 * HOUR, 1 * HOUR, trtimes1, DateTimeRule.WALL_TIME);
// AnnualTimeZoneRule#getRule
if (!a1.getRule().equals(a2.getRule())) {
errln("FAIL: The same DateTimeRule must be returned from AnnualTimeZoneRule a1 and a2");
}
// AnnualTimeZoneRule#getStartYear
int startYear = a1.getStartYear();
if (startYear != 2000) {
errln("FAIL: The start year of AnnualTimeZoneRule a1 must be 2000 - returned: " + startYear);
}
// AnnualTimeZoneRule#getEndYear
int endYear = a1.getEndYear();
if (endYear != AnnualTimeZoneRule.MAX_YEAR) {
errln("FAIL: The start year of AnnualTimeZoneRule a1 must be MAX_YEAR - returned: " + endYear);
}
endYear = a3.getEndYear();
if (endYear != 2010) {
errln("FAIL: The start year of AnnualTimeZoneRule a3 must be 2010 - returned: " + endYear);
}
// AnnualTimeZone#getStartInYear
Date d1 = a1.getStartInYear(2005, -3 * HOUR, 0);
Date d2 = a3.getStartInYear(2005, -3 * HOUR, 0);
if (d1 == null || d2 == null || !d1.equals(d2)) {
errln("FAIL: AnnualTimeZoneRule#getStartInYear did not work as expected");
}
d2 = a3.getStartInYear(2015, -3 * HOUR, 0);
if (d2 != null) {
errln("FAIL: AnnualTimeZoneRule#getSTartInYear returned non-null date for 2015 which is out of rule range");
}
// AnnualTimeZone#getFirstStart
d1 = a1.getFirstStart(-3 * HOUR, 0);
d2 = a1.getFirstStart(-4 * HOUR, 1 * HOUR);
if (d1 == null || d2 == null || !d1.equals(d2)) {
errln("FAIL: The same start time should be returned by getFirstStart");
}
// AnnualTimeZone#getFinalStart
d1 = a1.getFinalStart(-3 * HOUR, 0);
if (d1 != null) {
errln("FAIL: Non-null Date is returned by getFinalStart for a1");
}
d1 = a1.getStartInYear(2010, -3 * HOUR, 0);
d2 = a3.getFinalStart(-3 * HOUR, 0);
if (d1 == null || d2 == null || !d1.equals(d2)) {
errln("FAIL: Bad date is returned by getFinalStart");
}
// AnnualTimeZone#getNextStart / getPreviousStart
d1 = a1.getNextStart(time1, -3 * HOUR, 0, false);
if (d1 == null) {
errln("FAIL: Null Date is returned by getNextStart");
} else {
d2 = a1.getPreviousStart(d1.getTime(), -3 * HOUR, 0, true);
if (d2 == null || !d1.equals(d2)) {
errln("FAIL: Bad Date is returned by getPreviousStart");
}
}
d1 = a3.getNextStart(time2, -3 * HOUR, 0, false);
if (d1 != null) {
errln("FAIL: getNextStart must return null when no start time is available after the base time");
}
d1 = a3.getFinalStart(-3 * HOUR, 0);
d2 = a3.getPreviousStart(time2, -3 * HOUR, 0, false);
if (d1 == null || d2 == null || !d1.equals(d2)) {
errln("FAIL: getPreviousStart does not match with getFinalStart after the end year");
}
// AnnualTimeZone#isEquavalentTo
if (!a1.isEquivalentTo(a2)) {
errln("FAIL: AnnualTimeZoneRule a1 is equivalent to a2, but returned false");
}
if (a1.isEquivalentTo(a3)) {
errln("FAIL: AnnualTimeZoneRule a1 is not equivalent to a3, but returned true");
}
if (!a1.isEquivalentTo(a1)) {
errln("FAIL: AnnualTimeZoneRule a1 is equivalent to itself, but returned false");
}
if (a1.isEquivalentTo(t1)) {
errln("FAIL: AnnualTimeZoneRule is not equivalent to TimeArrayTimeZoneRule, but returned true");
}
// AnnualTimeZone#isTransitionRule
if (!a1.isTransitionRule()) {
errln("FAIL: An AnnualTimeZoneRule is a transition rule, but returned false");
}
// AnnualTimeZone#toString
String str = a1.toString();
if (str == null || str.length() == 0) {
errln("FAIL: AnnualTimeZoneRule#toString for a1 returns null or empty string");
} else {
logln("AnnualTimeZoneRule a1 : " + str);
}
str = a3.toString();
if (str == null || str.length() == 0) {
errln("FAIL: AnnualTimeZoneRule#toString for a3 returns null or empty string");
} else {
logln("AnnualTimeZoneRule a3 : " + str);
}
// InitialTimeZoneRule#isEquivalentRule
if (!i1.isEquivalentTo(i2)) {
errln("FAIL: InitialTimeZoneRule i1 is equivalent to i2, but returned false");
}
if (i1.isEquivalentTo(i3)) {
errln("FAIL: InitialTimeZoneRule i1 is not equivalent to i3, but returned true");
}
if (i1.isEquivalentTo(a1)) {
errln("FAIL: An InitialTimeZoneRule is not equivalent to an AnnualTimeZoneRule, but returned true");
}
// InitialTimeZoneRule#getFirstStart/getFinalStart/getNextStart/getPreviousStart
d1 = i1.getFirstStart(0, 0);
if (d1 != null) {
errln("FAIL: Non-null Date is returned by InitialTimeZone#getFirstStart");
}
d1 = i1.getFinalStart(0, 0);
if (d1 != null) {
errln("FAIL: Non-null Date is returned by InitialTimeZone#getFinalStart");
}
d1 = i1.getNextStart(time1, 0, 0, false);
if (d1 != null) {
errln("FAIL: Non-null Date is returned by InitialTimeZone#getNextStart");
}
d1 = i1.getPreviousStart(time1, 0, 0, false);
if (d1 != null) {
errln("FAIL: Non-null Date is returned by InitialTimeZone#getPreviousStart");
}
// InitialTimeZoneRule#isTransitionRule
if (i1.isTransitionRule()) {
errln("FAIL: An InitialTimeZoneRule is not a transition rule, but returned true");
}
// InitialTimeZoneRule#toString
str = i1.toString();
if (str == null || str.length() == 0) {
errln("FAIL: InitialTimeZoneRule#toString returns null or empty string");
} else {
logln("InitialTimeZoneRule i1 : " + str);
}
// TimeArrayTimeZoneRule#getStartTimes
long[] times = t1.getStartTimes();
if (times == null || times.length == 0 || times[0] != 0) {
errln("FAIL: Bad start times are returned by TimeArrayTimeZoneRule#getStartTimes");
}
// TimeArrayTimeZoneRule#getTimeType
if (t1.getTimeType() != DateTimeRule.UTC_TIME) {
errln("FAIL: TimeArrayTimeZoneRule t1 uses UTC_TIME, but different type is returned");
}
if (t4.getTimeType() != DateTimeRule.STANDARD_TIME) {
errln("FAIL: TimeArrayTimeZoneRule t4 uses STANDARD_TIME, but different type is returned");
}
if (t5.getTimeType() != DateTimeRule.WALL_TIME) {
errln("FAIL: TimeArrayTimeZoneRule t5 uses WALL_TIME, but different type is returned");
}
// TimeArrayTimeZoneRule#getFirstStart/getFinalStart
d1 = t1.getFirstStart(0, 0);
if (d1 == null || d1.getTime() != trtimes1[0]) {
errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t1");
}
d1 = t1.getFinalStart(0, 0);
if (d1 == null || d1.getTime() != trtimes1[0]) {
errln("FAIL: Bad final start time returned from TimeArrayTimeZoneRule t1");
}
d1 = t4.getFirstStart(-4 * HOUR, 1 * HOUR);
if (d1 == null || (d1.getTime() != trtimes1[0] + 4 * HOUR)) {
errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t4");
}
d1 = t5.getFirstStart(-4 * HOUR, 1 * HOUR);
if (d1 == null || (d1.getTime() != trtimes1[0] + 3 * HOUR)) {
errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t5");
}
// TimeArrayTimeZoneRule#getNextStart/getPreviousStart
d1 = t3.getNextStart(time1, -3 * HOUR, 1 * HOUR, false);
if (d1 != null) {
errln("FAIL: Non-null Date is returned by getNextStart after the final transition for t3");
}
d1 = t3.getPreviousStart(time1, -3 * HOUR, 1 * HOUR, false);
if (d1 == null || d1.getTime() != trtimes2[1]) {
errln("FAIL: Bad start time returned by getPreviousStart for t3");
} else {
d2 = t3.getPreviousStart(d1.getTime(), -3 * HOUR, 1 * HOUR, false);
if (d2 == null || d2.getTime() != trtimes2[0]) {
errln("FAIL: Bad start time returned by getPreviousStart for t3");
}
}
// time3 - year 1950, no result expected
d1 = t3.getPreviousStart(time3, -3 * HOUR, 1 * HOUR, false);
if (d1 != null) {
errln("FAIL: Non-null Date is returned by getPrevoousStart for t3");
}
// TimeArrayTimeZoneRule#isEquivalentTo
if (!t1.isEquivalentTo(t2)) {
errln("FAIL: TimeArrayTimeZoneRule t1 is equivalent to t2, but returned false");
}
if (t1.isEquivalentTo(t3)) {
errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t3, but returned true");
}
if (t1.isEquivalentTo(t4)) {
errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t4, but returned true");
}
if (t1.isEquivalentTo(a1)) {
errln("FAIL: TimeArrayTimeZoneRule is not equivalent to AnnualTimeZoneRule, but returned true");
}
// TimeArrayTimeZoneRule#isTransitionRule
if (!t1.isTransitionRule()) {
errln("FAIL: A TimeArrayTimeZoneRule is a transition rule, but returned false");
}
// TimeArrayTimeZoneRule#toString
str = t3.toString();
if (str == null || str.length() == 0) {
errln("FAIL: TimeArrayTimeZoneRule#toString returns null or empty string");
} else {
logln("TimeArrayTimeZoneRule t3 : " + str);
}
// DateTimeRule#toString
str = dtr1.toString();
if (str == null || str.length() == 0) {
errln("FAIL: DateTimeRule#toString for dtr1 returns null or empty string");
} else {
logln("DateTimeRule dtr1 : " + str);
}
str = dtr2.toString();
if (str == null || str.length() == 0) {
errln("FAIL: DateTimeRule#toString for dtr2 returns null or empty string");
} else {
logln("DateTimeRule dtr1 : " + str);
}
str = dtr3.toString();
if (str == null || str.length() == 0) {
errln("FAIL: DateTimeRule#toString for dtr3 returns null or empty string");
} else {
logln("DateTimeRule dtr1 : " + str);
}
str = dtr4.toString();
if (str == null || str.length() == 0) {
errln("FAIL: DateTimeRule#toString for dtr4 returns null or empty string");
} else {
logln("DateTimeRule dtr1 : " + str);
}
}
use of android.icu.util.AnnualTimeZoneRule in project j2objc by google.
the class TimeZoneRuleTest method TestSimpleRuleBasedTimeZone.
/*
* RuleBasedTimeZone test cases
*/
@Test
public void TestSimpleRuleBasedTimeZone() {
SimpleTimeZone stz = new SimpleTimeZone(-1 * HOUR, "TestSTZ", Calendar.SEPTEMBER, -30, -Calendar.SATURDAY, 1 * HOUR, SimpleTimeZone.WALL_TIME, Calendar.FEBRUARY, 2, Calendar.SUNDAY, 1 * HOUR, SimpleTimeZone.WALL_TIME, 1 * HOUR);
DateTimeRule dtr;
AnnualTimeZoneRule atzr;
final int STARTYEAR = 2000;
InitialTimeZoneRule ir = new InitialTimeZoneRule(// Initial time Name
"RBTZ_Initial", // Raw offset
-1 * HOUR, // DST saving amount
1 * HOUR);
// RBTZ
RuleBasedTimeZone rbtz1 = new RuleBasedTimeZone("RBTZ1", ir);
dtr = new DateTimeRule(Calendar.SEPTEMBER, 30, Calendar.SATURDAY, false, 1 * HOUR, // SUN<=30 in September, at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST1", -1 * HOUR, /* rawOffset */
1 * HOUR, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz1.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.FEBRUARY, 2, Calendar.SUNDAY, 1 * HOUR, // 2nd Sunday in February, at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD1", -1 * HOUR, /* rawOffset */
0, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz1.addTransitionRule(atzr);
// Equivalent, but different date rule type
RuleBasedTimeZone rbtz2 = new RuleBasedTimeZone("RBTZ2", ir);
dtr = new DateTimeRule(Calendar.SEPTEMBER, -1, Calendar.SATURDAY, 1 * HOUR, // Last Sunday in September at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST2", -1 * HOUR, 1 * HOUR, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz2.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.FEBRUARY, 8, Calendar.SUNDAY, true, 1 * HOUR, // SUN>=8 in February, at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD2", -1 * HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz2.addTransitionRule(atzr);
// Equivalent, but different time rule type
RuleBasedTimeZone rbtz3 = new RuleBasedTimeZone("RBTZ3", ir);
dtr = new DateTimeRule(Calendar.SEPTEMBER, 30, Calendar.SATURDAY, false, 2 * HOUR, DateTimeRule.UTC_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST3", -1 * HOUR, 1 * HOUR, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz3.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.FEBRUARY, 2, Calendar.SUNDAY, 0 * HOUR, DateTimeRule.STANDARD_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD3", -1 * HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz3.addTransitionRule(atzr);
// Check equivalency for 10 years
long start = getUTCMillis(STARTYEAR, Calendar.JANUARY, 1);
long until = getUTCMillis(STARTYEAR + 10, Calendar.JANUARY, 1);
if (!(stz.hasEquivalentTransitions(rbtz1, start, until))) {
errln("FAIL: rbtz1 must be equivalent to the SimpleTimeZone in the time range.");
}
if (!(stz.hasEquivalentTransitions(rbtz2, start, until))) {
errln("FAIL: rbtz2 must be equivalent to the SimpleTimeZone in the time range.");
}
if (!(stz.hasEquivalentTransitions(rbtz3, start, until))) {
errln("FAIL: rbtz3 must be equivalent to the SimpleTimeZone in the time range.");
}
// hasSameRules
if (rbtz1.hasSameRules(rbtz2)) {
errln("FAIL: rbtz1 and rbtz2 have different rules, but returned true.");
}
if (rbtz1.hasSameRules(rbtz3)) {
errln("FAIL: rbtz1 and rbtz3 have different rules, but returned true.");
}
RuleBasedTimeZone rbtz1c = (RuleBasedTimeZone) rbtz1.clone();
if (!rbtz1.hasSameRules(rbtz1c)) {
errln("FAIL: Cloned RuleBasedTimeZone must have the same rules with the original.");
}
// getOffset
GregorianCalendar cal = new GregorianCalendar();
int[] offsets = new int[2];
int offset;
boolean dst;
cal.setTimeZone(rbtz1);
cal.clear();
// Jan 1, 1000 BC
cal.set(Calendar.ERA, GregorianCalendar.BC);
cal.set(1000, Calendar.JANUARY, 1);
offset = rbtz1.getOffset(cal.get(Calendar.ERA), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_WEEK), cal.get(Calendar.MILLISECONDS_IN_DAY));
if (offset != 0) {
errln("FAIL: Invalid time zone offset: " + offset + " /expected: 0");
}
dst = rbtz1.inDaylightTime(cal.getTime());
if (!dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1.getOffset(cal.getTimeInMillis(), true, offsets);
if (offsets[0] != -3600000) {
errln("FAIL: Invalid time zone raw offset: " + offsets[0] + " /expected: -3600000");
}
if (offsets[1] != 3600000) {
errln("FAIL: Invalid DST amount: " + offsets[1] + " /expected: 3600000");
}
// July 1, 2000, AD
cal.set(Calendar.ERA, GregorianCalendar.AD);
cal.set(2000, Calendar.JULY, 1);
offset = rbtz1.getOffset(cal.get(Calendar.ERA), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_WEEK), cal.get(Calendar.MILLISECONDS_IN_DAY));
if (offset != -3600000) {
errln("FAIL: Invalid time zone offset: " + offset + " /expected: -3600000");
}
dst = rbtz1.inDaylightTime(cal.getTime());
if (dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1.getOffset(cal.getTimeInMillis(), true, offsets);
if (offsets[0] != -3600000) {
errln("FAIL: Invalid time zone raw offset: " + offsets[0] + " /expected: -3600000");
}
if (offsets[1] != 0) {
errln("FAIL: Invalid DST amount: " + offsets[1] + " /expected: 0");
}
// July 1, 2000, AD
// Try to add 3rd final rule
dtr = new DateTimeRule(Calendar.OCTOBER, 15, 1 * HOUR, DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("3RD_ATZ", -1 * HOUR, 2 * HOUR, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
boolean bException = false;
try {
rbtz1.addTransitionRule(atzr);
} catch (IllegalStateException ise) {
bException = true;
}
if (!bException) {
errln("FAIL: 3rd final rule must be rejected");
}
// Try to add an initial rule
bException = false;
try {
rbtz1.addTransitionRule(new InitialTimeZoneRule("Test Initial", 2 * HOUR, 0));
} catch (IllegalArgumentException iae) {
bException = true;
}
if (!bException) {
errln("FAIL: InitialTimeZoneRule must be rejected");
}
}
use of android.icu.util.AnnualTimeZoneRule in project j2objc by google.
the class TimeZoneOffsetLocalTest method TestGetOffsetAroundTransition.
/*
* Testing getOffset APIs around rule transition by local standard/wall time.
*/
@Test
public void TestGetOffsetAroundTransition() {
final int HOUR = 60 * 60 * 1000;
final int MINUTE = 60 * 1000;
int[][] DATES = { { 2006, Calendar.APRIL, 2, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 2, 00, 2 * HOUR }, { 2006, Calendar.APRIL, 2, 2, 30, 2 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 3, 00, 3 * HOUR }, { 2006, Calendar.APRIL, 2, 3, 30, 3 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 0, 30, 0 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 1, 00, 1 * HOUR }, { 2006, Calendar.OCTOBER, 29, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 2, 00, 2 * HOUR }, { 2006, Calendar.OCTOBER, 29, 2, 30, 2 * HOUR + 30 * MINUTE } };
// Expected offsets by getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)
int[] OFFSETS1 = { // April 2, 2006
-8 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, // October 29, 2006
-7 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR };
// Expected offsets by getOffset(long time, boolean local, int[] offsets) with local = true
// or getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
// with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
int[][] OFFSETS2 = { // April 2, 2006
{ -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // Oct 29, 2006
{ -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
// Expected offsets by getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
// with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
int[][] OFFSETS3 = { // April 2, 2006
{ -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // October 29, 2006
{ -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
int[] offsets = new int[2];
TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(utc);
cal.clear();
// Set up TimeZone objects - OlsonTimeZone, SimpleTimeZone and RuleBasedTimeZone
BasicTimeZone[] TESTZONES = new BasicTimeZone[3];
TESTZONES[0] = (BasicTimeZone) TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
TESTZONES[1] = new SimpleTimeZone(-8 * HOUR, "Simple Pacific Time", Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR);
InitialTimeZoneRule ir = new InitialTimeZoneRule(// Initial time Name
"Pacific Standard Time", // Raw offset
-8 * HOUR, // DST saving amount
0 * HOUR);
RuleBasedTimeZone rbPT = new RuleBasedTimeZone("Rule based Pacific Time", ir);
DateTimeRule dtr;
AnnualTimeZoneRule atzr;
final int STARTYEAR = 2000;
dtr = new DateTimeRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, // 1st Sunday in April, at 2AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("Pacific Daylight Time", -8 * HOUR, /* rawOffset */
1 * HOUR, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbPT.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR, // last Sunday in October, at 2AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("Pacific Standard Time", -8 * HOUR, /* rawOffset */
0, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbPT.addTransitionRule(atzr);
TESTZONES[2] = rbPT;
// Calculate millis
long[] MILLIS = new long[DATES.length];
for (int i = 0; i < DATES.length; i++) {
cal.clear();
cal.set(DATES[i][0], DATES[i][1], DATES[i][2], DATES[i][3], DATES[i][4]);
MILLIS[i] = cal.getTimeInMillis();
}
DateFormat df = DateFormat.getInstance();
df.setTimeZone(utc);
// Test getOffset(int era, int year, int month, int day, int dayOfWeek, int millis)
for (int i = 0; i < TESTZONES.length; i++) {
for (int d = 0; d < DATES.length; d++) {
int offset = TESTZONES[i].getOffset(GregorianCalendar.AD, DATES[d][0], DATES[d][1], DATES[d][2], Calendar.SUNDAY, DATES[d][5]);
if (offset != OFFSETS1[d]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[d])) + "(standard) - Got: " + offset + " Expected: " + OFFSETS1[d]);
}
}
}
// Test getOffset(long time, boolean local, int[] offsets) with local=true
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffset(MILLIS[m], true, offsets);
if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_STD, BasicTimeZone.LOCAL_STD, offsets);
if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/STD/STD) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_DST, BasicTimeZone.LOCAL_DST, offsets);
if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/DST/DST) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_FORMER/duplicatedTimeOpt = LOCAL_LATTER
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_FORMER, BasicTimeZone.LOCAL_LATTER, offsets);
if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/FORMER/LATTER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_LATTER/duplicatedTimeOpt = LOCAL_FORMER
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_LATTER, BasicTimeZone.LOCAL_FORMER, offsets);
if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/LATTER/FORMER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
}
}
}
}
use of android.icu.util.AnnualTimeZoneRule in project j2objc by google.
the class OlsonTimeZone method setRawOffset.
/* (non-Javadoc)
* @see android.icu.util.TimeZone#setRawOffset(int)
*/
@Override
public void setRawOffset(int offsetMillis) {
if (isFrozen()) {
throw new UnsupportedOperationException("Attempt to modify a frozen OlsonTimeZone instance.");
}
if (getRawOffset() == offsetMillis) {
return;
}
long current = System.currentTimeMillis();
if (current < finalStartMillis) {
SimpleTimeZone stz = new SimpleTimeZone(offsetMillis, getID());
boolean bDst = useDaylightTime();
if (bDst) {
TimeZoneRule[] currentRules = getSimpleTimeZoneRulesNear(current);
if (currentRules.length != 3) {
// DST was observed at the beginning of this year, so useDaylightTime
// returned true. getSimpleTimeZoneRulesNear requires at least one
// future transition for making a pair of rules. This implementation
// rolls back the time before the latest offset transition.
TimeZoneTransition tzt = getPreviousTransition(current, false);
if (tzt != null) {
currentRules = getSimpleTimeZoneRulesNear(tzt.getTime() - 1);
}
}
if (currentRules.length == 3 && (currentRules[1] instanceof AnnualTimeZoneRule) && (currentRules[2] instanceof AnnualTimeZoneRule)) {
// A pair of AnnualTimeZoneRule
AnnualTimeZoneRule r1 = (AnnualTimeZoneRule) currentRules[1];
AnnualTimeZoneRule r2 = (AnnualTimeZoneRule) currentRules[2];
DateTimeRule start, end;
int offset1 = r1.getRawOffset() + r1.getDSTSavings();
int offset2 = r2.getRawOffset() + r2.getDSTSavings();
int sav;
if (offset1 > offset2) {
start = r1.getRule();
end = r2.getRule();
sav = offset1 - offset2;
} else {
start = r2.getRule();
end = r1.getRule();
sav = offset2 - offset1;
}
// getSimpleTimeZoneRulesNear always return rules using DOW / WALL_TIME
stz.setStartRule(start.getRuleMonth(), start.getRuleWeekInMonth(), start.getRuleDayOfWeek(), start.getRuleMillisInDay());
stz.setEndRule(end.getRuleMonth(), end.getRuleWeekInMonth(), end.getRuleDayOfWeek(), end.getRuleMillisInDay());
// set DST saving amount and start year
stz.setDSTSavings(sav);
} else {
// This could only happen if last rule is DST
// and the rule used forever. For example, Asia/Dhaka
// in tzdata2009i stays in DST forever.
// Hack - set DST starting at midnight on Jan 1st,
// ending 23:59:59.999 on Dec 31st
stz.setStartRule(0, 1, 0);
stz.setEndRule(11, 31, Grego.MILLIS_PER_DAY - 1);
}
}
int[] fields = Grego.timeToFields(current, null);
finalStartYear = fields[0];
finalStartMillis = Grego.fieldsToDay(fields[0], 0, 1);
if (bDst) {
// we probably do not need to set start year of final rule
// to finalzone itself, but we always do this for now.
stz.setStartYear(finalStartYear);
}
finalZone = stz;
} else {
finalZone.setRawOffset(offsetMillis);
}
transitionRulesInitialized = false;
}
use of android.icu.util.AnnualTimeZoneRule in project j2objc by google.
the class TimeZoneRuleTest method TestT8943.
// Test case for ticket#8943
// RuleBasedTimeZone#getOffsets throws NPE
@Test
public void TestT8943() {
String id = "Ekaterinburg Time";
String stdName = "Ekaterinburg Standard Time";
String dstName = "Ekaterinburg Daylight Time";
InitialTimeZoneRule initialRule = new InitialTimeZoneRule(stdName, 18000000, 0);
RuleBasedTimeZone rbtz = new RuleBasedTimeZone(id, initialRule);
DateTimeRule dtRule = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 10800000, DateTimeRule.WALL_TIME);
AnnualTimeZoneRule atzRule = new AnnualTimeZoneRule(stdName, 18000000, 0, dtRule, 2000, 2010);
rbtz.addTransitionRule(atzRule);
dtRule = new DateTimeRule(Calendar.MARCH, -1, Calendar.SUNDAY, 7200000, DateTimeRule.WALL_TIME);
atzRule = new AnnualTimeZoneRule(dstName, 18000000, 3600000, dtRule, 2000, 2010);
rbtz.addTransitionRule(atzRule);
dtRule = new DateTimeRule(Calendar.JANUARY, 1, 0, DateTimeRule.WALL_TIME);
atzRule = new AnnualTimeZoneRule(stdName, 21600000, 0, dtRule, 2011, AnnualTimeZoneRule.MAX_YEAR);
rbtz.addTransitionRule(atzRule);
dtRule = new DateTimeRule(Calendar.JANUARY, 1, 1, DateTimeRule.WALL_TIME);
atzRule = new AnnualTimeZoneRule(dstName, 21600000, 0, dtRule, 2011, AnnualTimeZoneRule.MAX_YEAR);
rbtz.addTransitionRule(atzRule);
int[] expected = { 21600000, 0 };
int[] offsets = new int[2];
try {
rbtz.getOffset(1293822000000L, /* 2010-12-31 19:00:00 UTC */
false, offsets);
if (offsets[0] != expected[0] || offsets[1] != expected[1]) {
errln("Fail: Wrong offsets: " + offsets[0] + "/" + offsets[1] + " Expected: " + expected[0] + "/" + expected[1]);
}
} catch (Exception e) {
errln("Fail: Exception thrown - " + e.getMessage());
}
}
Aggregations