Search in sources :

Example 1 with TimeZoneNames

use of android.icu.text.TimeZoneNames in project android_frameworks_base by ResurrectionRemix.

the class ZoneGetter method getTimeZoneOffsetAndName.

public static String getTimeZoneOffsetAndName(TimeZone tz, Date now) {
    Locale locale = Locale.getDefault();
    String gmtString = getGmtOffsetString(locale, tz, now);
    TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
    String zoneNameString = getZoneLongName(timeZoneNames, tz, now);
    if (zoneNameString == null) {
        return gmtString;
    }
    // We don't use punctuation here to avoid having to worry about localizing that too!
    return gmtString + " " + zoneNameString;
}
Also used : Locale(java.util.Locale) TimeZoneNames(android.icu.text.TimeZoneNames)

Example 2 with TimeZoneNames

use of android.icu.text.TimeZoneNames in project android_frameworks_base by ResurrectionRemix.

the class ZoneGetter method getZonesList.

public static List<Map<String, Object>> getZonesList(Context context) {
    final Locale locale = Locale.getDefault();
    final Date now = new Date();
    final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
    // The display name chosen for each zone entry depends on whether the zone is one associated
    // with the country of the user's chosen locale. For "local" zones we prefer the "long name"
    // (e.g. "Europe/London" -> "British Summer Time" for people in the UK). For "non-local"
    // zones we prefer the exemplar location (e.g. "Europe/London" -> "London" for English
    // speakers from outside the UK). This heuristic is based on the fact that people are
    // typically familiar with their local timezones and exemplar locations don't always match
    // modern-day expectations for people living in the country covered. Large countries like
    // China that mostly use a single timezone (olson id: "Asia/Shanghai") may not live near
    // "Shanghai" and prefer the long name over the exemplar location. The only time we don't
    // follow this policy for local zones is when Android supplies multiple olson IDs to choose
    // from and the use of a zone's long name leads to ambiguity. For example, at the time of
    // writing Android lists 5 olson ids for Australia which collapse to 2 different zone names
    // in winter but 4 different zone names in summer. The ambiguity leads to the users
    // selecting the wrong olson ids.
    // Get the list of olson ids to display to the user.
    List<String> olsonIdsToDisplayList = readTimezonesToDisplay(context);
    // Store the information we are going to need more than once.
    final int zoneCount = olsonIdsToDisplayList.size();
    final String[] olsonIdsToDisplay = new String[zoneCount];
    final TimeZone[] timeZones = new TimeZone[zoneCount];
    final String[] gmtOffsetStrings = new String[zoneCount];
    for (int i = 0; i < zoneCount; i++) {
        String olsonId = olsonIdsToDisplayList.get(i);
        olsonIdsToDisplay[i] = olsonId;
        TimeZone tz = TimeZone.getTimeZone(olsonId);
        timeZones[i] = tz;
        gmtOffsetStrings[i] = getGmtOffsetString(locale, tz, now);
    }
    // Create a lookup of local zone IDs.
    Set<String> localZoneIds = new HashSet<String>();
    for (String olsonId : libcore.icu.TimeZoneNames.forLocale(locale)) {
        localZoneIds.add(olsonId);
    }
    // Work out whether the display names we would show by default would be ambiguous.
    Set<String> localZoneNames = new HashSet<String>();
    boolean useExemplarLocationForLocalNames = false;
    for (int i = 0; i < zoneCount; i++) {
        String olsonId = olsonIdsToDisplay[i];
        if (localZoneIds.contains(olsonId)) {
            TimeZone tz = timeZones[i];
            String displayName = getZoneLongName(timeZoneNames, tz, now);
            if (displayName == null) {
                displayName = gmtOffsetStrings[i];
            }
            boolean nameIsUnique = localZoneNames.add(displayName);
            if (!nameIsUnique) {
                useExemplarLocationForLocalNames = true;
                break;
            }
        }
    }
    // Generate the list of zone entries to return.
    List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < zoneCount; i++) {
        String olsonId = olsonIdsToDisplay[i];
        TimeZone tz = timeZones[i];
        String gmtOffsetString = gmtOffsetStrings[i];
        boolean isLocalZoneId = localZoneIds.contains(olsonId);
        boolean preferLongName = isLocalZoneId && !useExemplarLocationForLocalNames;
        String displayName;
        if (preferLongName) {
            displayName = getZoneLongName(timeZoneNames, tz, now);
        } else {
            displayName = timeZoneNames.getExemplarLocationName(tz.getID());
            if (displayName == null || displayName.isEmpty()) {
                // getZoneExemplarLocation can return null. Fall back to the long name.
                displayName = getZoneLongName(timeZoneNames, tz, now);
            }
        }
        if (displayName == null || displayName.isEmpty()) {
            displayName = gmtOffsetString;
        }
        int offsetMillis = tz.getOffset(now.getTime());
        Map<String, Object> displayEntry = createDisplayEntry(tz, gmtOffsetString, displayName, offsetMillis);
        zones.add(displayEntry);
    }
    return zones;
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) TimeZoneNames(android.icu.text.TimeZoneNames) Date(java.util.Date) TimeZone(java.util.TimeZone) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with TimeZoneNames

