TimeZoneInfo.java revision 9489460af729fad751cbf42838b303ae85b22079
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.timezonepicker; 18 19import android.content.Context; 20import android.text.format.DateUtils; 21import android.text.format.Time; 22import android.util.Log; 23import android.util.SparseArray; 24 25import java.lang.reflect.Field; 26import java.text.DateFormat; 27import java.util.Arrays; 28import java.util.Date; 29import java.util.Formatter; 30import java.util.Locale; 31import java.util.TimeZone; 32 33public class TimeZoneInfo implements Comparable<TimeZoneInfo> { 34 private static final char SEPARATOR = ','; 35 private static final String TAG = null; 36 public static int NUM_OF_TRANSITIONS = 6; 37 public static long time = System.currentTimeMillis() / 1000; 38 public static boolean is24HourFormat; 39 40 TimeZone mTz; 41 public String mTzId; 42 int mRawoffset; 43 public int[] mTransitions; // may have trailing 0's. 44 public String mCountry; 45 public int groupId; 46 private boolean hasDst; 47 public String mDisplayName; 48 private Time recycledTime = new Time(); 49 private static StringBuilder mSB = new StringBuilder(50); 50 private static Formatter mFormatter = new Formatter(mSB, Locale.getDefault()); 51 52 public TimeZoneInfo(TimeZone tz, String country) { 53 mTz = tz; 54 mTzId = tz.getID(); 55 mCountry = country; 56 mRawoffset = tz.getRawOffset(); 57 hasDst = tz.useDaylightTime(); 58 59 try { 60 mTransitions = getTransitions(tz, time); 61 } catch (NoSuchFieldException ignored) { 62 } catch (IllegalAccessException ignored) { 63 ignored.printStackTrace(); 64 } 65 } 66 67 SparseArray<String> mLocalTimeCache = new SparseArray<String>(); 68 long mLocalTimeCacheReferenceTime = 0; 69 static private long mGmtDisplayNameUpdateTime; 70 static private SparseArray<String> mGmtDisplayNameCache = new SparseArray<String>(); 71 72 public String getLocalTime(long referenceTime) { 73 recycledTime.timezone = TimeZone.getDefault().getID(); 74 recycledTime.set(referenceTime); 75 76 int currYearDay = recycledTime.year * 366 + recycledTime.yearDay; 77 78 recycledTime.timezone = mTzId; 79 recycledTime.set(referenceTime); 80 81 String localTimeStr = null; 82 83 int hourMinute = recycledTime.hour * 60 + 84 recycledTime.minute; 85 86 if (mLocalTimeCacheReferenceTime != referenceTime) { 87 mLocalTimeCacheReferenceTime = referenceTime; 88 mLocalTimeCache.clear(); 89 } else { 90 localTimeStr = mLocalTimeCache.get(hourMinute); 91 } 92 93 if (localTimeStr == null) { 94 String format = "%I:%M %p"; 95 if (currYearDay != (recycledTime.year * 366 + recycledTime.yearDay)) { 96 if (is24HourFormat) { 97 format = "%b %d %H:%M"; 98 } else { 99 format = "%b %d %I:%M %p"; 100 } 101 } else if (is24HourFormat) { 102 format = "%H:%M"; 103 } 104 105 // format = "%Y-%m-%d %H:%M"; 106 localTimeStr = recycledTime.format(format); 107 mLocalTimeCache.put(hourMinute, localTimeStr); 108 } 109 110 return localTimeStr; 111 } 112 113 public int getLocalHr(long referenceTime) { 114 recycledTime.timezone = mTzId; 115 recycledTime.set(referenceTime); 116 return recycledTime.hour; 117 } 118 119 public int getNowOffsetMillis() { 120 return mTz.getOffset(System.currentTimeMillis()); 121 } 122 123 /* 124 * The method is synchronized because there's one mSB, which is used by 125 * mFormatter, per instance. If there are multiple callers for 126 * getGmtDisplayName, the output may be mangled. 127 */ 128 public synchronized String getGmtDisplayName(Context context) { 129 // TODO Note: The local time is shown in current time (current GMT 130 // offset) which may be different from the time specified by 131 // mTimeMillis 132 133 final long nowMinute = System.currentTimeMillis() / DateUtils.MINUTE_IN_MILLIS; 134 final long now = nowMinute * DateUtils.MINUTE_IN_MILLIS; 135 final int gmtOffset = mTz.getOffset(now); 136 int cacheKey; 137 138 boolean hasFutureDST = mTz.useDaylightTime(); 139 if (hasFutureDST) { 140 cacheKey = (int) (gmtOffset + 36 * DateUtils.HOUR_IN_MILLIS); 141 } else { 142 cacheKey = (int) (gmtOffset - 36 * DateUtils.HOUR_IN_MILLIS); 143 } 144 145 String displayName = null; 146 if (mGmtDisplayNameUpdateTime != nowMinute) { 147 mGmtDisplayNameUpdateTime = nowMinute; 148 mGmtDisplayNameCache.clear(); 149 } else { 150 displayName = mGmtDisplayNameCache.get(cacheKey); 151 } 152 153 if (displayName == null) { 154 mSB.setLength(0); 155 int flags = DateUtils.FORMAT_ABBREV_ALL; 156 flags |= DateUtils.FORMAT_SHOW_TIME; 157 if (TimeZoneInfo.is24HourFormat) { 158 flags |= DateUtils.FORMAT_24HOUR; 159 } 160 161 // mFormatter writes to mSB 162 DateUtils.formatDateRange(context, mFormatter, now, now, flags, mTzId); 163 mSB.append(" (GMT"); 164 165 if (gmtOffset < 0) { 166 mSB.append('-'); 167 } else { 168 mSB.append('+'); 169 } 170 171 final int p = Math.abs(gmtOffset); 172 mSB.append(p / DateUtils.HOUR_IN_MILLIS); // Hour 173 174 final int min = (p / 60000) % 60; 175 if (min != 0) { // Show minutes if non-zero 176 mSB.append(':'); 177 if (min < 10) { 178 mSB.append('0'); 179 } 180 mSB.append(min); 181 } 182 mSB.append(')'); 183 184 if (hasFutureDST) { 185 String dstSymbol = TimeZonePickerUtils.getDstSymbol(); 186 mSB.append(" "); 187 mSB.append(dstSymbol); // Sun symbol 188 } 189 190 displayName = mSB.toString(); 191 mGmtDisplayNameCache.put(cacheKey, displayName); 192 } 193 return displayName; 194 } 195 196 private static int[] getTransitions(TimeZone tz, long time) 197 throws IllegalAccessException, NoSuchFieldException { 198 Class<?> zoneInfoClass = tz.getClass(); 199 Field mTransitionsField = zoneInfoClass.getDeclaredField("mTransitions"); 200 mTransitionsField.setAccessible(true); 201 int[] objTransitions = (int[]) mTransitionsField.get(tz); 202 int[] transitions = null; 203 if (objTransitions.length != 0) { 204 transitions = new int[NUM_OF_TRANSITIONS]; 205 int numOfTransitions = 0; 206 for (int i = 0; i < objTransitions.length; ++i) { 207 if (objTransitions[i] < time) { 208 continue; 209 } 210 transitions[numOfTransitions++] = objTransitions[i]; 211 if (numOfTransitions == NUM_OF_TRANSITIONS) { 212 break; 213 } 214 } 215 } 216 return transitions; 217 } 218 219 public boolean hasSameRules(TimeZoneInfo tzi) { 220 // this.mTz.hasSameRules(tzi.mTz) 221 222 return this.mRawoffset == tzi.mRawoffset 223 && Arrays.equals(this.mTransitions, tzi.mTransitions); 224 } 225 226 @Override 227 public String toString() { 228 StringBuilder sb = new StringBuilder(); 229 230 final String country = this.mCountry; 231 final TimeZone tz = this.mTz; 232 233 sb.append(mTzId); 234 sb.append(SEPARATOR); 235 sb.append(tz.getDisplayName(false /* daylightTime */, TimeZone.LONG)); 236 sb.append(SEPARATOR); 237 sb.append(tz.getDisplayName(false /* daylightTime */, TimeZone.SHORT)); 238 sb.append(SEPARATOR); 239 if (tz.useDaylightTime()) { 240 sb.append(tz.getDisplayName(true, TimeZone.LONG)); 241 sb.append(SEPARATOR); 242 sb.append(tz.getDisplayName(true, TimeZone.SHORT)); 243 } else { 244 sb.append(SEPARATOR); 245 } 246 sb.append(SEPARATOR); 247 sb.append(tz.getRawOffset() / 3600000f); 248 sb.append(SEPARATOR); 249 sb.append(tz.getDSTSavings() / 3600000f); 250 sb.append(SEPARATOR); 251 sb.append(country); 252 sb.append(SEPARATOR); 253 254 // 1-1-2013 noon GMT 255 sb.append(getLocalTime(1357041600000L)); 256 sb.append(SEPARATOR); 257 258 // 3-15-2013 noon GMT 259 sb.append(getLocalTime(1363348800000L)); 260 sb.append(SEPARATOR); 261 262 // 7-1-2013 noon GMT 263 sb.append(getLocalTime(1372680000000L)); 264 sb.append(SEPARATOR); 265 266 // 11-01-2013 noon GMT 267 sb.append(getLocalTime(1383307200000L)); 268 sb.append(SEPARATOR); 269 270 // if (this.mTransitions != null && this.mTransitions.length != 0) { 271 // sb.append('"'); 272 // DateFormat df = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss Z", 273 // Locale.US); 274 // df.setTimeZone(tz); 275 // DateFormat weekdayFormat = new SimpleDateFormat("EEEE", Locale.US); 276 // weekdayFormat.setTimeZone(tz); 277 // Formatter f = new Formatter(sb); 278 // for (int i = 0; i < this.mTransitions.length; ++i) { 279 // if (this.mTransitions[i] < time) { 280 // continue; 281 // } 282 // 283 // String fromTime = formatTime(df, this.mTransitions[i] - 1); 284 // String toTime = formatTime(df, this.mTransitions[i]); 285 // f.format("%s -> %s (%d)", fromTime, toTime, this.mTransitions[i]); 286 // 287 // String weekday = weekdayFormat.format(new Date(1000L * 288 // this.mTransitions[i])); 289 // if (!weekday.equals("Sunday")) { 290 // f.format(" -- %s", weekday); 291 // } 292 // sb.append("##"); 293 // } 294 // sb.append('"'); 295 // } 296 // sb.append(SEPARATOR); 297 sb.append('\n'); 298 return sb.toString(); 299 } 300 301 private static String formatTime(DateFormat df, int s) { 302 long ms = s * 1000L; 303 return df.format(new Date(ms)); 304 } 305 306 /* 307 * Returns a negative integer if this instance is less than the other; a 308 * positive integer if this instance is greater than the other; 0 if this 309 * instance has the same order as the other. 310 */ 311 @Override 312 public int compareTo(TimeZoneInfo other) { 313 if (this.getNowOffsetMillis() != other.getNowOffsetMillis()) { 314 return other.getNowOffsetMillis() - this.getNowOffsetMillis(); 315 } 316 317 // By country 318 if (this.mCountry == null) { 319 if (other.mCountry != null) { 320 return 1; 321 } 322 } 323 324 if (other.mCountry == null) { 325 return -1; 326 } else { 327 int diff = this.mCountry.compareTo(other.mCountry); 328 329 if (diff != 0) { 330 return diff; 331 } 332 } 333 334 if (Arrays.equals(this.mTransitions, other.mTransitions)) { 335 Log.e(TAG, "Not expected to be comparing tz with the same country, same offset," + 336 " same dst, same transitions:\n" + this.toString() + "\n" + other.toString()); 337 } 338 339 // Finally diff by display name 340 return this.mTz.getDisplayName(Locale.getDefault()).compareTo( 341 other.mTz.getDisplayName(Locale.getDefault())); 342 } 343} 344