Search in sources :

Example 96 with TimeZone

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

the class TimeZoneTest method TestGenericAPI.

@Test
public void TestGenericAPI() {
    String id = "NewGMT";
    int offset = 12345;
    SimpleTimeZone zone = new SimpleTimeZone(offset, id);
    if (zone.useDaylightTime())
        errln("FAIL: useDaylightTime should return false");
    TimeZone zoneclone = (TimeZone) zone.clone();
    if (!zoneclone.equals(zone))
        errln("FAIL: clone or operator== failed");
    zoneclone.setID("abc");
    if (zoneclone.equals(zone))
        errln("FAIL: clone or operator!= failed");
    // delete zoneclone;
    zoneclone = (TimeZone) zone.clone();
    if (!zoneclone.equals(zone))
        errln("FAIL: clone or operator== failed");
    zoneclone.setRawOffset(45678);
    if (zoneclone.equals(zone))
        errln("FAIL: clone or operator!= failed");
    // C++ only
    /*
          SimpleTimeZone copy(*zone);
          if (!(copy == *zone)) errln("FAIL: copy constructor or operator== failed");
          copy = *(SimpleTimeZone*)zoneclone;
          if (!(copy == *zoneclone)) errln("FAIL: assignment operator or operator== failed");
          */
    TimeZone saveDefault = TimeZone.getDefault();
    TimeZone.setDefault(zone);
    TimeZone defaultzone = TimeZone.getDefault();
    if (defaultzone == zone)
        errln("FAIL: Default object is identical, not clone");
    // Android patch (http://b/28949992) start.
    /*
         * {icu}TimeZone.setDefault() calls {java}TimeZone.setDefault().
         * On Android {java}TimeZone.setDefault() clears the cached ICU default timezone by calling
         * {icu}TimeZone.clearCachedDefault().
         * Due to this, and some of the logic in {icu}TimeZone.getDefault(), it is not possible to
         * guarantee on Android that {icu}TimeZone.getDefault() returns something that is equal to
         * the object passed to setDefault().
         * {icu}TimeZone.setDefault() is not public / supported on Android.
         */
    // if (!defaultzone.equals(zone)) errln("FAIL: Default object is not equal");
    // Android patch (http://b/28949992) end.
    TimeZone.setDefault(saveDefault);
    // delete defaultzone;
    // delete zoneclone;
    // // ICU 2.6 Coverage
    // logln(zone.toString());
    // logln(zone.getDisplayName());
    // SimpleTimeZoneAdapter stza = new SimpleTimeZoneAdapter((SimpleTimeZone) TimeZone.getTimeZone("GMT"));
    // stza.setID("Foo");
    // if (stza.hasSameRules(java.util.TimeZone.getTimeZone("GMT"))) {
    // errln("FAIL: SimpleTimeZoneAdapter.hasSameRules");
    // }
    // stza.setRawOffset(3000);
    // offset = stza.getOffset(GregorianCalendar.BC, 2001, Calendar.DECEMBER,
    // 25, Calendar.TUESDAY, 12*60*60*1000);
    // if (offset != 3000) {
    // errln("FAIL: SimpleTimeZoneAdapter.getOffset");
    // }
    // SimpleTimeZoneAdapter dup = (SimpleTimeZoneAdapter) stza.clone();
    // if (stza.hashCode() != dup.hashCode()) {
    // errln("FAIL: SimpleTimeZoneAdapter.hashCode");
    // }
    // if (!stza.equals(dup)) {
    // errln("FAIL: SimpleTimeZoneAdapter.equals");
    // }
    // logln(stza.toString());
    String tzver = TimeZone.getTZDataVersion();
    if (tzver.length() != 5) /* 4 digits + 1 letter */
    {
        errln("FAIL: getTZDataVersion returned " + tzver);
    } else {
        logln("PASS: tzdata version: " + tzver);
    }
}
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) Test(org.junit.Test)

Example 97 with TimeZone

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

the class TimeZoneTest method TestOddTimeZoneNames.

