Search in sources :

Example 1 with RuleBasedTimeZone

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

Example 2 with RuleBasedTimeZone

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

the class TimeZoneTest method TestObservesDaylightTime.

@Test
public void TestObservesDaylightTime() {
    boolean observesDaylight;
    long current = System.currentTimeMillis();
    // J2ObjC change: time zones going through adjustments are problematic when comparing
    // the ICU tz data vs. the operating system tz data.
    Set<String> unstableTzids = new HashSet<>();
    // https://github.com/eggert/tz/blob/379f7ba9b81eee97f0209a322540b4a522122b5d/africa#L847
    unstableTzids.add("Africa/Casablanca");
    unstableTzids.add("Africa/El_Aaiun");
    String[] tzids = TimeZone.getAvailableIDs();
    for (String tzid : tzids) {
        // OlsonTimeZone
        TimeZone tz = TimeZone.getTimeZone(tzid, TimeZone.TIMEZONE_ICU);
        observesDaylight = tz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [OlsonTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }
        // RuleBasedTimeZone
        RuleBasedTimeZone rbtz = createRBTZ((BasicTimeZone) tz, current);
        boolean observesDaylightRBTZ = rbtz.observesDaylightTime();
        if (observesDaylightRBTZ != isDaylightTimeAvailable(rbtz, current)) {
            errln("Fail: [RuleBasedTimeZone] observesDaylightTime() returned " + observesDaylightRBTZ + " for " + rbtz.getID());
        } else if (observesDaylight != observesDaylightRBTZ) {
            errln("Fail: RuleBasedTimeZone " + rbtz.getID() + " returns " + observesDaylightRBTZ + ", but different from match OlsonTimeZone");
        }
        // JavaTimeZone
        tz = TimeZone.getTimeZone(tzid, TimeZone.TIMEZONE_JDK);
        observesDaylight = tz.observesDaylightTime();
        if (!unstableTzids.contains(tzid) && observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [JavaTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }
        // VTimeZone
        tz = VTimeZone.getTimeZone(tzid);
        observesDaylight = tz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [VTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }
    }
    // SimpleTimeZone
    SimpleTimeZone[] stzs = { new SimpleTimeZone(0, "STZ0"), new SimpleTimeZone(-5 * 60 * 60 * 1000, "STZ-5D", Calendar.MARCH, 2, Calendar.SUNDAY, 2 * 60 * 60 * 1000, Calendar.NOVEMBER, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000) };
    for (SimpleTimeZone stz : stzs) {
        observesDaylight = stz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(stz, current)) {
            errln("Fail: [SimpleTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + stz.getID());
        }
    }
}
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) SimpleTimeZone(android.icu.util.SimpleTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with RuleBasedTimeZone

use of android.icu.util.RuleBasedTimeZone 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]);
            }
        }
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date) DateTimeRule(android.icu.util.DateTimeRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 4 with RuleBasedTimeZone

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

the class TimeZoneRuleTest method TestRBTZTransition.

/*
     * Check if an OlsonTimeZone and its equivalent RBTZ have the exact same
     * transitions.
     */
@Test
public void TestRBTZTransition() {
    int[] STARTYEARS = { 1950, 1975, 2000, 2010 };
    String[] zids = getTestZIDs();
    for (int i = 0; i < zids.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(zids[i], TimeZone.TIMEZONE_ICU);
        if (tz == null) {
            break;
        }
        for (int j = 0; j < STARTYEARS.length; j++) {
            long startTime = getUTCMillis(STARTYEARS[j], Calendar.JANUARY, 1);
            TimeZoneRule[] rules = ((BasicTimeZone) tz).getTimeZoneRules(startTime);
            RuleBasedTimeZone rbtz = new RuleBasedTimeZone(tz.getID() + "(RBTZ)", (InitialTimeZoneRule) rules[0]);
            for (int k = 1; k < rules.length; k++) {
                rbtz.addTransitionRule(rules[k]);
            }
            // Compare the original OlsonTimeZone with the RBTZ starting the startTime for 20 years
            long until = getUTCMillis(STARTYEARS[j] + 20, Calendar.JANUARY, 1);
            // Ascending
            compareTransitionsAscending(tz, rbtz, startTime, until, false);
            // Ascending/inclusive
            compareTransitionsAscending(tz, rbtz, startTime + 1, until, true);
            // Descending
            compareTransitionsDescending(tz, rbtz, startTime, until, false);
            // Descending/inclusive
            compareTransitionsDescending(tz, rbtz, startTime + 1, until, true);
        }
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) 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) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Test(org.junit.Test)

Example 5 with RuleBasedTimeZone

use of android.icu.util.RuleBasedTimeZone 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)

Aggregations

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