Java Code Examples for android.icu.util.TimeZone#getAvailableIDs()

The following examples show how to use android.icu.util.TimeZone#getAvailableIDs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeZoneGenericNames.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a collection of time zone display name matches for the specified types in the
 * given text at the given offset. This method only finds matches from the local trie,
 * that contains 1) generic location names and 2) long/short generic partial location names,
 * used by this object.
 * @param text the text
 * @param start the start offset in the text
 * @param types the set of name types.
 * @return A collection of match info.
 */
private synchronized Collection<GenericMatchInfo> findLocal(String text, int start, EnumSet<GenericNameType> types) {
    GenericNameSearchHandler handler = new GenericNameSearchHandler(types);
    _gnamesTrie.find(text, start, handler);
    if (handler.getMaxMatchLen() == (text.length() - start) || _gnamesTrieFullyLoaded) {
        // perfect match
        return handler.getMatches();
    }

    // All names are not yet loaded into the local trie.
    // Load all available names into the trie. This could be very heavy.

    Set<String> tzIDs = TimeZone.getAvailableIDs(SystemTimeZoneType.CANONICAL, null, null);
    for (String tzID : tzIDs) {
        loadStrings(tzID);
    }
    _gnamesTrieFullyLoaded = true;

    // now, try it again
    handler.resetResults();
    _gnamesTrie.find(text, start, handler);
    return handler.getMatches();
}
 
Example 2
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@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.");
        }
        // string representation should be also same
        /* 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.");
        }
    }
}
 
Example 3
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * TimeZone.getAvailableIDs(int) throws exception for certain values,
 * due to a faulty constant in TimeZone.java.
 */
@Test
public void Test4151406() {
    int max = 0;
    for (int h=-28; h<=30; ++h) {
        // h is in half-hours from GMT; rawoffset is in millis
        int rawoffset = h * 1800000;
        int hh = (h<0) ? -h : h;
        String hname = ((h<0) ? "GMT-" : "GMT+") +
            ((hh/2 < 10) ? "0" : "") +
            (hh/2) + ':' +
            ((hh%2==0) ? "00" : "30");
        try {
            String[] ids = TimeZone.getAvailableIDs(rawoffset);
            if (ids.length > max) max = ids.length;
            logln(hname + ' ' + ids.length +
                  ((ids.length > 0) ? (" e.g. " + ids[0]) : ""));
        } catch (Exception e) {
            errln(hname + ' ' + "Fail: " + e);
        }
    }
    logln("Maximum zones per offset = " + max);
}
 
