Search in sources :

Example 1 with BasicTimeZone

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

the class TimeZoneRuleTest method TestHasEquivalentTransitions.

/*
     * Test cases for HasTimeZoneRules#hasEquivalentTransitions
     */
@Test
public void TestHasEquivalentTransitions() {
    // America/New_York and America/Indiana/Indianapolis are equivalent
    // since 2006
    TimeZone newyork = TimeZone.getTimeZone("America/New_York", TimeZone.TIMEZONE_ICU);
    TimeZone indianapolis = TimeZone.getTimeZone("America/Indiana/Indianapolis", TimeZone.TIMEZONE_ICU);
    TimeZone gmt_5 = TimeZone.getTimeZone("Etc/GMT+5", TimeZone.TIMEZONE_ICU);
    long jan1_1971 = getUTCMillis(1971, Calendar.JANUARY, 1);
    long jan1_2005 = getUTCMillis(2005, Calendar.JANUARY, 1);
    long jan1_2006 = getUTCMillis(2006, Calendar.JANUARY, 1);
    long jan1_2007 = getUTCMillis(2007, Calendar.JANUARY, 1);
    long jan1_2011 = getUTCMillis(2010, Calendar.JANUARY, 1);
    if (((BasicTimeZone) newyork).hasEquivalentTransitions(indianapolis, jan1_2005, jan1_2011)) {
        errln("FAIL: New_York is not equivalent to Indianapolis between 2005 and 2010, but returned true");
    }
    if (!((BasicTimeZone) newyork).hasEquivalentTransitions(indianapolis, jan1_2006, jan1_2011)) {
        errln("FAIL: New_York is equivalent to Indianapolis between 2006 and 2010, but returned false");
    }
    if (!((BasicTimeZone) indianapolis).hasEquivalentTransitions(gmt_5, jan1_1971, jan1_2006)) {
        errln("FAIL: Indianapolis is equivalent to GMT+5 between 1971 and 2005, but returned false");
    }
    if (((BasicTimeZone) indianapolis).hasEquivalentTransitions(gmt_5, jan1_1971, jan1_2007)) {
        errln("FAIL: Indianapolis is not equivalent to GMT+5 between 1971 and 2006, but returned true");
    }
    // Cloned TimeZone
    TimeZone newyork2 = (TimeZone) newyork.clone();
    if (!((BasicTimeZone) newyork).hasEquivalentTransitions(newyork2, jan1_1971, jan1_2011)) {
        errln("FAIL: Cloned TimeZone must have the same transitions");
    }
    if (!((BasicTimeZone) newyork).hasEquivalentTransitions(newyork2, jan1_1971, jan1_2011, true)) {
        errln("FAIL: Cloned TimeZone must have the same transitions");
    }
    // America/New_York and America/Los_Angeles has same DST start rules, but
    // raw offsets are different
    TimeZone losangeles = TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
    if (((BasicTimeZone) newyork).hasEquivalentTransitions(losangeles, jan1_2006, jan1_2011)) {
        errln("FAIL: New_York is not equivalent to Los Angeles, but returned true");
    }
}
Also used : 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) Test(org.junit.Test)

Example 2 with BasicTimeZone

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

the class TimeZoneRuleTest method verifyTransitions.

// Internal utility methods -----------------------------------------
/*
     * Check if a time shift really happens on each transition returned by getNextTransition or
     * getPreviousTransition in the specified time range
     */
