From 6caa954eadaf078f9ed2121473c10e9f30544f58 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Wed, 3 Oct 2018 13:59:29 +0100 Subject: API for retrieving time zone IDs by country New method has been requested by Setup Wizard. Test: CTS: run cts-dev -m CtsUtilTestCases -t android.util.cts.TimeUtilsTest Bug: 116544863 Merged-In: I31a9e3d07d5c3fbc8ba1d9c9b398cb2661aa71f9 Change-Id: I31a9e3d07d5c3fbc8ba1d9c9b398cb2661aa71f9 (cherry picked from commit f9bb2d8f020c2c7815359cae8dc8255fd8e9d1de) --- core/java/android/util/TimeUtils.java | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'core/java/android/util/TimeUtils.java') 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. */ @@ -64,6 +72,38 @@ public class TimeUtils { .lookupTimeZoneByCountryAndOffset(country, offset, dst, when, bias); } + /** + * Returns time zone IDs for time zones known to be associated with a country. + * + *

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 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 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 -- cgit v1.2.3