diff options
author | James Kung <kingkung@google.com> | 2013-04-03 18:42:46 -0700 |
---|---|---|
committer | James Kung <kingkung@google.com> | 2013-04-04 10:40:34 -0700 |
commit | cc955106e2889ec351593b913cb7af4328dca153 (patch) | |
tree | c902504a912ff67aab9f19d663adcac8bbcbfc2a /src/com/android | |
parent | 41fa43b962c94ce4a7e82d60737bf96ca0abd6bf (diff) | |
download | android_frameworks_opt_timezonepicker-cc955106e2889ec351593b913cb7af4328dca153.tar.gz android_frameworks_opt_timezonepicker-cc955106e2889ec351593b913cb7af4328dca153.tar.bz2 android_frameworks_opt_timezonepicker-cc955106e2889ec351593b913cb7af4328dca153.zip |
Adding Utils class to timezonepicker
Change-Id: Ib9345e3256309e682221830a2dbad0347fc66317
Diffstat (limited to 'src/com/android')
-rw-r--r-- | src/com/android/timezonepicker/TimeZonePickerUtils.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/com/android/timezonepicker/TimeZonePickerUtils.java b/src/com/android/timezonepicker/TimeZonePickerUtils.java new file mode 100644 index 0000000..0580df3 --- /dev/null +++ b/src/com/android/timezonepicker/TimeZonePickerUtils.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.timezonepicker; + +import android.text.format.DateUtils; +import android.text.format.Time; + +import java.util.Locale; +import java.util.TimeZone; + +public class TimeZonePickerUtils { + + /** + * Given a timezone id (e.g. America/Los_Angeles), returns the corresponding timezone + * display name (e.g. (GMT-7.00) Pacific Time). + * + * @param id The timezone id + * @param millis The time (daylight savings or not) + * @return The display name of the timezone. + */ + public static String getGmtDisplayName(String id, long millis) { + TimeZone timezone = TimeZone.getTimeZone(id); + if (timezone == null) { + return null; + } + return buildGmtDisplayName(timezone, millis); + } + + private static String buildGmtDisplayName(TimeZone tz, long timeMillis) { + Time time = new Time(tz.getID()); + time.set(timeMillis); + + StringBuilder sb = new StringBuilder(); + sb.append("(GMT"); + + final int gmtOffset = tz.getOffset(timeMillis); + if (gmtOffset < 0) { + sb.append('-'); + } else { + sb.append('+'); + } + + final int p = Math.abs(gmtOffset); + sb.append(p / DateUtils.HOUR_IN_MILLIS); // Hour + + final int min = (p / (int) DateUtils.MINUTE_IN_MILLIS) % 60; + if (min != 0) { // Show minutes if non-zero + sb.append(':'); + if (min < 10) { + sb.append('0'); + } + sb.append(min); + } + sb.append(") "); + + // tz.inDaylightTime(new Date(timeMillis)) + String displayName = tz.getDisplayName(time.isDst != 0, TimeZone.LONG, + Locale.getDefault()); + sb.append(displayName); + + if (tz.useDaylightTime()) { + sb.append(" \u2600"); // Sun symbol + } + return sb.toString(); + } + +} |