Search in sources :

Example 6 with SimpleTimeZone

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

the class TimeZoneTest method TestFebruary.

// Test that a transition at the end of February is handled correctly.
@Test
public void TestFebruary() {
    // Time zone with daylight savings time from the first Sunday in November
    // to the last Sunday in February.
    // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
    // 
    // Note: In tzdata2007h, the rule had changed, so no actual zones uses
    // lastSun in Feb anymore.
    SimpleTimeZone tz1 = new SimpleTimeZone(// raw offset: 3h before (west of) GMT
    -3 * MILLIS_PER_HOUR, "nov-feb", // start: November, first, Sunday
    Calendar.NOVEMBER, // start: November, first, Sunday
    1, // start: November, first, Sunday
    Calendar.SUNDAY, // midnight wall time
    0, // end:   February, last, Sunday
    Calendar.FEBRUARY, // end:   February, last, Sunday
    -1, // end:   February, last, Sunday
    Calendar.SUNDAY, // midnight wall time
    0);
    // Now hardcode the same rules as for Brazil in tzdata 2006n, so that
    // we cover the intended code even when in the future zoneinfo hardcodes
    // these transition dates.
    SimpleTimeZone tz2 = new SimpleTimeZone(// raw offset: 3h before (west of) GMT
    -3 * MILLIS_PER_HOUR, "nov-feb2", // start: November, 1 or after, Sunday
    Calendar.NOVEMBER, // start: November, 1 or after, Sunday
    1, // start: November, 1 or after, Sunday
    -Calendar.SUNDAY, // midnight wall time
    0, // end:   February, 29 or before, Sunday
    Calendar.FEBRUARY, // end:   February, 29 or before, Sunday
    -29, // end:   February, 29 or before, Sunday
    -Calendar.SUNDAY, // midnight wall time
    0);
    // Gregorian calendar with the UTC time zone for getting sample test date/times.
    GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
    // "Unable to create the UTC calendar: %s"
    int[] data = { // int year, month, day, hour, minute, second, offsetHours
    2006, Calendar.NOVEMBER, 5, 02, 59, 59, -3, 2006, Calendar.NOVEMBER, 5, 03, 00, 00, -2, 2007, Calendar.FEBRUARY, 25, 01, 59, 59, -2, 2007, Calendar.FEBRUARY, 25, 02, 00, 00, -3, 2007, Calendar.NOVEMBER, 4, 02, 59, 59, -3, 2007, Calendar.NOVEMBER, 4, 03, 00, 00, -2, 2008, Calendar.FEBRUARY, 24, 01, 59, 59, -2, 2008, Calendar.FEBRUARY, 24, 02, 00, 00, -3, 2008, Calendar.NOVEMBER, 2, 02, 59, 59, -3, 2008, Calendar.NOVEMBER, 2, 03, 00, 00, -2, 2009, Calendar.FEBRUARY, 22, 01, 59, 59, -2, 2009, Calendar.FEBRUARY, 22, 02, 00, 00, -3, 2009, Calendar.NOVEMBER, 1, 02, 59, 59, -3, 2009, Calendar.NOVEMBER, 1, 03, 00, 00, -2, 2010, Calendar.FEBRUARY, 28, 01, 59, 59, -2, 2010, Calendar.FEBRUARY, 28, 02, 00, 00, -3 };
    TimeZone[] timezones = { tz1, tz2 };
    TimeZone tz;
    Date dt;
    int t, i, raw, dst;
    // raw = offsets[0], dst = offsets[1]
    int[] offsets = new int[2];
    for (t = 0; t < timezones.length; ++t) {
        tz = timezones[t];
        for (i = 0; i < data.length; i += 7) {
            gc.set(data[i], data[i + 1], data[i + 2], data[i + 3], data[i + 4], data[i + 5]);
            dt = gc.getTime();
            tz.getOffset(dt.getTime(), false, offsets);
            raw = offsets[0];
            dst = offsets[1];
            if ((raw + dst) != data[i + 6] * MILLIS_PER_HOUR) {
                errln("test case " + t + "." + (i / 7) + ": " + "tz.getOffset(" + data[i] + "-" + (data[i + 1] + 1) + "-" + data[i + 2] + " " + data[i + 3] + ":" + data[i + 4] + ":" + data[i + 5] + ") returns " + raw + "+" + dst + " != " + data[i + 6] * MILLIS_PER_HOUR);
            }
        }
    }
}
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) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 7 with SimpleTimeZone

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

the class TimeZoneTest method TestRuleAPI.

