diff options
author | Andreas Gampe <agampe@google.com> | 2015-04-13 17:06:11 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-04-13 17:06:11 +0000 |
commit | 47083c5cb7dcef4a0f43a521f8ccfd8566971cd4 (patch) | |
tree | 396ed771ef51e05c302b7a3f1075a88e648ea9d4 /luni | |
parent | 77393064b0eb80cb3ead9243f707c0e70d2743ee (diff) | |
parent | d65a3c71d7af3ae097f0722fba6be80f057b3d18 (diff) | |
download | libcore-47083c5cb7dcef4a0f43a521f8ccfd8566971cd4.zip libcore-47083c5cb7dcef4a0f43a521f8ccfd8566971cd4.tar.gz libcore-47083c5cb7dcef4a0f43a521f8ccfd8566971cd4.tar.bz2 |
am d65a3c71: am a936d452: Merge "Libcore: Refactor TimeZone for compile-time initialization"
* commit 'd65a3c71d7af3ae097f0722fba6be80f057b3d18':
Libcore: Refactor TimeZone for compile-time initialization
Diffstat (limited to 'luni')
-rw-r--r-- | luni/src/main/java/java/util/TimeZone.java | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/luni/src/main/java/java/util/TimeZone.java b/luni/src/main/java/java/util/TimeZone.java index 854a4a6..d7adbf2 100644 --- a/luni/src/main/java/java/util/TimeZone.java +++ b/luni/src/main/java/java/util/TimeZone.java @@ -68,7 +68,51 @@ import org.apache.harmony.luni.internal.util.TimezoneGetter; public abstract class TimeZone implements Serializable, Cloneable { private static final long serialVersionUID = 3581463369166924961L; - private static final Pattern CUSTOM_ZONE_ID_PATTERN = Pattern.compile("^GMT[-+](\\d{1,2})(:?(\\d\\d))?$"); + /** + * Helper class to parse a custom timezone. This is in a separate class as regular expressions + * cannot be compile-time initialized, so that static field is separated out from TimeZone + * proper. + */ + private final static class CustomTimeZoneParser { + private static final Pattern CUSTOM_ZONE_ID_PATTERN = + Pattern.compile("^GMT[-+](\\d{1,2})(:?(\\d\\d))?$"); + + private CustomTimeZoneParser() {} + + /** + * Returns a new SimpleTimeZone for an ID of the form "GMT[+|-]hh[[:]mm]", or null. + */ + private static TimeZone getCustomTimeZone(String id) { + Matcher m = CUSTOM_ZONE_ID_PATTERN.matcher(id); + if (!m.matches()) { + return null; + } + + int hour; + int minute = 0; + try { + hour = Integer.parseInt(m.group(1)); + if (m.group(3) != null) { + minute = Integer.parseInt(m.group(3)); + } + } catch (NumberFormatException impossible) { + throw new AssertionError(impossible); + } + + if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { + return null; + } + + char sign = id.charAt(3); + int raw = (hour * 3600000) + (minute * 60000); + if (sign == '-') { + raw = -raw; + } + + String cleanId = String.format("GMT%c%02d:%02d", sign, hour, minute); + return new SimpleTimeZone(raw, cleanId); + } + } /** * The short display name style, such as {@code PDT}. Requests for this @@ -368,7 +412,7 @@ public abstract class TimeZone implements Serializable, Cloneable { // Custom time zone? if (zone == null && id.length() > 3 && id.startsWith("GMT")) { - zone = getCustomTimeZone(id); + zone = CustomTimeZoneParser.getCustomTimeZone(id); } // We never return null; on failure we return the equivalent of "GMT". @@ -376,40 +420,6 @@ public abstract class TimeZone implements Serializable, Cloneable { } /** - * Returns a new SimpleTimeZone for an ID of the form "GMT[+|-]hh[[:]mm]", or null. - */ - private static TimeZone getCustomTimeZone(String id) { - Matcher m = CUSTOM_ZONE_ID_PATTERN.matcher(id); - if (!m.matches()) { - return null; - } - - int hour; - int minute = 0; - try { - hour = Integer.parseInt(m.group(1)); - if (m.group(3) != null) { - minute = Integer.parseInt(m.group(3)); - } - } catch (NumberFormatException impossible) { - throw new AssertionError(impossible); - } - - if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { - return null; - } - - char sign = id.charAt(3); - int raw = (hour * 3600000) + (minute * 60000); - if (sign == '-') { - raw = -raw; - } - - String cleanId = String.format("GMT%c%02d:%02d", sign, hour, minute); - return new SimpleTimeZone(raw, cleanId); - } - - /** * Returns true if {@code timeZone} has the same rules as this time zone. * * <p>The base implementation returns true if both time zones have the same |