Search in sources :

Example 91 with TimeZone

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

the class TimeZoneRegressionTest method Test4151429.

@Test
public void Test4151429() {
    try {
        TimeZone tz = TimeZone.getTimeZone("GMT");
        /*String name =*/
        tz.getDisplayName(true, Integer.MAX_VALUE, Locale.getDefault());
        errln("IllegalArgumentException not thrown by TimeZone.getDisplayName()");
    } catch (IllegalArgumentException e) {
        System.out.print("");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) Test(org.junit.Test)

Example 92 with TimeZone

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

the class TimeZoneRegressionTest method Test4173604.

/**
 * TimeZone broken in last hour of year
 */
@Test
public void Test4173604() {
    TimeZone pst = TimeZone.getTimeZone("PST");
    int o22 = pst.getOffset(1, 1998, 11, 31, Calendar.THURSDAY, 22 * 60 * 60 * 1000);
    int o23 = pst.getOffset(1, 1998, 11, 31, Calendar.THURSDAY, 23 * 60 * 60 * 1000);
    int o00 = pst.getOffset(1, 1999, 0, 1, Calendar.FRIDAY, 0);
    if (o22 != o23 || o22 != o00) {
        errln("Offsets should be the same (for PST), but got: " + "12/31 22:00 " + o22 + ", 12/31 23:00 " + o23 + ", 01/01 00:00 " + o00);
    }
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeZone(pst);
    cal.clear();
    cal.set(1998, Calendar.JANUARY, 1);
    int lastDST = cal.get(Calendar.DST_OFFSET);
    int transitions = 0;
    int delta = 5;
    while (cal.get(Calendar.YEAR) < 2000) {
        cal.add(Calendar.MINUTE, delta);
        if (cal.get(Calendar.DST_OFFSET) != lastDST) {
            ++transitions;
            Calendar t = (Calendar) cal.clone();
            t.add(Calendar.MINUTE, -delta);
            logln(t.getTime() + "  " + t.get(Calendar.DST_OFFSET));
            logln(cal.getTime() + "  " + (lastDST = cal.get(Calendar.DST_OFFSET)));
        }
    }
    if (transitions != 4) {
        errln("Saw " + transitions + " transitions; should have seen 4");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 93 with TimeZone

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

the class TimeZoneRuleTest method TestBasicTimeZoneCoverage.

@Test
public void TestBasicTimeZoneCoverage() {
    TimeZone tz = TimeZone.getTimeZone("PST");
    if (tz instanceof BasicTimeZone) {
        BasicTimeZone btz = (BasicTimeZone) tz;
        int[] offsets = new int[2];
        btz.getOffsetFromLocal(Calendar.getInstance().getTimeInMillis(), BasicTimeZone.LOCAL_STD, BasicTimeZone.LOCAL_STD, offsets);
        if (offsets[0] > offsets[1]) {
            errln("Error calling getOffsetFromLocal().");
        }
    } else {
        logln("Skipping TestBasicTimeZoneCoverage: ICU4J is configured to use JDK TimeZone");
    }
}
Also used : 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) Test(org.junit.Test)

Example 94 with TimeZone

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

the class TimeZoneRuleTest method TestHistoricalRuleBasedTimeZone.

/*
     * Test equivalency between OlsonTimeZone and custom RBTZ representing the
     * equivalent rules in a certain time range
     */
@Test
public void TestHistoricalRuleBasedTimeZone() {
    // Compare to America/New_York with equivalent RBTZ
    TimeZone ny = TimeZone.getTimeZone("America/New_York", TimeZone.TIMEZONE_ICU);
    // RBTZ
    InitialTimeZoneRule ir = new InitialTimeZoneRule("EST", -5 * HOUR, 0);
    RuleBasedTimeZone rbtz = new RuleBasedTimeZone("EST5EDT", ir);
    DateTimeRule dtr;
    AnnualTimeZoneRule tzr;
    // Standard time
    dtr = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR, // Last Sunday in October, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EST", -5 * HOUR, /* rawOffset */
    0, /* dstSavings */
    dtr, 1967, 2006);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, true, 2 * HOUR, // SUN>=1 in November, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EST", -5 * HOUR, 0, dtr, 2007, AnnualTimeZoneRule.MAX_YEAR);
    rbtz.addTransitionRule(tzr);
    // Daylight saving time
    dtr = new DateTimeRule(Calendar.APRIL, -1, Calendar.SUNDAY, 2 * HOUR, // Last Sunday in April, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 1967, 1973);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.JANUARY, 6, 2 * HOUR, // January 6, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 1974, 1974);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.FEBRUARY, 23, 2 * HOUR, // February 23, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 1975, 1975);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.APRIL, -1, Calendar.SUNDAY, 2 * HOUR, // Last Sunday in April, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 1976, 1986);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.APRIL, 1, Calendar.SUNDAY, true, 2 * HOUR, // SUN>=1 in April, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 1987, 2006);
    rbtz.addTransitionRule(tzr);
    dtr = new DateTimeRule(Calendar.MARCH, 8, Calendar.SUNDAY, true, 2 * HOUR, // SUN>=8 in March, at 2AM wall time
    DateTimeRule.WALL_TIME);
    tzr = new AnnualTimeZoneRule("EDT", -5 * HOUR, 1 * HOUR, dtr, 2007, AnnualTimeZoneRule.MAX_YEAR);
    rbtz.addTransitionRule(tzr);
    // hasEquivalentTransitions
    long jan1_1950 = getUTCMillis(1950, Calendar.JANUARY, 1);
    long jan1_1967 = getUTCMillis(1971, Calendar.JANUARY, 1);
    long jan1_2010 = getUTCMillis(2010, Calendar.JANUARY, 1);
    if (!(((BasicTimeZone) ny).hasEquivalentTransitions(rbtz, jan1_1967, jan1_2010))) {
        errln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
    }
    if (((BasicTimeZone) ny).hasEquivalentTransitions(rbtz, jan1_1950, jan1_2010)) {
        errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
    }
    // Same with above, but calling RBTZ#hasEquivalentTransitions against OlsonTimeZone
    if (!rbtz.hasEquivalentTransitions(ny, jan1_1967, jan1_2010)) {
        errln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
    }
    if (rbtz.hasEquivalentTransitions(ny, jan1_1950, jan1_2010)) {
        errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
    }
    // TimeZone APIs
    if (ny.hasSameRules(rbtz) || rbtz.hasSameRules(ny)) {
        errln("FAIL: hasSameRules must return false");
    }
    RuleBasedTimeZone rbtzc = (RuleBasedTimeZone) rbtz.clone();
    if (!rbtz.hasSameRules(rbtzc) || !rbtz.hasEquivalentTransitions(rbtzc, jan1_1950, jan1_2010)) {
        errln("FAIL: hasSameRules/hasEquivalentTransitions must return true for cloned RBTZs");
    }
    long[] times = { getUTCMillis(2006, Calendar.MARCH, 15), getUTCMillis(2006, Calendar.NOVEMBER, 1), getUTCMillis(2007, Calendar.MARCH, 15), getUTCMillis(2007, Calendar.NOVEMBER, 1), getUTCMillis(2008, Calendar.MARCH, 15), getUTCMillis(2008, Calendar.NOVEMBER, 1) };
    int[] offsets1 = new int[2];
    int[] offsets2 = new int[2];
    for (int i = 0; i < times.length; i++) {
        // Check getOffset - must return the same results for these time data
        rbtz.getOffset(times[i], false, offsets1);
        ny.getOffset(times[i], false, offsets2);
        if (offsets1[0] != offsets2[0] || offsets1[1] != offsets2[1]) {
            errln("FAIL: Incompatible time zone offsets for ny and rbtz");
        }
        // Check inDaylightTime
        Date d = new Date(times[i]);
        if (rbtz.inDaylightTime(d) != ny.inDaylightTime(d)) {
            errln("FAIL: Incompatible daylight saving time for ny and rbtz");
        }
    }
}
Also used : DateTimeRule(android.icu.util.DateTimeRule) 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) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date) Test(org.junit.Test)

Example 95 with TimeZone

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

the class TimeZoneTest method TestMark.

@Test
public void TestMark() {
    String tzid = "America/Argentina/ComodRivadavia";
    TimeZone tz = TimeZone.getTimeZone(tzid);
    int offset = tz.getOffset(new Date().getTime());
    logln(tzid + ":\t" + offset);
    List list = Arrays.asList(TimeZone.getAvailableIDs());
    if (!list.contains(tzid)) {
        errln("Could create the time zone but it is not in getAvailableIDs");
    }
}
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) List(java.util.List) 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