@Test
public void TestOddTimeZoneNames() {
    for (int i = 0; i < timeZoneTestNames.length; i += 2) {
        String funkyName = timeZoneTestNames[i];
        String correctName = timeZoneTestNames[i + 1];
        TimeZone ftz = TimeZone.getTimeZone(funkyName);
        TimeZone ctz = TimeZone.getTimeZone(correctName);
        String fdn = ftz.getDisplayName();
        long fro = ftz.getRawOffset();
        long fds = ftz.getDSTSavings();
        boolean fdy = ftz.useDaylightTime();
        String cdn = ctz.getDisplayName();
        long cro = ctz.getRawOffset();
        long cds = ctz.getDSTSavings();
        boolean cdy = ctz.useDaylightTime();
        if (!fdn.equals(cdn)) {
            logln("display name (" + funkyName + ", " + correctName + ") expected: " + cdn + " but got: " + fdn);
        } else if (fro != cro) {
            logln("offset (" + funkyName + ", " + correctName + ") expected: " + cro + " but got: " + fro);
        } else if (fds != cds) {
            logln("daylight (" + funkyName + ", " + correctName + ") expected: " + cds + " but got: " + fds);
        } else if (fdy != cdy) {
            logln("uses daylight (" + funkyName + ", " + correctName + ") expected: " + cdy + " but got: " + fdy);
        } else {
        // no error, assume we're referencing the same internal java object
        }
    }
}
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) Test(org.junit.Test)

Example 98 with TimeZone

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

the class TimeZoneTest method TestFreezable.

/*
     * Test case for Freezable
     */
@Test
public void TestFreezable() {
    // Test zones - initially thawed
    TimeZone[] ZA1 = { TimeZone.getDefault(), TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU), TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_JDK), new SimpleTimeZone(0, "stz"), new RuleBasedTimeZone("rbtz", new InitialTimeZoneRule("rbtz0", 0, 0)), VTimeZone.create("America/New_York") };
    checkThawed(ZA1, "ZA1");
    // freeze
    for (int i = 0; i < ZA1.length; i++) {
        ZA1[i].freeze();
    }
    checkFrozen(ZA1, "ZA1(frozen)");
    // Test zones - initially frozen
    final TimeZone[] ZA2 = { TimeZone.GMT_ZONE, TimeZone.UNKNOWN_ZONE, TimeZone.getFrozenTimeZone("America/Los_Angeles"), new SimpleTimeZone(3600000, "frz_stz").freeze(), new RuleBasedTimeZone("frz_rbtz", new InitialTimeZoneRule("frz_rbtz0", 3600000, 0)).freeze(), VTimeZone.create("Asia/Tokyo").freeze() };
    checkFrozen(ZA2, "ZA2");
    TimeZone[] ZA2_thawed = new TimeZone[ZA2.length];
    // create thawed clone
    for (int i = 0; i < ZA2_thawed.length; i++) {
        ZA2_thawed[i] = ZA2[i].cloneAsThawed();
    }
    checkThawed(ZA2_thawed, "ZA2(thawed)");
}
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) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Test(org.junit.Test)

Example 99 with TimeZone

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

the class TimeZoneTest method checkFrozen.

