Search in sources :

Example 16 with BasicTimeZone

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

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

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

the class TimeZoneRuleTest method TestVTimeZoneRoundTrip.

/*
     * Write out time zone rules of OlsonTimeZone into VTIMEZONE format, create a new
     * VTimeZone from the VTIMEZONE data, then compare transitions
     */
@Test
public void TestVTimeZoneRoundTrip() {
    long startTime = getUTCMillis(1850, Calendar.JANUARY, 1);
    long endTime = getUTCMillis(2050, Calendar.JANUARY, 1);
    String[] tzids = getTestZIDs();
    for (int i = 0; i < tzids.length; i++) {
        BasicTimeZone olsontz = (BasicTimeZone) TimeZone.getTimeZone(tzids[i], TimeZone.TIMEZONE_ICU);
        VTimeZone vtz_org = VTimeZone.create(tzids[i]);
        vtz_org.setTZURL("http://source.icu-project.org/timezone");
        vtz_org.setLastModified(new Date());
        VTimeZone vtz_new = null;
        try {
            // Write out VTIMEZONE
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(baos);
            vtz_org.write(writer);
            writer.close();
            byte[] vtzdata = baos.toByteArray();
            // Read VTIMEZONE
            ByteArrayInputStream bais = new ByteArrayInputStream(vtzdata);
            InputStreamReader reader = new InputStreamReader(bais);
            vtz_new = VTimeZone.create(reader);
            reader.close();
            // Write out VTIMEZONE one more time
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            OutputStreamWriter writer1 = new OutputStreamWriter(baos1);
            vtz_new.write(writer1);
            writer1.close();
            byte[] vtzdata1 = baos1.toByteArray();
            // Make sure VTIMEZONE data is exactly same with the first one
            if (vtzdata.length != vtzdata1.length) {
                errln("FAIL: different VTIMEZONE data length");
            }
            for (int j = 0; j < vtzdata.length; j++) {
                if (vtzdata[j] != vtzdata1[j]) {
                    errln("FAIL: different VTIMEZONE data");
                    break;
                }
            }
        } catch (IOException ioe) {
            errln("FAIL: IO error while writing/reading VTIMEZONE data");
        }
        // VTIMEZONE.
        if (vtz_new.getOffset(startTime) != olsontz.getOffset(startTime)) {
            errln("FAIL: VTimeZone for " + tzids[i] + " is not equivalent to its OlsonTimeZone corresponding at " + startTime);
        }
        TimeZoneTransition tzt = olsontz.getNextTransition(startTime, false);
        if (tzt != null) {
            if (!vtz_new.hasEquivalentTransitions(olsontz, tzt.getTime(), endTime, true)) {
                int maxDelta = 1000;
                if (!hasEquivalentTransitions(vtz_new, olsontz, tzt.getTime() + maxDelta, endTime, true, maxDelta)) {
                    errln("FAIL: VTimeZone for " + tzids[i] + " is not equivalent to its OlsonTimeZone corresponding.");
                } else {
                    logln("VTimeZone for " + tzids[i] + " differs from its OlsonTimeZone corresponding with maximum transition time delta - " + maxDelta);
                }
            }
            if (!vtz_new.hasEquivalentTransitions(olsontz, tzt.getTime(), endTime, false)) {
                logln("VTimeZone for " + tzids[i] + " is not equivalent to its OlsonTimeZone corresponding in strict comparison mode.");
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) VTimeZone(android.icu.util.VTimeZone) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Date(java.util.Date) BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneTransition(android.icu.util.TimeZoneTransition) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) Test(org.junit.Test)

Example 19 with BasicTimeZone

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

the class TimeZoneRuleTest method compareTransitionsAscending.

/*
     * Compare all time transitions in 2 time zones in the specified time range in ascending order
     */
private void compareTransitionsAscending(TimeZone tz1, TimeZone tz2, long start, long end, boolean inclusive) {
    BasicTimeZone z1 = (BasicTimeZone) tz1;
    BasicTimeZone z2 = (BasicTimeZone) tz2;
    String zid1 = tz1.getID();
    String zid2 = tz2.getID();
    long time = start;
    while (true) {
        TimeZoneTransition tzt1 = z1.getNextTransition(time, inclusive);
        TimeZoneTransition tzt2 = z2.getNextTransition(time, inclusive);
        boolean inRange1 = false;
        boolean inRange2 = false;
        if (tzt1 != null) {
            if (tzt1.getTime() < end || (inclusive && tzt1.getTime() == end)) {
                inRange1 = true;
            }
        }
        if (tzt2 != null) {
            if (tzt2.getTime() < end || (inclusive && tzt2.getTime() == end)) {
                inRange2 = true;
            }
        }
        if (!inRange1 && !inRange2) {
            // No more transition in the range
            break;
        }
        if (!inRange1) {
            errln("FAIL: " + zid1 + " does not have any transitions after " + time + " before " + end);
            break;
        }
        if (!inRange2) {
            errln("FAIL: " + zid2 + " does not have any transitions after " + time + " before " + end);
            break;
        }
        if (tzt1.getTime() != tzt2.getTime()) {
            errln("FAIL: First transition after " + time + " " + zid1 + "[" + tzt1.getTime() + "] " + zid2 + "[" + tzt2.getTime() + "]");
            break;
        }
        time = tzt1.getTime();
        if (inclusive) {
            time++;
        }
    }
}
Also used : BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneTransition(android.icu.util.TimeZoneTransition)

Example 20 with BasicTimeZone

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

the class TimeZoneTest method isDaylightTimeAvailable.

private static boolean isDaylightTimeAvailable(TimeZone tz, long start) {
    if (tz.inDaylightTime(new Date(start))) {
        return true;
    }
    long date;
    if (tz instanceof BasicTimeZone) {
        BasicTimeZone btz = (BasicTimeZone) tz;
        // check future transitions, up to 100
        date = start;
        for (int i = 0; i < 100; i++) {
            TimeZoneTransition tzt = btz.getNextTransition(date, false);
            if (tzt == null) {
                // no more transitions
                break;
            }
            if (tzt.getTo().getDSTSavings() != 0) {
                return true;
            }
            date = tzt.getTime();
        }
    } else {
        // check future times by incrementing 30 days, up to 200 times (about 16 years)
        final long inc = 30L * 24 * 60 * 60 * 1000;
        int[] offsets = new int[2];
        date = start + inc;
        for (int i = 0; i < 200; i++, date += inc) {
            tz.getOffset(date, false, offsets);
            if (offsets[1] != 0) {
                return true;
            }
        }
    }
    return false;
}
Also used : BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneTransition(android.icu.util.TimeZoneTransition) Date(java.util.Date)

Aggregations

BasicTimeZone (android.icu.util.BasicTimeZone)23 TimeZoneTransition (android.icu.util.TimeZoneTransition)15 Test (org.junit.Test)14 TimeZone (android.icu.util.TimeZone)11 SimpleTimeZone (android.icu.util.SimpleTimeZone)8 VTimeZone (android.icu.util.VTimeZone)8 Date (java.util.Date)8 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)6 InitialTimeZoneRule (android.icu.util.InitialTimeZoneRule)6 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)6 Calendar (android.icu.util.Calendar)4 TimeZoneRule (android.icu.util.TimeZoneRule)4 DateTimeRule (android.icu.util.DateTimeRule)3 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)3 SimpleDateFormat (android.icu.text.SimpleDateFormat)2 ULocale (android.icu.util.ULocale)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2