Search in sources :

Example 26 with TimeZone

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

the class TimeZoneTest method TestGetOffsetDate.

@Test
public void TestGetOffsetDate() {
    Calendar cal = Calendar.getInstance();
    cal.set(1997, Calendar.JANUARY, 30);
    long date = cal.getTimeInMillis();
    TimeZone tz_icu = TimeZone.getTimeZone("America/Los_Angeles");
    int offset = tz_icu.getOffset(date);
    if (offset != -28800000) {
        errln("expected offset -28800000, got: " + offset);
    }
    cal.set(1997, Calendar.JULY, 30);
    date = cal.getTimeInMillis();
    offset = tz_icu.getOffset(date);
    if (offset != -25200000) {
        errln("expected offset -25200000, got: " + offset);
    }
}
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) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Test(org.junit.Test)

Example 27 with TimeZone

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

the class TimeZoneTest method TestDisplayName2.

@Test
public void TestDisplayName2() {
    Date now = new Date();
    String[] timezones = { "America/Chicago", "Europe/Moscow", "Europe/Rome", "Asia/Shanghai", "WET" };
    String[] locales = { "en", "fr", "de", "ja", "zh_TW", "zh_Hans" };
    for (int j = 0; j < locales.length; ++j) {
        ULocale locale = new ULocale(locales[j]);
        for (int i = 0; i < timezones.length; ++i) {
            TimeZone tz = TimeZone.getTimeZone(timezones[i]);
            String displayName0 = tz.getDisplayName(locale);
            SimpleDateFormat dt = new SimpleDateFormat("vvvv", locale);
            dt.setTimeZone(tz);
            // date value _does_ matter if we fallback to GMT
            String displayName1 = dt.format(now);
            logln(locale.getDisplayName() + ", " + tz.getID() + ": " + displayName0);
            if (!displayName1.equals(displayName0)) {
                // time.
                if (tz.inDaylightTime(now)) {
                    // Try getDisplayName with daylight argument
                    displayName0 = tz.getDisplayName(true, TimeZone.LONG_GENERIC, locale);
                }
                if (!displayName1.equals(displayName0)) {
                    errln(locale.getDisplayName() + ", " + tz.getID() + ": expected " + displayName1 + " but got: " + displayName0);
                }
            }
        }
    }
}
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) ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 28 with TimeZone

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

the class TimeZoneTest method TestSetDefault.

@Test
public void TestSetDefault() {
    java.util.TimeZone save = java.util.TimeZone.getDefault();
    /*
         * America/Caracs (Venezuela) changed the base offset from -4:00 to
         * -4:30 on Dec 9, 2007.
         */
    TimeZone icuCaracas = TimeZone.getTimeZone("America/Caracas", TimeZone.TIMEZONE_ICU);
    java.util.TimeZone jdkCaracas = java.util.TimeZone.getTimeZone("America/Caracas");
    // Set JDK America/Caracas as the default
    java.util.TimeZone.setDefault(jdkCaracas);
    java.util.Calendar jdkCal = java.util.Calendar.getInstance();
    jdkCal.clear();
    jdkCal.set(2007, java.util.Calendar.JANUARY, 1);
    int rawOffset = jdkCal.get(java.util.Calendar.ZONE_OFFSET);
    int dstSavings = jdkCal.get(java.util.Calendar.DST_OFFSET);
    int[] offsets = new int[2];
    icuCaracas.getOffset(jdkCal.getTime().getTime(), /*jdkCal.getTimeInMillis()*/
    false, offsets);
    boolean isTimeZoneSynchronized = true;
    if (rawOffset != offsets[0] || dstSavings != offsets[1]) {
        // JDK time zone rule is out of sync...
        logln("Rule for JDK America/Caracas is not same with ICU.  Skipping the rest.");
        isTimeZoneSynchronized = false;
    }
    if (isTimeZoneSynchronized) {
        // If JDK America/Caracas uses the same rule with ICU,
        // the following code should work well.
        TimeZone.setDefault(icuCaracas);
        // Create a new JDK calendar instance again.
        // This calendar should reflect the new default
        // set by ICU TimeZone#setDefault.
        jdkCal = java.util.Calendar.getInstance();
        jdkCal.clear();
        jdkCal.set(2007, java.util.Calendar.JANUARY, 1);
        rawOffset = jdkCal.get(java.util.Calendar.ZONE_OFFSET);
        dstSavings = jdkCal.get(java.util.Calendar.DST_OFFSET);
        if (rawOffset != offsets[0] || dstSavings != offsets[1]) {
            errln("ERROR: Got offset [raw:" + rawOffset + "/dst:" + dstSavings + "] Expected [raw:" + offsets[0] + "/dst:" + offsets[1] + "]");
        }
    }
    // Restore the original JDK time zone
    java.util.TimeZone.setDefault(save);
}
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 29 with TimeZone

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

