use of android.icu.text.TimeZoneNames in project android_frameworks_base by DirtyUnicorns.
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;
}
use of android.icu.text.TimeZoneNames in project j2objc by google.
the class TimeZone method _getDisplayName.
/**
* internal version (which is called by public APIs) accepts
* SHORT, LONG, SHORT_GENERIC, LONG_GENERIC, SHORT_GMT, LONG_GMT,
* SHORT_COMMONLY_USED and GENERIC_LOCATION.
*/
private String _getDisplayName(int style, boolean daylight, ULocale locale) {
if (locale == null) {
throw new NullPointerException("locale is null");
}
String result = null;
if (style == GENERIC_LOCATION || style == LONG_GENERIC || style == SHORT_GENERIC) {
// Generic format
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
long date = System.currentTimeMillis();
Output<TimeType> timeType = new Output<TimeType>(TimeType.UNKNOWN);
switch(style) {
case GENERIC_LOCATION:
result = tzfmt.format(Style.GENERIC_LOCATION, this, date, timeType);
break;
case LONG_GENERIC:
result = tzfmt.format(Style.GENERIC_LONG, this, date, timeType);
break;
case SHORT_GENERIC:
result = tzfmt.format(Style.GENERIC_SHORT, this, date, timeType);
break;
}
// appropriate for the requested daylight value.
if (daylight && timeType.value == TimeType.STANDARD || !daylight && timeType.value == TimeType.DAYLIGHT) {
int offset = daylight ? getRawOffset() + getDSTSavings() : getRawOffset();
result = (style == SHORT_GENERIC) ? tzfmt.formatOffsetShortLocalizedGMT(offset) : tzfmt.formatOffsetLocalizedGMT(offset);
}
} else if (style == LONG_GMT || style == SHORT_GMT) {
// Offset format
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
switch(style) {
case LONG_GMT:
result = tzfmt.formatOffsetLocalizedGMT(offset);
break;
case SHORT_GMT:
result = tzfmt.formatOffsetISO8601Basic(offset, false, false, false);
break;
}
} else {
// Specific format
assert (style == LONG || style == SHORT || style == SHORT_COMMONLY_USED);
// Gets the name directly from TimeZoneNames
long date = System.currentTimeMillis();
TimeZoneNames tznames = TimeZoneNames.getInstance(locale);
NameType nameType = null;
switch(style) {
case LONG:
nameType = daylight ? NameType.LONG_DAYLIGHT : NameType.LONG_STANDARD;
break;
case SHORT:
case SHORT_COMMONLY_USED:
nameType = daylight ? NameType.SHORT_DAYLIGHT : NameType.SHORT_STANDARD;
break;
}
result = tznames.getDisplayName(ZoneMeta.getCanonicalCLDRID(this), nameType, date);
if (result == null) {
// Fallback to localized GMT
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
result = (style == LONG) ? tzfmt.formatOffsetLocalizedGMT(offset) : tzfmt.formatOffsetShortLocalizedGMT(offset);
}
}
assert (result != null);
return result;
}
use of android.icu.text.TimeZoneNames in project j2objc by google.
the class ZoneName method toZid.
public static String toZid(String zid, Locale locale) {
// Android-changed: delegate to ICU.
TimeZoneNames tzNames = TimeZoneNames.getInstance(locale);
if (tzNames.getAvailableMetaZoneIDs().contains(zid)) {
// Compare TimeZoneFormat#getTargetRegion.
ULocale uLocale = ULocale.forLocale(locale);
String region = uLocale.getCountry();
if (region.length() == 0) {
uLocale = ULocale.addLikelySubtags(uLocale);
region = uLocale.getCountry();
}
zid = tzNames.getReferenceZoneID(zid, region);
}
return toZid(zid);
}
use of android.icu.text.TimeZoneNames in project j2objc by google.
the class TimeZoneFormatTest method TestFormatTZDBNames.
@Test
public void TestFormatTZDBNames() {
// 2013-01-15T00:00:00Z
final Date dateJan = new Date(1358208000000L);
// 2013-07-15T00:00:00Z
final Date dateJul = new Date(1373846400000L);
final Object[][] TESTDATA = { { "en", "America/Chicago", dateJan, Style.SPECIFIC_SHORT, "CST", TimeType.STANDARD }, { "en", "Asia/Shanghai", dateJan, Style.SPECIFIC_SHORT, "CST", TimeType.STANDARD }, { "zh_Hans", "Asia/Shanghai", dateJan, Style.SPECIFIC_SHORT, "CST", TimeType.STANDARD }, { "en", "America/Los_Angeles", dateJul, Style.SPECIFIC_LONG, // No long display names
"GMT-07:00", TimeType.DAYLIGHT }, { "ja", "America/Los_Angeles", dateJul, Style.SPECIFIC_SHORT, "PDT", TimeType.DAYLIGHT }, { "en", "Australia/Sydney", dateJan, Style.SPECIFIC_SHORT, "AEDT", TimeType.DAYLIGHT }, { "en", "Australia/Sydney", dateJul, Style.SPECIFIC_SHORT, "AEST", TimeType.STANDARD } };
for (Object[] testCase : TESTDATA) {
ULocale loc = new ULocale((String) testCase[0]);
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(loc).cloneAsThawed();
TimeZoneNames tzdbNames = TimeZoneNames.getTZDBInstance(loc);
tzfmt.setTimeZoneNames(tzdbNames);
TimeZone tz = TimeZone.getTimeZone((String) testCase[1]);
Output<TimeType> timeType = new Output<TimeType>();
String out = tzfmt.format((Style) testCase[3], tz, ((Date) testCase[2]).getTime(), timeType);
if (!out.equals(testCase[4]) || timeType.value != testCase[5]) {
errln("Format result for [locale=" + testCase[0] + ",tzid=" + testCase[1] + ",date=" + testCase[2] + ",style=" + testCase[3] + "]: expected [output=" + testCase[4] + ",type=" + testCase[5] + "]; actual [output=" + out + ",type=" + timeType.value + "]");
}
}
}
use of android.icu.text.TimeZoneNames in project j2objc by google.
the class TimeZoneFormatTest method TestDefaultTimeZoneNames.
// Coverage for default implementation and abstract methods in base class.
@Test
public void TestDefaultTimeZoneNames() {
long date = System.currentTimeMillis();
TimeZoneNames.Factory factory;
try {
Class cls = Class.forName("android.icu.text.TimeZoneNames$DefaultTimeZoneNames$FactoryImpl");
factory = (Factory) cls.newInstance();
} catch (Exception e) {
errln("Could not create class DefaultTimeZoneNames.FactoryImpl: " + e.getClass() + ": " + e.getMessage());
return;
}
TimeZoneNames tzn = factory.getTimeZoneNames(ULocale.ENGLISH);
assertEquals("Abstract: getAvailableMetaZoneIDs()", tzn.getAvailableMetaZoneIDs(), Collections.emptySet());
assertEquals("Abstract: getAvailableMetaZoneIDs(String tzID)", tzn.getAvailableMetaZoneIDs("America/Chicago"), Collections.emptySet());
assertEquals("Abstract: getMetaZoneID(String tzID, long date)", tzn.getMetaZoneID("America/Chicago", date), null);
assertEquals("Abstract: getReferenceZoneID(String mzID, String region)", tzn.getReferenceZoneID("America_Central", "IT"), null);
assertEquals("Abstract: getMetaZoneDisplayName(String mzID, NameType type)", tzn.getMetaZoneDisplayName("America_Central", NameType.LONG_DAYLIGHT), null);
assertEquals("Abstract: getTimeZoneDisplayName(String mzID, NameType type)", tzn.getTimeZoneDisplayName("America/Chicago", NameType.LONG_DAYLIGHT), null);
assertEquals("Abstract: find(CharSequence text, int start, EnumSet<NameType> nameTypes)", tzn.find("foo", 0, EnumSet.noneOf(NameType.class)), Collections.emptyList());
// Other abstract-class methods that aren't covered
tzn = new TimeZoneNamesInheriter();
try {
tzn.find(null, 0, null);
} catch (UnsupportedOperationException e) {
assertEquals("find() exception", "The method is not implemented in TimeZoneNames base class.", e.getMessage());
}
}
Aggregations