Search in sources :

Example 6 with RuleBasedTimeZone

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

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

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

Example 9 with RuleBasedTimeZone

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

the class TimeZoneTest method checkFrozen.

private void checkFrozen(TimeZone[] frozenZones, String zaName) {
    for (int i = 0; i < frozenZones.length; i++) {
        if (!frozenZones[i].isFrozen()) {
            errln("Fail: " + zaName + "[" + i + "] is not frozen.");
        }
        // clone
        TimeZone copy = (TimeZone) frozenZones[i].clone();
        if (frozenZones[i] != copy) {
            errln("Fail: " + zaName + "[" + i + "] - clone does not return the object itself.");
        }
        // cloneAsThawed
        TimeZone thawed = (TimeZone) frozenZones[i].cloneAsThawed();
        if (thawed.isFrozen() || !frozenZones[i].equals(thawed)) {
            errln("Fail: " + zaName + "[" + i + "] - cloneAsThawed does not work.");
        }
        // setID
        try {
            String newID = "foo";
            frozenZones[i].setID(newID);
            errln("Fail: " + zaName + "[" + i + "] - setID must throw UnsupportedOperationException.");
        } catch (UnsupportedOperationException e) {
        // OK
        }
        // setRawOffset
        if (!(frozenZones[i] instanceof RuleBasedTimeZone)) {
            // RuleBasedTimeZone does not supprot setRawOffset
            try {
                int newOffset = -3600000;
                frozenZones[i].setRawOffset(newOffset);
                errln("Fail: " + zaName + "[" + i + "] - setRawOffset must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        }
        if (frozenZones[i] instanceof SimpleTimeZone) {
            SimpleTimeZone stz = (SimpleTimeZone) frozenZones[i];
            // setDSTSavings
            try {
                int newDSTSavings = 1800000;
                stz.setDSTSavings(newDSTSavings);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setStartRule
            try {
                stz.setStartRule(Calendar.JANUARY, -1, Calendar.SUNDAY, 0);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setEndRule
            try {
                stz.setEndRule(Calendar.DECEMBER, 1, Calendar.SUNDAY, 0);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setEndRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setStartYear
            try {
                stz.setStartYear(2000);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartYear must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        } else if (frozenZones[i] instanceof RuleBasedTimeZone) {
            RuleBasedTimeZone rbtz = (RuleBasedTimeZone) frozenZones[i];
            // addTransitionRule
            try {
                TimeArrayTimeZoneRule tr1 = new TimeArrayTimeZoneRule("tr1", 7200000, 0, new long[] { 0 }, DateTimeRule.UTC_TIME);
                rbtz.addTransitionRule(tr1);
                errln("Fail: (RuleBasedTimeZone)" + zaName + "[" + i + "] - addTransitionRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        } else if (frozenZones[i] instanceof VTimeZone) {
            VTimeZone vtz = (VTimeZone) frozenZones[i];
            // setTZURL
            try {
                String tzUrl = "http://icu-project.org/timezone";
                vtz.setTZURL(tzUrl);
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setLastModified
            try {
                Date d = new Date();
                vtz.setLastModified(d);
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        }
    }
}
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) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date)

Example 10 with RuleBasedTimeZone

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

the class TimeZoneTest method checkThawed.

private void checkThawed(TimeZone[] thawedZones, String zaName) {
    for (int i = 0; i < thawedZones.length; i++) {
        if (thawedZones[i].isFrozen()) {
            errln("Fail: " + zaName + "[" + i + "] is frozen.");
        }
        // clone
        TimeZone copy = (TimeZone) thawedZones[i].clone();
        if (thawedZones[i] == copy || !thawedZones[i].equals(copy)) {
            errln("Fail: " + zaName + "[" + i + "] - clone does not work.");
        }
        // cloneAsThawed
        TimeZone thawed = (TimeZone) thawedZones[i].cloneAsThawed();
        if (thawed.isFrozen() || !thawedZones[i].equals(thawed)) {
            errln("Fail: " + zaName + "[" + i + "] - cloneAsThawed does not work.");
        }
        // setID
        try {
            String newID = "foo";
            thawedZones[i].setID(newID);
            if (!thawedZones[i].getID().equals(newID)) {
                errln("Fail: " + zaName + "[" + i + "] - setID(\"" + newID + "\") does not work.");
            }
        } catch (UnsupportedOperationException e) {
            errln("Fail: " + zaName + "[" + i + "] - setID throws UnsupportedOperationException.");
        }
        // setRawOffset
        // J2ObjC: RuleBasedTimeZone and NativeTimeZone do not support setRawOffset.
        boolean isNativeTimeZone = Optional.of(thawedZones[i]).filter(JavaTimeZone.class::isInstance).map(JavaTimeZone.class::cast).map(JavaTimeZone::unwrap).filter(NativeTimeZone.class::isInstance).isPresent();
        if (!(thawedZones[i] instanceof RuleBasedTimeZone) && !isNativeTimeZone) {
            try {
                int newOffset = -3600000;
                thawedZones[i].setRawOffset(newOffset);
                if (thawedZones[i].getRawOffset() != newOffset) {
                    errln("Fail: " + zaName + "[" + i + "] - setRawOffset(" + newOffset + ") does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: " + zaName + "[" + i + "] - setRawOffset throws UnsupportedOperationException.");
            }
        }
        if (thawedZones[i] instanceof SimpleTimeZone) {
            SimpleTimeZone stz = (SimpleTimeZone) thawedZones[i];
            // setDSTSavings
            try {
                int newDSTSavings = 1800000;
                stz.setDSTSavings(newDSTSavings);
                if (stz.getDSTSavings() != newDSTSavings) {
                    errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings(" + newDSTSavings + ") does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings throws UnsupportedOperationException.");
            }
            // setStartRule
            try {
                stz.setStartRule(Calendar.JANUARY, -1, Calendar.SUNDAY, 0);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartRule throws UnsupportedOperationException.");
            }
            // setEndRule
            try {
                stz.setEndRule(Calendar.DECEMBER, 1, Calendar.SUNDAY, 0);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setEndRule throws UnsupportedOperationException.");
            }
            // setStartYear
            try {
                stz.setStartYear(2000);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartYear throws UnsupportedOperationException.");
            }
        } else if (thawedZones[i] instanceof RuleBasedTimeZone) {
            RuleBasedTimeZone rbtz = (RuleBasedTimeZone) thawedZones[i];
            // addTransitionRule
            try {
                TimeArrayTimeZoneRule tr1 = new TimeArrayTimeZoneRule("tr1", 7200000, 0, new long[] { 0 }, DateTimeRule.UTC_TIME);
                rbtz.addTransitionRule(tr1);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (RuleBasedTimeZone)" + zaName + "[" + i + "] - addTransitionRule throws UnsupportedOperationException.");
            }
        } else if (thawedZones[i] instanceof VTimeZone) {
            VTimeZone vtz = (VTimeZone) thawedZones[i];
            // setTZURL
            try {
                String tzUrl = "http://icu-project.org/timezone";
                vtz.setTZURL(tzUrl);
                if (!vtz.getTZURL().equals(tzUrl)) {
                    errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL throws UnsupportedOperationException.");
            }
            // setLastModified
            try {
                Date d = new Date();
                vtz.setLastModified(d);
                if (!vtz.getLastModified().equals(d)) {
                    errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified throws UnsupportedOperationException.");
            }
        }
    }
}
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) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date) JavaTimeZone(android.icu.impl.JavaTimeZone)

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