Search in sources :

Example 16 with TimeZone

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

the class DateFormatRegressionTest method Test4065240.

/**
 * @bug 4065240
 */
@Test
public void Test4065240() {
    Date curDate;
    DateFormat shortdate, fulldate;
    String strShortDate, strFullDate;
    Locale saveLocale = Locale.getDefault();
    TimeZone saveZone = TimeZone.getDefault();
    try {
        Locale curLocale = new Locale("de", "DE");
        Locale.setDefault(curLocale);
        // {sfb} adoptDefault instead of setDefault
        // TimeZone.setDefault(TimeZone.createTimeZone("EST"));
        TimeZone.setDefault(TimeZone.getTimeZone("EST"));
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(98 + 1900, 0, 1);
        curDate = cal.getTime();
        shortdate = DateFormat.getDateInstance(DateFormat.SHORT);
        fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
        strShortDate = "The current date (short form) is ";
        String temp;
        temp = shortdate.format(curDate);
        strShortDate += temp;
        strFullDate = "The current date (long form) is ";
        String temp2 = fulldate.format(curDate);
        strFullDate += temp2;
        logln(strShortDate);
        logln(strFullDate);
    // {sfb} What to do with resource bundle stuff?????
    // Check to see if the resource is present; if not, we can't test
    // ResourceBundle bundle = //The variable is never used
    // ICULocaleData.getBundle("DateFormatZoneData", curLocale);
    // {sfb} API change to ResourceBundle -- add getLocale()
    /*if (bundle.getLocale().getLanguage().equals("de")) {
                // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
                if (!strFullDate.endsWith("GMT-05:00"))
                    errln("Fail: Want GMT-05:00");
            } else {
                logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
                logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
            }*/
    } catch (Exception e) {
        logln(e.getMessage());
    } finally {
        Locale.setDefault(saveLocale);
        TimeZone.setDefault(saveZone);
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) OptionalDataException(java.io.OptionalDataException) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 17 with TimeZone

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

the class DateFormatRegressionTest method Test4103341.

/**
 * @bug 4103341
 */
@Test
public void Test4103341() {
    TimeZone saveZone = TimeZone.getDefault();
    try {
        // {sfb} changed from adoptDefault to setDefault
        TimeZone.setDefault(TimeZone.getTimeZone("CST"));
        SimpleDateFormat simple = new SimpleDateFormat("MM/dd/yyyy HH:mm");
        TimeZone temp = TimeZone.getDefault();
        if (!simple.getTimeZone().equals(temp))
            errln("Fail: SimpleDateFormat not using default zone");
    } finally {
        TimeZone.setDefault(saveZone);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 18 with TimeZone

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

the class DateFormatRoundTripTest method TestDateFormatRoundTrip.

// TODO: test is randomly failing depending on the randomly generated date
@Test
public void TestDateFormatRoundTrip() {
    dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
    getFieldCal = Calendar.getInstance();
    // use test framework's random seed
    ran = createRandom();
    final Locale[] avail = DateFormat.getAvailableLocales();
    int locCount = avail.length;
    logln("DateFormat available locales: " + locCount);
    if (quick) {
        if (locCount > 5)
            locCount = 5;
        logln("Quick mode: only testing first 5 Locales");
    }
    TimeZone tz = TimeZone.getDefault();
    logln("Default TimeZone:             " + tz.getID());
    if (INFINITE) {
        // Special infinite loop test mode for finding hard to reproduce errors
        Locale loc = Locale.getDefault();
        logln("ENTERING INFINITE TEST LOOP FOR Locale: " + loc.getDisplayName());
        for (; ; ) {
            _test(loc);
        }
    } else {
        _test(Locale.getDefault());
        for (int i = 0; i < locCount; ++i) {
            _test(avail[i]);
        }
    }
}
Also used : Locale(java.util.Locale) TimeZone(android.icu.util.TimeZone) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 19 with TimeZone

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

the class TimeZoneRuleTest method TestHasEquivalentTransitions.

/*
     * Test cases for HasTimeZoneRules#hasEquivalentTransitions
     */
@Test
public void TestHasEquivalentTransitions() {
    // America/New_York and America/Indiana/Indianapolis are equivalent
    // since 2006
    TimeZone newyork = TimeZone.getTimeZone("America/New_York", TimeZone.TIMEZONE_ICU);
    TimeZone indianapolis = TimeZone.getTimeZone("America/Indiana/Indianapolis", TimeZone.TIMEZONE_ICU);
    TimeZone gmt_5 = TimeZone.getTimeZone("Etc/GMT+5", TimeZone.TIMEZONE_ICU);
    long jan1_1971 = getUTCMillis(1971, Calendar.JANUARY, 1);
    long jan1_2005 = getUTCMillis(2005, Calendar.JANUARY, 1);
    long jan1_2006 = getUTCMillis(2006, Calendar.JANUARY, 1);
    long jan1_2007 = getUTCMillis(2007, Calendar.JANUARY, 1);
    long jan1_2011 = getUTCMillis(2010, Calendar.JANUARY, 1);
    if (((BasicTimeZone) newyork).hasEquivalentTransitions(indianapolis, jan1_2005, jan1_2011)) {
        errln("FAIL: New_York is not equivalent to Indianapolis between 2005 and 2010, but returned true");
    }
    if (!((BasicTimeZone) newyork).hasEquivalentTransitions(indianapolis, jan1_2006, jan1_2011)) {
        errln("FAIL: New_York is equivalent to Indianapolis between 2006 and 2010, but returned false");
    }
    if (!((BasicTimeZone) indianapolis).hasEquivalentTransitions(gmt_5, jan1_1971, jan1_2006)) {
        errln("FAIL: Indianapolis is equivalent to GMT+5 between 1971 and 2005, but returned false");
    }
    if (((BasicTimeZone) indianapolis).hasEquivalentTransitions(gmt_5, jan1_1971, jan1_2007)) {
        errln("FAIL: Indianapolis is not equivalent to GMT+5 between 1971 and 2006, but returned true");
    }
    // Cloned TimeZone
    TimeZone newyork2 = (TimeZone) newyork.clone();
    if (!((BasicTimeZone) newyork).hasEquivalentTransitions(newyork2, jan1_1971, jan1_2011)) {
        errln("FAIL: Cloned TimeZone must have the same transitions");
    }
    if (!((BasicTimeZone) newyork).hasEquivalentTransitions(newyork2, jan1_1971, jan1_2011, true)) {
        errln("FAIL: Cloned TimeZone must have the same transitions");
    }
    // America/New_York and America/Los_Angeles has same DST start rules, but
    // raw offsets are different
    TimeZone losangeles = TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
    if (((BasicTimeZone) newyork).hasEquivalentTransitions(losangeles, jan1_2006, jan1_2011)) {
        errln("FAIL: New_York is not equivalent to Los Angeles, but returned true");
    }
}
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 20 with TimeZone

use of android.icu.util.TimeZone 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)

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