Merge "API for retrieving time zone IDs by country"

This commit is contained in:
Neil Fuller
2018-11-21 15:18:27 +00:00
committed by Gerrit Code Review
2 changed files with 41 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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.
*
* <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