Search in sources :

Example 1 with JavaTimeZone

use of android.icu.impl.JavaTimeZone 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 2 with JavaTimeZone

use of android.icu.impl.JavaTimeZone in project j2objc by google.

the class TimeZone method getDefault.

/**
 * Gets the default <code>TimeZone</code> for this host.
 * The source of the default <code>TimeZone</code>
 * may vary with implementation.
 * @return a default <code>TimeZone</code>.
 */
public static TimeZone getDefault() {
    // Android patch (http://b/30979219) start.
    // Avoid race condition by copying defaultZone to a local variable.
    TimeZone result = defaultZone;
    if (result == null) {
        // icu.util.TimeZone.clearCachedDefault() so always acquires them in order (1) then (2).
        synchronized (java.util.TimeZone.class) {
            synchronized (TimeZone.class) {
                result = defaultZone;
                if (result == null) {
                    if (TZ_IMPL == TIMEZONE_JDK) {
                        result = new JavaTimeZone();
                    } else {
                        java.util.TimeZone temp = java.util.TimeZone.getDefault();
                        result = getFrozenTimeZone(temp.getID());
                    }
                    defaultZone = result;
                }
            }
        }
    // Android patch (http://b/30937209) end.
    }
    return result.cloneAsThawed();
// Android patch (http://b/30979219) end.
}
Also used : JavaTimeZone(android.icu.impl.JavaTimeZone) JavaTimeZone(android.icu.impl.JavaTimeZone)

Example 3 with JavaTimeZone

use of android.icu.impl.JavaTimeZone 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

JavaTimeZone (android.icu.impl.JavaTimeZone)3 BasicTimeZone (android.icu.util.BasicTimeZone)2 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)2 SimpleTimeZone (android.icu.util.SimpleTimeZone)2 TimeZone (android.icu.util.TimeZone)2 VTimeZone (android.icu.util.VTimeZone)2 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)2 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1