the class TimeZoneOffsetLocalTest method TestGetOffsetAroundTransition.

/*
     * Testing getOffset APIs around rule transition by local standard/wall time.
     */
@Test
public void TestGetOffsetAroundTransition() {
    final int HOUR = 60 * 60 * 1000;
    final int MINUTE = 60 * 1000;
    int[][] DATES = { { 2006, Calendar.APRIL, 2, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 2, 00, 2 * HOUR }, { 2006, Calendar.APRIL, 2, 2, 30, 2 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 3, 00, 3 * HOUR }, { 2006, Calendar.APRIL, 2, 3, 30, 3 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 0, 30, 0 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 1, 00, 1 * HOUR }, { 2006, Calendar.OCTOBER, 29, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 2, 00, 2 * HOUR }, { 2006, Calendar.OCTOBER, 29, 2, 30, 2 * HOUR + 30 * MINUTE } };
    // Expected offsets by getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)
    int[] OFFSETS1 = { // April 2, 2006
    -8 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, // October 29, 2006
    -7 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR };
    // Expected offsets by getOffset(long time, boolean local, int[] offsets) with local = true
    // or getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
    // with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
    int[][] OFFSETS2 = { // April 2, 2006
    { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // Oct 29, 2006
    { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
    // Expected offsets by getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
    // with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
    int[][] OFFSETS3 = { // April 2, 2006
    { -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // October 29, 2006
    { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
    int[] offsets = new int[2];
    TimeZone utc = TimeZone.getTimeZone("UTC");
    Calendar cal = Calendar.getInstance(utc);
    cal.clear();
    // Set up TimeZone objects - OlsonTimeZone, SimpleTimeZone and RuleBasedTimeZone
    BasicTimeZone[] TESTZONES = new BasicTimeZone[3];
    TESTZONES[0] = (BasicTimeZone) TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
    TESTZONES[1] = new SimpleTimeZone(-8 * HOUR, "Simple Pacific Time", Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR);
    InitialTimeZoneRule ir = new InitialTimeZoneRule(// Initial time Name
    "Pacific Standard Time", // Raw offset
    -8 * HOUR, // DST saving amount
    0 * HOUR);
    RuleBasedTimeZone rbPT = new RuleBasedTimeZone("Rule based Pacific Time", ir);
    DateTimeRule dtr;
    AnnualTimeZoneRule atzr;
    final int STARTYEAR = 2000;
    dtr = new DateTimeRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, // 1st Sunday in April, at 2AM wall time
    DateTimeRule.WALL_TIME);
    atzr = new AnnualTimeZoneRule("Pacific Daylight Time", -8 * HOUR, /* rawOffset */
    1 * HOUR, /* dstSavings */
    dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
    rbPT.addTransitionRule(atzr);
    dtr = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR, // last Sunday in October, at 2AM wall time
    DateTimeRule.WALL_TIME);
    atzr = new AnnualTimeZoneRule("Pacific Standard Time", -8 * HOUR, /* rawOffset */
    0, /* dstSavings */
    dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
    rbPT.addTransitionRule(atzr);
    TESTZONES[2] = rbPT;
    // Calculate millis
    long[] MILLIS = new long[DATES.length];
    for (int i = 0; i < DATES.length; i++) {
        cal.clear();
        cal.set(DATES[i][0], DATES[i][1], DATES[i][2], DATES[i][3], DATES[i][4]);
        MILLIS[i] = cal.getTimeInMillis();
    }
    DateFormat df = DateFormat.getInstance();
    df.setTimeZone(utc);
    // Test getOffset(int era, int year, int month, int day, int dayOfWeek, int millis)
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int d = 0; d < DATES.length; d++) {
            int offset = TESTZONES[i].getOffset(GregorianCalendar.AD, DATES[d][0], DATES[d][1], DATES[d][2], Calendar.SUNDAY, DATES[d][5]);
            if (offset != OFFSETS1[d]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[d])) + "(standard) - Got: " + offset + " Expected: " + OFFSETS1[d]);
            }
        }
    }
    // Test getOffset(long time, boolean local, int[] offsets) with local=true
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffset(MILLIS[m], true, offsets);
            if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_STD, BasicTimeZone.LOCAL_STD, offsets);
            if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/STD/STD) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_DST, BasicTimeZone.LOCAL_DST, offsets);
            if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/DST/DST) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_FORMER/duplicatedTimeOpt = LOCAL_LATTER
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_FORMER, BasicTimeZone.LOCAL_LATTER, offsets);
            if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/FORMER/LATTER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_LATTER/duplicatedTimeOpt = LOCAL_FORMER
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_LATTER, BasicTimeZone.LOCAL_FORMER, offsets);
            if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/LATTER/FORMER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
            }
        }
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date) DateTimeRule(android.icu.util.DateTimeRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 30 with TimeZone

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

the class TimeZoneRegressionTest method TestJ5134.

/**
 * getOffset returns wrong offset for days in early 20th century
 */
@Test
public void TestJ5134() {
    GregorianCalendar testCal = (GregorianCalendar) Calendar.getInstance();
    TimeZone icuEastern = TimeZone.getTimeZone("America/New_York");
    testCal.setTimeZone(icuEastern);
    testCal.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
    long time = testCal.getTimeInMillis();
    int offset = icuEastern.getOffset(time);
    if (offset != -18000000) {
        errln("FAIL: UTC offset in time zone America/New_York on Jan 1, 1900 -> " + offset);
    }
    boolean isDst = icuEastern.inDaylightTime(new Date(time));
    if (isDst) {
        errln("FAIL: DST is observed in time zone America/New_York on Jan 1, 1900");
    }
    if (System.getProperty("java.vendor", "").startsWith("IBM") && System.getProperty("java.version", "").equals("1.4.1")) {
        // IBM JDK 1.4.1 has a bug and fails to run this test case.
        return;
    }
    java.util.TimeZone jdkEastern = java.util.TimeZone.getTimeZone("America/New_York");
    // Compare offset and DST observation with JDK and ICU for 50 years since 1900
    testCal.add(Calendar.YEAR, 50);
    long endTime = testCal.getTimeInMillis();
    int jdkOffset;
    boolean isDstJdk;
    while (time < endTime) {
        offset = icuEastern.getOffset(time);
        jdkOffset = jdkEastern.getOffset(time);
        if (offset != jdkOffset) {
            errln("FAIL: Incompatible UTC offset -> JDK:" + jdkOffset + "/ICU:" + offset + " [" + time + "]");
        }
        Date d = new Date(time);
        isDst = icuEastern.inDaylightTime(d);
        isDstJdk = jdkEastern.inDaylightTime(d);
        if (isDst != isDstJdk) {
            errln("FAIL: Incompatible DST -> JDK:" + isDstJdk + "/ICU:" + isDst + " [" + time + "]");
        }
        // increment 1 day
        time += 24 * 60 * 60 * 1000L;
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) 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