Example 4
Source File: DateFormatSymbols.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Returns time zone strings.
 * <p>
 * The array returned by this API is a two dimensional String array and
 * each row contains at least following strings:
 * <ul>
 * <li>ZoneStrings[n][0] - System time zone ID
 * <li>ZoneStrings[n][1] - Long standard time display name
 * <li>ZoneStrings[n][2] - Short standard time display name
 * <li>ZoneStrings[n][3] - Long daylight saving time display name
 * <li>ZoneStrings[n][4] - Short daylight saving time display name
 * </ul>
 * When a localized display name is not available, the corresponding
 * array element will be <code>null</code>.
 * <p>
 * <b>Note</b>: ICU implements the time zone display name formatting algorithm
 * specified by <a href="http://www.unicode.org/reports/tr35/">UTS#35 Unicode
 * Locale Data Markup Language(LDML)</a>. The algorithm supports historic
 * display name changes and various different types of names not available in
 * {@link java.text.DateFormatSymbols#getZoneStrings()}. For accessing the full
 * set of time zone string data used by ICU implementation, you should use
 * {@link TimeZoneNames} APIs instead.
 *
 * @return the time zone strings.
 */
public String[][] getZoneStrings() {
    if (zoneStrings != null) {
        return duplicate(zoneStrings);
    }

    String[] tzIDs = TimeZone.getAvailableIDs();
    TimeZoneNames tznames = TimeZoneNames.getInstance(validLocale);
    tznames.loadAllDisplayNames();
    NameType types[] = {
        NameType.LONG_STANDARD, NameType.SHORT_STANDARD,
        NameType.LONG_DAYLIGHT, NameType.SHORT_DAYLIGHT
    };
    long now = System.currentTimeMillis();
    String[][] array = new String[tzIDs.length][5];
    for (int i = 0; i < tzIDs.length; i++) {
        String canonicalID = TimeZone.getCanonicalID(tzIDs[i]);
        if (canonicalID == null) {
            canonicalID = tzIDs[i];
        }

        array[i][0] = tzIDs[i];
        tznames.getDisplayNames(canonicalID, types, now, array[i], 1);
    }

    zoneStrings = array;
    return zoneStrings;
}
 
Example 5
Source File: TimeZoneFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Parse a zone ID.
 * @param text the text contains a time zone ID string at the position.
 * @param pos the position.
 * @return The zone ID parsed.
 */
private static String parseZoneID(String text, ParsePosition pos) {
    String resolvedID = null;
    if (ZONE_ID_TRIE == null) {
        synchronized (TimeZoneFormat.class) {
            if (ZONE_ID_TRIE == null) {
                // Build zone ID trie
                TextTrieMap<String> trie = new TextTrieMap<String>(true);
                String[] ids = TimeZone.getAvailableIDs();
                for (String id : ids) {
                    trie.put(id, id);
                }
                ZONE_ID_TRIE = trie;
            }
        }
    }

    int[] matchLen = new int[] {0};
    Iterator<String> itr = ZONE_ID_TRIE.get(text, pos.getIndex(), matchLen);
    if (itr != null) {
        resolvedID = itr.next();
        pos.setIndex(pos.getIndex() + matchLen[0]);
    } else {
        // TODO
        // We many need to handle rule based custom zone ID (See ZoneMeta.parseCustomID),
        // such as GM+05:00. However, the public parse method in this class also calls
        // parseOffsetLocalizedGMT and custom zone IDs are likely supported by the parser,
        // so we might not need to handle them here.
        pos.setErrorIndex(pos.getIndex());
    }
    return resolvedID;
}
 
Example 6
Source File: TimeZoneFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Parse a short zone ID.
 * @param text the text contains a time zone ID string at the position.
 * @param pos the position.
 * @return The zone ID for the parsed short zone ID.
 */
private static String parseShortZoneID(String text, ParsePosition pos) {
    String resolvedID = null;
    if (SHORT_ZONE_ID_TRIE == null) {
        synchronized (TimeZoneFormat.class) {
            if (SHORT_ZONE_ID_TRIE == null) {
                // Build short zone ID trie
                TextTrieMap<String> trie = new TextTrieMap<String>(true);
                Set<String> canonicalIDs = TimeZone.getAvailableIDs(SystemTimeZoneType.CANONICAL, null, null);
                for (String id : canonicalIDs) {
                    String shortID = ZoneMeta.getShortID(id);
                    if (shortID != null) {
                        trie.put(shortID, id);
                    }
                }
                // Canonical list does not contain Etc/Unknown
                trie.put(UNKNOWN_SHORT_ZONE_ID, UNKNOWN_ZONE_ID);
                SHORT_ZONE_ID_TRIE = trie;
            }
        }
    }

    int[] matchLen = new int[] {0};
    Iterator<String> itr = SHORT_ZONE_ID_TRIE.get(text, pos.getIndex(), matchLen);
    if (itr != null) {
        resolvedID = itr.next();
        pos.setIndex(pos.getIndex() + matchLen[0]);
    } else {
        pos.setErrorIndex(pos.getIndex());
    }

    return resolvedID;
}
 
Example 7
Source File: IBMCalendarTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Make sure that when adding a day, we actually wind up in a
 * different day.  The DST adjustments we use to keep the hour
 * constant across DST changes can backfire and change the day.
 */
@Test
public void TestTimeZoneTransitionAdd() {
    Locale locale = Locale.US; // could also be CHINA
    SimpleDateFormat dateFormat =
        new SimpleDateFormat("MM/dd/yyyy HH:mm z", locale);

    String tz[] = TimeZone.getAvailableIDs();

    for (int z=0; z<tz.length; ++z) {
        TimeZone t = TimeZone.getTimeZone(tz[z]);
        dateFormat.setTimeZone(t);

        Calendar cal = Calendar.getInstance(t, locale);
        cal.clear();
        // Scan the year 2003, overlapping the edges of the year
        cal.set(Calendar.YEAR, 2002);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 25);

        for (int i=0; i<365+10; ++i) {
            Date yesterday = cal.getTime();
            int yesterday_day = cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 1);
            if (yesterday_day == cal.get(Calendar.DAY_OF_MONTH)) {
                errln(tz[z] + " " +
                      dateFormat.format(yesterday) + " +1d= " +
                      dateFormat.format(cal.getTime()));
            }
        }
    }
}
 