private void checkFrozen(TimeZone[] frozenZones, String zaName) {
    for (int i = 0; i < frozenZones.length; i++) {
        if (!frozenZones[i].isFrozen()) {
            errln("Fail: " + zaName + "[" + i + "] is not frozen.");
        }
        // clone
        TimeZone copy = (TimeZone) frozenZones[i].clone();
        if (frozenZones[i] != copy) {
            errln("Fail: " + zaName + "[" + i + "] - clone does not return the object itself.");
        }
        // cloneAsThawed
        TimeZone thawed = (TimeZone) frozenZones[i].cloneAsThawed();
        if (thawed.isFrozen() || !frozenZones[i].equals(thawed)) {
            errln("Fail: " + zaName + "[" + i + "] - cloneAsThawed does not work.");
        }
        // setID
        try {
            String newID = "foo";
            frozenZones[i].setID(newID);
            errln("Fail: " + zaName + "[" + i + "] - setID must throw UnsupportedOperationException.");
        } catch (UnsupportedOperationException e) {
        // OK
        }
        // setRawOffset
        if (!(frozenZones[i] instanceof RuleBasedTimeZone)) {
            // RuleBasedTimeZone does not supprot setRawOffset
            try {
                int newOffset = -3600000;
                frozenZones[i].setRawOffset(newOffset);
                errln("Fail: " + zaName + "[" + i + "] - setRawOffset must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        }
        if (frozenZones[i] instanceof SimpleTimeZone) {
            SimpleTimeZone stz = (SimpleTimeZone) frozenZones[i];
            // setDSTSavings
            try {
                int newDSTSavings = 1800000;
                stz.setDSTSavings(newDSTSavings);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setStartRule
            try {
                stz.setStartRule(Calendar.JANUARY, -1, Calendar.SUNDAY, 0);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setEndRule
            try {
                stz.setEndRule(Calendar.DECEMBER, 1, Calendar.SUNDAY, 0);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setEndRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setStartYear
            try {
                stz.setStartYear(2000);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartYear must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        } else if (frozenZones[i] instanceof RuleBasedTimeZone) {
            RuleBasedTimeZone rbtz = (RuleBasedTimeZone) frozenZones[i];
            // addTransitionRule
            try {
                TimeArrayTimeZoneRule tr1 = new TimeArrayTimeZoneRule("tr1", 7200000, 0, new long[] { 0 }, DateTimeRule.UTC_TIME);
                rbtz.addTransitionRule(tr1);
                errln("Fail: (RuleBasedTimeZone)" + zaName + "[" + i + "] - addTransitionRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        } else if (frozenZones[i] instanceof VTimeZone) {
            VTimeZone vtz = (VTimeZone) frozenZones[i];
            // setTZURL
            try {
                String tzUrl = "http://icu-project.org/timezone";
                vtz.setTZURL(tzUrl);
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setLastModified
            try {
                Date d = new Date();
                vtz.setLastModified(d);
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        }
    }
}
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)

Example 100 with TimeZone

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

the class TimeZoneTest method TestGetAvailableIDs913.

// Test getAvailableID API
@Test
public void TestGetAvailableIDs913() {
    StringBuffer buf = new StringBuffer("TimeZone.getAvailableIDs() = { ");
    String[] s = TimeZone.getAvailableIDs();
    for (int i = 0; i < s.length; ++i) {
        if (i > 0)
            buf.append(", ");
        buf.append(s[i]);
    }
    buf.append(" };");
    logln(buf.toString());
    buf.setLength(0);
    buf.append("TimeZone.getAvailableIDs(GMT+02:00) = { ");
    s = TimeZone.getAvailableIDs(+2 * 60 * 60 * 1000);
    for (int i = 0; i < s.length; ++i) {
        if (i > 0)
            buf.append(", ");
        buf.append(s[i]);
    }
    buf.append(" };");
    logln(buf.toString());
    TimeZone tz = TimeZone.getTimeZone("PST");
    if (tz != null)
        logln("getTimeZone(PST) = " + tz.getID());
    else
        errln("FAIL: getTimeZone(PST) = null");
    tz = TimeZone.getTimeZone("America/Los_Angeles");
    if (tz != null)
        logln("getTimeZone(America/Los_Angeles) = " + tz.getID());
    else
        errln("FAIL: getTimeZone(PST) = null");
    // Bug 4096694
    tz = TimeZone.getTimeZone("NON_EXISTENT");
    if (tz == null)
        errln("FAIL: getTimeZone(NON_EXISTENT) = null");
    else if (!tz.getID().equals(TimeZone.UNKNOWN_ZONE_ID))
        errln("FAIL: getTimeZone(NON_EXISTENT) = " + tz.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) Test(org.junit.Test)

Aggregations

TimeZone (android.icu.util.TimeZone)125 Test (org.junit.Test)98 SimpleTimeZone (android.icu.util.SimpleTimeZone)76 Date (java.util.Date)63 GregorianCalendar (android.icu.util.GregorianCalendar)49 Calendar (android.icu.util.Calendar)42 BasicTimeZone (android.icu.util.BasicTimeZone)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)36 ULocale (android.icu.util.ULocale)31 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)24 VTimeZone (android.icu.util.VTimeZone)23 DateFormat (android.icu.text.DateFormat)19 JavaTimeZone (android.icu.impl.JavaTimeZone)18 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)15 FieldPosition (java.text.FieldPosition)15 BuddhistCalendar (android.icu.util.BuddhistCalendar)13 ParsePosition (java.text.ParsePosition)13 ChineseCalendar (android.icu.util.ChineseCalendar)12 ParseException (java.text.ParseException)12