use of android.icu.text.TimeZoneNames in project platform_frameworks_base by android.

the class ZoneGetter method getTimeZoneOffsetAndName.

public static String getTimeZoneOffsetAndName(TimeZone tz, Date now) {
    Locale locale = Locale.getDefault();
    String gmtString = getGmtOffsetString(locale, tz, now);
    TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
    String zoneNameString = getZoneLongName(timeZoneNames, tz, now);
    if (zoneNameString == null) {
        return gmtString;
    }
    // We don't use punctuation here to avoid having to worry about localizing that too!
    return gmtString + " " + zoneNameString;
}
Also used : Locale(java.util.Locale) TimeZoneNames(android.icu.text.TimeZoneNames)

Example 4 with TimeZoneNames

use of android.icu.text.TimeZoneNames in project android_frameworks_base by crdroidandroid.

the class ZoneGetter method getZonesList.

public static List<Map<String, Object>> getZonesList(Context context) {
    final Locale locale = Locale.getDefault();
    final Date now = new Date();
    final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
    // The display name chosen for each zone entry depends on whether the zone is one associated
    // with the country of the user's chosen locale. For "local" zones we prefer the "long name"
    // (e.g. "Europe/London" -> "British Summer Time" for people in the UK). For "non-local"
    // zones we prefer the exemplar location (e.g. "Europe/London" -> "London" for English
    // speakers from outside the UK). This heuristic is based on the fact that people are
    // typically familiar with their local timezones and exemplar locations don't always match
    // modern-day expectations for people living in the country covered. Large countries like
    // China that mostly use a single timezone (olson id: "Asia/Shanghai") may not live near
    // "Shanghai" and prefer the long name over the exemplar location. The only time we don't
    // follow this policy for local zones is when Android supplies multiple olson IDs to choose
    // from and the use of a zone's long name leads to ambiguity. For example, at the time of
    // writing Android lists 5 olson ids for Australia which collapse to 2 different zone names
    // in winter but 4 different zone names in summer. The ambiguity leads to the users
    // selecting the wrong olson ids.
    // Get the list of olson ids to display to the user.
    List<String> olsonIdsToDisplayList = readTimezonesToDisplay(context);
    // Store the information we are going to need more than once.
    final int zoneCount = olsonIdsToDisplayList.size();
    final String[] olsonIdsToDisplay = new String[zoneCount];
    final TimeZone[] timeZones = new TimeZone[zoneCount];
    final String[] gmtOffsetStrings = new String[zoneCount];
    for (int i = 0; i < zoneCount; i++) {
        String olsonId = olsonIdsToDisplayList.get(i);
        olsonIdsToDisplay[i] = olsonId;
        TimeZone tz = TimeZone.getTimeZone(olsonId);
        timeZones[i] = tz;
        gmtOffsetStrings[i] = getGmtOffsetString(locale, tz, now);
    }
    // Create a lookup of local zone IDs.
    Set<String> localZoneIds = new HashSet<String>();
    for (String olsonId : libcore.icu.TimeZoneNames.forLocale(locale)) {
        localZoneIds.add(olsonId);
    }
    // Work out whether the display names we would show by default would be ambiguous.
    Set<String> localZoneNames = new HashSet<String>();
    boolean useExemplarLocationForLocalNames = false;
    for (int i = 0; i < zoneCount; i++) {
        String olsonId = olsonIdsToDisplay[i];
        if (localZoneIds.contains(olsonId)) {
            TimeZone tz = timeZones[i];
            String displayName = getZoneLongName(timeZoneNames, tz, now);
            if (displayName == null) {
                displayName = gmtOffsetStrings[i];
            }
            boolean nameIsUnique = localZoneNames.add(displayName);
            if (!nameIsUnique) {
                useExemplarLocationForLocalNames = true;
                break;
            }
        }
    }
    // Generate the list of zone entries to return.
    List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < zoneCount; i++) {
        String olsonId = olsonIdsToDisplay[i];
        TimeZone tz = timeZones[i];
        String gmtOffsetString = gmtOffsetStrings[i];
        boolean isLocalZoneId = localZoneIds.contains(olsonId);
        boolean preferLongName = isLocalZoneId && !useExemplarLocationForLocalNames;
        String displayName;
        if (preferLongName) {
            displayName = getZoneLongName(timeZoneNames, tz, now);
        } else {
            displayName = timeZoneNames.getExemplarLocationName(tz.getID());
            if (displayName == null || displayName.isEmpty()) {
                // getZoneExemplarLocation can return null. Fall back to the long name.
                displayName = getZoneLongName(timeZoneNames, tz, now);
            }
        }
        if (displayName == null || displayName.isEmpty()) {
            displayName = gmtOffsetString;
        }
        int offsetMillis = tz.getOffset(now.getTime());
        Map<String, Object> displayEntry = createDisplayEntry(tz, gmtOffsetString, displayName, offsetMillis);
        zones.add(displayEntry);
    }
    return zones;
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) TimeZoneNames(android.icu.text.TimeZoneNames) Date(java.util.Date) TimeZone(java.util.TimeZone) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 5 with TimeZoneNames