Example 8
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4040996() {
    try {
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
        Calendar calendar = new GregorianCalendar(pdt);

        calendar.set(Calendar.MONTH,3);
        calendar.set(Calendar.DAY_OF_MONTH,18);
        calendar.set(Calendar.SECOND, 30);

        logln("MONTH: " + calendar.get(Calendar.MONTH));
        logln("DAY_OF_MONTH: " + 
                           calendar.get(Calendar.DAY_OF_MONTH));
        logln("MINUTE: " + calendar.get(Calendar.MINUTE));
        logln("SECOND: " + calendar.get(Calendar.SECOND));

        calendar.add(Calendar.SECOND,6);
        //This will print out todays date for MONTH and DAY_OF_MONTH
        //instead of the date it was set to.
        //This happens when adding MILLISECOND or MINUTE also
        logln("MONTH: " + calendar.get(Calendar.MONTH));
        logln("DAY_OF_MONTH: " + 
                           calendar.get(Calendar.DAY_OF_MONTH));
        logln("MINUTE: " + calendar.get(Calendar.MINUTE));
        logln("SECOND: " + calendar.get(Calendar.SECOND));
        if (calendar.get(Calendar.MONTH) != 3 ||
            calendar.get(Calendar.DAY_OF_MONTH) != 18 ||
            calendar.get(Calendar.SECOND) != 36)
            errln("Fail: Calendar.add misbehaves");
    } catch (Exception e) {
        warnln("Could not load data. "+ e.getMessage());
    }
}
 
Example 9
Source File: ZoneMeta.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Return the canonical country code for this tzid.  If we have none, or if the time zone
 * is not associated with a country or unknown, return null. When the given zone is the
 * primary zone of the country, true is set to isPrimary.
 */
public static String getCanonicalCountry(String tzid, Output<Boolean> isPrimary) {
    isPrimary.value = Boolean.FALSE;

    String country = getRegion(tzid);
    if (country != null && country.equals(kWorld)) {
        return null;
    }

    // Check the cache
    Boolean singleZone = SINGLE_COUNTRY_CACHE.get(tzid);
    if (singleZone == null) {
        Set<String> ids = TimeZone.getAvailableIDs(SystemTimeZoneType.CANONICAL_LOCATION, country, null);
        assert(ids.size() >= 1);
        singleZone = Boolean.valueOf(ids.size() <= 1);
        SINGLE_COUNTRY_CACHE.put(tzid, singleZone);
    }

    if (singleZone) {
        isPrimary.value = Boolean.TRUE;
    } else {
        // Note: We may cache the primary zone map in future.

        // Even a country has multiple zones, one of them might be
        // dominant and treated as a primary zone.
        try {
            UResourceBundle bundle = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "metaZones");
            UResourceBundle primaryZones = bundle.get("primaryZones");
            String primaryZone = primaryZones.getString(country);
            if (tzid.equals(primaryZone)) {
                isPrimary.value = Boolean.TRUE;
            } else {
                // The given ID might not be a canonical ID
                String canonicalID = getCanonicalCLDRID(tzid);
                if (canonicalID != null && canonicalID.equals(primaryZone)) {
                    isPrimary.value = Boolean.TRUE;
                }
            }
        } catch (MissingResourceException e) {
            // ignore
        }
    }

    return country;
}
 
Example 10
Source File: TimeZoneNamesImpl.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized Collection<MatchInfo> find(CharSequence text, int start, EnumSet<NameType> nameTypes) {
    if (text == null || text.length() == 0 || start < 0 || start >= text.length()) {
        throw new IllegalArgumentException("bad input text or range");
    }
    NameSearchHandler handler = new NameSearchHandler(nameTypes);
    Collection<MatchInfo> matches;

    // First try of lookup.
    matches = doFind(handler, text, start);
    if (matches != null) {
        return matches;
    }

    // All names are not yet loaded into the trie.
    // We may have loaded names for formatting several time zones,
    // and might be parsing one of those.
    // Populate the parsing trie from all of the already-loaded names.
    addAllNamesIntoTrie();

    // Second try of lookup.
    matches = doFind(handler, text, start);
    if (matches != null) {
        return matches;
    }

    // There are still some names we haven't loaded into the trie yet.
    // Load everything now.
    internalLoadAllDisplayNames();

    // Set default time zone location names
    // for time zones without explicit display names.
    // TODO: Should this logic be moved into internalLoadAllDisplayNames?
    Set<String> tzIDs = TimeZone.getAvailableIDs(SystemTimeZoneType.CANONICAL, null, null);
    for (String tzID : tzIDs) {
        if (!_tzNamesMap.containsKey(tzID)) {
            ZNames.createTimeZoneAndPutInCache(_tzNamesMap, null, tzID);
        }
    }
    addAllNamesIntoTrie();
    _namesTrieFullyLoaded = true;

    // Third try: we must return this one.
    return doFind(handler, text, start);
}
 
