Search in sources :

Example 6 with InitialTimeZoneRule

use of android.icu.util.InitialTimeZoneRule 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());
    }
}
Also used : DateTimeRule(android.icu.util.DateTimeRule) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with InitialTimeZoneRule

use of android.icu.util.InitialTimeZoneRule in project j2objc by google.

the class TimeZoneRuleTest method TestGetSimpleRules.

/*
     * Extract simple rules from an OlsonTimeZone and make sure the rule format matches
     * the expected format.
     */
@Test
public void TestGetSimpleRules() {
    long[] testTimes = new long[] { getUTCMillis(1970, Calendar.JANUARY, 1), getUTCMillis(2000, Calendar.MARCH, 31), getUTCMillis(2005, Calendar.JULY, 1), getUTCMillis(2010, Calendar.NOVEMBER, 1) };
    String[] tzids = getTestZIDs();
    for (int n = 0; n < testTimes.length; n++) {
        long time = testTimes[n];
        for (int i = 0; i < tzids.length; i++) {
            BasicTimeZone tz = (BasicTimeZone) TimeZone.getTimeZone(tzids[i], TimeZone.TIMEZONE_ICU);
            TimeZoneRule[] rules = tz.getSimpleTimeZoneRulesNear(time);
            if (rules == null) {
                errln("FAIL: Failed to extract simple rules for " + tzids[i] + " at " + time);
            } else {
                if (rules.length == 1) {
                    if (!(rules[0] instanceof InitialTimeZoneRule)) {
                        errln("FAIL: Unexpected rule object type is returned for " + tzids[i] + " at " + time);
                    }
                } else if (rules.length == 3) {
                    if (!(rules[0] instanceof InitialTimeZoneRule) || !(rules[1] instanceof AnnualTimeZoneRule) || !(rules[2] instanceof AnnualTimeZoneRule)) {
                        errln("FAIL: Unexpected rule object type is returned for " + tzids[i] + " at " + time);
                    }
                    for (int idx = 1; idx <= 2; idx++) {
                        DateTimeRule dtr = ((AnnualTimeZoneRule) rules[idx]).getRule();
                        if (dtr.getTimeRuleType() != DateTimeRule.WALL_TIME) {
                            errln("FAIL: WALL_TIME is not used as the time rule in the time zone rule(" + idx + ") for " + tzids[i] + " at " + time);
                        }
                        if (dtr.getDateRuleType() != DateTimeRule.DOW) {
                            errln("FAIL: DOW is not used as the date rule in the time zone rule(" + idx + ") for " + tzids[i] + " at " + time);
                        }
                    }
                } else {
                    errln("FAIL: Unexpected number of rules returned for " + tzids[i] + " at " + time);
                }
            }
        }
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) DateTimeRule(android.icu.util.DateTimeRule) BasicTimeZone(android.icu.util.BasicTimeZone) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) Test(org.junit.Test)

Example 8 with InitialTimeZoneRule

use of android.icu.util.InitialTimeZoneRule in project j2objc by google.

the class TimeZoneRuleTest method TestHistoricalRuleBasedTimeZone.

/*
     * Test equivalency between OlsonTimeZone and custom RBTZ representing the
     * equivalent rules in a certain time range
     */
@Test
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, // Last Sunday in October, at 2AM wall time
    DateTimeRule.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, // SUN>=1 in November, at 2AM wall time
    DateTimeRule.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, // Last Sunday in April, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 1967, 1973);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.JANUARY, 6, 2 * HOUR, // January 6, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 1974, 1974);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.FEBRUARY, 23, 2 * HOUR, // February 23, at 2AM wall time
    DateTimeRule.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, // Last Sunday in April, at 2AM wall time
    DateTimeRule.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, // SUN>=1 in April, at 2AM wall time
    DateTimeRule.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, // SUN>=8 in March, at 2AM wall time
    DateTimeRule.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");
        }
    }
}
Also used : DateTimeRule(android.icu.util.DateTimeRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date) Test(org.junit.Test)

Example 9 with InitialTimeZoneRule

use of android.icu.util.InitialTimeZoneRule in project j2objc by google.

the class TimeZoneTest method TestFreezable.

/*
     * Test case for Freezable
     */
@Test
public void TestFreezable() {
    // Test zones - initially thawed
    TimeZone[] ZA1 = { TimeZone.getDefault(), TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU), TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_JDK), new SimpleTimeZone(0, "stz"), new RuleBasedTimeZone("rbtz", new InitialTimeZoneRule("rbtz0", 0, 0)), VTimeZone.create("America/New_York") };
    checkThawed(ZA1, "ZA1");
    // freeze
    for (int i = 0; i < ZA1.length; i++) {
        ZA1[i].freeze();
    }
    checkFrozen(ZA1, "ZA1(frozen)");
    // Test zones - initially frozen
    final TimeZone[] ZA2 = { TimeZone.GMT_ZONE, TimeZone.UNKNOWN_ZONE, TimeZone.getFrozenTimeZone("America/Los_Angeles"), new SimpleTimeZone(3600000, "frz_stz").freeze(), new RuleBasedTimeZone("frz_rbtz", new InitialTimeZoneRule("frz_rbtz0", 3600000, 0)).freeze(), VTimeZone.create("Asia/Tokyo").freeze() };
    checkFrozen(ZA2, "ZA2");
    TimeZone[] ZA2_thawed = new TimeZone[ZA2.length];
    // create thawed clone
    for (int i = 0; i < ZA2_thawed.length; i++) {
        ZA2_thawed[i] = ZA2[i].cloneAsThawed();
    }
    checkThawed(ZA2_thawed, "ZA2(thawed)");
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) NativeTimeZone(com.google.j2objc.util.NativeTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) JavaTimeZone(android.icu.impl.JavaTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Test(org.junit.Test)

Example 10 with InitialTimeZoneRule

use of android.icu.util.InitialTimeZoneRule in project j2objc by google.

the class TimeZoneTest method createRBTZ.

private static RuleBasedTimeZone createRBTZ(BasicTimeZone btz, long start) {
    TimeZoneRule[] rules = btz.getTimeZoneRules(start);
    RuleBasedTimeZone rbtz = new RuleBasedTimeZone("RBTZ:btz.getID()", (InitialTimeZoneRule) rules[0]);
    for (int i = 1; i < rules.length; i++) {
        rbtz.addTransitionRule(rules[i]);
    }
    return rbtz;
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone)

Aggregations

InitialTimeZoneRule (android.icu.util.InitialTimeZoneRule)12 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)10 Test (org.junit.Test)9 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)7 BasicTimeZone (android.icu.util.BasicTimeZone)6 DateTimeRule (android.icu.util.DateTimeRule)6 SimpleTimeZone (android.icu.util.SimpleTimeZone)6 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)6 TimeZoneRule (android.icu.util.TimeZoneRule)6 TimeZone (android.icu.util.TimeZone)5 TimeZoneTransition (android.icu.util.TimeZoneTransition)3 VTimeZone (android.icu.util.VTimeZone)3 Date (java.util.Date)3 GregorianCalendar (android.icu.util.GregorianCalendar)2 JavaTimeZone (android.icu.impl.JavaTimeZone)1 DateFormat (android.icu.text.DateFormat)1 Calendar (android.icu.util.Calendar)1 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)1 IOException (java.io.IOException)1 ZoneOffset (java.time.ZoneOffset)1