Search in sources :

Example 31 with TimeZone

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

the class TimeZoneRegressionTest method TestT5280.

/**
 * Test setRawOffset works OK with system timezone
 */
@Test
public void TestT5280() {
    boolean isJdkZone = (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK);
    String[] tzids = TimeZone.getAvailableIDs();
    for (int i = 0; i < tzids.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(tzids[i]);
        boolean useDst = tz.useDaylightTime();
        // Increase offset for 30 minutes
        int newRawOffset = tz.getRawOffset() + 30 * 60 * 1000;
        try {
            tz.setRawOffset(newRawOffset);
        } catch (Exception e) {
            errln("FAIL: setRawOffset throws an exception");
        }
        int offset = tz.getRawOffset();
        if (offset != newRawOffset) {
            if (isJdkZone) {
                // JDK TimeZone#setRawOffset() only update the last rule, and getRawOffset() returns
                // the current raw offset. Therefore, they might be different.
                logln("Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            }
        }
        // Ticket#5917
        // Check if DST observation status is not unexpectedly changed.
        boolean newDst = tz.useDaylightTime();
        if (useDst != newDst) {
            errln("FAIL: Modified zone(" + tz.getID() + ") - useDaylightTime has changed from " + useDst + " to " + newDst);
        }
        // Make sure the offset is preserved in a clone
        TimeZone tzClone = (TimeZone) tz.clone();
        int offsetC = tzClone.getRawOffset();
        if (offsetC != newRawOffset) {
            if (isJdkZone) {
                logln("Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            }
        }
        if (offset != offsetC) {
            errln("FAIL: Different raw offset - Original:" + offset + ", Cloned:" + offsetC);
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) Test(org.junit.Test)

Example 32 with TimeZone

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

the class TimeZoneRegressionTest method Test4159922.

/**
 * TimeZone constructors allow null IDs.
 */
@Test
public void Test4159922() {
    TimeZone z = null;
    // allow null.
    try {
        z = TimeZone.getTimeZone(null);
        errln("FAIL: Null allowed in getTimeZone");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    z = TimeZone.getTimeZone("GMT");
    try {
        // {dlf} requiring cast for disambiguation is ok for compatibility since null
        // is not a valid argument to this API
        z.getDisplayName(false, TimeZone.SHORT, (ULocale) null);
        errln("FAIL: Null allowed in getDisplayName(3)");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    try {
        // {dlf} see above
        z.getDisplayName((ULocale) null);
        errln("FAIL: Null allowed in getDisplayName(1)");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    try {
        if (z.hasSameRules(null)) {
            errln("FAIL: hasSameRules returned true");
        }
    } catch (NullPointerException e) {
        errln("FAIL: Null NOT allowed in hasSameRules");
    }
    try {
        z.inDaylightTime(null);
        errln("FAIL: Null allowed in inDaylightTime");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    try {
        z.setID(null);
        errln("FAIL: Null allowed in setID");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    TimeZone save = TimeZone.getDefault();
    try {
        TimeZone.setDefault(null);
    } catch (NullPointerException e) {
        errln("FAIL: Null NOT allowed in setDefault");
    } finally {
        TimeZone.setDefault(save);
    }
    // SimpleTimeZone API
    SimpleTimeZone s = null;
    try {
        s = new SimpleTimeZone(0, null);
        errln("FAIL: Null allowed in SimpleTimeZone(2)");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    try {
        s = new SimpleTimeZone(0, null, 0, 1, 0, 0, 0, 1, 0, 0);
        errln("FAIL: Null allowed in SimpleTimeZone(10)");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    try {
        s = new SimpleTimeZone(0, null, 0, 1, 0, 0, 0, 1, 0, 0, 1000);
        errln("FAIL: Null allowed in SimpleTimeZone(11)");
    } catch (NullPointerException e) {
        System.out.print("");
    }
    if (s != null) {
        errln("FAIL: Did not get the expected Exception");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 33 with TimeZone

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

the class TimeZoneRegressionTest method TestJohannesburg.

// test bug #4265
@Test
public void TestJohannesburg() {
    String j_id = "Africa/Johannesburg";
    TimeZone johannesburg = TimeZone.getTimeZone(j_id);
    final int ONE_HOUR = 60 * 60 * 1000;
    // GMT+2 - NO DST
    int expectedOffset = ONE_HOUR * 2;
    int offset = johannesburg.getOffset(GregorianCalendar.AD, 2007, Calendar.JULY, 5, Calendar.THURSDAY, 0);
    if (offset != expectedOffset) {
        errln("FAIL: zone " + j_id + " returned offset in July " + offset + ", expected " + expectedOffset);
    } else {
        logln("OK: zone " + j_id + " returned offset in July: " + offset);
    }
    int offset2 = johannesburg.getOffset(GregorianCalendar.AD, 2007, Calendar.DECEMBER, 12, Calendar.WEDNESDAY, 0);
    if (offset2 != expectedOffset) {
        errln("FAIL: zone " + j_id + " returned offset in December " + offset2 + ", expected " + expectedOffset);
    } else {
        logln("OK: zone " + j_id + " returned offset in December: " + offset2);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) Test(org.junit.Test)

Example 34 with TimeZone

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

the class TimeZoneRegressionTest method TestJ449.

/**
 * Test to see if DateFormat understands zone equivalency groups.  It
 * might seem that this should be a DateFormat test, but it's really a
 * TimeZone test -- the changes to DateFormat are minor.
 *
 * We use two known, zones that are equivalent, where one zone has
 * localized name data, and the other doesn't, in some locale.
 */
@Test
public void TestJ449() {
    // not used String str;
    // Modify the following three as necessary.  The two IDs must
    // specify two zones in the same equivalency group.  One must have
    // locale data in 'loc'; the other must not.
    String idWithLocaleData = "America/Los_Angeles";
    // "US/Pacific";
    String idWithoutLocaleData = "PST";
    Locale loc = new Locale("en", "", "");
    TimeZone zoneWith = TimeZone.getTimeZone(idWithLocaleData);
    TimeZone zoneWithout = TimeZone.getTimeZone(idWithoutLocaleData);
    // Make sure we got valid zones
    if (!(zoneWith.getID().equals(idWithLocaleData) && zoneWithout.getID().equals(idWithoutLocaleData))) {
        warnln("Fail: Unable to create zones");
    } else {
        GregorianCalendar calWith = new GregorianCalendar(zoneWith);
        GregorianCalendar calWithout = new GregorianCalendar(zoneWithout);
        SimpleDateFormat fmt = new SimpleDateFormat("MMM d yyyy hh:mm a zzz", loc);
        Date date = new Date(0L);
        fmt.setCalendar(calWith);
        String strWith = fmt.format(date);
        fmt.setCalendar(calWithout);
        String strWithout = fmt.format(date);
        if (strWith.equals(strWithout)) {
            logln("Ok: " + idWithLocaleData + " -> " + strWith + "; " + idWithoutLocaleData + " -> " + strWithout);
        } else {
            errln("FAIL: " + idWithLocaleData + " -> " + strWith + "; " + idWithoutLocaleData + " -> " + strWithout);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 35 with TimeZone

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

the class TimeZoneRuleTest method TestRBTZTransition.

/*
     * Check if an OlsonTimeZone and its equivalent RBTZ have the exact same
     * transitions.
     */
@Test
public void TestRBTZTransition() {
    int[] STARTYEARS = { 1950, 1975, 2000, 2010 };
    String[] zids = getTestZIDs();
    for (int i = 0; i < zids.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(zids[i], TimeZone.TIMEZONE_ICU);
        if (tz == null) {
            break;
        }
        for (int j = 0; j < STARTYEARS.length; j++) {
            long startTime = getUTCMillis(STARTYEARS[j], Calendar.JANUARY, 1);
            TimeZoneRule[] rules = ((BasicTimeZone) tz).getTimeZoneRules(startTime);
            RuleBasedTimeZone rbtz = new RuleBasedTimeZone(tz.getID() + "(RBTZ)", (InitialTimeZoneRule) rules[0]);
            for (int k = 1; k < rules.length; k++) {
                rbtz.addTransitionRule(rules[k]);
            }
            // Compare the original OlsonTimeZone with the RBTZ starting the startTime for 20 years
            long until = getUTCMillis(STARTYEARS[j] + 20, Calendar.JANUARY, 1);
            // Ascending
            compareTransitionsAscending(tz, rbtz, startTime, until, false);
            // Ascending/inclusive
            compareTransitionsAscending(tz, rbtz, startTime + 1, until, true);
            // Descending
            compareTransitionsDescending(tz, rbtz, startTime, until, false);
            // Descending/inclusive
            compareTransitionsDescending(tz, rbtz, startTime + 1, until, true);
        }
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) 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