Search in sources :

Example 26 with SimpleTimeZone

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

the class TimeZoneRegressionTest method Test4154537.

/**
 * SimpleTimeZone.hasSameRules() doesn't work for zones with no DST
 * and different DST parameters.
 */
@Test
public void Test4154537() {
    // tz1 and tz2 have no DST and different rule parameters
    SimpleTimeZone tz1 = new SimpleTimeZone(0, "1", 0, 0, 0, 0, 2, 0, 0, 0);
    SimpleTimeZone tz2 = new SimpleTimeZone(0, "2", 1, 0, 0, 0, 3, 0, 0, 0);
    // tza and tzA have the same rule params
    SimpleTimeZone tza = new SimpleTimeZone(0, "a", 0, 1, 0, 0, 3, 2, 0, 0);
    SimpleTimeZone tzA = new SimpleTimeZone(0, "A", 0, 1, 0, 0, 3, 2, 0, 0);
    // tzb differs from tza
    SimpleTimeZone tzb = new SimpleTimeZone(0, "b", 0, 1, 0, 0, 3, 1, 0, 0);
    if (tz1.useDaylightTime() || tz2.useDaylightTime() || !tza.useDaylightTime() || !tzA.useDaylightTime() || !tzb.useDaylightTime()) {
        errln("Test is broken -- rewrite it");
    }
    if (!tza.hasSameRules(tzA) || tza.hasSameRules(tzb)) {
        errln("Fail: hasSameRules() broken for zones with rules");
    }
    if (!tz1.hasSameRules(tz2)) {
        errln("Fail: hasSameRules() returns false for zones without rules");
        errln("zone 1 = " + tz1);
        errln("zone 2 = " + tz2);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 27 with SimpleTimeZone

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

the class TimeZoneRegressionTest method Test4184229.

/**
 * SimpleTimeZone allows invalid DOM values.
 */
// Current orgnaization of data in zoneinfor.res allows negative
// values from DOM so comment these tests out
@Test
public void Test4184229() {
    SimpleTimeZone zone = null;
    try {
        zone = new SimpleTimeZone(0, "A", 0, -1, 0, 0, 0, 0, 0, 0);
        errln("Failed. No exception has been thrown for DOM -1 startDay");
    } catch (IllegalArgumentException e) {
        logln("(a) " + e.getMessage());
    }
    try {
        zone = new SimpleTimeZone(0, "A", 0, 0, 0, 0, 0, -1, 0, 0);
        errln("Failed. No exception has been thrown for DOM -1 endDay");
    } catch (IllegalArgumentException e) {
        logln("(b) " + e.getMessage());
    }
    try {
        zone = new SimpleTimeZone(0, "A", 0, -1, 0, 0, 0, 0, 0, 0, 1000);
        errln("Failed. No exception has been thrown for DOM -1 startDay +savings");
    } catch (IllegalArgumentException e) {
        logln("(c) " + e.getMessage());
    }
    try {
        zone = new SimpleTimeZone(0, "A", 0, 0, 0, 0, 0, -1, 0, 0, 1000);
        errln("Failed. No exception has been thrown for DOM -1 endDay +savings");
    } catch (IllegalArgumentException e) {
        logln("(d) " + e.getMessage());
    }
    // Make a valid constructor call for subsequent tests.
    zone = new SimpleTimeZone(0, "A", 0, 1, 0, 0, 0, 1, 0, 0);
    try {
        zone.setStartRule(0, -1, 0, 0);
        errln("Failed. No exception has been thrown for DOM -1 setStartRule +savings");
    } catch (IllegalArgumentException e) {
        logln("(e) " + e.getMessage());
    }
    try {
        zone.setStartRule(0, -1, 0);
        errln("Failed. No exception has been thrown for DOM -1 setStartRule");
    } catch (IllegalArgumentException e) {
        logln("(f) " + e.getMessage());
    }
    try {
        zone.setEndRule(0, -1, 0, 0);
        errln("Failed. No exception has been thrown for DOM -1 setEndRule +savings");
    } catch (IllegalArgumentException e) {
        logln("(g) " + e.getMessage());
    }
    try {
        zone.setEndRule(0, -1, 0);
        errln("Failed. No exception has been thrown for DOM -1 setEndRule");
    } catch (IllegalArgumentException e) {
        logln("(h) " + e.getMessage());
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 28 with SimpleTimeZone

use of android.icu.util.SimpleTimeZone 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 29 with SimpleTimeZone

use of android.icu.util.SimpleTimeZone 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 30 with SimpleTimeZone

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

Aggregations

SimpleTimeZone (android.icu.util.SimpleTimeZone)39 Test (org.junit.Test)35 TimeZone (android.icu.util.TimeZone)17 Date (java.util.Date)15 GregorianCalendar (android.icu.util.GregorianCalendar)14 BasicTimeZone (android.icu.util.BasicTimeZone)10 Calendar (android.icu.util.Calendar)9 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)9 JavaTimeZone (android.icu.impl.JavaTimeZone)7 VTimeZone (android.icu.util.VTimeZone)7 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)7 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 InitialTimeZoneRule (android.icu.util.InitialTimeZoneRule)5 CalendarAstronomer (android.icu.impl.CalendarAstronomer)4 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)4 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)4 DateFormat (android.icu.text.DateFormat)3 DateTimeRule (android.icu.util.DateTimeRule)3 TimeZoneTransition (android.icu.util.TimeZoneTransition)3 ULocale (android.icu.util.ULocale)3