Example 11
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void Test4031502() {
    try{
        // This bug actually occurs on Windows NT as well, and doesn't
        // require the host zone to be set; it can be set in Java.
        String[] ids = TimeZone.getAvailableIDs();
        boolean bad = false;
        for (int i=0; i<ids.length; ++i) {
            TimeZone zone = TimeZone.getTimeZone(ids[i]);
            GregorianCalendar cal = new GregorianCalendar(zone);
            cal.clear();
            cal.set(1900, 15, 5, 5, 8, 13);
            if (cal.get(Calendar.HOUR) != 5) {
                logln("Fail: " + zone.getID() + " " +
                      zone.useDaylightTime() + "; DST_OFFSET = " +
                      cal.get(Calendar.DST_OFFSET) / (60*60*1000.0) + "; ZONE_OFFSET = " +
                      cal.get(Calendar.ZONE_OFFSET) / (60*60*1000.0) + "; getRawOffset() = " +
                      zone.getRawOffset() / (60*60*1000.0) +
                      "; HOUR = " + cal.get(Calendar.HOUR));
                cal.clear();
                cal.set(1900, 15, 5, 5, 8, 13);
                if (cal.get(Calendar.HOUR) != 5) {
                    logln("Fail: " + zone.getID() + " " +
                          zone.useDaylightTime() + "; DST_OFFSET = " +
                          cal.get(Calendar.DST_OFFSET) / (60*60*1000.0) + "; ZONE_OFFSET = " +
                          cal.get(Calendar.ZONE_OFFSET) / (60*60*1000.0) + "; getRawOffset() = " +
                          zone.getRawOffset() / (60*60*1000.0) +
                          "; HOUR = " + cal.get(Calendar.HOUR));
                    cal.clear();
                    cal.set(1900, 15, 5, 5, 8, 13);
                    logln("ms = " + cal.getTime() + " (" + cal.getTime().getTime() + ")");
                    cal.get(Calendar.HOUR);
                    java.util.GregorianCalendar cal2 = new java.util.GregorianCalendar(java.util.TimeZone.getTimeZone(ids[i]));
                    cal2.clear();
                    cal2.set(1900, 15, 5, 5, 8, 13);
                    cal2.get(Calendar.HOUR);
                    logln("java.util.GC: " + zone.getID() + " " +
                        zone.useDaylightTime() + "; DST_OFFSET = " +
                        cal2.get(Calendar.DST_OFFSET) / (60*60*1000.0) + "; ZONE_OFFSET = " +
                        cal2.get(Calendar.ZONE_OFFSET) / (60*60*1000.0) + "; getRawOffset() = " +
                        zone.getRawOffset() / (60*60*1000.0) +
                        "; HOUR = " + cal.get(Calendar.HOUR));
                    logln("ms = " + cal2.getTime() + " (" + cal2.getTime().getTime() + ")");
                    bad = true;
                } else if (false) { // Change to true to debug
                    logln("OK: " + zone.getID() + " " +
                          zone.useDaylightTime() + " " +
                          cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
                          zone.getRawOffset() / (60*60*1000) +
                          ": HOUR = " + cal.get(Calendar.HOUR));
                }
            }
            if (bad) errln("TimeZone problems with GC");
        }
    } catch (MissingResourceException e) {
        warnln("Could not load data. "+ e.getMessage());
    }
}
 