use of android.icu.text.TimeZoneNames in project android_frameworks_base by crdroidandroid.

the class ZoneGetter method getTimeZoneOffsetAndName.

public static String getTimeZoneOffsetAndName(TimeZone tz, Date now) {
    Locale locale = Locale.getDefault();
    String gmtString = getGmtOffsetString(locale, tz, now);
    TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
    String zoneNameString = getZoneLongName(timeZoneNames, tz, now);
    if (zoneNameString == null) {
        return gmtString;
    }
    // We don't use punctuation here to avoid having to worry about localizing that too!
    return gmtString + " " + zoneNameString;
}
Also used : Locale(java.util.Locale) TimeZoneNames(android.icu.text.TimeZoneNames)

Aggregations

TimeZoneNames (android.icu.text.TimeZoneNames)16 Locale (java.util.Locale)10 Date (java.util.Date)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 TimeZone (java.util.TimeZone)5 TZDBTimeZoneNames (android.icu.impl.TZDBTimeZoneNames)4 ULocale (android.icu.util.ULocale)4 Test (org.junit.Test)4 TimeZoneFormat (android.icu.text.TimeZoneFormat)3 TimeType (android.icu.text.TimeZoneFormat.TimeType)2 NameType (android.icu.text.TimeZoneNames.NameType)2 Factory (android.icu.text.TimeZoneNames.Factory)1 BasicTimeZone (android.icu.util.BasicTimeZone)1 Output (android.icu.util.Output)1 SimpleTimeZone (android.icu.util.SimpleTimeZone)1 TimeZone (android.icu.util.TimeZone)1 ParseException (java.text.ParseException)1