summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Neil Fuller <nfuller@google.com> 2018-11-21 07:55:55 -0800
committer android-build-merger <android-build-merger@google.com> 2018-11-21 07:55:55 -0800
commitf35dd4fa6c50b52f5e8f0254acb3eb9df5e834d0 (patch)
tree79d59da4a4c9aeeb51a5e6bd75c32d39f8576de5
parent535bdc236798d8e0b40bfbef0e962a7aef8ee1b6 (diff)
parentb5ab9c2e1ff2e066ee898ed92b89766fe4020a5f (diff)
Merge "API for retrieving time zone IDs by country" am: 4492ec573a
am: b5ab9c2e1f Change-Id: I69b477e36d760e5d06c232fc7fd0891d98dd52b6
-rwxr-xr-xapi/current.txt1
-rw-r--r--core/java/android/util/TimeUtils.java40
2 files changed, 41 insertions, 0 deletions
diff --git a/api/current.txt b/api/current.txt
index 3c014cbd6ee7..2e4b4ba8cef8 100755
--- a/api/current.txt
+++ b/api/current.txt
@@ -46255,6 +46255,7 @@ package android.util {
public class TimeUtils {
method public static java.util.TimeZone getTimeZone(int, boolean, long, java.lang.String);
method public static java.lang.String getTimeZoneDatabaseVersion();
+ method public static java.util.List<java.lang.String> getTimeZoneIdsForCountryCode(java.lang.String);
}
public class TimingLogger {
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