Example 12
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestGetAvailableIDs913()
{
    StringBuffer buf = new StringBuffer("TimeZone.getAvailableIDs() = { ");
    String[] s = TimeZone.getAvailableIDs();
    for (int i=0; i<s.length; ++i)
    {
        if (i > 0) buf.append(", ");
        buf.append(s[i]);
    }
    buf.append(" };");
    logln(buf.toString());

    buf.setLength(0);
    buf.append("TimeZone.getAvailableIDs(GMT+02:00) = { ");
    s = TimeZone.getAvailableIDs(+2 * 60 * 60 * 1000);
    for (int i=0; i<s.length; ++i)
    {
        if (i > 0) buf.append(", ");
        buf.append(s[i]);
    }
    buf.append(" };");
    logln(buf.toString());

    TimeZone tz = TimeZone.getTimeZone("PST");
    if (tz != null)
        logln("getTimeZone(PST) = " + tz.getID());
    else
        errln("FAIL: getTimeZone(PST) = null");

    tz = TimeZone.getTimeZone("America/Los_Angeles");
    if (tz != null)
        logln("getTimeZone(America/Los_Angeles) = " + tz.getID());
    else
        errln("FAIL: getTimeZone(PST) = null");

    // Bug 4096694
    tz = TimeZone.getTimeZone("NON_EXISTENT");
    if (tz == null)
        errln("FAIL: getTimeZone(NON_EXISTENT) = null");
    else if (!tz.getID().equals(TimeZone.UNKNOWN_ZONE_ID))
        errln("FAIL: getTimeZone(NON_EXISTENT) = " + tz.getID());
}
 
Example 13
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestCountries() {
    // Make sure America/Los_Angeles is in the "US" group, and
    // Asia/Tokyo isn't.  Vice versa for the "JP" group.

    String[] s = TimeZone.getAvailableIDs("US");
    boolean la = false, tokyo = false;
    String laZone = "America/Los_Angeles", tokyoZone = "Asia/Tokyo";

    for (int i=0; i<s.length; ++i) {
        if (s[i].equals(laZone)) {
            la = true;
        }
        if (s[i].equals(tokyoZone)) {
            tokyo = true;
        }
    }
    if (!la ) {
        errln("FAIL: " + laZone + " in US = " + la);
    }
    if (tokyo) {
        errln("FAIL: " + tokyoZone + " in US = " + tokyo);
    }
    s = TimeZone.getAvailableIDs("JP");
    la = false; tokyo = false;

    for (int i=0; i<s.length; ++i) {
        if (s[i].equals(laZone)) {
            la = true;
        }
        if (s[i].equals(tokyoZone)) {
            tokyo = true;
        }
    }
    if (la) {
        errln("FAIL: " + laZone + " in JP = " + la);
    }
    if (!tokyo) {
        errln("FAIL: " + tokyoZone + " in JP = " + tokyo);
    }
}
 
Example 14
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@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());
        }
    }
}
 
Example 15
Source File: TimeZoneRuleTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private String[] getTestZIDs() {
    if (TestFmwk.getExhaustiveness() > 5) {
        return TimeZone.getAvailableIDs();
    }
    return TESTZIDS;
}
 
Example 16
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test setRawOffset works OK with system timezone
 */
@Test
public void TestT5280() {
    boolean isJdkZone = (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK);
    String[] tzids = TimeZone.getAvailableIDs();
    for (int i = 0; i < tzids.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(tzids[i]);
        boolean useDst = tz.useDaylightTime();

        // Increase offset for 30 minutes
        int newRawOffset = tz.getRawOffset() + 30*60*1000;
        try {
            tz.setRawOffset(newRawOffset);
        } catch (Exception e) {
            errln("FAIL: setRawOffset throws an exception");
        }
        int offset = tz.getRawOffset();
        if (offset != newRawOffset) {
            if (isJdkZone) {
                // JDK TimeZone#setRawOffset() only update the last rule, and getRawOffset() returns
                // the current raw offset. Therefore, they might be different.
                logln("Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            }
        }
        // Ticket#5917
        // Check if DST observation status is not unexpectedly changed.
        boolean newDst = tz.useDaylightTime();
        if (useDst != newDst) {
            errln("FAIL: Modified zone(" + tz.getID() + ") - useDaylightTime has changed from " + useDst + " to " + newDst);
        }
        // Make sure the offset is preserved in a clone
        TimeZone tzClone = (TimeZone)tz.clone();
        int offsetC = tzClone.getRawOffset();
        if (offsetC != newRawOffset) {
            if (isJdkZone) {
                logln("Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            }
        }

        if (offset != offsetC) {
            errln("FAIL: Different raw offset - Original:" + offset + ", Cloned:" + offsetC);
        }
    }
}