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

The following examples show how to use com.ibm.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 fitnotifications 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: DateFormatSymbols.java    From fitnotifications 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.
 * @stable ICU 2.0
 */
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 3
Source File: TimeZoneFormat.java    From fitnotifications 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 4
Source File: TimeZoneFormat.java    From fitnotifications 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 5
Source File: TimeOptionDialog.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private void createRightComponent( Composite composite )
{
	Label formatLabel = new Label( composite, SWT.CENTER | SWT.SINGLE );
	formatLabel.setText( LABEL_FORMAT );
	formatLabel.setBounds( 0, 8, 60, 30 );

	combo = new Combo( composite, SWT.READ_ONLY | SWT.DROP_DOWN );
	List list = TimeFormat.getDefaultFormat( ).getSupportList( );
	String[] items = new String[list.size( )];
	list.toArray( items );
	combo.setBounds( 60, 2, 150, 30 );
	combo.setVisibleItemCount( 30 );
	combo.setItems( items );
	combo.select( 0 );

	Label zoneLabel = new Label( composite, SWT.CENTER );
	zoneLabel.setText( LABEL_TIMEZONE );
	zoneLabel.setBounds( 0, 108, 60, 30 );

	zoneCombo = new Combo( composite, SWT.READ_ONLY | SWT.SINGLE );
	zoneCombo.setVisibleItemCount( 30 );
	items = TimeZone.getAvailableIDs( );
	zoneCombo.setBounds( 60, 102, 150, 30 );
	zoneCombo.setItems( items );
	zoneCombo.select( 0 );

}
 
Example 6
Source File: HolidayEventsLoader.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
private String getTimeZoneForCountry(String countryCode) {
  // if time zone of a country is set explicitly
  if (COUNTRY_TO_TIMEZONE.containsKey(countryCode)) {
    return COUNTRY_TO_TIMEZONE.get(countryCode);
  }
  // guess the time zone from country code
  String timeZone = "GMT";
  String[] timeZones = TimeZone.getAvailableIDs(countryCode);
  if (timeZones.length != 0) {
    timeZone = timeZones[0];
  }
  return timeZone;
}
 
Example 7
Source File: ZoneMeta.java    From fitnotifications 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 8
Source File: TimeZoneNamesImpl.java    From fitnotifications 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);
}