Search in sources :

Example 1 with VTimeZone

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

the class TimeZoneRuleTest method TestVTimeZoneHeaderProps.

/*
     * Write out time zone rules of OlsonTimeZone into VTIMEZONE format with RFC2445 header TZURL and
     * LAST-MODIFIED, create a new VTimeZone from the VTIMEZONE data to see if the headers are preserved.
     */
@Test
public void TestVTimeZoneHeaderProps() {
    String tzid = "America/Chicago";
    String tzurl = "http://source.icu-project.org";
    Date lastmod = new Date(getUTCMillis(2007, Calendar.JUNE, 1));
    VTimeZone vtz = VTimeZone.create(tzid);
    vtz.setTZURL(tzurl);
    vtz.setLastModified(lastmod);
    // Roundtrip conversion
    VTimeZone newvtz1 = null;
    try {
        // Write out VTIMEZONE
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(baos);
        vtz.write(writer);
        writer.close();
        byte[] vtzdata = baos.toByteArray();
        // Read VTIMEZONE
        ByteArrayInputStream bais = new ByteArrayInputStream(vtzdata);
        InputStreamReader reader = new InputStreamReader(bais);
        newvtz1 = VTimeZone.create(reader);
        reader.close();
        // Check if TZURL and LAST-MODIFIED headers are preserved
        if (!(tzurl.equals(newvtz1.getTZURL()))) {
            errln("FAIL: TZURL property is not preserved during the roundtrip conversion.  Before:" + tzurl + "/After:" + newvtz1.getTZURL());
        }
        if (!(lastmod.equals(newvtz1.getLastModified()))) {
            errln("FAIL: LAST-MODIFIED property is not preserved during the roundtrip conversion.  Before:" + lastmod.getTime() + "/After:" + newvtz1.getLastModified().getTime());
        }
    } catch (IOException ioe) {
        errln("FAIL: IO error while writing/reading VTIMEZONE data");
    }
    // Second roundtrip, with a cutoff
    VTimeZone newvtz2 = null;
    try {
        // Set different tzurl
        String newtzurl = "http://www.ibm.com";
        newvtz1.setTZURL(newtzurl);
        // Write out VTIMEZONE
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(baos);
        newvtz1.write(writer, getUTCMillis(2000, Calendar.JANUARY, 1));
        writer.close();
        byte[] vtzdata = baos.toByteArray();
        // Read VTIMEZONE
        ByteArrayInputStream bais = new ByteArrayInputStream(vtzdata);
        InputStreamReader reader = new InputStreamReader(bais);
        newvtz2 = VTimeZone.create(reader);
        reader.close();
        // Check if TZURL and LAST-MODIFIED headers are preserved
        if (!(newtzurl.equals(newvtz2.getTZURL()))) {
            errln("FAIL: TZURL property is not preserved during the second roundtrip conversion.  Before:" + newtzurl + "/After:" + newvtz2.getTZURL());
        }
        if (!(lastmod.equals(newvtz2.getLastModified()))) {
            errln("FAIL: LAST-MODIFIED property is not preserved during the second roundtrip conversion.  Before:" + lastmod.getTime() + "/After:" + newvtz2.getLastModified().getTime());
        }
    } catch (IOException ioe) {
        errln("FAIL: IO error while writing/reading VTIMEZONE data");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) VTimeZone(android.icu.util.VTimeZone) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Date(java.util.Date) Test(org.junit.Test)

Example 2 with VTimeZone

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

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

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

the class TimeZoneTest method Test11619_UnrecognizedTimeZoneID.

@Test
public void Test11619_UnrecognizedTimeZoneID() {
    VTimeZone vzone = VTimeZone.create("ABadTimeZoneId");
    TestFmwk.assertNull("", vzone);
}
Also used : VTimeZone(android.icu.util.VTimeZone) Test(org.junit.Test)

Example 5 with VTimeZone

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

the class TimeZoneRuleTest method TestVTimeZoneCoverage.

/*
     * API coverage test for VTimeZone
     */
@Test
public void TestVTimeZoneCoverage() {
    final String TZID = "Europe/Moscow";
    BasicTimeZone otz = (BasicTimeZone) TimeZone.getTimeZone(TZID, TimeZone.TIMEZONE_ICU);
    VTimeZone vtz = VTimeZone.create(TZID);
    // getOffset(era, year, month, day, dayOfWeek, milliseconds)
    int offset1 = otz.getOffset(GregorianCalendar.AD, 2007, Calendar.JULY, 1, Calendar.SUNDAY, 0);
    int offset2 = vtz.getOffset(GregorianCalendar.AD, 2007, Calendar.JULY, 1, Calendar.SUNDAY, 0);
    if (offset1 != offset2) {
        errln("FAIL: getOffset(int,int,int,int,int,int) returned different results in VTimeZone and OlsonTimeZone");
    }
    // getOffset(date, local, offsets)
    int[] offsets1 = new int[2];
    int[] offsets2 = new int[2];
    long t = System.currentTimeMillis();
    otz.getOffset(t, false, offsets1);
    vtz.getOffset(t, false, offsets2);
    if (offsets1[0] != offsets2[0] || offsets1[1] != offsets2[1]) {
        errln("FAIL: getOffset(long,boolean,int[]) returned different results in VTimeZone and OlsonTimeZone");
    }
    // getRawOffset
    if (otz.getRawOffset() != vtz.getRawOffset()) {
        errln("FAIL: getRawOffset returned different results in VTimeZone and OlsonTimeZone");
    }
    // inDaylightTime
    Date d = new Date();
    if (otz.inDaylightTime(d) != vtz.inDaylightTime(d)) {
        errln("FAIL: inDaylightTime returned different results in VTimeZone and OlsonTimeZone");
    }
    // useDaylightTime
    if (otz.useDaylightTime() != vtz.useDaylightTime()) {
        errln("FAIL: useDaylightTime returned different results in VTimeZone and OlsonTimeZone");
    }
    // setRawOffset
    final int RAW = -10 * HOUR;
    VTimeZone tmpvtz = (VTimeZone) vtz.clone();
    tmpvtz.setRawOffset(RAW);
    if (tmpvtz.getRawOffset() != RAW) {
        logln("setRawOffset is implemented");
    }
    // hasSameRules
    boolean bSame = otz.hasSameRules(vtz);
    logln("OlsonTimeZone#hasSameRules(VTimeZone) should return false always for now - actual: " + bSame);
    // getTZURL/setTZURL
    final String TZURL = "http://icu-project.org/timezone";
    String tzurl = vtz.getTZURL();
    if (tzurl != null) {
        errln("FAIL: getTZURL returned non-null value");
    }
    vtz.setTZURL(TZURL);
    tzurl = vtz.getTZURL();
    if (!TZURL.equals(tzurl)) {
        errln("FAIL: URL returned by getTZURL does not match the one set by setTZURL");
    }
    // getLastModified/setLastModified
    Date lastmod = vtz.getLastModified();
    if (lastmod != null) {
        errln("FAIL: getLastModified returned non-null value");
    }
    Date newdate = new Date();
    vtz.setLastModified(newdate);
    lastmod = vtz.getLastModified();
    if (!newdate.equals(lastmod)) {
        errln("FAIL: Date returned by getLastModified does not match the one set by setLastModified");
    }
    // getNextTransition/getPreviousTransition
    long base = getUTCMillis(2007, Calendar.JULY, 1);
    TimeZoneTransition tzt1 = otz.getNextTransition(base, true);
    TimeZoneTransition tzt2 = vtz.getNextTransition(base, true);
    if (tzt1.equals(tzt2)) {
        errln("FAIL: getNextTransition returned different results in VTimeZone and OlsonTimeZone");
    }
    tzt1 = otz.getPreviousTransition(base, false);
    tzt2 = vtz.getPreviousTransition(base, false);
    if (tzt1.equals(tzt2)) {
        errln("FAIL: getPreviousTransition returned different results in VTimeZone and OlsonTimeZone");
    }
    // hasEquivalentTransitions
    long time1 = getUTCMillis(1950, Calendar.JANUARY, 1);
    long time2 = getUTCMillis(2020, Calendar.JANUARY, 1);
    if (!vtz.hasEquivalentTransitions(otz, time1, time2)) {
        errln("FAIL: hasEquivalentTransitons returned false for the same time zone");
    }
    // getTimeZoneRules
    TimeZoneRule[] rulesetAll = vtz.getTimeZoneRules();
    TimeZoneRule[] ruleset1 = vtz.getTimeZoneRules(time1);
    TimeZoneRule[] ruleset2 = vtz.getTimeZoneRules(time2);
    if (rulesetAll.length < ruleset1.length || ruleset1.length < ruleset2.length) {
        errln("FAIL: Number of rules returned by getRules is invalid");
    }
    int[] offsets_vtzc = new int[2];
    VTimeZone vtzc = VTimeZone.create("PST");
    vtzc.getOffsetFromLocal(Calendar.getInstance(vtzc).getTimeInMillis(), VTimeZone.LOCAL_STD, VTimeZone.LOCAL_STD, offsets_vtzc);
    if (offsets_vtzc[0] > offsets_vtzc[1]) {
        errln("Error getOffsetFromLocal()");
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneTransition(android.icu.util.TimeZoneTransition) VTimeZone(android.icu.util.VTimeZone) Date(java.util.Date) Test(org.junit.Test)

Aggregations

VTimeZone (android.icu.util.VTimeZone)11 Test (org.junit.Test)9 BasicTimeZone (android.icu.util.BasicTimeZone)6 IOException (java.io.IOException)6 Date (java.util.Date)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4 JavaTimeZone (android.icu.impl.JavaTimeZone)3 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)3 SimpleTimeZone (android.icu.util.SimpleTimeZone)3 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)3 TimeZone (android.icu.util.TimeZone)3 TimeZoneTransition (android.icu.util.TimeZoneTransition)3 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)3 StringReader (java.io.StringReader)2 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)1 Calendar (android.icu.util.Calendar)1 GregorianCalendar (android.icu.util.GregorianCalendar)1