Search in sources :

Example 91 with GregorianCalendar

use of android.icu.util.GregorianCalendar 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 92 with GregorianCalendar

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

the class TimeScaleDataTest method TestDotNet.

/*
     * ICU's Universal Time Scale is designed to be tick-for-tick compatible with
     * .Net System.DateTime. Verify that this is so for the
     * .Net-supported date range (years 1-9999 AD).
     * This requires a proleptic Gregorian calendar because that's what .Net uses.
     * Proleptic: No Julian/Gregorian switchover, or a switchover before
     * any date that we test, that is, before 0001 AD.
     */
@Test
public void TestDotNet() {
    TimeZone utc;
    final long dayMillis = 86400 * 1000L;
    /* 1 day = 86400 seconds */
    final long dayTicks = 86400 * 10000000L;
    // offset for dotNetDateTimeTicks[] field
    final int kYear = 0;
    final int kMonth = 1;
    final int kDay = 2;
    final int kTicks = 3;
    final int kIncrement = 4;
    GregorianCalendar cal;
    long icuDate;
    long ticks, millis;
    int i;
    /* Open a proleptic Gregorian calendar. */
    long before0001AD = -1000000 * dayMillis;
    utc = new SimpleTimeZone(0, "UTC");
    cal = new GregorianCalendar(utc, Locale.ENGLISH);
    cal.setGregorianChange(new Date(before0001AD));
    for (i = 0; i < dotNetDateTimeTicks.length; i += kIncrement) {
        /* Test conversion from .Net/Universal time to ICU time. */
        millis = UniversalTimeScale.toLong(dotNetDateTimeTicks[i + kTicks], UniversalTimeScale.ICU4C_TIME);
        cal.clear();
        cal.set((int) dotNetDateTimeTicks[i + kYear], (int) dotNetDateTimeTicks[i + kMonth] - 1, /* Java & ICU use January = month 0. */
        (int) dotNetDateTimeTicks[i + kDay]);
        icuDate = cal.getTimeInMillis();
        if (millis != icuDate) {
            /* Print days not millis. */
            errln("UniversalTimeScale.toLong(ticks[" + i + "], ICU4C)=" + (millis / dayMillis) + " != " + (icuDate / dayMillis) + "=ucal_getMillis(" + dotNetDateTimeTicks[i + kYear] + "-" + dotNetDateTimeTicks[i + kMonth] + "-" + dotNetDateTimeTicks[i + kDay] + ")");
        }
        /* Test conversion from ICU time to .Net/Universal time. */
        ticks = UniversalTimeScale.from(icuDate, UniversalTimeScale.ICU4C_TIME);
        if (ticks != dotNetDateTimeTicks[i + kTicks]) {
            /* Print days not ticks. */
            errln("UniversalTimeScale.from(date[" + i + "], ICU4C)=" + (ticks / dayTicks) + " != " + dotNetDateTimeTicks[i + kTicks] / dayTicks + "=.Net System.DateTime(" + dotNetDateTimeTicks[i + kYear] + "-" + dotNetDateTimeTicks[i + kMonth] + "-" + dotNetDateTimeTicks[i + kDay] + ").Ticks");
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 93 with GregorianCalendar

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

the class TimeZoneRuleTest method TestT6216.

@Test
public void TestT6216() {
    // Test case in #6216
    String tokyoTZ = "BEGIN:VCALENDAR\r\n" + "VERSION:2.0\r\n" + "PRODID:-//PYVOBJECT//NONSGML Version 1//EN\r\n" + "BEGIN:VTIMEZONE\r\n" + "TZID:Asia/Tokyo\r\n" + "BEGIN:STANDARD\r\n" + "DTSTART:20000101T000000\r\n" + "RRULE:FREQ=YEARLY;BYMONTH=1\r\n" + "TZNAME:Asia/Tokyo\r\n" + "TZOFFSETFROM:+0900\r\n" + "TZOFFSETTO:+0900\r\n" + "END:STANDARD\r\n" + "END:VTIMEZONE\r\n" + "END:VCALENDAR";
    // Single final rule, overlapping with another
    String finalOverlap = "BEGIN:VCALENDAR\r\n" + "BEGIN:VTIMEZONE\r\n" + "TZID:FinalOverlap\r\n" + "BEGIN:STANDARD\r\n" + "TZOFFSETFROM:-0200\r\n" + "TZOFFSETTO:-0300\r\n" + "TZNAME:STD\r\n" + "DTSTART:20001029T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r\n" + "END:STANDARD\r\n" + "BEGIN:DAYLIGHT\r\n" + "TZOFFSETFROM:-0300\r\n" + "TZOFFSETTO:-0200\r\n" + "TZNAME:DST\r\n" + "DTSTART:19990404T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=20050403T040000Z\r\n" + "END:DAYLIGHT\r\n" + "END:VTIMEZONE\r\n" + "END:VCALENDAR";
    // Single final rule, no overlapping with another
    String finalNonOverlap = "BEGIN:VCALENDAR\r\n" + "BEGIN:VTIMEZONE\r\n" + "TZID:FinalNonOverlap\r\n" + "BEGIN:STANDARD\r\n" + "TZOFFSETFROM:-0200\r\n" + "TZOFFSETTO:-0300\r\n" + "TZNAME:STD\r\n" + "DTSTART:20001029T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10;UNTIL=20041031T040000Z\r\n" + "END:STANDARD\r\n" + "BEGIN:DAYLIGHT\r\n" + "TZOFFSETFROM:-0300\r\n" + "TZOFFSETTO:-0200\r\n" + "TZNAME:DST\r\n" + "DTSTART:19990404T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=20050403T040000Z\r\n" + "END:DAYLIGHT\r\n" + "BEGIN:STANDARD\r\n" + "TZOFFSETFROM:-0200\r\n" + "TZOFFSETTO:-0300\r\n" + "TZNAME:STDFINAL\r\n" + "DTSTART:20071028T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r\n" + "END:STANDARD\r\n" + "END:VTIMEZONE\r\n" + "END:VCALENDAR";
    int[][] TestDates = { { 1995, Calendar.JANUARY, 1 }, { 1995, Calendar.JULY, 1 }, { 2000, Calendar.JANUARY, 1 }, { 2000, Calendar.JULY, 1 }, { 2005, Calendar.JANUARY, 1 }, { 2005, Calendar.JULY, 1 }, { 2010, Calendar.JANUARY, 1 }, { 2010, Calendar.JULY, 1 } };
    String[] TestZones = { tokyoTZ, finalOverlap, finalNonOverlap };
    int[][] Expected = { // JAN90      JUL90      JAN00      JUL00      JAN05      JUL05      JAN10      JUL10
    { 32400000, 32400000, 32400000, 32400000, 32400000, 32400000, 32400000, 32400000 }, { -10800000, -10800000, -7200000, -7200000, -10800000, -7200000, -10800000, -10800000 }, { -10800000, -10800000, -7200000, -7200000, -10800000, -7200000, -10800000, -10800000 } };
    // Get test times
    long[] times = new long[TestDates.length];
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
    for (int i = 0; i < TestDates.length; i++) {
        cal.clear();
        cal.set(TestDates[i][0], TestDates[i][1], TestDates[i][2]);
        times[i] = cal.getTimeInMillis();
    }
    for (int i = 0; i < TestZones.length; i++) {
        try {
            VTimeZone vtz = VTimeZone.create(new StringReader(TestZones[i]));
            for (int j = 0; j < times.length; j++) {
                int offset = vtz.getOffset(times[j]);
                if (offset != Expected[i][j]) {
                    errln("FAIL: Invalid offset at time(" + times[j] + "):" + offset + " Expected:" + Expected[i][j]);
                }
            }
        } catch (Exception e) {
            errln("FAIL: Failed to calculate the offset for VTIMEZONE data " + i);
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) VTimeZone(android.icu.util.VTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) StringReader(java.io.StringReader) IOException(java.io.IOException) Test(org.junit.Test)

Example 94 with GregorianCalendar

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

the class TimeZoneRuleTest method getUTCMillis.

private long getUTCMillis(int year, int month, int dayOfMonth) {
    if (utcCal == null) {
        utcCal = new GregorianCalendar(TimeZone.getTimeZone("UTC"), ULocale.ROOT);
    }
    utcCal.clear();
    utcCal.set(year, month, dayOfMonth);
    return utcCal.getTimeInMillis();
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar)

Example 95 with GregorianCalendar

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

the class DateFormatTest method TestRoundtripWithCalendar.

/*
     * Test for format/parse method with calendar which is different
     * from what DateFormat instance internally use.  See ticket#6420.
     */
@Test
public void TestRoundtripWithCalendar() {
    TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
    TimeZone gmt = TimeZone.getTimeZone("Etc/GMT");
    final Calendar[] calendars = { new GregorianCalendar(tz), new BuddhistCalendar(tz), new HebrewCalendar(tz), new IslamicCalendar(tz), new JapaneseCalendar(tz) };
    final String pattern = "GyMMMMdEEEEHHmmssVVVV";
    // FIXME The formatters commented out below are currently failing because of
    // the calendar calculation problem reported by #6691
    // The order of test formatters mus match the order of calendars above.
    final DateFormat[] formatters = { // calendar=gregorian
    DateFormat.getPatternInstance(pattern, new ULocale("en_US")), // calendar=buddhist
    DateFormat.getPatternInstance(pattern, new ULocale("th_TH")), DateFormat.getPatternInstance(pattern, new ULocale("he_IL@calendar=hebrew")) // DateFormat.getPatternInstance(pattern, new ULocale("ar_EG@calendar=islamic")),
    // DateFormat.getPatternInstance(pattern, new ULocale("ja_JP@calendar=japanese")),
    };
    Date d = new Date();
    StringBuffer buf = new StringBuffer();
    FieldPosition fpos = new FieldPosition(0);
    ParsePosition ppos = new ParsePosition(0);
    for (int i = 0; i < formatters.length; i++) {
        buf.setLength(0);
        fpos.setBeginIndex(0);
        fpos.setEndIndex(0);
        calendars[i].setTime(d);
        // Normal case output - the given calendar matches the calendar
        // used by the formatter
        formatters[i].format(calendars[i], buf, fpos);
        String refStr = buf.toString();
        for (int j = 0; j < calendars.length; j++) {
            if (j == i) {
                continue;
            }
            buf.setLength(0);
            fpos.setBeginIndex(0);
            fpos.setEndIndex(0);
            calendars[j].setTime(d);
            // Even the different calendar type is specified,
            // we should get the same result.
            formatters[i].format(calendars[j], buf, fpos);
            if (!refStr.equals(buf.toString())) {
                errln("FAIL: Different format result with a different calendar for the same time -" + "\n Reference calendar type=" + calendars[i].getType() + "\n Another calendar type=" + calendars[j].getType() + "\n Expected result=" + refStr + "\n Actual result=" + buf.toString());
            }
        }
        calendars[i].setTimeZone(gmt);
        calendars[i].clear();
        ppos.setErrorIndex(-1);
        ppos.setIndex(0);
        // Normal case parse result - the given calendar matches the calendar
        // used by the formatter
        formatters[i].parse(refStr, calendars[i], ppos);
        for (int j = 0; j < calendars.length; j++) {
            if (j == i) {
                continue;
            }
            calendars[j].setTimeZone(gmt);
            calendars[j].clear();
            ppos.setErrorIndex(-1);
            ppos.setIndex(0);
            // Even the different calendar type is specified,
            // we should get the same time and time zone.
            formatters[i].parse(refStr, calendars[j], ppos);
            if (calendars[i].getTimeInMillis() != calendars[j].getTimeInMillis() || !calendars[i].getTimeZone().equals(calendars[j].getTimeZone())) {
                errln("FAIL: Different parse result with a different calendar for the same string -" + "\n Reference calendar type=" + calendars[i].getType() + "\n Another calendar type=" + calendars[j].getType() + "\n Date string=" + refStr + "\n Expected time=" + calendars[i].getTimeInMillis() + "\n Expected time zone=" + calendars[i].getTimeZone().getID() + "\n Actual time=" + calendars[j].getTimeInMillis() + "\n Actual time zone=" + calendars[j].getTimeZone().getID());
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) BuddhistCalendar(android.icu.util.BuddhistCalendar) HebrewCalendar(android.icu.util.HebrewCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) HebrewCalendar(android.icu.util.HebrewCalendar) TimeZone(android.icu.util.TimeZone) BuddhistCalendar(android.icu.util.BuddhistCalendar) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

GregorianCalendar (android.icu.util.GregorianCalendar)114 Test (org.junit.Test)102 Date (java.util.Date)64 Calendar (android.icu.util.Calendar)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)34 TimeZone (android.icu.util.TimeZone)29 SimpleTimeZone (android.icu.util.SimpleTimeZone)28 DateFormat (android.icu.text.DateFormat)24 JapaneseCalendar (android.icu.util.JapaneseCalendar)17 ULocale (android.icu.util.ULocale)16 ChineseCalendar (android.icu.util.ChineseCalendar)15 IslamicCalendar (android.icu.util.IslamicCalendar)14 BuddhistCalendar (android.icu.util.BuddhistCalendar)12 ChineseDateFormat (android.icu.text.ChineseDateFormat)8 IOException (java.io.IOException)8 ParsePosition (java.text.ParsePosition)8 CalendarAstronomer (android.icu.impl.CalendarAstronomer)7 HebrewCalendar (android.icu.util.HebrewCalendar)7 FieldPosition (java.text.FieldPosition)7 ParseException (java.text.ParseException)6