Search in sources :

Example 21 with TimeZone

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

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

the class TimeZoneTest method TestVariousAPI518.

// Test various calls
@Test
public void TestVariousAPI518() {
    TimeZone time_zone = TimeZone.getTimeZone("PST");
    Calendar cal = Calendar.getInstance();
    cal.set(1997, Calendar.APRIL, 30);
    Date d = cal.getTime();
    logln("The timezone is " + time_zone.getID());
    if (time_zone.inDaylightTime(d) != true)
        errln("FAIL: inDaylightTime returned false");
    if (time_zone.useDaylightTime() != true)
        errln("FAIL: useDaylightTime returned false");
    if (time_zone.getRawOffset() != -8 * millisPerHour)
        errln("FAIL: getRawOffset returned wrong value");
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(d);
    if (time_zone.getOffset(GregorianCalendar.AD, gc.get(GregorianCalendar.YEAR), gc.get(GregorianCalendar.MONTH), gc.get(GregorianCalendar.DAY_OF_MONTH), gc.get(GregorianCalendar.DAY_OF_WEEK), 0) != -7 * millisPerHour)
        errln("FAIL: getOffset returned wrong value");
}
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) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 23 with TimeZone

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

the class TimeZoneTest method TestDisplayName.

/**
 * Test the basic functionality of the getDisplayName() API.
 *
 * Bug 4112869
 * Bug 4028006
 *
 * See also API change request A41.
 *
 * 4/21/98 - make smarter, so the test works if the ext resources
 * are present or not.
 */
@Test
public void TestDisplayName() {
    TimeZone zone = TimeZone.getTimeZone("PST");
    String name = zone.getDisplayName(Locale.ENGLISH);
    logln("PST->" + name);
    // dlf - we now (3.4.1) return generic time
    if (!name.equals("Pacific Time"))
        errln("Fail: Expected \"Pacific Time\", got " + name + " for " + zone);
    // *****************************************************************
    // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
    // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
    // THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
    // *****************************************************************
    // Test to allow the user to choose to get all the forms
    // (z, zzzz, Z, ZZZZ, v, vvvv)
    // todo: check to see whether we can test for all of pst, pdt, pt
    Object[] DATA = { // z and zzzz
    Boolean.FALSE, new Integer(TimeZone.SHORT), "PST", Boolean.TRUE, new Integer(TimeZone.SHORT), "PDT", Boolean.FALSE, new Integer(TimeZone.LONG), "Pacific Standard Time", Boolean.TRUE, new Integer(TimeZone.LONG), "Pacific Daylight Time", // v and vvvv
    Boolean.FALSE, new Integer(TimeZone.SHORT_GENERIC), "PT", Boolean.TRUE, new Integer(TimeZone.SHORT_GENERIC), "PT", Boolean.FALSE, new Integer(TimeZone.LONG_GENERIC), "Pacific Time", Boolean.TRUE, new Integer(TimeZone.LONG_GENERIC), "Pacific Time", // z and ZZZZ
    Boolean.FALSE, new Integer(TimeZone.SHORT_GMT), "-0800", Boolean.TRUE, new Integer(TimeZone.SHORT_GMT), "-0700", Boolean.FALSE, new Integer(TimeZone.LONG_GMT), "GMT-08:00", Boolean.TRUE, new Integer(TimeZone.LONG_GMT), "GMT-07:00", // V and VVVV
    Boolean.FALSE, new Integer(TimeZone.SHORT_COMMONLY_USED), "PST", Boolean.TRUE, new Integer(TimeZone.SHORT_COMMONLY_USED), "PDT", Boolean.FALSE, new Integer(TimeZone.GENERIC_LOCATION), "Los Angeles Time", Boolean.TRUE, new Integer(TimeZone.GENERIC_LOCATION), "Los Angeles Time" };
    for (int i = 0; i < DATA.length; i += 3) {
        name = zone.getDisplayName(((Boolean) DATA[i]).booleanValue(), ((Integer) DATA[i + 1]).intValue(), Locale.ENGLISH);
        if (!name.equals(DATA[i + 2]))
            errln("Fail: Expected " + DATA[i + 2] + "; got " + name);
    }
    // Make sure that we don't display the DST name by constructing a fake
    // PST zone that has DST all year long.
    // dlf - this test is no longer relevant, we display generic time now
    // so the behavior of the timezone doesn't matter
    SimpleTimeZone zone2 = new SimpleTimeZone(0, "PST");
    zone2.setStartRule(Calendar.JANUARY, 1, 0);
    zone2.setEndRule(Calendar.DECEMBER, 31, 86399999);
    logln("Modified PST inDaylightTime->" + zone2.inDaylightTime(new Date()));
    name = zone2.getDisplayName(Locale.ENGLISH);
    logln("Modified PST->" + name);
    if (!name.equals("Pacific Time"))
        errln("Fail: Expected \"Pacific Time\"");
    // Make sure we get the default display format for Locales
    // with no display name data.
    Locale mt_MT = new Locale("mt", "MT");
    name = zone.getDisplayName(mt_MT);
    // *****************************************************************
    // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
    // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
    // THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
    // *****************************************************************
    logln("PST(mt_MT)->" + name);
    // Now be smart -- check to see if zh resource is even present.
    // If not, we expect the en fallback behavior.
    // in icu4j 2.1 we know we have the zh_CN locale data, though it's incomplete
    // /"DateFormatZoneData",
    UResourceBundle enRB = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, Locale.ENGLISH);
    UResourceBundle mtRB = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, mt_MT);
    boolean noZH = enRB == mtRB;
    if (noZH) {
        logln("Warning: Not testing the mt_MT behavior because resource is absent");
        if (!name.equals("Pacific Standard Time"))
            errln("Fail: Expected Pacific Standard Time for PST in mt_MT but got ");
    } else // - we now (3.4.1) have localizations for this zone, so change test string
    if (!name.equals("\u0126in ta\u2019 Los Angeles") && !name.equals("GMT-08:00") && !name.equals("GMT-8:00") && !name.equals("GMT-0800") && !name.equals("GMT-800")) {
        errln("Fail: got '" + name + "', expected GMT-08:00 or something similar\n" + "************************************************************\n" + "THE ABOVE FAILURE MAY JUST MEAN THE LOCALE DATA HAS CHANGED\n" + "************************************************************");
    }
    // Now try a non-existent zone
    zone2 = new SimpleTimeZone(90 * 60 * 1000, "xyzzy");
    name = zone2.getDisplayName(Locale.ENGLISH);
    logln("GMT+90min->" + name);
    if (!name.equals("GMT+01:30") && !name.equals("GMT+1:30") && !name.equals("GMT+0130") && !name.equals("GMT+130"))
        errln("Fail: Expected GMT+01:30 or something similar");
    // cover getDisplayName() - null arg
    ULocale save = ULocale.getDefault();
    ULocale.setDefault(ULocale.US);
    name = zone2.getDisplayName();
    logln("GMT+90min->" + name + "for default display locale");
    if (!name.equals("GMT+01:30") && !name.equals("GMT+1:30") && !name.equals("GMT+0130") && !name.equals("GMT+130"))
        errln("Fail: Expected GMT+01:30 or something similar");
    ULocale.setDefault(save);
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) UResourceBundle(android.icu.util.UResourceBundle) ULocale(android.icu.util.ULocale) Date(java.util.Date) 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) Test(org.junit.Test)