@Test
public void TestRuleAPI() {
    // ErrorCode status = ZERO_ERROR;
    // Pick a weird offset
    int offset = (int) (60 * 60 * 1000 * 1.75);
    SimpleTimeZone zone = new SimpleTimeZone(offset, "TestZone");
    if (zone.useDaylightTime())
        errln("FAIL: useDaylightTime should return false");
    // Establish our expected transition times.  Do this with a non-DST
    // calendar with the (above) declared local offset.
    GregorianCalendar gc = new GregorianCalendar(zone);
    gc.clear();
    gc.set(1990, Calendar.MARCH, 1);
    // Local Std time midnight
    long marchOneStd = gc.getTime().getTime();
    gc.clear();
    gc.set(1990, Calendar.JULY, 1);
    // Local Std time midnight
    long julyOneStd = gc.getTime().getTime();
    // Starting and ending hours, WALL TIME
    int startHour = (int) (2.25 * 3600000);
    int endHour = (int) (3.5 * 3600000);
    zone.setStartRule(Calendar.MARCH, 1, 0, startHour);
    zone.setEndRule(Calendar.JULY, 1, 0, endHour);
    gc = new GregorianCalendar(zone);
    // if (failure(status, "new GregorianCalendar")) return;
    long marchOne = marchOneStd + startHour;
    // Adjust from wall to Std time
    long julyOne = julyOneStd + endHour - 3600000;
    long expMarchOne = 636251400000L;
    if (marchOne != expMarchOne) {
        errln("FAIL: Expected start computed as " + marchOne + " = " + new Date(marchOne));
        logln("      Should be                  " + expMarchOne + " = " + new Date(expMarchOne));
    }
    long expJulyOne = 646793100000L;
    if (julyOne != expJulyOne) {
        errln("FAIL: Expected start computed as " + julyOne + " = " + new Date(julyOne));
        logln("      Should be                  " + expJulyOne + " = " + new Date(expJulyOne));
    }
    Calendar cal1 = Calendar.getInstance();
    cal1.set(1990, Calendar.JANUARY, 1);
    Calendar cal2 = Calendar.getInstance();
    cal2.set(1990, Calendar.JUNE, 1);
    _testUsingBinarySearch(zone, cal1.getTimeInMillis(), cal2.getTimeInMillis(), marchOne);
    cal1.set(1990, Calendar.JUNE, 1);
    cal2.set(1990, Calendar.DECEMBER, 31);
    _testUsingBinarySearch(zone, cal1.getTimeInMillis(), cal2.getTimeInMillis(), julyOne);
    if (zone.inDaylightTime(new Date(marchOne - 1000)) || !zone.inDaylightTime(new Date(marchOne)))
        errln("FAIL: Start rule broken");
    if (!zone.inDaylightTime(new Date(julyOne - 1000)) || zone.inDaylightTime(new Date(julyOne)))
        errln("FAIL: End rule broken");
    zone.setStartYear(1991);
    if (zone.inDaylightTime(new Date(marchOne)) || zone.inDaylightTime(new Date(julyOne - 1000)))
        errln("FAIL: Start year broken");
// failure(status, "TestRuleAPI");
// delete gc;
// delete zone;
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 8 with SimpleTimeZone

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

the class TimeZoneTest method TestAlternateRules.

/**
 * Bug 4107570
 */
@Test
public void TestAlternateRules() {
    // Like TestDSTSavings, this test should probably be integrated somehow with the main
    // test at the top of this class, but I didn't have time to figure out how to do that.
    // --rtg 1/28/98
    SimpleTimeZone tz = new SimpleTimeZone(-5 * millisPerHour, "alternateRuleTest");
    // test the day-of-month API
    tz.setStartRule(Calendar.MARCH, 10, 12 * millisPerHour);
    tz.setEndRule(Calendar.OCTOBER, 20, 12 * millisPerHour);
    int offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 5, Calendar.THURSDAY, 10 * millisPerHour);
    if (offset != -5 * millisPerHour)
        errln("The offset for 10AM, 3/5/98 should have been -5 hours, but we got " + (offset / millisPerHour) + " hours.");
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 15, Calendar.SUNDAY, 10 * millisPerHour);
    if (offset != -4 * millisPerHour)
        errln("The offset for 10AM, 3/15/98 should have been -4 hours, but we got " + (offset / millisPerHour) + " hours.");
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 15, Calendar.THURSDAY, 10 * millisPerHour);
    if (offset != -4 * millisPerHour)
        errln("The offset for 10AM, 10/15/98 should have been -4 hours, but we got " + (offset / millisPerHour) + " hours.");
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 25, Calendar.SUNDAY, 10 * millisPerHour);
    if (offset != -5 * millisPerHour)
        errln("The offset for 10AM, 10/25/98 should have been -5 hours, but we got " + (offset / millisPerHour) + " hours.");
    // test the day-of-week-after-day-in-month API
    tz.setStartRule(Calendar.MARCH, 10, Calendar.FRIDAY, 12 * millisPerHour, true);
    tz.setEndRule(Calendar.OCTOBER, 20, Calendar.FRIDAY, 12 * millisPerHour, false);
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 11, Calendar.WEDNESDAY, 10 * millisPerHour);
    if (offset != -5 * millisPerHour)
        errln("The offset for 10AM, 3/11/98 should have been -5 hours, but we got " + (offset / millisPerHour) + " hours.");
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 14, Calendar.SATURDAY, 10 * millisPerHour);
    if (offset != -4 * millisPerHour)
        errln("The offset for 10AM, 3/14/98 should have been -4 hours, but we got " + (offset / millisPerHour) + " hours.");
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 15, Calendar.THURSDAY, 10 * millisPerHour);
    if (offset != -4 * millisPerHour)
        errln("The offset for 10AM, 10/15/98 should have been -4 hours, but we got " + (offset / millisPerHour) + " hours.");
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 17, Calendar.SATURDAY, 10 * millisPerHour);
    if (offset != -5 * millisPerHour)
        errln("The offset for 10AM, 10/17/98 should have been -5 hours, but we got " + (offset / millisPerHour) + " hours.");
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 9 with SimpleTimeZone

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

