J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.util.calendar; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.ObjectInputStream; |
| 30 | import java.lang.ref.SoftReference; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Date; |
| 33 | import java.util.HashMap; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import java.util.SimpleTimeZone; |
| 37 | import java.util.TimeZone; |
| 38 | |
| 39 | /** |
| 40 | * <code>ZoneInfo</code> is an implementation subclass of {@link |
| 41 | * java.util.TimeZone TimeZone} that represents GMT offsets and |
| 42 | * daylight saving time transitions of a time zone. |
| 43 | * <p> |
| 44 | * The daylight saving time transitions are described in the {@link |
| 45 | * #transitions transitions} table consisting of a chronological |
| 46 | * sequence of transitions of GMT offset and/or daylight saving time |
| 47 | * changes. Since all transitions are represented in UTC, in theory, |
| 48 | * <code>ZoneInfo</code> can be used with any calendar systems except |
| 49 | * for the {@link #getOffset(int,int,int,int,int,int) getOffset} |
| 50 | * method that takes Gregorian calendar date fields. |
| 51 | * <p> |
| 52 | * This table covers transitions from 1900 until 2037 (as of version |
| 53 | * 1.4), Before 1900, it assumes that there was no daylight saving |
| 54 | * time and the <code>getOffset</code> methods always return the |
| 55 | * {@link #getRawOffset} value. No Local Mean Time is supported. If a |
| 56 | * specified date is beyond the transition table and this time zone is |
| 57 | * supposed to observe daylight saving time in 2037, it delegates |
| 58 | * operations to a {@link java.util.SimpleTimeZone SimpleTimeZone} |
| 59 | * object created using the daylight saving time schedule as of 2037. |
| 60 | * <p> |
| 61 | * The date items, transitions, GMT offset(s), etc. are read from a database |
| 62 | * file. See {@link ZoneInfoFile} for details. |
| 63 | * @see java.util.SimpleTimeZone |
| 64 | * @since 1.4 |
| 65 | */ |
| 66 | |
| 67 | public class ZoneInfo extends TimeZone { |
| 68 | |
| 69 | private static final int UTC_TIME = 0; |
| 70 | private static final int STANDARD_TIME = 1; |
| 71 | private static final int WALL_TIME = 2; |
| 72 | |
| 73 | private static final long OFFSET_MASK = 0x0fL; |
| 74 | private static final long DST_MASK = 0xf0L; |
| 75 | private static final int DST_NSHIFT = 4; |
| 76 | // this bit field is reserved for abbreviation support |
| 77 | private static final long ABBR_MASK = 0xf00L; |
| 78 | private static final int TRANSITION_NSHIFT = 12; |
| 79 | |
| 80 | private static final CalendarSystem gcal = CalendarSystem.getGregorianCalendar(); |
| 81 | |
| 82 | /** |
| 83 | * The raw GMT offset in milliseconds between this zone and GMT. |
| 84 | * Negative offsets are to the west of Greenwich. To obtain local |
| 85 | * <em>standard</em> time, add the offset to GMT time. |
| 86 | * @serial |
| 87 | */ |
| 88 | private int rawOffset; |
| 89 | |
| 90 | /** |
| 91 | * Difference in milliseconds from the original GMT offset in case |
| 92 | * the raw offset value has been modified by calling {@link |
| 93 | * #setRawOffset}. The initial value is 0. |
| 94 | * @serial |
| 95 | */ |
| 96 | private int rawOffsetDiff = 0; |
| 97 | |
| 98 | /** |
| 99 | * A CRC32 value of all pairs of transition time (in milliseconds |
| 100 | * in <code>long</code>) in local time and its GMT offset (in |
| 101 | * seconds in <code>int</code>) in the chronological order. Byte |
| 102 | * values of each <code>long</code> and <code>int</code> are taken |
| 103 | * in the big endian order (i.e., MSB to LSB). |
| 104 | * @serial |
| 105 | */ |
| 106 | private int checksum; |
| 107 | |
| 108 | /** |
| 109 | * The amount of time in milliseconds saved during daylight saving |
| 110 | * time. If <code>useDaylight</code> is false, this value is 0. |
| 111 | * @serial |
| 112 | */ |
| 113 | private int dstSavings; |
| 114 | |
| 115 | /** |
| 116 | * This array describes transitions of GMT offsets of this time |
| 117 | * zone, including both raw offset changes and daylight saving |
| 118 | * time changes. |
| 119 | * A long integer consists of four bit fields. |
| 120 | * <ul> |
| 121 | * <li>The most significant 52-bit field represents transition |
| 122 | * time in milliseconds from Gregorian January 1 1970, 00:00:00 |
| 123 | * GMT.</li> |
| 124 | * <li>The next 4-bit field is reserved and must be 0.</li> |
| 125 | * <li>The next 4-bit field is an index value to {@link #offsets |
| 126 | * offsets[]} for the amount of daylight saving at the |
| 127 | * transition. If this value is zero, it means that no daylight |
| 128 | * saving, not the index value zero.</li> |
| 129 | * <li>The least significant 4-bit field is an index value to |
| 130 | * {@link #offsets offsets[]} for <em>total</em> GMT offset at the |
| 131 | * transition.</li> |
| 132 | * </ul> |
| 133 | * If this time zone doesn't observe daylight saving time and has |
| 134 | * never changed any GMT offsets in the past, this value is null. |
| 135 | * @serial |
| 136 | */ |
| 137 | private long[] transitions; |
| 138 | |
| 139 | /** |
| 140 | * This array holds all unique offset values in |
| 141 | * milliseconds. Index values to this array are stored in the |
| 142 | * transitions array elements. |
| 143 | * @serial |
| 144 | */ |
| 145 | private int[] offsets; |
| 146 | |
| 147 | /** |
| 148 | * SimpleTimeZone parameter values. It has to have either 8 for |
| 149 | * {@link java.util.SimpleTimeZone#SimpleTimeZone(int, String, |
| 150 | * int, int , int , int , int , int , int , int , int) the |
| 151 | * 11-argument SimpleTimeZone constructor} or 10 for {@link |
| 152 | * java.util.SimpleTimeZone#SimpleTimeZone(int, String, int, int, |
| 153 | * int , int , int , int , int , int , int, int, int) the |
| 154 | * 13-argument SimpleTimeZone constructor} parameters. |
| 155 | * @serial |
| 156 | */ |
| 157 | private int[] simpleTimeZoneParams; |
| 158 | |
| 159 | /** |
| 160 | * True if the raw GMT offset value would change after the time |
| 161 | * zone data has been generated; false, otherwise. The default |
| 162 | * value is false. |
| 163 | * @serial |
| 164 | */ |
| 165 | private boolean willGMTOffsetChange = false; |
| 166 | |
| 167 | /** |
| 168 | * True if the object has been modified after its instantiation. |
| 169 | */ |
| 170 | transient private boolean dirty = false; |
| 171 | |
| 172 | private static final long serialVersionUID = 2653134537216586139L; |
| 173 | |
| 174 | /** |
| 175 | * A constructor. |
| 176 | */ |
| 177 | public ZoneInfo() { |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * A Constructor for CustomID. |
| 182 | */ |
| 183 | public ZoneInfo(String ID, int rawOffset) { |
| 184 | this(ID, rawOffset, 0, 0, null, null, null, false); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Constructs a ZoneInfo instance. |
| 189 | * |
| 190 | * @param ID time zone name |
| 191 | * @param rawOffset GMT offset in milliseconds |
| 192 | * @param dstSavings daylight saving value in milliseconds or 0 |
| 193 | * (zero) if this time zone doesn't observe Daylight Saving Time. |
| 194 | * @param checksum CRC32 value with all transitions table entry |
| 195 | * values |
| 196 | * @param transitions transition table |
| 197 | * @param offsets offset value table |
| 198 | * @param simpleTimeZoneParams parameter values for constructing |
| 199 | * SimpleTimeZone |
| 200 | * @param willGMTOffsetChange the value of willGMTOffsetChange |
| 201 | */ |
| 202 | ZoneInfo(String ID, |
| 203 | int rawOffset, |
| 204 | int dstSavings, |
| 205 | int checksum, |
| 206 | long[] transitions, |
| 207 | int[] offsets, |
| 208 | int[] simpleTimeZoneParams, |
| 209 | boolean willGMTOffsetChange) { |
| 210 | setID(ID); |
| 211 | this.rawOffset = rawOffset; |
| 212 | this.dstSavings = dstSavings; |
| 213 | this.checksum = checksum; |
| 214 | this.transitions = transitions; |
| 215 | this.offsets = offsets; |
| 216 | this.simpleTimeZoneParams = simpleTimeZoneParams; |
| 217 | this.willGMTOffsetChange = willGMTOffsetChange; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns the difference in milliseconds between local time and UTC |
| 222 | * of given time, taking into account both the raw offset and the |
| 223 | * effect of daylight savings. |
| 224 | * |
| 225 | * @param date the milliseconds in UTC |
| 226 | * @return the milliseconds to add to UTC to get local wall time |
| 227 | */ |
| 228 | public int getOffset(long date) { |
| 229 | return getOffsets(date, null, UTC_TIME); |
| 230 | } |
| 231 | |
| 232 | public int getOffsets(long utc, int[] offsets) { |
| 233 | return getOffsets(utc, offsets, UTC_TIME); |
| 234 | } |
| 235 | |
| 236 | public int getOffsetsByStandard(long standard, int[] offsets) { |
| 237 | return getOffsets(standard, offsets, STANDARD_TIME); |
| 238 | } |
| 239 | |
| 240 | public int getOffsetsByWall(long wall, int[] offsets) { |
| 241 | return getOffsets(wall, offsets, WALL_TIME); |
| 242 | } |
| 243 | |
| 244 | private int getOffsets(long date, int[] offsets, int type) { |
| 245 | // if dst is never observed, there is no transition. |
| 246 | if (transitions == null) { |
| 247 | int offset = getLastRawOffset(); |
| 248 | if (offsets != null) { |
| 249 | offsets[0] = offset; |
| 250 | offsets[1] = 0; |
| 251 | } |
| 252 | return offset; |
| 253 | } |
| 254 | |
| 255 | date -= rawOffsetDiff; |
| 256 | int index = getTransitionIndex(date, type); |
| 257 | |
| 258 | // prior to the transition table, returns the raw offset. |
| 259 | // should support LMT. |
| 260 | if (index < 0) { |
| 261 | int offset = getLastRawOffset(); |
| 262 | if (offsets != null) { |
| 263 | offsets[0] = offset; |
| 264 | offsets[1] = 0; |
| 265 | } |
| 266 | return offset; |
| 267 | } |
| 268 | |
| 269 | if (index < transitions.length) { |
| 270 | long val = transitions[index]; |
| 271 | int offset = this.offsets[(int)(val & OFFSET_MASK)] + rawOffsetDiff; |
| 272 | if (offsets != null) { |
| 273 | int dst = (int)((val >>> DST_NSHIFT) & 0xfL); |
| 274 | int save = (dst == 0) ? 0 : this.offsets[dst]; |
| 275 | offsets[0] = offset - save; |
| 276 | offsets[1] = save; |
| 277 | } |
| 278 | return offset; |
| 279 | } |
| 280 | |
| 281 | // beyond the transitions, delegate to SimpleTimeZone if there |
| 282 | // is a rule; otherwise, return rawOffset. |
| 283 | SimpleTimeZone tz = getLastRule(); |
| 284 | if (tz != null) { |
| 285 | int rawoffset = tz.getRawOffset(); |
| 286 | long msec = date; |
| 287 | if (type != UTC_TIME) { |
| 288 | msec -= rawOffset; |
| 289 | } |
| 290 | int dstoffset = tz.inDaylightTime(new Date(msec)) ? tz.getDSTSavings() : 0; |
| 291 | if (offsets != null) { |
| 292 | offsets[0] = rawoffset; |
| 293 | offsets[1] = dstoffset; |
| 294 | } |
| 295 | return rawoffset + dstoffset; |
| 296 | } |
| 297 | int offset = getLastRawOffset(); |
| 298 | if (offsets != null) { |
| 299 | offsets[0] = offset; |
| 300 | offsets[1] = 0; |
| 301 | } |
| 302 | return offset; |
| 303 | } |
| 304 | |
| 305 | private final int getTransitionIndex(long date, int type) { |
| 306 | int low = 0; |
| 307 | int high = transitions.length - 1; |
| 308 | |
| 309 | while (low <= high) { |
| 310 | int mid = (low + high) / 2; |
| 311 | long val = transitions[mid]; |
| 312 | long midVal = val >> TRANSITION_NSHIFT; // sign extended |
| 313 | if (type != UTC_TIME) { |
| 314 | midVal += offsets[(int)(val & OFFSET_MASK)]; // wall time |
| 315 | } |
| 316 | if (type == STANDARD_TIME) { |
| 317 | int dstIndex = (int)((val >>> DST_NSHIFT) & 0xfL); |
| 318 | if (dstIndex != 0) { |
| 319 | midVal -= offsets[dstIndex]; // make it standard time |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (midVal < date) { |
| 324 | low = mid + 1; |
| 325 | } else if (midVal > date) { |
| 326 | high = mid - 1; |
| 327 | } else { |
| 328 | return mid; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // if beyond the transitions, returns that index. |
| 333 | if (low >= transitions.length) { |
| 334 | return low; |
| 335 | } |
| 336 | return low - 1; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Returns the difference in milliseconds between local time and |
| 341 | * UTC, taking into account both the raw offset and the effect of |
| 342 | * daylight savings, for the specified date and time. This method |
| 343 | * assumes that the start and end month are distinct. This method |
| 344 | * assumes a Gregorian calendar for calculations. |
| 345 | * <p> |
| 346 | * <em>Note: In general, clients should use |
| 347 | * {@link Calendar#ZONE_OFFSET Calendar.get(ZONE_OFFSET)} + |
| 348 | * {@link Calendar#DST_OFFSET Calendar.get(DST_OFFSET)} |
| 349 | * instead of calling this method.</em> |
| 350 | * |
| 351 | * @param era The era of the given date. The value must be either |
| 352 | * GregorianCalendar.AD or GregorianCalendar.BC. |
| 353 | * @param year The year in the given date. |
| 354 | * @param month The month in the given date. Month is 0-based. e.g., |
| 355 | * 0 for January. |
| 356 | * @param day The day-in-month of the given date. |
| 357 | * @param dayOfWeek The day-of-week of the given date. |
| 358 | * @param millis The milliseconds in day in <em>standard</em> local time. |
| 359 | * @return The milliseconds to add to UTC to get local time. |
| 360 | */ |
| 361 | public int getOffset(int era, int year, int month, int day, |
| 362 | int dayOfWeek, int milliseconds) { |
| 363 | if (milliseconds < 0 || milliseconds >= AbstractCalendar.DAY_IN_MILLIS) { |
| 364 | throw new IllegalArgumentException(); |
| 365 | } |
| 366 | |
| 367 | if (era == java.util.GregorianCalendar.BC) { // BC |
| 368 | year = 1 - year; |
| 369 | } else if (era != java.util.GregorianCalendar.AD) { |
| 370 | throw new IllegalArgumentException(); |
| 371 | } |
| 372 | |
| 373 | CalendarDate date = gcal.newCalendarDate(null); |
| 374 | date.setDate(year, month + 1, day); |
| 375 | if (gcal.validate(date) == false) { |
| 376 | throw new IllegalArgumentException(); |
| 377 | } |
| 378 | |
| 379 | // bug-for-bug compatible argument checking |
| 380 | if (dayOfWeek < java.util.GregorianCalendar.SUNDAY |
| 381 | || dayOfWeek > java.util.GregorianCalendar.SATURDAY) { |
| 382 | throw new IllegalArgumentException(); |
| 383 | } |
| 384 | |
| 385 | if (transitions == null) { |
| 386 | return getLastRawOffset(); |
| 387 | } |
| 388 | |
| 389 | long dateInMillis = gcal.getTime(date) + milliseconds; |
| 390 | dateInMillis -= (long) rawOffset; // make it UTC |
| 391 | return getOffsets(dateInMillis, null, UTC_TIME); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Sets the base time zone offset from GMT. This operation |
| 396 | * modifies all the transitions of this ZoneInfo object, including |
| 397 | * historical ones, if applicable. |
| 398 | * |
| 399 | * @param offsetMillis the base time zone offset to GMT. |
| 400 | * @see getRawOffset |
| 401 | */ |
| 402 | public synchronized void setRawOffset(int offsetMillis) { |
| 403 | if (offsetMillis == rawOffset + rawOffsetDiff) { |
| 404 | return; |
| 405 | } |
| 406 | rawOffsetDiff = offsetMillis - rawOffset; |
| 407 | if (lastRule != null) { |
| 408 | lastRule.setRawOffset(offsetMillis); |
| 409 | } |
| 410 | dirty = true; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Returns the GMT offset of the current date. This GMT offset |
| 415 | * value is not modified during Daylight Saving Time. |
| 416 | * |
| 417 | * @return the GMT offset value in milliseconds to add to UTC time |
| 418 | * to get local standard time |
| 419 | */ |
| 420 | public int getRawOffset() { |
| 421 | if (!willGMTOffsetChange) { |
| 422 | return rawOffset + rawOffsetDiff; |
| 423 | } |
| 424 | |
| 425 | int[] offsets = new int[2]; |
| 426 | getOffsets(System.currentTimeMillis(), offsets, UTC_TIME); |
| 427 | return offsets[0]; |
| 428 | } |
| 429 | |
| 430 | public boolean isDirty() { |
| 431 | return dirty; |
| 432 | } |
| 433 | |
| 434 | private int getLastRawOffset() { |
| 435 | return rawOffset + rawOffsetDiff; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Queries if this time zone uses Daylight Saving Time in the last known rule. |
| 440 | */ |
| 441 | public boolean useDaylightTime() { |
| 442 | return (simpleTimeZoneParams != null); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Queries if the specified date is in Daylight Saving Time. |
| 447 | */ |
| 448 | public boolean inDaylightTime(Date date) { |
| 449 | if (date == null) { |
| 450 | throw new NullPointerException(); |
| 451 | } |
| 452 | |
| 453 | if (transitions == null) { |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | long utc = date.getTime() - rawOffsetDiff; |
| 458 | int index = getTransitionIndex(utc, UTC_TIME); |
| 459 | |
| 460 | // before transitions in the transition table |
| 461 | if (index < 0) { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | // the time is in the table range. |
| 466 | if (index < transitions.length) { |
| 467 | return (transitions[index] & DST_MASK) != 0; |
| 468 | } |
| 469 | |
| 470 | // beyond the transition table |
| 471 | SimpleTimeZone tz = getLastRule(); |
| 472 | if (tz != null) { |
| 473 | return tz.inDaylightTime(date); |
| 474 | } |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Returns the amount of time in milliseconds that the clock is advanced |
| 480 | * during daylight saving time is in effect in its last daylight saving time rule. |
| 481 | * |
| 482 | * @return the number of milliseconds the time is advanced with respect to |
| 483 | * standard time when daylight saving time is in effect. |
| 484 | */ |
| 485 | public int getDSTSavings() { |
| 486 | return dstSavings; |
| 487 | } |
| 488 | |
| 489 | // /** |
| 490 | // * @return the last year in the transition table or -1 if this |
| 491 | // * time zone doesn't observe any daylight saving time. |
| 492 | // */ |
| 493 | // public int getMaxTransitionYear() { |
| 494 | // if (transitions == null) { |
| 495 | // return -1; |
| 496 | // } |
| 497 | // long val = transitions[transitions.length - 1]; |
| 498 | // int offset = this.offsets[(int)(val & OFFSET_MASK)] + rawOffsetDiff; |
| 499 | // val = (val >> TRANSITION_NSHIFT) + offset; |
| 500 | // CalendarDate lastDate = Gregorian.getCalendarDate(val); |
| 501 | // return lastDate.getYear(); |
| 502 | // } |
| 503 | |
| 504 | /** |
| 505 | * Returns a string representation of this time zone. |
| 506 | * @return the string |
| 507 | */ |
| 508 | public String toString() { |
| 509 | return getClass().getName() + |
| 510 | "[id=\"" + getID() + "\"" + |
| 511 | ",offset=" + getLastRawOffset() + |
| 512 | ",dstSavings=" + dstSavings + |
| 513 | ",useDaylight=" + useDaylightTime() + |
| 514 | ",transitions=" + ((transitions != null) ? transitions.length : 0) + |
| 515 | ",lastRule=" + (lastRule == null ? getLastRuleInstance() : lastRule) + |
| 516 | "]"; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Gets all available IDs supported in the Java run-time. |
| 521 | * |
| 522 | * @return an array of time zone IDs. |
| 523 | */ |
| 524 | public static String[] getAvailableIDs() { |
| 525 | List<String> idList = ZoneInfoFile.getZoneIDs(); |
| 526 | List<String> excluded = ZoneInfoFile.getExcludedZones(); |
| 527 | if (excluded != null) { |
| 528 | // List all zones from the idList and excluded lists |
| 529 | List<String> list = new ArrayList<String>(idList.size() + excluded.size()); |
| 530 | list.addAll(idList); |
| 531 | list.addAll(excluded); |
| 532 | idList = list; |
| 533 | } |
| 534 | String[] ids = new String[idList.size()]; |
| 535 | return idList.toArray(ids); |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Gets all available IDs that have the same value as the |
| 540 | * specified raw GMT offset. |
| 541 | * |
| 542 | * @param rawOffset the GMT offset in milliseconds. This |
| 543 | * value should not include any daylight saving time. |
| 544 | * |
| 545 | * @return an array of time zone IDs. |
| 546 | */ |
| 547 | public static String[] getAvailableIDs(int rawOffset) { |
| 548 | String[] result; |
| 549 | List<String> matched = new ArrayList<String>(); |
| 550 | List<String> IDs = ZoneInfoFile.getZoneIDs(); |
| 551 | int[] rawOffsets = ZoneInfoFile.getRawOffsets(); |
| 552 | |
| 553 | loop: |
| 554 | for (int index = 0; index < rawOffsets.length; index++) { |
| 555 | if (rawOffsets[index] == rawOffset) { |
| 556 | byte[] indices = ZoneInfoFile.getRawOffsetIndices(); |
| 557 | for (int i = 0; i < indices.length; i++) { |
| 558 | if (indices[i] == index) { |
| 559 | matched.add(IDs.get(i++)); |
| 560 | while (i < indices.length && indices[i] == index) { |
| 561 | matched.add(IDs.get(i++)); |
| 562 | } |
| 563 | break loop; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | // We need to add any zones from the excluded zone list that |
| 570 | // currently have the same GMT offset as the specified |
| 571 | // rawOffset. The zones returned by this method may not be |
| 572 | // correct as of return to the caller if any GMT offset |
| 573 | // transition is happening during this GMT offset checking... |
| 574 | List<String> excluded = ZoneInfoFile.getExcludedZones(); |
| 575 | if (excluded != null) { |
| 576 | for (String id : excluded) { |
| 577 | TimeZone zi = getTimeZone(id); |
| 578 | if (zi != null && zi.getRawOffset() == rawOffset) { |
| 579 | matched.add(id); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | result = new String[matched.size()]; |
| 585 | matched.toArray(result); |
| 586 | return result; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Gets the ZoneInfo for the given ID. |
| 591 | * |
| 592 | * @param ID the ID for a ZoneInfo. See TimeZone for detail. |
| 593 | * |
| 594 | * @return the specified ZoneInfo object, or null if there is no |
| 595 | * time zone of the ID. |
| 596 | */ |
| 597 | public static TimeZone getTimeZone(String ID) { |
| 598 | ZoneInfo zi = null; |
| 599 | |
| 600 | zi = ZoneInfoFile.getZoneInfo(ID); |
| 601 | if (zi == null) { |
| 602 | // if we can't create an object for the ID, try aliases. |
| 603 | try { |
| 604 | Map<String, String> map = getAliasTable(); |
| 605 | String alias = ID; |
| 606 | while ((alias = map.get(alias)) != null) { |
| 607 | zi = ZoneInfoFile.getZoneInfo(alias); |
| 608 | if (zi != null) { |
| 609 | zi.setID(ID); |
| 610 | zi = ZoneInfoFile.addToCache(ID, zi); |
| 611 | zi = (ZoneInfo) zi.clone(); |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | } catch (Exception e) { |
| 616 | // ignore exceptions |
| 617 | } |
| 618 | } |
| 619 | return zi; |
| 620 | } |
| 621 | |
| 622 | private transient SimpleTimeZone lastRule; |
| 623 | |
| 624 | /** |
| 625 | * Returns a SimpleTimeZone object representing the last GMT |
| 626 | * offset and DST schedule or null if this time zone doesn't |
| 627 | * observe DST. |
| 628 | */ |
| 629 | private synchronized SimpleTimeZone getLastRule() { |
| 630 | if (lastRule == null) { |
| 631 | lastRule = getLastRuleInstance(); |
| 632 | } |
| 633 | return lastRule; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Returns a SimpleTimeZone object that represents the last |
| 638 | * known daylight saving time rules. |
| 639 | * |
| 640 | * @return a SimpleTimeZone object or null if this time zone |
| 641 | * doesn't observe DST. |
| 642 | */ |
| 643 | public SimpleTimeZone getLastRuleInstance() { |
| 644 | if (simpleTimeZoneParams == null) { |
| 645 | return null; |
| 646 | } |
| 647 | if (simpleTimeZoneParams.length == 10) { |
| 648 | return new SimpleTimeZone(getLastRawOffset(), getID(), |
| 649 | simpleTimeZoneParams[0], |
| 650 | simpleTimeZoneParams[1], |
| 651 | simpleTimeZoneParams[2], |
| 652 | simpleTimeZoneParams[3], |
| 653 | simpleTimeZoneParams[4], |
| 654 | simpleTimeZoneParams[5], |
| 655 | simpleTimeZoneParams[6], |
| 656 | simpleTimeZoneParams[7], |
| 657 | simpleTimeZoneParams[8], |
| 658 | simpleTimeZoneParams[9], |
| 659 | dstSavings); |
| 660 | } |
| 661 | return new SimpleTimeZone(getLastRawOffset(), getID(), |
| 662 | simpleTimeZoneParams[0], |
| 663 | simpleTimeZoneParams[1], |
| 664 | simpleTimeZoneParams[2], |
| 665 | simpleTimeZoneParams[3], |
| 666 | simpleTimeZoneParams[4], |
| 667 | simpleTimeZoneParams[5], |
| 668 | simpleTimeZoneParams[6], |
| 669 | simpleTimeZoneParams[7], |
| 670 | dstSavings); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Returns a copy of this <code>ZoneInfo</code>. |
| 675 | */ |
| 676 | public Object clone() { |
| 677 | ZoneInfo zi = (ZoneInfo) super.clone(); |
| 678 | zi.lastRule = null; |
| 679 | return zi; |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Returns a hash code value calculated from the GMT offset and |
| 684 | * transitions. |
| 685 | * @return a hash code of this time zone |
| 686 | */ |
| 687 | public int hashCode() { |
| 688 | return getLastRawOffset() ^ checksum; |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Compares the equity of two ZoneInfo objects. |
| 693 | * |
| 694 | * @param obj the object to be compared with |
| 695 | * @return true if given object is same as this ZoneInfo object, |
| 696 | * false otherwise. |
| 697 | */ |
| 698 | public boolean equals(Object obj) { |
| 699 | if (this == obj) { |
| 700 | return true; |
| 701 | } |
| 702 | if (!(obj instanceof ZoneInfo)) { |
| 703 | return false; |
| 704 | } |
| 705 | ZoneInfo that = (ZoneInfo) obj; |
| 706 | return (getID().equals(that.getID()) |
| 707 | && (getLastRawOffset() == that.getLastRawOffset()) |
| 708 | && (checksum == that.checksum)); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Returns true if this zone has the same raw GMT offset value and |
| 713 | * transition table as another zone info. If the specified |
| 714 | * TimeZone object is not a ZoneInfo instance, this method returns |
| 715 | * true if the specified TimeZone object has the same raw GMT |
| 716 | * offset value with no daylight saving time. |
| 717 | * |
| 718 | * @param other the ZoneInfo object to be compared with |
| 719 | * @return true if the given <code>TimeZone</code> has the same |
| 720 | * GMT offset and transition information; false, otherwise. |
| 721 | */ |
| 722 | public boolean hasSameRules(TimeZone other) { |
| 723 | if (this == other) { |
| 724 | return true; |
| 725 | } |
| 726 | if (other == null) { |
| 727 | return false; |
| 728 | } |
| 729 | if (!(other instanceof ZoneInfo)) { |
| 730 | if (getRawOffset() != other.getRawOffset()) { |
| 731 | return false; |
| 732 | } |
| 733 | // if both have the same raw offset and neither observes |
| 734 | // DST, they have the same rule. |
| 735 | if ((transitions == null) |
| 736 | && (useDaylightTime() == false) |
| 737 | && (other.useDaylightTime() == false)) { |
| 738 | return true; |
| 739 | } |
| 740 | return false; |
| 741 | } |
| 742 | if (getLastRawOffset() != ((ZoneInfo)other).getLastRawOffset()) { |
| 743 | return false; |
| 744 | } |
| 745 | return (checksum == ((ZoneInfo)other).checksum); |
| 746 | } |
| 747 | |
| 748 | private static SoftReference<Map> aliasTable; |
| 749 | |
| 750 | /** |
| 751 | * Returns a Map from alias time zone IDs to their standard |
| 752 | * time zone IDs. |
| 753 | * |
| 754 | * @return the Map that holds the mappings from alias time zone IDs |
| 755 | * to their standard time zone IDs, or null if |
| 756 | * <code>ZoneInfoMappings</code> file is not available. |
| 757 | */ |
| 758 | public synchronized static Map<String, String> getAliasTable() { |
| 759 | Map<String, String> aliases = null; |
| 760 | |
| 761 | SoftReference<Map> cache = aliasTable; |
| 762 | if (cache != null) { |
| 763 | aliases = cache.get(); |
| 764 | if (aliases != null) { |
| 765 | return aliases; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | aliases = ZoneInfoFile.getZoneAliases(); |
| 770 | if (aliases != null) { |
| 771 | aliasTable = new SoftReference<Map>(aliases); |
| 772 | } |
| 773 | return aliases; |
| 774 | } |
| 775 | |
| 776 | private void readObject(ObjectInputStream stream) |
| 777 | throws IOException, ClassNotFoundException { |
| 778 | stream.defaultReadObject(); |
| 779 | // We don't know how this object from 1.4.x or earlier has |
| 780 | // been mutated. So it should always be marked as `dirty'. |
| 781 | dirty = true; |
| 782 | } |
| 783 | } |