Example 24 with TimeZone

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

the class TimeZoneTest method TestHashCode.

/*
     * Test case for hashCode problem reported by ticket#7690 OlsonTimeZone.hashCode() throws NPE.
     */
@Test
public void TestHashCode() {
    String[] ids = TimeZone.getAvailableIDs();
    for (String id : ids) {
        TimeZone tz1 = TimeZone.getTimeZone(id);
        TimeZone tz2 = TimeZone.getTimeZone(id);
        // hash code are same for the same time zone
        if (tz1.hashCode() != tz2.hashCode()) {
            errln("Fail: Two time zone instances for " + id + " have different hash values.");
        }
        /* J2ObjC: compare classes instead of string representations. The NSObject's description
             * includes the object's address (e.g. <AndroidIcuImplOlsonTimeZone: 0x7ffbbbf061d0>) */
        if (!tz1.getClass().equals(tz2.getClass())) {
            errln("Fail: Two time zone instances for " + id + " have different classes.");
        }
    }
}
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 25 with TimeZone

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

the class TimeZoneTest method TestDisplayNamesMeta.

/*
     * Test Display Names, choosing zones and lcoales where there are multiple
     * meta-zones defined.
     */
@Test
public void TestDisplayNamesMeta() {
    final Integer TZSHORT = new Integer(TimeZone.SHORT);
    final Integer TZLONG = new Integer(TimeZone.LONG);
    final Object[][] zoneDisplayTestData = { // zone id             locale  summer          format      expected display name
    { "Europe/London", "en", Boolean.FALSE, TZSHORT, "GMT" }, { "Europe/London", "en", Boolean.FALSE, TZLONG, "Greenwich Mean Time" }, { "Europe/London", "en", Boolean.TRUE, TZSHORT, "GMT+1" /*"BST"*/
    }, { "Europe/London", "en", Boolean.TRUE, TZLONG, "British Summer Time" }, { "America/Anchorage", "en", Boolean.FALSE, TZSHORT, "AKST" }, { "America/Anchorage", "en", Boolean.FALSE, TZLONG, "Alaska Standard Time" }, { "America/Anchorage", "en", Boolean.TRUE, TZSHORT, "AKDT" }, { "America/Anchorage", "en", Boolean.TRUE, TZLONG, "Alaska Daylight Time" }, // Southern Hemisphere, all data from meta:Australia_Western
    { "Australia/Perth", "en", Boolean.FALSE, TZSHORT, "GMT+8" /*"AWST"*/
    }, { "Australia/Perth", "en", Boolean.FALSE, TZLONG, "Australian Western Standard Time" }, // requested. See #9350.
    { "Australia/Perth", "en", Boolean.TRUE, TZSHORT, "GMT+8" /*"AWDT"*/
    }, { "Australia/Perth", "en", Boolean.TRUE, TZLONG, "Australian Western Daylight Time" }, { "America/Sao_Paulo", "en", Boolean.FALSE, TZSHORT, "GMT-3" /*"BRT"*/
    }, { "America/Sao_Paulo", "en", Boolean.FALSE, TZLONG, "Brasilia Standard Time" }, { "America/Sao_Paulo", "en", Boolean.TRUE, TZSHORT, "GMT-2" /*"BRST"*/
    }, { "America/Sao_Paulo", "en", Boolean.TRUE, TZLONG, "Brasilia Summer Time" }, // No Summer Time, but had it before 1983.
    { "Pacific/Honolulu", "en", Boolean.FALSE, TZSHORT, "HST" }, { "Pacific/Honolulu", "en", Boolean.FALSE, TZLONG, "Hawaii-Aleutian Standard Time" }, { "Pacific/Honolulu", "en", Boolean.TRUE, TZSHORT, "HDT" }, { "Pacific/Honolulu", "en", Boolean.TRUE, TZLONG, "Hawaii-Aleutian Daylight Time" }, // Northern, has Summer, not commonly used.
    { "Europe/Helsinki", "en", Boolean.FALSE, TZSHORT, "GMT+2" /*"EET"*/
    }, { "Europe/Helsinki", "en", Boolean.FALSE, TZLONG, "Eastern European Standard Time" }, { "Europe/Helsinki", "en", Boolean.TRUE, TZSHORT, "GMT+3" /*"EEST"*/
    }, { "Europe/Helsinki", "en", Boolean.TRUE, TZLONG, "Eastern European Summer Time" }, // by Ticket#6644
    { "Europe/London", "en", Boolean.TRUE, TZSHORT, "GMT+1" /*"BST"*/
    }, { "Europe/London", "en", Boolean.TRUE, TZLONG, "British Summer Time" } };
    boolean isICUTimeZone = (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_ICU);
    boolean sawAnError = false;
    for (int testNum = 0; testNum < zoneDisplayTestData.length; testNum++) {
        ULocale locale = new ULocale((String) zoneDisplayTestData[testNum][1]);
        TimeZone zone = TimeZone.getTimeZone((String) zoneDisplayTestData[testNum][0]);
        String displayName = zone.getDisplayName(((Boolean) zoneDisplayTestData[testNum][2]).booleanValue(), ((Integer) zoneDisplayTestData[testNum][3]).intValue());
        if (!displayName.equals(zoneDisplayTestData[testNum][4])) {
            if (isDevelopmentBuild && (isICUTimeZone || !((Boolean) zoneDisplayTestData[testNum][2]).booleanValue())) {
                sawAnError = true;
                errln("Incorrect time zone display name.  zone = " + zoneDisplayTestData[testNum][0] + ",\n" + "   locale = " + locale + ",   style = " + (zoneDisplayTestData[testNum][3] == TZSHORT ? "SHORT" : "LONG") + ",   Summertime = " + zoneDisplayTestData[testNum][2] + "\n" + "   Expected " + zoneDisplayTestData[testNum][4] + ",   Got " + displayName);
            } else {
                logln("Incorrect time zone display name.  zone = " + zoneDisplayTestData[testNum][0] + ",\n" + "   locale = " + locale + ",   style = " + (zoneDisplayTestData[testNum][3] == TZSHORT ? "SHORT" : "LONG") + ",   Summertime = " + zoneDisplayTestData[testNum][2] + "\n" + "   Expected " + zoneDisplayTestData[testNum][4] + ",   Got " + displayName);
            }
        }
    }
    if (sawAnError) {
        logln("Note: Errors could be the result of changes to zoneStrings locale data");
    }
}
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) 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