the class TimeZoneTest method TestObservesDaylightTime.

@Test
public void TestObservesDaylightTime() {
    boolean observesDaylight;
    long current = System.currentTimeMillis();
    // J2ObjC change: time zones going through adjustments are problematic when comparing
    // the ICU tz data vs. the operating system tz data.
    Set<String> unstableTzids = new HashSet<>();
    // https://github.com/eggert/tz/blob/379f7ba9b81eee97f0209a322540b4a522122b5d/africa#L847
    unstableTzids.add("Africa/Casablanca");
    unstableTzids.add("Africa/El_Aaiun");
    String[] tzids = TimeZone.getAvailableIDs();
    for (String tzid : tzids) {
        // OlsonTimeZone
        TimeZone tz = TimeZone.getTimeZone(tzid, TimeZone.TIMEZONE_ICU);
        observesDaylight = tz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [OlsonTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }
        // RuleBasedTimeZone
        RuleBasedTimeZone rbtz = createRBTZ((BasicTimeZone) tz, current);
        boolean observesDaylightRBTZ = rbtz.observesDaylightTime();
        if (observesDaylightRBTZ != isDaylightTimeAvailable(rbtz, current)) {
            errln("Fail: [RuleBasedTimeZone] observesDaylightTime() returned " + observesDaylightRBTZ + " for " + rbtz.getID());
        } else if (observesDaylight != observesDaylightRBTZ) {
            errln("Fail: RuleBasedTimeZone " + rbtz.getID() + " returns " + observesDaylightRBTZ + ", but different from match OlsonTimeZone");
        }
        // JavaTimeZone
        tz = TimeZone.getTimeZone(tzid, TimeZone.TIMEZONE_JDK);
        observesDaylight = tz.observesDaylightTime();
        if (!unstableTzids.contains(tzid) && observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [JavaTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }
        // VTimeZone
        tz = VTimeZone.getTimeZone(tzid);
        observesDaylight = tz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [VTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }
    }
    // SimpleTimeZone
    SimpleTimeZone[] stzs = { new SimpleTimeZone(0, "STZ0"), new SimpleTimeZone(-5 * 60 * 60 * 1000, "STZ-5D", Calendar.MARCH, 2, Calendar.SUNDAY, 2 * 60 * 60 * 1000, Calendar.NOVEMBER, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000) };
    for (SimpleTimeZone stz : stzs) {
        observesDaylight = stz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(stz, current)) {
            errln("Fail: [SimpleTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + stz.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) SimpleTimeZone(android.icu.util.SimpleTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with SimpleTimeZone

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

the class TimeZoneTest method TestSimpleTimeZoneSerialization.

// jb4484
@Test
public void TestSimpleTimeZoneSerialization() {
    SimpleTimeZone stz0 = new SimpleTimeZone(32400000, "MyTimeZone");
    SimpleTimeZone stz1 = new SimpleTimeZone(32400000, "Asia/Tokyo");
    SimpleTimeZone stz2 = new SimpleTimeZone(32400000, "Asia/Tokyo");
    stz2.setRawOffset(0);
    SimpleTimeZone stz3 = new SimpleTimeZone(32400000, "Asia/Tokyo");
    stz3.setStartYear(100);
    SimpleTimeZone stz4 = new SimpleTimeZone(32400000, "Asia/Tokyo");
    stz4.setStartYear(1000);
    stz4.setDSTSavings(1800000);
    stz4.setStartRule(3, 4, 180000);
    stz4.setEndRule(6, 3, 4, 360000);
    SimpleTimeZone stz5 = new SimpleTimeZone(32400000, "Asia/Tokyo");
    stz5.setStartRule(2, 3, 4, 360000);
    stz5.setEndRule(6, 3, 4, 360000);
    SimpleTimeZone[] stzs = { stz0, stz1, stz2, stz3, stz4, stz5 };
    for (int i = 0; i < stzs.length; ++i) {
        SimpleTimeZone stz = stzs[i];
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(stz);
            oos.close();
            byte[] bytes = baos.toByteArray();
            logln("id: " + stz.getID() + " length: " + bytes.length);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            SimpleTimeZone stzDeserialized = (SimpleTimeZone) ois.readObject();
            ois.close();
            assertEquals("time zones", stz, stzDeserialized);
        } catch (ClassCastException cce) {
            cce.printStackTrace();
            errln("could not deserialize SimpleTimeZone");
        } catch (IOException ioe) {
            errln(ioe.getMessage());
        } catch (ClassNotFoundException cnfe) {
            errln(cnfe.getMessage());
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

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