diff options
| author | Neil Fuller <nfuller@google.com> | 2018-11-21 15:18:27 +0000 |
|---|---|---|
| committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2018-11-21 15:18:27 +0000 |
| commit | 4492ec573ae421affd3adebb1d583fcf33508bb4 (patch) | |
| tree | f7e4dbbd8e351cf6f58653a309f73a61df5c9fd1 /core/java | |
| parent | 3d2b099dc0d7019be2348215fd13e8afa0a14746 (diff) | |
| parent | 6caa954eadaf078f9ed2121473c10e9f30544f58 (diff) | |
Merge "API for retrieving time zone IDs by country"
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/util/TimeUtils.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/core/java/android/util/TimeUtils.java b/core/java/android/util/TimeUtils.java index 0e25038bbb38..717a85801eba 100644 --- a/core/java/android/util/TimeUtils.java +++ b/core/java/android/util/TimeUtils.java @@ -16,16 +16,24 @@ package android.util; +import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.UnsupportedAppUsage; import android.os.SystemClock; +import libcore.util.CountryTimeZones; +import libcore.util.CountryTimeZones.TimeZoneMapping; import libcore.util.TimeZoneFinder; import libcore.util.ZoneInfoDB; import java.io.PrintWriter; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; import java.util.Date; +import java.util.List; + /** * A class containing utility methods related to time zones. */ @@ -65,6 +73,38 @@ public class TimeUtils { } /** + * Returns time zone IDs for time zones known to be associated with a country. + * + * <p>The list returned may be different from other on-device sources like + * {@link android.icu.util.TimeZone#getRegion(String)} as it can be curated to avoid + * contentious mappings. + * + * @param countryCode the ISO 3166-1 alpha-2 code for the country as can be obtained using + * {@link java.util.Locale#getCountry()} + * @return IDs that can be passed to {@link java.util.TimeZone#getTimeZone(String)} or similar + * methods, or {@code null} if the countryCode is unrecognized + */ + public static @Nullable List<String> getTimeZoneIdsForCountryCode(@NonNull String countryCode) { + if (countryCode == null) { + throw new NullPointerException("countryCode == null"); + } + TimeZoneFinder timeZoneFinder = TimeZoneFinder.getInstance(); + CountryTimeZones countryTimeZones = + timeZoneFinder.lookupCountryTimeZones(countryCode.toLowerCase()); + if (countryTimeZones == null) { + return null; + } + + List<String> timeZoneIds = new ArrayList<>(); + for (TimeZoneMapping timeZoneMapping : countryTimeZones.getTimeZoneMappings()) { + if (timeZoneMapping.showInPicker) { + timeZoneIds.add(timeZoneMapping.timeZoneId); + } + } + return Collections.unmodifiableList(timeZoneIds); + } + + /** * Returns a String indicating the version of the time zone database currently * in use. The format of the string is dependent on the underlying time zone * database implementation, but will typically contain the year in which the database |
