1/* GENERATED SOURCE. DO NOT MODIFY. */ 2// © 2016 and later: Unicode, Inc. and others. 3// License & terms of use: http://www.unicode.org/copyright.html#License 4/* 5 * @(#)TimeZone.java 1.51 00/01/19 6 * 7 * Copyright (C) 1996-2016, International Business Machines 8 * Corporation and others. All Rights Reserved. 9 */ 10 11package android.icu.util; 12 13import java.io.Serializable; 14import java.util.Date; 15import java.util.Locale; 16import java.util.MissingResourceException; 17import java.util.Set; 18import java.util.logging.Logger; 19 20import android.icu.impl.Grego; 21import android.icu.impl.ICUConfig; 22import android.icu.impl.ICUData; 23import android.icu.impl.ICUResourceBundle; 24import android.icu.impl.JavaTimeZone; 25import android.icu.impl.TimeZoneAdapter; 26import android.icu.impl.ZoneMeta; 27import android.icu.text.TimeZoneFormat; 28import android.icu.text.TimeZoneFormat.Style; 29import android.icu.text.TimeZoneFormat.TimeType; 30import android.icu.text.TimeZoneNames; 31import android.icu.text.TimeZoneNames.NameType; 32import android.icu.util.ULocale.Category; 33 34/** 35 * <strong>[icu enhancement]</strong> ICU's replacement for {@link java.util.TimeZone}. Methods, fields, and other functionality specific to ICU are labeled '<strong>[icu]</strong>'. 36 * 37 * <p><code>TimeZone</code> represents a time zone offset, and also computes daylight 38 * savings. 39 * 40 * <p>Typically, you get a <code>TimeZone</code> using {@link #getDefault()} 41 * which creates a <code>TimeZone</code> based on the time zone where the program 42 * is running. For example, for a program running in Japan, <code>getDefault</code> 43 * creates a <code>TimeZone</code> object based on Japanese Standard Time. 44 * 45 * <p>You can also get a <code>TimeZone</code> using {@link #getTimeZone(String)} 46 * along with a time zone ID. For instance, the time zone ID for the 47 * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a 48 * U.S. Pacific Time <code>TimeZone</code> object with: 49 * 50 * <blockquote> 51 * <pre> 52 * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); 53 * </pre> 54 * </blockquote> 55 * You can use the {@link #getAvailableIDs()} method to iterate through 56 * all the supported time zone IDs, or getCanonicalID method to check 57 * if a time zone ID is supported or not. You can then choose a 58 * supported ID to get a <code>TimeZone</code>. 59 * If the time zone you want is not represented by one of the 60 * supported IDs, then you can create a custom time zone ID with 61 * the following syntax: 62 * 63 * <blockquote> 64 * <pre> 65 * GMT[+|-]hh[[:]mm] 66 * </pre> 67 * </blockquote> 68 * 69 * For example, you might specify GMT+14:00 as a custom 70 * time zone ID. The <code>TimeZone</code> that is returned 71 * when you specify a custom time zone ID uses the specified 72 * offset from GMT(=UTC) and does not observe daylight saving 73 * time. For example, you might specify GMT+14:00 as a custom 74 * time zone ID to create a TimeZone representing 14 hours ahead 75 * of GMT (with no daylight saving time). In addition, 76 * <code>getCanonicalID</code> can also be used to 77 * normalize a custom time zone ID. 78 * 79 * <p>For compatibility with JDK 1.1.x, some other three-letter time zone IDs 80 * (such as "PST", "CTT", "AST") are also supported. However, <strong>their 81 * use is deprecated</strong> because the same abbreviation is often used 82 * for multiple time zones (for example, "CST" could be U.S. "Central Standard 83 * Time" and "China Standard Time"), and the Java platform can then only 84 * recognize one of them. 85 * 86 * @see Calendar 87 * @see GregorianCalendar 88 * @see SimpleTimeZone 89 * @author Mark Davis, Deborah Goldsmith, Chen-Lieh Huang, Alan Liu 90 */ 91abstract public class TimeZone implements Serializable, Cloneable, Freezable<TimeZone> { 92 /** 93 * Logger instance for this class 94 */ 95 private static final Logger LOGGER = Logger.getLogger("android.icu.util.TimeZone"); 96 97 // using serialver from jdk1.4.2_05 98 private static final long serialVersionUID = -744942128318337471L; 99 100 /** 101 * Default constructor. (For invocation by subclass constructors, 102 * typically implicit.) 103 */ 104 public TimeZone() { 105 } 106 107 /** 108 * Constructing a TimeZone with the given time zone ID. 109 * @param ID the time zone ID. 110 * @deprecated This API is ICU internal only. 111 * @hide original deprecated declaration 112 * @hide draft / provisional / internal are hidden on Android 113 */ 114 @Deprecated 115 protected TimeZone(String ID) { 116 if (ID == null) { 117 throw new NullPointerException(); 118 } 119 this.ID = ID; 120 } 121 122 /** 123 * <strong>[icu]</strong> A time zone implementation type indicating ICU's own TimeZone used by 124 * <code>getTimeZone</code>. 125 */ 126 public static final int TIMEZONE_ICU = 0; 127 /** 128 * <strong>[icu]</strong> A time zone implementation type indicating the {@link java.util.TimeZone} 129 * used by <code>getTimeZone</code>. 130 */ 131 public static final int TIMEZONE_JDK = 1; 132 133 /** 134 * A style specifier for <code>getDisplayName()</code> indicating 135 * a short name, such as "PST." 136 * @see #LONG 137 */ 138 public static final int SHORT = 0; 139 140 /** 141 * A style specifier for <code>getDisplayName()</code> indicating 142 * a long name, such as "Pacific Standard Time." 143 * @see #SHORT 144 */ 145 public static final int LONG = 1; 146 147 /** 148 * <strong>[icu]</strong> A style specifier for <code>getDisplayName()</code> indicating 149 * a short generic name, such as "PT." 150 * @see #LONG_GENERIC 151 */ 152 public static final int SHORT_GENERIC = 2; 153 154 /** 155 * <strong>[icu]</strong> A style specifier for <code>getDisplayName()</code> indicating 156 * a long generic name, such as "Pacific Time." 157 * @see #SHORT_GENERIC 158 */ 159 public static final int LONG_GENERIC = 3; 160 161 /** 162 * <strong>[icu]</strong> A style specifier for <code>getDisplayName()</code> indicating 163 * a short name derived from the timezone's offset, such as "-0800." 164 * @see #LONG_GMT 165 */ 166 public static final int SHORT_GMT = 4; 167 168 /** 169 * <strong>[icu]</strong> A style specifier for <code>getDisplayName()</code> indicating 170 * a long name derived from the timezone's offset, such as "GMT-08:00." 171 * @see #SHORT_GMT 172 */ 173 public static final int LONG_GMT = 5; 174 175 /** 176 * <strong>[icu]</strong> A style specifier for <code>getDisplayName()</code> indicating 177 * a short name derived from the timezone's short standard or daylight 178 * timezone name ignoring commonlyUsed, such as "PDT." 179 */ 180 181 public static final int SHORT_COMMONLY_USED = 6; 182 183 /** 184 * <strong>[icu]</strong> A style specifier for <code>getDisplayName()</code> indicating 185 * a long name derived from the timezone's fallback name, such as 186 * "United States (Los Angeles)." 187 */ 188 public static final int GENERIC_LOCATION = 7; 189 190 /** 191 * <strong>[icu]</strong> The time zone ID reserved for unknown time zone. 192 * @see #getTimeZone(String) 193 */ 194 public static final String UNKNOWN_ZONE_ID = "Etc/Unknown"; 195 196 /** 197 * The canonical ID for GMT(UTC) time zone. 198 */ 199 static final String GMT_ZONE_ID = "Etc/GMT"; 200 201 /** 202 * <strong>[icu]</strong> The immutable (frozen) "unknown" time zone. 203 * It behaves like the GMT/UTC time zone but has the UNKNOWN_ZONE_ID = "Etc/Unknown". 204 * {@link TimeZone#getTimeZone(String)} returns a mutable clone of this 205 * time zone if the input ID is not recognized. 206 * 207 * @see #UNKNOWN_ZONE_ID 208 * @see #getTimeZone(String) 209 */ 210 public static final TimeZone UNKNOWN_ZONE = new ConstantZone(0, UNKNOWN_ZONE_ID).freeze(); 211 212 /** 213 * <strong>[icu]</strong> The immutable GMT (=UTC) time zone. Its ID is "Etc/GMT". 214 */ 215 public static final TimeZone GMT_ZONE = new ConstantZone(0, GMT_ZONE_ID).freeze(); 216 217 /** 218 * <strong>[icu]</strong> System time zone type constants used by filtering zones in 219 * {@link TimeZone#getAvailableIDs(SystemTimeZoneType, String, Integer)} 220 */ 221 public enum SystemTimeZoneType { 222 /** 223 * Any system zones. 224 * @hide draft / provisional / internal are hidden on Android 225 */ 226 ANY, 227 228 /** 229 * Canonical system zones. 230 * @hide draft / provisional / internal are hidden on Android 231 */ 232 CANONICAL, 233 234 /** 235 * Canonical system zones associated with actual locations. 236 * @hide draft / provisional / internal are hidden on Android 237 */ 238 CANONICAL_LOCATION, 239 } 240 241 /** 242 * Gets the time zone offset, for current date, modified in case of 243 * daylight savings. This is the offset to add *to* UTC to get local time. 244 * @param era the era of the given date. 245 * @param year the year in the given date. 246 * @param month the month in the given date. 247 * Month is 0-based. e.g., 0 for January. 248 * @param day the day-in-month of the given date. 249 * @param dayOfWeek the day-of-week of the given date. 250 * @param milliseconds the millis in day in <em>standard</em> local time. 251 * @return the offset to add *to* GMT to get local time. 252 */ 253 abstract public int getOffset(int era, int year, int month, int day, 254 int dayOfWeek, int milliseconds); 255 256 257 /** 258 * Returns the offset of this time zone from UTC at the specified 259 * date. If Daylight Saving Time is in effect at the specified 260 * date, the offset value is adjusted with the amount of daylight 261 * saving. 262 * 263 * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT 264 * @return the amount of time in milliseconds to add to UTC to get local time. 265 * 266 * @see Calendar#ZONE_OFFSET 267 * @see Calendar#DST_OFFSET 268 * @see #getOffset(long, boolean, int[]) 269 */ 270 public int getOffset(long date) { 271 int[] result = new int[2]; 272 getOffset(date, false, result); 273 return result[0]+result[1]; 274 } 275 276 /** 277 * Returns the time zone raw and GMT offset for the given moment 278 * in time. Upon return, local-millis = GMT-millis + rawOffset + 279 * dstOffset. All computations are performed in the proleptic 280 * Gregorian calendar. The default implementation in the TimeZone 281 * class delegates to the 8-argument getOffset(). 282 * 283 * @param date moment in time for which to return offsets, in 284 * units of milliseconds from January 1, 1970 0:00 GMT, either GMT 285 * time or local wall time, depending on `local'. 286 * @param local if true, `date' is local wall time; otherwise it 287 * is in GMT time. 288 * @param offsets output parameter to receive the raw offset, that 289 * is, the offset not including DST adjustments, in offsets[0], 290 * and the DST offset, that is, the offset to be added to 291 * `rawOffset' to obtain the total offset between local and GMT 292 * time, in offsets[1]. If DST is not in effect, the DST offset is 293 * zero; otherwise it is a positive value, typically one hour. 294 */ 295 public void getOffset(long date, boolean local, int[] offsets) { 296 offsets[0] = getRawOffset(); 297 if (!local) { 298 date += offsets[0]; // now in local standard millis 299 } 300 301 // When local == true, date might not be in local standard 302 // millis. getOffset taking 6 parameters used here assume 303 // the given time in day is local standard time. 304 // At STD->DST transition, there is a range of time which 305 // does not exist. When 'date' is in this time range 306 // (and local == true), this method interprets the specified 307 // local time as DST. At DST->STD transition, there is a 308 // range of time which occurs twice. In this case, this 309 // method interprets the specified local time as STD. 310 // To support the behavior above, we need to call getOffset 311 // (with 6 args) twice when local == true and DST is 312 // detected in the initial call. 313 int fields[] = new int[6]; 314 for (int pass = 0; ; pass++) { 315 Grego.timeToFields(date, fields); 316 offsets[1] = getOffset(GregorianCalendar.AD, 317 fields[0], fields[1], fields[2], 318 fields[3], fields[5]) - offsets[0]; 319 320 if (pass != 0 || !local || offsets[1] == 0) { 321 break; 322 } 323 // adjust to local standard millis 324 date -= offsets[1]; 325 } 326 } 327 328 /** 329 * Sets the base time zone offset to GMT. 330 * This is the offset to add *to* UTC to get local time. 331 * @param offsetMillis the given base time zone offset to GMT. 332 */ 333 abstract public void setRawOffset(int offsetMillis); 334 335 /** 336 * Gets unmodified offset, NOT modified in case of daylight savings. 337 * This is the offset to add *to* UTC to get local time. 338 * @return the unmodified offset to add *to* UTC to get local time. 339 */ 340 abstract public int getRawOffset(); 341 342 /** 343 * Gets the ID of this time zone. 344 * @return the ID of this time zone. 345 */ 346 public String getID() { 347 return ID; 348 } 349 350 /** 351 * Sets the time zone ID. This does not change any other data in 352 * the time zone object. 353 * @param ID the new time zone ID. 354 */ 355 public void setID(String ID) { 356 if (ID == null) { 357 throw new NullPointerException(); 358 } 359 if (isFrozen()) { 360 throw new UnsupportedOperationException("Attempt to modify a frozen TimeZone instance."); 361 } 362 this.ID = ID; 363 } 364 365 /** 366 * Returns a name of this time zone suitable for presentation to the user 367 * in the default <code>DISPLAY</code> locale. 368 * This method returns the long generic name. 369 * If the display name is not available for the locale, 370 * a fallback based on the country, city, or time zone id will be used. 371 * @return the human-readable name of this time zone in the default locale. 372 * @see Category#DISPLAY 373 */ 374 public final String getDisplayName() { 375 return _getDisplayName(LONG_GENERIC, false, ULocale.getDefault(Category.DISPLAY)); 376 } 377 378 /** 379 * Returns a name of this time zone suitable for presentation to the user 380 * in the specified locale. 381 * This method returns the long generic name. 382 * If the display name is not available for the locale, 383 * a fallback based on the country, city, or time zone id will be used. 384 * @param locale the locale in which to supply the display name. 385 * @return the human-readable name of this time zone in the given locale 386 * or in the default locale if the given locale is not recognized. 387 */ 388 public final String getDisplayName(Locale locale) { 389 return _getDisplayName(LONG_GENERIC, false, ULocale.forLocale(locale)); 390 } 391 392 /** 393 * Returns a name of this time zone suitable for presentation to the user 394 * in the specified locale. 395 * This method returns the long name, not including daylight savings. 396 * If the display name is not available for the locale, 397 * a fallback based on the country, city, or time zone id will be used. 398 * @param locale the ulocale in which to supply the display name. 399 * @return the human-readable name of this time zone in the given locale 400 * or in the default ulocale if the given ulocale is not recognized. 401 */ 402 public final String getDisplayName(ULocale locale) { 403 return _getDisplayName(LONG_GENERIC, false, locale); 404 } 405 406 /** 407 * Returns a name of this time zone suitable for presentation to the user 408 * in the default <code>DISPLAY</code> locale. 409 * If the display name is not available for the locale, 410 * then this method returns a string in the localized GMT offset format 411 * such as <code>GMT[+-]HH:mm</code>. 412 * @param daylight if true, return the daylight savings name. 413 * @param style the output style of the display name. Valid styles are 414 * <code>SHORT</code>, <code>LONG</code>, <code>SHORT_GENERIC</code>, 415 * <code>LONG_GENERIC</code>, <code>SHORT_GMT</code>, <code>LONG_GMT</code>, 416 * <code>SHORT_COMMONLY_USED</code> or <code>GENERIC_LOCATION</code>. 417 * @return the human-readable name of this time zone in the default locale. 418 * @see Category#DISPLAY 419 */ 420 public final String getDisplayName(boolean daylight, int style) { 421 return getDisplayName(daylight, style, ULocale.getDefault(Category.DISPLAY)); 422 } 423 424 /** 425 * Returns a name of this time zone suitable for presentation to the user 426 * in the specified locale. 427 * If the display name is not available for the locale, 428 * then this method returns a string in the localized GMT offset format 429 * such as <code>GMT[+-]HH:mm</code>. 430 * @param daylight if true, return the daylight savings name. 431 * @param style the output style of the display name. Valid styles are 432 * <code>SHORT</code>, <code>LONG</code>, <code>SHORT_GENERIC</code>, 433 * <code>LONG_GENERIC</code>, <code>SHORT_GMT</code>, <code>LONG_GMT</code>, 434 * <code>SHORT_COMMONLY_USED</code> or <code>GENERIC_LOCATION</code>. 435 * @param locale the locale in which to supply the display name. 436 * @return the human-readable name of this time zone in the given locale 437 * or in the default locale if the given locale is not recognized. 438 * @exception IllegalArgumentException style is invalid. 439 */ 440 public String getDisplayName(boolean daylight, int style, Locale locale) { 441 return getDisplayName(daylight, style, ULocale.forLocale(locale)); 442 } 443 444 /** 445 * Returns a name of this time zone suitable for presentation to the user 446 * in the specified locale. 447 * If the display name is not available for the locale, 448 * then this method returns a string in the localized GMT offset format 449 * such as <code>GMT[+-]HH:mm</code>. 450 * @param daylight if true, return the daylight savings name. 451 * @param style the output style of the display name. Valid styles are 452 * <code>SHORT</code>, <code>LONG</code>, <code>SHORT_GENERIC</code>, 453 * <code>LONG_GENERIC</code>, <code>SHORT_GMT</code>, <code>LONG_GMT</code>, 454 * <code>SHORT_COMMONLY_USED</code> or <code>GENERIC_LOCATION</code>. 455 * @param locale the locale in which to supply the display name. 456 * @return the human-readable name of this time zone in the given locale 457 * or in the default locale if the given locale is not recognized. 458 * @exception IllegalArgumentException style is invalid. 459 */ 460 public String getDisplayName(boolean daylight, int style, ULocale locale) { 461 if (style < SHORT || style > GENERIC_LOCATION) { 462 throw new IllegalArgumentException("Illegal style: " + style); 463 } 464 465 return _getDisplayName(style, daylight, locale); 466 } 467 468 /** 469 * internal version (which is called by public APIs) accepts 470 * SHORT, LONG, SHORT_GENERIC, LONG_GENERIC, SHORT_GMT, LONG_GMT, 471 * SHORT_COMMONLY_USED and GENERIC_LOCATION. 472 */ 473 private String _getDisplayName(int style, boolean daylight, ULocale locale) { 474 if (locale == null) { 475 throw new NullPointerException("locale is null"); 476 } 477 478 String result = null; 479 480 if (style == GENERIC_LOCATION || style == LONG_GENERIC || style == SHORT_GENERIC) { 481 // Generic format 482 TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale); 483 long date = System.currentTimeMillis(); 484 Output<TimeType> timeType = new Output<TimeType>(TimeType.UNKNOWN); 485 486 switch (style) { 487 case GENERIC_LOCATION: 488 result = tzfmt.format(Style.GENERIC_LOCATION, this, date, timeType); 489 break; 490 case LONG_GENERIC: 491 result = tzfmt.format(Style.GENERIC_LONG, this, date, timeType); 492 break; 493 case SHORT_GENERIC: 494 result = tzfmt.format(Style.GENERIC_SHORT, this, date, timeType); 495 break; 496 } 497 498 // Generic format many use Localized GMT as the final fallback. 499 // When Localized GMT format is used, the result might not be 500 // appropriate for the requested daylight value. 501 if (daylight && timeType.value == TimeType.STANDARD || 502 !daylight && timeType.value == TimeType.DAYLIGHT) { 503 int offset = daylight ? getRawOffset() + getDSTSavings() : getRawOffset(); 504 result = (style == SHORT_GENERIC) ? 505 tzfmt.formatOffsetShortLocalizedGMT(offset) : tzfmt.formatOffsetLocalizedGMT(offset); 506 } 507 508 } else if (style == LONG_GMT || style == SHORT_GMT) { 509 // Offset format 510 TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale); 511 int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset(); 512 switch (style) { 513 case LONG_GMT: 514 result = tzfmt.formatOffsetLocalizedGMT(offset); 515 break; 516 case SHORT_GMT: 517 result = tzfmt.formatOffsetISO8601Basic(offset, false, false, false); 518 break; 519 } 520 } else { 521 // Specific format 522 assert(style == LONG || style == SHORT || style == SHORT_COMMONLY_USED); 523 524 // Gets the name directly from TimeZoneNames 525 long date = System.currentTimeMillis(); 526 TimeZoneNames tznames = TimeZoneNames.getInstance(locale); 527 NameType nameType = null; 528 switch (style) { 529 case LONG: 530 nameType = daylight ? NameType.LONG_DAYLIGHT : NameType.LONG_STANDARD; 531 break; 532 case SHORT: 533 case SHORT_COMMONLY_USED: 534 nameType = daylight ? NameType.SHORT_DAYLIGHT : NameType.SHORT_STANDARD; 535 break; 536 } 537 result = tznames.getDisplayName(ZoneMeta.getCanonicalCLDRID(this), nameType, date); 538 if (result == null) { 539 // Fallback to localized GMT 540 TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale); 541 int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset(); 542 result = (style == LONG) ? 543 tzfmt.formatOffsetLocalizedGMT(offset) : tzfmt.formatOffsetShortLocalizedGMT(offset); 544 } 545 } 546 assert(result != null); 547 548 return result; 549 } 550 551 /** 552 * Returns the amount of time to be added to local standard time 553 * to get local wall clock time. 554 * <p> 555 * The default implementation always returns 3600000 milliseconds 556 * (i.e., one hour) if this time zone observes Daylight Saving 557 * Time. Otherwise, 0 (zero) is returned. 558 * <p> 559 * If an underlying TimeZone implementation subclass supports 560 * historical Daylight Saving Time changes, this method returns 561 * the known latest daylight saving value. 562 * 563 * @return the amount of saving time in milliseconds 564 */ 565 public int getDSTSavings() { 566 if (useDaylightTime()) { 567 return 3600000; 568 } 569 return 0; 570 } 571 572 /** 573 * Queries if this time zone uses daylight savings time. 574 * @return true if this time zone uses daylight savings time, 575 * false, otherwise. 576 * <p><strong>Note:</strong>The default implementation of 577 * ICU TimeZone uses the tz database, which supports historic 578 * rule changes, for system time zones. With the implementation, 579 * there are time zones that used daylight savings time in the 580 * past, but no longer used currently. For example, Asia/Tokyo has 581 * never used daylight savings time since 1951. Most clients would 582 * expect that this method to return <code>false</code> for such case. 583 * The default implementation of this method returns <code>true</code> 584 * when the time zone uses daylight savings time in the current 585 * (Gregorian) calendar year. 586 */ 587 abstract public boolean useDaylightTime(); 588 589 /** 590 * Queries if this time zone is in daylight saving time or will observe 591 * daylight saving time at any future time. 592 * <p>The default implementation in this class returns <code>true</code> if {@link #useDaylightTime()} 593 * or {@link #inDaylightTime(Date) inDaylightTime(new Date())} returns <code>true</code>. 594 * <p> 595 * <strong>Note:</strong> This method was added for {@link java.util.TimeZone} compatibility 596 * support. The {@link java.util.TimeZone#useDaylightTime()} method only checks the last known 597 * rule(s), therefore it may return false even the zone observes daylight saving time currently. 598 * {@link java.util.TimeZone} added <code>observesDaylightTime()</code> to resolve the issue. 599 * In ICU, {@link #useDaylightTime()} works differently. The ICU implementation checks if the 600 * zone uses daylight saving time in the current calendar year. Therefore, it will never return 601 * <code>false</code> if daylight saving time is currently used. 602 * <p> 603 * ICU's TimeZone subclass implementations override this method to support the same behavior 604 * with {@link java.util.TimeZone#observesDaylightTime()}. Unlike {@link #useDaylightTime()}, 605 * the implementation does not take past daylight saving time into account, so 606 * that this method may return <code>false</code> even when {@link #useDaylightTime()} returns 607 * <code>true</code>. 608 * 609 * @return <code>true</code> if this time zone is in daylight saving time or will observe 610 * daylight saving time at any future time. 611 * @see #useDaylightTime 612 */ 613 @SuppressWarnings("javadoc") // java.util.TimeZone#observesDaylightTime() is introduced in Java 7 614 public boolean observesDaylightTime() { 615 return useDaylightTime() || inDaylightTime(new Date()); 616 } 617 618 /** 619 * Queries if the given date is in daylight savings time in 620 * this time zone. 621 * @param date the given Date. 622 * @return true if the given date is in daylight savings time, 623 * false, otherwise. 624 */ 625 abstract public boolean inDaylightTime(Date date); 626 627 /** 628 * Gets the <code>TimeZone</code> for the given ID. 629 * 630 * @param ID the ID for a <code>TimeZone</code>, such as "America/Los_Angeles", 631 * or a custom ID such as "GMT-8:00". Note that the support of abbreviations, 632 * such as "PST", is for JDK 1.1.x compatibility only and full names should be used. 633 * 634 * @return the specified <code>TimeZone</code>, or a mutable clone of the UNKNOWN_ZONE 635 * if the given ID cannot be understood or if the given ID is "Etc/Unknown". 636 * @see #UNKNOWN_ZONE 637 */ 638 public static TimeZone getTimeZone(String ID) { 639 return getTimeZone(ID, TZ_IMPL, false); 640 } 641 642 /** 643 * Gets the <code>TimeZone</code> for the given ID. The instance of <code>TimeZone</code> 644 * returned by this method is immutable. Any methods mutate the instance({@link #setID(String)}, 645 * {@link #setRawOffset(int)}) will throw <code>UnsupportedOperationException</code> upon its 646 * invocation. 647 * 648 * @param ID the ID for a <code>TimeZone</code>, such as "America/Los_Angeles", 649 * or a custom ID such as "GMT-8:00". Note that the support of abbreviations, 650 * such as "PST", is for JDK 1.1.x compatibility only and full names should be used. 651 * 652 * @return the specified <code>TimeZone</code>, or the UNKNOWN_ZONE 653 * if the given ID cannot be understood. 654 * @see #UNKNOWN_ZONE 655 */ 656 public static TimeZone getFrozenTimeZone(String ID) { 657 return getTimeZone(ID, TZ_IMPL, true); 658 } 659 660 /** 661 * Gets the <code>TimeZone</code> for the given ID and the timezone type. 662 * @param ID the ID for a <code>TimeZone</code>, such as "America/Los_Angeles", or a 663 * custom ID such as "GMT-8:00". Note that the support of abbreviations, such as 664 * "PST", is for JDK 1.1.x compatibility only and full names should be used. 665 * @param type Time zone type, either <code>TIMEZONE_ICU</code> or 666 * <code>TIMEZONE_JDK</code>. 667 * @return the specified <code>TimeZone</code>, or a mutable clone of the UNKNOWN_ZONE if the given ID 668 * cannot be understood or if the given ID is "Etc/Unknown". 669 * @see #UNKNOWN_ZONE 670 */ 671 public static TimeZone getTimeZone(String ID, int type) { 672 return getTimeZone(ID, type, false); 673 } 674 675 /** 676 * Gets the <code>TimeZone</code> for the given ID and the timezone type. 677 * @param id time zone ID 678 * @param type time zone implementation type, TIMEZONE_JDK or TIMEZONE_ICU 679 * @param frozen specify if the returned object can be frozen 680 * @return the specified <code>TimeZone</code> or UNKNOWN_ZONE if the given ID 681 * cannot be understood. 682 */ 683 private static TimeZone getTimeZone(String id, int type, boolean frozen) { 684 TimeZone result; 685 if (type == TIMEZONE_JDK) { 686 result = JavaTimeZone.createTimeZone(id); 687 if (result != null) { 688 return frozen ? result.freeze() : result; 689 } 690 result = getFrozenICUTimeZone(id, false); 691 } else { 692 result = getFrozenICUTimeZone(id, true); 693 } 694 if (result == null) { 695 LOGGER.fine("\"" +id + "\" is a bogus id so timezone is falling back to Etc/Unknown(GMT)."); 696 result = UNKNOWN_ZONE; 697 } 698 return frozen ? result : result.cloneAsThawed(); 699 } 700 701 /** 702 * Returns a frozen ICU type TimeZone object given a time zone ID. 703 * @param id the time zone ID 704 * @param trySystem if true tries the system time zones first otherwise skip to the 705 * custom time zones. 706 * @return the frozen ICU TimeZone or null if one could not be created. 707 */ 708 static BasicTimeZone getFrozenICUTimeZone(String id, boolean trySystem) { 709 BasicTimeZone result = null; 710 if (trySystem) { 711 result = ZoneMeta.getSystemTimeZone(id); 712 } 713 if (result == null) { 714 result = ZoneMeta.getCustomTimeZone(id); 715 } 716 return result; 717 } 718 719 /** 720 * Sets the default time zone type used by <code>getTimeZone</code>. 721 * @param type time zone type, either <code>TIMEZONE_ICU</code> or 722 * <code>TIMEZONE_JDK</code>. 723 * @hide unsupported on Android 724 */ 725 public static synchronized void setDefaultTimeZoneType(int type) { 726 if (type != TIMEZONE_ICU && type != TIMEZONE_JDK) { 727 throw new IllegalArgumentException("Invalid timezone type"); 728 } 729 TZ_IMPL = type; 730 } 731 732 /** 733 * <strong>[icu]</strong> Returns the default time zone type currently used. 734 * @return The default time zone type, either <code>TIMEZONE_ICU</code> or 735 * <code>TIMEZONE_JDK</code>. 736 * @hide unsupported on Android 737 */ 738 public static int getDefaultTimeZoneType() { 739 return TZ_IMPL; 740 } 741 742 /** 743 * <strong>[icu]</strong> Returns a set of time zone ID strings with the given filter conditions. 744 * <p><b>Note:</b>A <code>Set</code> returned by this method is 745 * immutable. 746 * @param zoneType The system time zone type. 747 * @param region The ISO 3166 two-letter country code or UN M.49 three-digit area code. 748 * When null, no filtering done by region. 749 * @param rawOffset An offset from GMT in milliseconds, ignoring the effect of daylight savings 750 * time, if any. When null, no filtering done by zone offset. 751 * @return an immutable set of system time zone IDs. 752 * @see SystemTimeZoneType 753 */ 754 public static Set<String> getAvailableIDs(SystemTimeZoneType zoneType, 755 String region, Integer rawOffset) { 756 return ZoneMeta.getAvailableIDs(zoneType, region, rawOffset); 757 } 758 759 /** 760 * Return a new String array containing all system TimeZone IDs 761 * with the given raw offset from GMT. These IDs may be passed to 762 * <code>get()</code> to construct the corresponding TimeZone 763 * object. 764 * @param rawOffset the offset in milliseconds from GMT 765 * @return an array of IDs for system TimeZones with the given 766 * raw offset. If there are none, return a zero-length array. 767 * @see #getAvailableIDs(SystemTimeZoneType, String, Integer) 768 */ 769 public static String[] getAvailableIDs(int rawOffset) { 770 Set<String> ids = getAvailableIDs(SystemTimeZoneType.ANY, null, Integer.valueOf(rawOffset)); 771 return ids.toArray(new String[0]); 772 } 773 774 /** 775 * Return a new String array containing all system TimeZone IDs 776 * associated with the given country. These IDs may be passed to 777 * <code>get()</code> to construct the corresponding TimeZone 778 * object. 779 * @param country a two-letter ISO 3166 country code, or <code>null</code> 780 * to return zones not associated with any country 781 * @return an array of IDs for system TimeZones in the given 782 * country. If there are none, return a zero-length array. 783 * @see #getAvailableIDs(SystemTimeZoneType, String, Integer) 784 */ 785 public static String[] getAvailableIDs(String country) { 786 Set<String> ids = getAvailableIDs(SystemTimeZoneType.ANY, country, null); 787 return ids.toArray(new String[0]); 788 } 789 790 /** 791 * Return a new String array containing all system TimeZone IDs. 792 * These IDs (and only these IDs) may be passed to 793 * <code>get()</code> to construct the corresponding TimeZone 794 * object. 795 * @return an array of all system TimeZone IDs 796 * @see #getAvailableIDs(SystemTimeZoneType, String, Integer) 797 */ 798 public static String[] getAvailableIDs() { 799 Set<String> ids = getAvailableIDs(SystemTimeZoneType.ANY, null, null); 800 return ids.toArray(new String[0]); 801 } 802 803 /** 804 * <strong>[icu]</strong> Returns the number of IDs in the equivalency group that 805 * includes the given ID. An equivalency group contains zones 806 * that have the same GMT offset and rules. 807 * 808 * <p>The returned count includes the given ID; it is always >= 1 809 * for valid IDs. The given ID must be a system time zone. If it 810 * is not, returns zero. 811 * @param id a system time zone ID 812 * @return the number of zones in the equivalency group containing 813 * 'id', or zero if 'id' is not a valid system ID 814 * @see #getEquivalentID 815 */ 816 public static int countEquivalentIDs(String id) { 817 return ZoneMeta.countEquivalentIDs(id); 818 } 819 820 /** 821 * Returns an ID in the equivalency group that 822 * includes the given ID. An equivalency group contains zones 823 * that have the same GMT offset and rules. 824 * 825 * <p>The given index must be in the range 0..n-1, where n is the 826 * value returned by <code>countEquivalentIDs(id)</code>. For 827 * some value of 'index', the returned value will be equal to the 828 * given id. If the given id is not a valid system time zone, or 829 * if 'index' is out of range, then returns an empty string. 830 * @param id a system time zone ID 831 * @param index a value from 0 to n-1, where n is the value 832 * returned by <code>countEquivalentIDs(id)</code> 833 * @return the ID of the index-th zone in the equivalency group 834 * containing 'id', or an empty string if 'id' is not a valid 835 * system ID or 'index' is out of range 836 * @see #countEquivalentIDs 837 */ 838 public static String getEquivalentID(String id, int index) { 839 return ZoneMeta.getEquivalentID(id, index); 840 } 841 842 /** 843 * Gets the default <code>TimeZone</code> for this host. 844 * The source of the default <code>TimeZone</code> 845 * may vary with implementation. 846 * @return a default <code>TimeZone</code>. 847 */ 848 public static TimeZone getDefault() { 849 // Android patch (http://b/30979219) start. 850 // Avoid race condition by copying defaultZone to a local variable. 851 TimeZone result = defaultZone; 852 if (result == null) { 853 // Android patch (http://b/30937209) start. 854 // Avoid a deadlock by always acquiring monitors in order (1) java.util.TimeZone.class 855 // then (2) icu.util.TimeZone.class and not (2) then (1). 856 // Without the synchronized here there is a possible deadlock between threads calling 857 // this method and other threads calling methods on java.util.TimeZone. e.g. 858 // java.util.TimeZone.setDefault() calls back into 859 // icu.util.TimeZone.clearCachedDefault() so always acquires them in order (1) then (2). 860 synchronized (java.util.TimeZone.class) { 861 synchronized (TimeZone.class) { 862 result = defaultZone; 863 if (result == null) { 864 if (TZ_IMPL == TIMEZONE_JDK) { 865 result = new JavaTimeZone(); 866 } else { 867 java.util.TimeZone temp = java.util.TimeZone.getDefault(); 868 result = getFrozenTimeZone(temp.getID()); 869 } 870 defaultZone = result; 871 } 872 } 873 } 874 // Android patch (http://b/30937209) end. 875 } 876 return result.cloneAsThawed(); 877 // Android patch (http://b/30979219) end. 878 } 879 880 // Android patch (http://b/28949992) start. 881 // ICU TimeZone.setDefault() not supported on Android. 882 /** 883 * Clears the cached default time zone. 884 * This causes {@link #getDefault()} to re-request the default time zone 885 * from {@link java.util.TimeZone}. 886 * @hide unsupported on Android 887 */ 888 public static synchronized void clearCachedDefault() { 889 defaultZone = null; 890 } 891 // Android patch (http://b/28949992) end. 892 893 /** 894 * Sets the <code>TimeZone</code> that is 895 * returned by the <code>getDefault</code> method. If <code>zone</code> 896 * is null, reset the default to the value it had originally when the 897 * VM first started. 898 * @param tz the new default time zone 899 * @hide unsupported on Android 900 */ 901 public static synchronized void setDefault(TimeZone tz) { 902 defaultZone = tz; 903 java.util.TimeZone jdkZone = null; 904 if (defaultZone instanceof JavaTimeZone) { 905 jdkZone = ((JavaTimeZone)defaultZone).unwrap(); 906 } else { 907 // Keep java.util.TimeZone default in sync so java.util.Date 908 // can interoperate with android.icu.util classes. 909 910 if (tz != null) { 911 if (tz instanceof android.icu.impl.OlsonTimeZone) { 912 // Because of the lack of APIs supporting historic 913 // zone offset/dst saving in JDK TimeZone, 914 // wrapping ICU TimeZone with JDK TimeZone will 915 // cause historic offset calculation in Calendar/Date. 916 // JDK calendar implementation calls getRawOffset() and 917 // getDSTSavings() when the instance of JDK TimeZone 918 // is not an instance of JDK internal TimeZone subclass 919 // (sun.util.calendar.ZoneInfo). Ticket#6459 920 String icuID = tz.getID(); 921 jdkZone = java.util.TimeZone.getTimeZone(icuID); 922 if (!icuID.equals(jdkZone.getID())) { 923 // If the ID was unknown, retry with the canonicalized 924 // ID instead. This will ensure that JDK 1.1.x 925 // compatibility IDs supported by ICU (but not 926 // necessarily supported by the platform) work. 927 // Ticket#11483 928 icuID = getCanonicalID(icuID); 929 jdkZone = java.util.TimeZone.getTimeZone(icuID); 930 if (!icuID.equals(jdkZone.getID())) { 931 // JDK does not know the ID.. 932 jdkZone = null; 933 } 934 } 935 } 936 if (jdkZone == null) { 937 jdkZone = TimeZoneAdapter.wrap(tz); 938 } 939 } 940 } 941 java.util.TimeZone.setDefault(jdkZone); 942 } 943 944 /** 945 * Returns true if this zone has the same rule and offset as another zone. 946 * That is, if this zone differs only in ID, if at all. Returns false 947 * if the other zone is null. 948 * @param other the <code>TimeZone</code> object to be compared with 949 * @return true if the other zone is not null and is the same as this one, 950 * with the possible exception of the ID 951 */ 952 public boolean hasSameRules(TimeZone other) { 953 return other != null && 954 getRawOffset() == other.getRawOffset() && 955 useDaylightTime() == other.useDaylightTime(); 956 } 957 958 /** 959 * Overrides clone. 960 */ 961 @Override 962 public Object clone() { 963 if (isFrozen()) { 964 return this; 965 } 966 return cloneAsThawed(); 967 } 968 969 /** 970 * Overrides equals. 971 */ 972 @Override 973 public boolean equals(Object obj){ 974 if (this == obj) return true; 975 if (obj == null || getClass() != obj.getClass()) return false; 976 return (ID.equals(((TimeZone)obj).ID)); 977 } 978 979 /** 980 * Overrides hashCode. 981 */ 982 @Override 983 public int hashCode(){ 984 return ID.hashCode(); 985 } 986 987 /** 988 * <strong>[icu]</strong> Returns the time zone data version currently used by ICU. 989 * 990 * @return the version string, such as "2007f" 991 * @throws MissingResourceException if ICU time zone resource bundle 992 * is missing or the version information is not available. 993 */ 994 public static String getTZDataVersion() { 995 // The implementation had been moved to VersionInfo. 996 return VersionInfo.getTZDataVersion(); 997 } 998 999 /** 1000 * <strong>[icu]</strong> Returns the canonical system time zone ID or the normalized 1001 * custom time zone ID for the given time zone ID. 1002 * @param id The input time zone ID to be canonicalized. 1003 * @return The canonical system time zone ID or the custom time zone ID 1004 * in normalized format for the given time zone ID. When the given time zone ID 1005 * is neither a known system time zone ID nor a valid custom time zone ID, 1006 * null is returned. 1007 */ 1008 public static String getCanonicalID(String id) { 1009 return getCanonicalID(id, null); 1010 } 1011 1012 /** 1013 * <strong>[icu]</strong> Returns the canonical system time zone ID or the normalized 1014 * custom time zone ID for the given time zone ID. 1015 * @param id The input time zone ID to be canonicalized. 1016 * @param isSystemID When non-null boolean array is specified and 1017 * the given ID is a known system time zone ID, true is set to <code>isSystemID[0]</code> 1018 * @return The canonical system time zone ID or the custom time zone ID 1019 * in normalized format for the given time zone ID. When the given time zone ID 1020 * is neither a known system time zone ID nor a valid custom time zone ID, 1021 * null is returned. 1022 */ 1023 public static String getCanonicalID(String id, boolean[] isSystemID) { 1024 String canonicalID = null; 1025 boolean systemTzid = false; 1026 if (id != null && id.length() != 0) { 1027 if (id.equals(TimeZone.UNKNOWN_ZONE_ID)) { 1028 // special case - Etc/Unknown is a canonical ID, but not system ID 1029 canonicalID = TimeZone.UNKNOWN_ZONE_ID; 1030 systemTzid = false; 1031 } else { 1032 canonicalID = ZoneMeta.getCanonicalCLDRID(id); 1033 if (canonicalID != null) { 1034 systemTzid = true; 1035 } else { 1036 canonicalID = ZoneMeta.getCustomID(id); 1037 } 1038 } 1039 } 1040 if (isSystemID != null) { 1041 isSystemID[0] = systemTzid; 1042 } 1043 return canonicalID; 1044 } 1045 1046 /** 1047 * <strong>[icu]</strong> Returns the region code associated with the given 1048 * system time zone ID. The region code is either ISO 3166 1049 * 2-letter country code or UN M.49 3-digit area code. 1050 * When the time zone is not associated with a specific location, 1051 * for example - "Etc/UTC", "EST5EDT", then this method returns 1052 * "001" (UN M.49 area code for World). 1053 * @param id the system time zone ID. 1054 * @return the region code associated with the given 1055 * system time zone ID. 1056 * @throws IllegalArgumentException if <code>id</code> is not a known system ID. 1057 * @see #getAvailableIDs(String) 1058 */ 1059 public static String getRegion(String id) { 1060 String region = null; 1061 // "Etc/Unknown" is not a system time zone ID, 1062 // but in the zone data. 1063 if (!id.equals(UNKNOWN_ZONE_ID)) { 1064 region = ZoneMeta.getRegion(id); 1065 } 1066 if (region == null) { 1067 // unknown id 1068 throw new IllegalArgumentException("Unknown system zone id: " + id); 1069 } 1070 return region; 1071 } 1072 1073 /** 1074 * <strong>[icu]</strong> Converts a system time zone ID to an equivalent Windows time zone ID. For example, 1075 * Windows time zone ID "Pacific Standard Time" is returned for input "America/Los_Angeles". 1076 * 1077 * <p>There are system time zones that cannot be mapped to Windows zones. When the input 1078 * system time zone ID is unknown or unmappable to a Windows time zone, then this 1079 * method returns <code>null</code>. 1080 * 1081 * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html"> 1082 * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes, 1083 * please read the ICU user guide section <a href="http://userguide.icu-project.org/datetime/timezone#TOC-Updating-the-Time-Zone-Data"> 1084 * Updating the Time Zone Data</a>. 1085 * 1086 * @param id A system time zone ID 1087 * @return A Windows time zone ID mapped from the input system time zone ID, 1088 * or <code>null</code> when the input ID is unknown or unmappable. 1089 * @see #getIDForWindowsID(String, String) 1090 */ 1091 public static String getWindowsID(String id) { 1092 // canonicalize the input ID 1093 boolean[] isSystemID = {false}; 1094 id = getCanonicalID(id, isSystemID); 1095 if (!isSystemID[0]) { 1096 // mapping data is only applicable to tz database IDs 1097 return null; 1098 } 1099 1100 UResourceBundle top = UResourceBundle.getBundleInstance( 1101 ICUData.ICU_BASE_NAME, "windowsZones", ICUResourceBundle.ICU_DATA_CLASS_LOADER); 1102 UResourceBundle mapTimezones = top.get("mapTimezones"); 1103 1104 UResourceBundleIterator resitr = mapTimezones.getIterator(); 1105 while (resitr.hasNext()) { 1106 UResourceBundle winzone = resitr.next(); 1107 if (winzone.getType() != UResourceBundle.TABLE) { 1108 continue; 1109 } 1110 UResourceBundleIterator rgitr = winzone.getIterator(); 1111 while (rgitr.hasNext()) { 1112 UResourceBundle regionalData = rgitr.next(); 1113 if (regionalData.getType() != UResourceBundle.STRING) { 1114 continue; 1115 } 1116 String[] tzids = regionalData.getString().split(" "); 1117 for (String tzid : tzids) { 1118 if (tzid.equals(id)) { 1119 return winzone.getKey(); 1120 } 1121 } 1122 } 1123 } 1124 1125 return null; 1126 } 1127 1128 /** 1129 * <strong>[icu]</strong> Converts a Windows time zone ID to an equivalent system time zone ID 1130 * for a region. For example, system time zone ID "America/Los_Angeles" is returned 1131 * for input Windows ID "Pacific Standard Time" and region "US" (or <code>null</code>), 1132 * "America/Vancouver" is returned for the same Windows ID "Pacific Standard Time" and 1133 * region "CA". 1134 * 1135 * <p>Not all Windows time zones can be mapped to system time zones. When the input 1136 * Windows time zone ID is unknown or unmappable to a system time zone, then this 1137 * method returns <code>null</code>. 1138 * 1139 * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html"> 1140 * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes, 1141 * please read the ICU user guide section <a href="http://userguide.icu-project.org/datetime/timezone#TOC-Updating-the-Time-Zone-Data"> 1142 * Updating the Time Zone Data</a>. 1143 * 1144 * @param winid A Windows time zone ID 1145 * @param region A region code, or <code>null</code> if no regional preference. 1146 * @return A system time zone ID mapped from the input Windows time zone ID, 1147 * or <code>null</code> when the input ID is unknown or unmappable. 1148 * @see #getWindowsID(String) 1149 */ 1150 public static String getIDForWindowsID(String winid, String region) { 1151 String id = null; 1152 1153 UResourceBundle top = UResourceBundle.getBundleInstance( 1154 ICUData.ICU_BASE_NAME, "windowsZones", ICUResourceBundle.ICU_DATA_CLASS_LOADER); 1155 UResourceBundle mapTimezones = top.get("mapTimezones"); 1156 1157 try { 1158 UResourceBundle zones = mapTimezones.get(winid); 1159 if (region != null) { 1160 try { 1161 id = zones.getString(region); 1162 if (id != null) { 1163 // first ID delimited by space is the default one 1164 int endIdx = id.indexOf(' '); 1165 if (endIdx > 0) { 1166 id = id.substring(0, endIdx); 1167 } 1168 } 1169 } catch (MissingResourceException e) { 1170 // no explicit region mapping found 1171 } 1172 } 1173 if (id == null) { 1174 id = zones.getString("001"); 1175 } 1176 } catch (MissingResourceException e) { 1177 // no mapping data found 1178 } 1179 1180 return id; 1181 } 1182 1183 // Freezable stuffs 1184 1185 /** 1186 * {@inheritDoc} 1187 */ 1188 @Override 1189 public boolean isFrozen() { 1190 return false; 1191 } 1192 1193 /** 1194 * {@inheritDoc} 1195 */ 1196 @Override 1197 public TimeZone freeze() { 1198 throw new UnsupportedOperationException("Needs to be implemented by the subclass."); 1199 } 1200 1201 /** 1202 * {@inheritDoc} 1203 */ 1204 @Override 1205 public TimeZone cloneAsThawed() { 1206 try { 1207 TimeZone other = (TimeZone) super.clone(); 1208 return other; 1209 } catch (CloneNotSupportedException e) { 1210 throw new ICUCloneNotSupportedException(e); 1211 } 1212 } 1213 1214 // =======================privates=============================== 1215 1216 /** 1217 * The string identifier of this <code>TimeZone</code>. This is a 1218 * programmatic identifier used internally to look up <code>TimeZone</code> 1219 * objects from the system table and also to map them to their localized 1220 * display names. <code>ID</code> values are unique in the system 1221 * table but may not be for dynamically created zones. 1222 * @serial 1223 */ 1224 private String ID; 1225 1226 /** 1227 * The default time zone, or null if not set. 1228 */ 1229 private static volatile TimeZone defaultZone = null; 1230 1231 /** 1232 * TimeZone implementation type 1233 */ 1234 private static int TZ_IMPL = TIMEZONE_ICU; 1235 1236 /** 1237 * TimeZone implementation type initialization 1238 */ 1239 private static final String TZIMPL_CONFIG_KEY = "android.icu.util.TimeZone.DefaultTimeZoneType"; 1240 private static final String TZIMPL_CONFIG_ICU = "ICU"; 1241 private static final String TZIMPL_CONFIG_JDK = "JDK"; 1242 1243 static { 1244 String type = ICUConfig.get(TZIMPL_CONFIG_KEY, TZIMPL_CONFIG_ICU); 1245 if (type.equalsIgnoreCase(TZIMPL_CONFIG_JDK)) { 1246 TZ_IMPL = TIMEZONE_JDK; 1247 } 1248 } 1249 1250 /* 1251 * ConstantZone is a private TimeZone subclass dedicated for the two TimeZone class 1252 * constants - TimeZone.GMT_ZONE and TimeZone.UNKNOWN_ZONE. Previously, these zones 1253 * are instances of SimpleTimeZone. However, when the SimpleTimeZone constructor and 1254 * TimeZone's static methods (such as TimeZone.getDefault()) are called from multiple 1255 * threads at the same time, it causes a deadlock by TimeZone's static initializer 1256 * and SimpleTimeZone's static initializer. To avoid this issue, these TimeZone 1257 * constants (GMT/UNKNOWN) must be implemented by a class not visible from users. 1258 * See ticket#11343. 1259 */ 1260 private static final class ConstantZone extends TimeZone { 1261 private static final long serialVersionUID = 1L; 1262 1263 private int rawOffset; 1264 1265 private ConstantZone(int rawOffset, String ID) { 1266 super(ID); 1267 this.rawOffset = rawOffset; 1268 } 1269 1270 @Override 1271 public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) { 1272 return rawOffset; 1273 } 1274 1275 @Override 1276 public void setRawOffset(int offsetMillis) { 1277 if (isFrozen()) { 1278 throw new UnsupportedOperationException("Attempt to modify a frozen TimeZone instance."); 1279 } 1280 rawOffset = offsetMillis; 1281 } 1282 1283 @Override 1284 public int getRawOffset() { 1285 return rawOffset; 1286 } 1287 1288 @Override 1289 public boolean useDaylightTime() { 1290 return false; 1291 } 1292 1293 @Override 1294 public boolean inDaylightTime(Date date) { 1295 return false; 1296 } 1297 1298 private volatile transient boolean isFrozen = false; 1299 1300 @Override 1301 public boolean isFrozen() { 1302 return isFrozen; 1303 } 1304 1305 @Override 1306 public TimeZone freeze() { 1307 isFrozen = true; 1308 return this; 1309 } 1310 1311 @Override 1312 public TimeZone cloneAsThawed() { 1313 ConstantZone tz = (ConstantZone)super.cloneAsThawed(); 1314 tz.isFrozen = false; 1315 return tz; 1316 } 1317 } 1318} 1319 1320//eof 1321