Search in sources :

Example 1 with TimeZoneTransition

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

the class TimeZoneRuleTest method hasEquivalentTransitions.

/*
     * Slightly modified version of BasicTimeZone#hasEquivalentTransitions.
     * This version returns true if transition time delta is within the given
     * delta range.
     */
private static boolean hasEquivalentTransitions(BasicTimeZone tz1, BasicTimeZone tz2, long start, long end, boolean ignoreDstAmount, int maxTransitionTimeDelta) {
    if (tz1.hasSameRules(tz2)) {
        return true;
    }
    // Check the offsets at the start time
    int[] offsets1 = new int[2];
    int[] offsets2 = new int[2];
    tz1.getOffset(start, false, offsets1);
    tz2.getOffset(start, false, offsets2);
    if (ignoreDstAmount) {
        if ((offsets1[0] + offsets1[1] != offsets2[0] + offsets2[1]) || (offsets1[1] != 0 && offsets2[1] == 0) || (offsets1[1] == 0 && offsets2[1] != 0)) {
            return false;
        }
    } else {
        if (offsets1[0] != offsets2[0] || offsets1[1] != offsets2[1]) {
            return false;
        }
    }
    // Check transitions in the range
    long time = start;
    while (true) {
        TimeZoneTransition tr1 = tz1.getNextTransition(time, false);
        TimeZoneTransition tr2 = tz2.getNextTransition(time, false);
        if (ignoreDstAmount) {
            // Skip a transition which only differ the amount of DST savings
            while (true) {
                if (tr1 != null && tr1.getTime() <= end && (tr1.getFrom().getRawOffset() + tr1.getFrom().getDSTSavings() == tr1.getTo().getRawOffset() + tr1.getTo().getDSTSavings()) && (tr1.getFrom().getDSTSavings() != 0 && tr1.getTo().getDSTSavings() != 0)) {
                    tr1 = tz1.getNextTransition(tr1.getTime(), false);
                } else {
                    break;
                }
            }
            while (true) {
                if (tr2 != null && tr2.getTime() <= end && (tr2.getFrom().getRawOffset() + tr2.getFrom().getDSTSavings() == tr2.getTo().getRawOffset() + tr2.getTo().getDSTSavings()) && (tr2.getFrom().getDSTSavings() != 0 && tr2.getTo().getDSTSavings() != 0)) {
                    tr2 = tz2.getNextTransition(tr2.getTime(), false);
                } else {
                    break;
                }
            }
        }
        boolean inRange1 = false;
        boolean inRange2 = false;
        if (tr1 != null) {
            if (tr1.getTime() <= end) {
                inRange1 = true;
            }
        }
        if (tr2 != null) {
            if (tr2.getTime() <= end) {
                inRange2 = true;
            }
        }
        if (!inRange1 && !inRange2) {
            // No more transition in the range
            break;
        }
        if (!inRange1 || !inRange2) {
            return false;
        }
        if (Math.abs(tr1.getTime() - tr2.getTime()) > maxTransitionTimeDelta) {
            return false;
        }
        if (ignoreDstAmount) {
            if (tr1.getTo().getRawOffset() + tr1.getTo().getDSTSavings() != tr2.getTo().getRawOffset() + tr2.getTo().getDSTSavings() || tr1.getTo().getDSTSavings() != 0 && tr2.getTo().getDSTSavings() == 0 || tr1.getTo().getDSTSavings() == 0 && tr2.getTo().getDSTSavings() != 0) {
                return false;
            }
        } else {
            if (tr1.getTo().getRawOffset() != tr2.getTo().getRawOffset() || tr1.getTo().getDSTSavings() != tr2.getTo().getDSTSavings()) {
                return false;
            }
        }
        time = tr1.getTime() > tr2.getTime() ? tr1.getTime() : tr2.getTime();
    }
    return true;
}
Also used : TimeZoneTransition(android.icu.util.TimeZoneTransition)

Example 2 with TimeZoneTransition

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

the class TimeZoneRuleTest method TestT6669.

@Test
public void TestT6669() {
    // getNext/PreviousTransition implementation in SimpleTimeZone
    // used to use a bad condition for detecting if DST is enabled or not.
    SimpleTimeZone stz = new SimpleTimeZone(0, "CustomID", Calendar.JANUARY, 1, Calendar.SUNDAY, 0, Calendar.JULY, 1, Calendar.SUNDAY, 0);
    // 2008-12-31T00:00:00
    long t = 1230681600000L;
    // 2009-01-04T00:00:00
    long expectedNext = 1231027200000L;
    // 2008-07-06T00:00:00
    long expectedPrev = 1215298800000L;
    TimeZoneTransition tzt = stz.getNextTransition(t, false);
    if (tzt == null) {
        errln("FAIL: No transition returned by getNextTransition.");
    } else if (tzt.getTime() != expectedNext) {
        errln("FAIL: Wrong transition time returned by getNextTransition - " + tzt.getTime() + " Expected: " + expectedNext);
    }
    tzt = stz.getPreviousTransition(t, true);
    if (tzt == null) {
        errln("FAIL: No transition returned by getPreviousTransition.");
    } else if (tzt.getTime() != expectedPrev) {
        errln("FAIL: Wrong transition time returned by getPreviousTransition - " + tzt.getTime() + " Expected: " + expectedPrev);
    }
}
Also used : TimeZoneTransition(android.icu.util.TimeZoneTransition) SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 3 with TimeZoneTransition

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

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

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

Aggregations

TimeZoneTransition (android.icu.util.TimeZoneTransition)20 BasicTimeZone (android.icu.util.BasicTimeZone)15 Test (org.junit.Test)8 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)5 InitialTimeZoneRule (android.icu.util.InitialTimeZoneRule)5 TimeZone (android.icu.util.TimeZone)5 TimeZoneRule (android.icu.util.TimeZoneRule)5 Date (java.util.Date)5 SimpleTimeZone (android.icu.util.SimpleTimeZone)4 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)4 Calendar (android.icu.util.Calendar)3 VTimeZone (android.icu.util.VTimeZone)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Instant (java.time.Instant)2 ZoneOffset (java.time.ZoneOffset)2 ZoneOffsetTransition (java.time.zone.ZoneOffsetTransition)2