Fix missing zone strings in some cases

ICU's TimeZoneNames requires a "canonical" zone ID, otherwise it will
return no data. Usually the ID being used is the same as the canonical
ID so there's no problem. TZDB version 2020a switched the ID of one of
Greenland's zones from America/Godthab to America/Nuuk, and ICU/CLDR
hold their translation data against America/Godthab in the current
version of ICU on Android.

Bug: 155167041
Test: Manual - viewed settings on a device with the tzdb 2020a update
Change-Id: Ia89c777a832f8b41c8d56d3327a9b7370ba44b52
This commit is contained in:
Neil Fuller
2020-04-29 10:16:19 +01:00
parent a7bf6a3046
commit eafec7a244

View File

@@ -149,22 +149,31 @@ public class TimeZoneInfo {
* @return TimeZoneInfo containing time zone names, exemplar locations and GMT offset
*/
public TimeZoneInfo format(TimeZone timeZone) {
final String id = timeZone.getID();
String canonicalZoneId = getCanonicalZoneId(timeZone);
final TimeZoneNames timeZoneNames = mTimeZoneFormat.getTimeZoneNames();
final java.util.TimeZone javaTimeZone = java.util.TimeZone.getTimeZone(id);
final java.util.TimeZone javaTimeZone = java.util.TimeZone.getTimeZone(canonicalZoneId);
final CharSequence gmtOffset = ZoneGetter.getGmtOffsetText(mTimeZoneFormat, mLocale,
javaTimeZone, mNow);
return new TimeZoneInfo.Builder(timeZone)
.setGenericName(timeZoneNames.getDisplayName(id,
.setGenericName(timeZoneNames.getDisplayName(canonicalZoneId,
TimeZoneNames.NameType.LONG_GENERIC, mNow.getTime()))
.setStandardName(timeZoneNames.getDisplayName(id,
.setStandardName(timeZoneNames.getDisplayName(canonicalZoneId,
TimeZoneNames.NameType.LONG_STANDARD, mNow.getTime()))
.setDaylightName(timeZoneNames.getDisplayName(id,
.setDaylightName(timeZoneNames.getDisplayName(canonicalZoneId,
TimeZoneNames.NameType.LONG_DAYLIGHT, mNow.getTime()))
.setExemplarLocation(timeZoneNames.getExemplarLocationName(id))
.setExemplarLocation(timeZoneNames.getExemplarLocationName(canonicalZoneId))
.setGmtOffset(gmtOffset)
.build();
}
private static String getCanonicalZoneId(TimeZone timeZone) {
final String id = timeZone.getID();
final String canonicalId = TimeZone.getCanonicalID(id);
if (canonicalId != null) {
return canonicalId;
}
return id;
}
}
}