private void verifyTransitions(TimeZone tz, long start, long end) {
    BasicTimeZone icutz = (BasicTimeZone) tz;
    long time;
    int[] before = new int[2];
    int[] after = new int[2];
    TimeZoneTransition tzt0;
    // Ascending
    tzt0 = null;
    time = start;
    while (true) {
        TimeZoneTransition tzt = icutz.getNextTransition(time, false);
        if (tzt == null) {
            break;
        }
        time = tzt.getTime();
        if (time >= end) {
            break;
        }
        icutz.getOffset(time, false, after);
        icutz.getOffset(time - 1, false, before);
        if (after[0] == before[0] && after[1] == before[1]) {
            errln("FAIL: False transition returned by getNextTransition for " + icutz.getID() + " at " + time);
        }
        if (tzt0 != null && (tzt0.getTo().getRawOffset() != tzt.getFrom().getRawOffset() || tzt0.getTo().getDSTSavings() != tzt.getFrom().getDSTSavings())) {
            errln("FAIL: TO rule of the previous transition does not match FROM rule of this transtion at " + time + " for " + icutz.getID());
        }
        tzt0 = tzt;
    }
    // Descending
    tzt0 = null;
    time = end;
    while (true) {
        TimeZoneTransition tzt = icutz.getPreviousTransition(time, false);
        if (tzt == null) {
            break;
        }
        time = tzt.getTime();
        if (time <= start) {
            break;
        }
        icutz.getOffset(time, false, after);
        icutz.getOffset(time - 1, false, before);
        if (after[0] == before[0] && after[1] == before[1]) {
            errln("FAIL: False transition returned by getPreviousTransition for " + icutz.getID() + " at " + time);
        }
        if (tzt0 != null && (tzt0.getFrom().getRawOffset() != tzt.getTo().getRawOffset() || tzt0.getFrom().getDSTSavings() != tzt.getTo().getDSTSavings())) {
            errln("FAIL: TO rule of the next transition does not match FROM rule in this transtion at " + time + " for " + icutz.getID());
        }
        tzt0 = tzt;
    }
}
Also used : BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneTransition(android.icu.util.TimeZoneTransition)

Example 3 with BasicTimeZone

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

the class TimeZoneRuleTest method compareTransitionsDescending.

/*
     * Compare all time transitions in 2 time zones in the specified time range in descending order
     */
private void compareTransitionsDescending(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 = end;
    while (true) {
        TimeZoneTransition tzt1 = z1.getPreviousTransition(time, inclusive);
        TimeZoneTransition tzt2 = z2.getPreviousTransition(time, inclusive);
        boolean inRange1 = false;
        boolean inRange2 = false;
        if (tzt1 != null) {
            if (tzt1.getTime() > start || (inclusive && tzt1.getTime() == start)) {
                inRange1 = true;
            }
        }
        if (tzt2 != null) {
            if (tzt2.getTime() > start || (inclusive && tzt2.getTime() == start)) {
                inRange2 = true;
            }
        }
        if (!inRange1 && !inRange2) {
            // No more transition in the range
            break;
        }
        if (!inRange1) {
            errln("FAIL: " + zid1 + " does not have any transitions before " + time + " after " + start);
            break;
        }
        if (!inRange2) {
            errln("FAIL: " + zid2 + " does not have any transitions before " + time + " after " + start);
            break;
        }
        if (tzt1.getTime() != tzt2.getTime()) {
            errln("FAIL: Last transition before " + 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 4 with BasicTimeZone

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

the class TimeZoneRuleTest method TestVTimeZoneRoundTripPartial.

/*
     * Write out time zone rules of OlsonTimeZone after a cutoff date into VTIMEZONE format,
     * create a new VTimeZone from the VTIMEZONE data, then compare transitions
     */
@Test
public void TestVTimeZoneRoundTripPartial() {
    long[] startTimes = new long[] { getUTCMillis(1900, Calendar.JANUARY, 1), getUTCMillis(1950, Calendar.JANUARY, 1), getUTCMillis(2020, Calendar.JANUARY, 1) };
    long endTime = getUTCMillis(2050, Calendar.JANUARY, 1);
    String[] tzids = getTestZIDs();
    for (int n = 0; n < startTimes.length; n++) {
        long startTime = startTimes[n];
        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]);
            VTimeZone vtz_new = null;
            try {
                // Write out VTIMEZONE
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                OutputStreamWriter writer = new OutputStreamWriter(baos);
                vtz_org.write(writer, startTime);
                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();
            } 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] + "(>=" + startTime + ") is not equivalent to its OlsonTimeZone corresponding.");
                    } else {
                        logln("VTimeZone for " + tzids[i] + "(>=" + startTime + ")  differs from its OlsonTimeZone corresponding with maximum transition time delta - " + maxDelta);
                    }
                }
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) VTimeZone(android.icu.util.VTimeZone) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneTransition(android.icu.util.TimeZoneTransition) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) Test(org.junit.Test)

Example 5 with BasicTimeZone

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

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