1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 /* 28 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved 29 * (C) Copyright IBM Corp. 1996 - All Rights Reserved 30 * 31 * The original version of this source code and documentation is copyrighted 32 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These 33 * materials are provided under terms of a License Agreement between Taligent 34 * and Sun. This technology is protected by multiple US and International 35 * patents. This notice and attribution to Taligent may not be removed. 36 * Taligent is a registered trademark of Taligent, Inc. 37 * 38 */ 39 40 package java.util; 41 42 import java.io.IOException; 43 import java.io.Serializable; 44 import java.util.regex.Matcher; 45 import java.util.regex.Pattern; 46 import java.lang.ref.SoftReference; 47 import java.security.AccessController; 48 import java.security.PrivilegedAction; 49 import java.util.concurrent.ConcurrentHashMap; 50 import libcore.icu.TimeZoneNames; 51 import libcore.io.IoUtils; 52 import libcore.util.ZoneInfoDB; 53 import sun.security.action.GetPropertyAction; 54 import org.apache.harmony.luni.internal.util.TimezoneGetter; 55 56 /** 57 * <code>TimeZone</code> represents a time zone offset, and also figures out daylight 58 * savings. 59 * 60 * <p> 61 * Typically, you get a <code>TimeZone</code> using <code>getDefault</code> 62 * which creates a <code>TimeZone</code> based on the time zone where the program 63 * is running. For example, for a program running in Japan, <code>getDefault</code> 64 * creates a <code>TimeZone</code> object based on Japanese Standard Time. 65 * 66 * <p> 67 * You can also get a <code>TimeZone</code> using <code>getTimeZone</code> 68 * along with a time zone ID. For instance, the time zone ID for the 69 * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a 70 * U.S. Pacific Time <code>TimeZone</code> object with: 71 * <blockquote><pre> 72 * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); 73 * </pre></blockquote> 74 * You can use the <code>getAvailableIDs</code> method to iterate through 75 * all the supported time zone IDs. You can then choose a 76 * supported ID to get a <code>TimeZone</code>. 77 * If the time zone you want is not represented by one of the 78 * supported IDs, then a custom time zone ID can be specified to 79 * produce a TimeZone. The syntax of a custom time zone ID is: 80 * 81 * <blockquote><pre> 82 * <a name="CustomID"><i>CustomID:</i></a> 83 * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> 84 * <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i> 85 * <code>GMT</code> <i>Sign</i> <i>Hours</i> 86 * <i>Sign:</i> one of 87 * <code>+ -</code> 88 * <i>Hours:</i> 89 * <i>Digit</i> 90 * <i>Digit</i> <i>Digit</i> 91 * <i>Minutes:</i> 92 * <i>Digit</i> <i>Digit</i> 93 * <i>Digit:</i> one of 94 * <code>0 1 2 3 4 5 6 7 8 9</code> 95 * </pre></blockquote> 96 * 97 * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be 98 * between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten 99 * hours and ten minutes ahead of GMT, respectively. 100 * <p> 101 * The format is locale independent and digits must be taken from the 102 * Basic Latin block of the Unicode standard. No daylight saving time 103 * transition schedule can be specified with a custom time zone ID. If 104 * the specified string doesn't match the syntax, <code>"GMT"</code> 105 * is used. 106 * <p> 107 * When creating a <code>TimeZone</code>, the specified custom time 108 * zone ID is normalized in the following syntax: 109 * <blockquote><pre> 110 * <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a> 111 * <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i> 112 * <i>Sign:</i> one of 113 * <code>+ -</code> 114 * <i>TwoDigitHours:</i> 115 * <i>Digit</i> <i>Digit</i> 116 * <i>Minutes:</i> 117 * <i>Digit</i> <i>Digit</i> 118 * <i>Digit:</i> one of 119 * <code>0 1 2 3 4 5 6 7 8 9</code> 120 * </pre></blockquote> 121 * For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00". 122 * 123 * <h4>Three-letter time zone IDs</h4> 124 * 125 * For compatibility with JDK 1.1.x, some other three-letter time zone IDs 126 * (such as "PST", "CTT", "AST") are also supported. However, <strong>their 127 * use is deprecated</strong> because the same abbreviation is often used 128 * for multiple time zones (for example, "CST" could be U.S. "Central Standard 129 * Time" and "China Standard Time"), and the Java platform can then only 130 * recognize one of them. 131 * 132 * 133 * @see Calendar 134 * @see GregorianCalendar 135 * @see SimpleTimeZone 136 * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu 137 * @since JDK1.1 138 */ 139 abstract public class TimeZone implements Serializable, Cloneable { 140 /** 141 * Sole constructor. (For invocation by subclass constructors, typically 142 * implicit.) 143 */ TimeZone()144 public TimeZone() { 145 } 146 147 /** 148 * A style specifier for <code>getDisplayName()</code> indicating 149 * a short name, such as "PST." 150 * @see #LONG 151 * @since 1.2 152 */ 153 public static final int SHORT = 0; 154 155 /** 156 * A style specifier for <code>getDisplayName()</code> indicating 157 * a long name, such as "Pacific Standard Time." 158 * @see #SHORT 159 * @since 1.2 160 */ 161 public static final int LONG = 1; 162 163 // Use a preload holder to allow compile-time initialization of TimeZone and dependents. 164 private static class NoImagePreloadHolder { 165 public static final Pattern CUSTOM_ZONE_ID_PATTERN = Pattern.compile("^GMT[-+](\\d{1,2})(:?(\\d\\d))?$"); 166 } 167 168 // Proclaim serialization compatibility with JDK 1.1 169 static final long serialVersionUID = 3581463369166924961L; 170 private static final TimeZone GMT = new SimpleTimeZone(0, "GMT"); 171 private static final TimeZone UTC = new SimpleTimeZone(0, "UTC"); 172 173 /** 174 * Gets the time zone offset, for current date, modified in case of 175 * daylight savings. This is the offset to add to UTC to get local time. 176 * <p> 177 * This method returns a historically correct offset if an 178 * underlying <code>TimeZone</code> implementation subclass 179 * supports historical Daylight Saving Time schedule and GMT 180 * offset changes. 181 * 182 * @param era the era of the given date. 183 * @param year the year in the given date. 184 * @param month the month in the given date. 185 * Month is 0-based. e.g., 0 for January. 186 * @param day the day-in-month of the given date. 187 * @param dayOfWeek the day-of-week of the given date. 188 * @param milliseconds the milliseconds in day in <em>standard</em> 189 * local time. 190 * 191 * @return the offset in milliseconds to add to GMT to get local time. 192 * 193 * @see Calendar#ZONE_OFFSET 194 * @see Calendar#DST_OFFSET 195 */ getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)196 public abstract int getOffset(int era, int year, int month, int day, 197 int dayOfWeek, int milliseconds); 198 199 /** 200 * Returns the offset of this time zone from UTC at the specified 201 * date. If Daylight Saving Time is in effect at the specified 202 * date, the offset value is adjusted with the amount of daylight 203 * saving. 204 * <p> 205 * This method returns a historically correct offset value if an 206 * underlying TimeZone implementation subclass supports historical 207 * Daylight Saving Time schedule and GMT offset changes. 208 * 209 * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT 210 * @return the amount of time in milliseconds to add to UTC to get local time. 211 * 212 * @see Calendar#ZONE_OFFSET 213 * @see Calendar#DST_OFFSET 214 * @since 1.4 215 */ getOffset(long date)216 public int getOffset(long date) { 217 if (inDaylightTime(new Date(date))) { 218 return getRawOffset() + getDSTSavings(); 219 } 220 return getRawOffset(); 221 } 222 223 /** 224 * Gets the raw GMT offset and the amount of daylight saving of this 225 * time zone at the given time. 226 * @param date the milliseconds (since January 1, 1970, 227 * 00:00:00.000 GMT) at which the time zone offset and daylight 228 * saving amount are found 229 * @param offset an array of int where the raw GMT offset 230 * (offset[0]) and daylight saving amount (offset[1]) are stored, 231 * or null if those values are not needed. The method assumes that 232 * the length of the given array is two or larger. 233 * @return the total amount of the raw GMT offset and daylight 234 * saving at the specified date. 235 * 236 * @see Calendar#ZONE_OFFSET 237 * @see Calendar#DST_OFFSET 238 */ getOffsets(long date, int[] offsets)239 int getOffsets(long date, int[] offsets) { 240 int rawoffset = getRawOffset(); 241 int dstoffset = 0; 242 if (inDaylightTime(new Date(date))) { 243 dstoffset = getDSTSavings(); 244 } 245 if (offsets != null) { 246 offsets[0] = rawoffset; 247 offsets[1] = dstoffset; 248 } 249 return rawoffset + dstoffset; 250 } 251 252 /** 253 * Sets the base time zone offset to GMT. 254 * This is the offset to add to UTC to get local time. 255 * <p> 256 * If an underlying <code>TimeZone</code> implementation subclass 257 * supports historical GMT offset changes, the specified GMT 258 * offset is set as the latest GMT offset and the difference from 259 * the known latest GMT offset value is used to adjust all 260 * historical GMT offset values. 261 * 262 * @param offsetMillis the given base time zone offset to GMT. 263 */ setRawOffset(int offsetMillis)264 abstract public void setRawOffset(int offsetMillis); 265 266 /** 267 * Returns the amount of time in milliseconds to add to UTC to get 268 * standard time in this time zone. Because this value is not 269 * affected by daylight saving time, it is called <I>raw 270 * offset</I>. 271 * <p> 272 * If an underlying <code>TimeZone</code> implementation subclass 273 * supports historical GMT offset changes, the method returns the 274 * raw offset value of the current date. In Honolulu, for example, 275 * its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and 276 * this method always returns -36000000 milliseconds (i.e., -10 277 * hours). 278 * 279 * @return the amount of raw offset time in milliseconds to add to UTC. 280 * @see Calendar#ZONE_OFFSET 281 */ getRawOffset()282 public abstract int getRawOffset(); 283 284 /** 285 * Gets the ID of this time zone. 286 * @return the ID of this time zone. 287 */ getID()288 public String getID() 289 { 290 return ID; 291 } 292 293 /** 294 * Sets the time zone ID. This does not change any other data in 295 * the time zone object. 296 * @param ID the new time zone ID. 297 */ setID(String ID)298 public void setID(String ID) 299 { 300 if (ID == null) { 301 throw new NullPointerException(); 302 } 303 this.ID = ID; 304 } 305 306 /** 307 * Returns a long standard time name of this {@code TimeZone} suitable for 308 * presentation to the user in the default locale. 309 * 310 * <p>This method is equivalent to: 311 * <pre><blockquote> 312 * getDisplayName(false, {@link #LONG}, 313 * Locale.getDefault({@link Locale.Category#DISPLAY})) 314 * </blockquote></pre> 315 * 316 * @return the human-readable name of this time zone in the default locale. 317 * @since 1.2 318 * @see #getDisplayName(boolean, int, Locale) 319 * @see Locale#getDefault(Locale.Category) 320 * @see Locale.Category 321 */ getDisplayName()322 public final String getDisplayName() { 323 return getDisplayName(false, LONG, 324 Locale.getDefault(Locale.Category.DISPLAY)); 325 } 326 327 /** 328 * Returns a long standard time name of this {@code TimeZone} suitable for 329 * presentation to the user in the specified {@code locale}. 330 * 331 * <p>This method is equivalent to: 332 * <pre><blockquote> 333 * getDisplayName(false, {@link #LONG}, locale) 334 * </blockquote></pre> 335 * 336 * @param locale the locale in which to supply the display name. 337 * @return the human-readable name of this time zone in the given locale. 338 * @exception NullPointerException if {@code locale} is {@code null}. 339 * @since 1.2 340 * @see #getDisplayName(boolean, int, Locale) 341 */ getDisplayName(Locale locale)342 public final String getDisplayName(Locale locale) { 343 return getDisplayName(false, LONG, locale); 344 } 345 346 /** 347 * Returns a name in the specified {@code style} of this {@code TimeZone} 348 * suitable for presentation to the user in the default locale. If the 349 * specified {@code daylight} is {@code true}, a Daylight Saving Time name 350 * is returned (even if this {@code TimeZone} doesn't observe Daylight Saving 351 * Time). Otherwise, a Standard Time name is returned. 352 * 353 * <p>This method is equivalent to: 354 * <pre><blockquote> 355 * getDisplayName(daylight, style, 356 * Locale.getDefault({@link Locale.Category#DISPLAY})) 357 * </blockquote></pre> 358 * 359 * @param daylight {@code true} specifying a Daylight Saving Time name, or 360 * {@code false} specifying a Standard Time name 361 * @param style either {@link #LONG} or {@link #SHORT} 362 * @return the human-readable name of this time zone in the default locale. 363 * @exception IllegalArgumentException if {@code style} is invalid. 364 * @since 1.2 365 * @see #getDisplayName(boolean, int, Locale) 366 * @see Locale#getDefault(Locale.Category) 367 * @see Locale.Category 368 * @see java.text.DateFormatSymbols#getZoneStrings() 369 */ getDisplayName(boolean daylight, int style)370 public final String getDisplayName(boolean daylight, int style) { 371 return getDisplayName(daylight, style, 372 Locale.getDefault(Locale.Category.DISPLAY)); 373 } 374 375 /** 376 * Returns the {@link #SHORT short} or {@link #LONG long} name of this time 377 * zone with either standard or daylight time, as written in {@code locale}. 378 * If the name is not available, the result is in the format 379 * {@code GMT[+-]hh:mm}. 380 * 381 * @param daylightTime true for daylight time, false for standard time. 382 * @param style either {@link TimeZone#LONG} or {@link TimeZone#SHORT}. 383 * @param locale the display locale. 384 */ getDisplayName(boolean daylightTime, int style, Locale locale)385 public String getDisplayName(boolean daylightTime, int style, Locale locale) { 386 if (style != SHORT && style != LONG) { 387 throw new IllegalArgumentException("Bad style: " + style); 388 } 389 390 String[][] zoneStrings = TimeZoneNames.getZoneStrings(locale); 391 String result = TimeZoneNames.getDisplayName(zoneStrings, getID(), daylightTime, style); 392 if (result != null) { 393 return result; 394 } 395 396 // If we get here, it's because icu4c has nothing for us. Most commonly, this is in the 397 // case of short names. For Pacific/Fiji, for example, icu4c has nothing better to offer 398 // than "GMT+12:00". Why do we re-do this work ourselves? Because we have up-to-date 399 // time zone transition data, which icu4c _doesn't_ use --- it uses its own baked-in copy, 400 // which only gets updated when we update icu4c. http://b/7955614 and http://b/8026776. 401 402 // TODO: should we generate these once, in TimeZoneNames.getDisplayName? Revisit when we 403 // upgrade to icu4c 50 and rewrite the underlying native code. See also the 404 // "element[j] != null" check in SimpleDateFormat.parseTimeZone, and the extra work in 405 // DateFormatSymbols.getZoneStrings. 406 int offsetMillis = getRawOffset(); 407 if (daylightTime) { 408 offsetMillis += getDSTSavings(); 409 } 410 return createGmtOffsetString(true /* includeGmt */, true /* includeMinuteSeparator */, 411 offsetMillis); 412 } 413 414 /** 415 * Returns a string representation of an offset from UTC. 416 * 417 * <p>The format is "[GMT](+|-)HH[:]MM". The output is not localized. 418 * 419 * @param includeGmt true to include "GMT", false to exclude 420 * @param includeMinuteSeparator true to include the separator between hours and minutes, false 421 * to exclude. 422 * @param offsetMillis the offset from UTC 423 * 424 * @hide used internally by SimpleDateFormat 425 */ createGmtOffsetString(boolean includeGmt, boolean includeMinuteSeparator, int offsetMillis)426 public static String createGmtOffsetString(boolean includeGmt, 427 boolean includeMinuteSeparator, int offsetMillis) { 428 int offsetMinutes = offsetMillis / 60000; 429 char sign = '+'; 430 if (offsetMinutes < 0) { 431 sign = '-'; 432 offsetMinutes = -offsetMinutes; 433 } 434 StringBuilder builder = new StringBuilder(9); 435 if (includeGmt) { 436 builder.append("GMT"); 437 } 438 builder.append(sign); 439 appendNumber(builder, 2, offsetMinutes / 60); 440 if (includeMinuteSeparator) { 441 builder.append(':'); 442 } 443 appendNumber(builder, 2, offsetMinutes % 60); 444 return builder.toString(); 445 } 446 appendNumber(StringBuilder builder, int count, int value)447 private static void appendNumber(StringBuilder builder, int count, int value) { 448 String string = Integer.toString(value); 449 for (int i = 0; i < count - string.length(); i++) { 450 builder.append('0'); 451 } 452 builder.append(string); 453 } 454 455 /** 456 * Returns the amount of time to be added to local standard time 457 * to get local wall clock time. 458 * 459 * <p>The default implementation returns 3600000 milliseconds 460 * (i.e., one hour) if a call to {@link #useDaylightTime()} 461 * returns {@code true}. Otherwise, 0 (zero) is returned. 462 * 463 * <p>If an underlying {@code TimeZone} implementation subclass 464 * supports historical and future Daylight Saving Time schedule 465 * changes, this method returns the amount of saving time of the 466 * last known Daylight Saving Time rule that can be a future 467 * prediction. 468 * 469 * <p>If the amount of saving time at any given time stamp is 470 * required, construct a {@link Calendar} with this {@code 471 * TimeZone} and the time stamp, and call {@link Calendar#get(int) 472 * Calendar.get}{@code (}{@link Calendar#DST_OFFSET}{@code )}. 473 * 474 * @return the amount of saving time in milliseconds 475 * @since 1.4 476 * @see #inDaylightTime(Date) 477 * @see #getOffset(long) 478 * @see #getOffset(int,int,int,int,int,int) 479 * @see Calendar#ZONE_OFFSET 480 */ getDSTSavings()481 public int getDSTSavings() { 482 if (useDaylightTime()) { 483 return 3600000; 484 } 485 return 0; 486 } 487 488 /** 489 * Queries if this {@code TimeZone} uses Daylight Saving Time. 490 * 491 * <p>If an underlying {@code TimeZone} implementation subclass 492 * supports historical and future Daylight Saving Time schedule 493 * changes, this method refers to the last known Daylight Saving Time 494 * rule that can be a future prediction and may not be the same as 495 * the current rule. Consider calling {@link #observesDaylightTime()} 496 * if the current rule should also be taken into account. 497 * 498 * @return {@code true} if this {@code TimeZone} uses Daylight Saving Time, 499 * {@code false}, otherwise. 500 * @see #inDaylightTime(Date) 501 * @see Calendar#DST_OFFSET 502 */ useDaylightTime()503 public abstract boolean useDaylightTime(); 504 505 /** 506 * Returns {@code true} if this {@code TimeZone} is currently in 507 * Daylight Saving Time, or if a transition from Standard Time to 508 * Daylight Saving Time occurs at any future time. 509 * 510 * <p>The default implementation returns {@code true} if 511 * {@code useDaylightTime()} or {@code inDaylightTime(new Date())} 512 * returns {@code true}. 513 * 514 * @return {@code true} if this {@code TimeZone} is currently in 515 * Daylight Saving Time, or if a transition from Standard Time to 516 * Daylight Saving Time occurs at any future time; {@code false} 517 * otherwise. 518 * @since 1.7 519 * @see #useDaylightTime() 520 * @see #inDaylightTime(Date) 521 * @see Calendar#DST_OFFSET 522 */ observesDaylightTime()523 public boolean observesDaylightTime() { 524 return useDaylightTime() || inDaylightTime(new Date()); 525 } 526 527 /** 528 * Queries if the given {@code date} is in Daylight Saving Time in 529 * this time zone. 530 * 531 * @param date the given Date. 532 * @return {@code true} if the given date is in Daylight Saving Time, 533 * {@code false}, otherwise. 534 */ inDaylightTime(Date date)535 abstract public boolean inDaylightTime(Date date); 536 537 /** 538 * Gets the <code>TimeZone</code> for the given ID. 539 * 540 * @param id the ID for a <code>TimeZone</code>, either an abbreviation 541 * such as "PST", a full name such as "America/Los_Angeles", or a custom 542 * ID such as "GMT-8:00". Note that the support of abbreviations is 543 * for JDK 1.1.x compatibility only and full names should be used. 544 * 545 * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID 546 * cannot be understood. 547 */ 548 // Android changed param s/ID/id getTimeZone(String id)549 public static synchronized TimeZone getTimeZone(String id) { 550 if (id == null) { 551 throw new NullPointerException("id == null"); 552 } 553 554 // Special cases? These can clone an existing instance. 555 if (id.length() == 3) { 556 if (id.equals("GMT")) { 557 return (TimeZone) GMT.clone(); 558 } 559 if (id.equals("UTC")) { 560 return (TimeZone) UTC.clone(); 561 } 562 } 563 564 // In the database? 565 TimeZone zone = null; 566 try { 567 zone = ZoneInfoDB.getInstance().makeTimeZone(id); 568 } catch (IOException ignored) { 569 } 570 571 // Custom time zone? 572 if (zone == null && id.length() > 3 && id.startsWith("GMT")) { 573 zone = getCustomTimeZone(id); 574 } 575 576 // We never return null; on failure we return the equivalent of "GMT". 577 return (zone != null) ? zone : (TimeZone) GMT.clone(); 578 } 579 580 /** 581 * Returns a new SimpleTimeZone for an ID of the form "GMT[+|-]hh[[:]mm]", or null. 582 */ getCustomTimeZone(String id)583 private static TimeZone getCustomTimeZone(String id) { 584 Matcher m = NoImagePreloadHolder.CUSTOM_ZONE_ID_PATTERN.matcher(id); 585 if (!m.matches()) { 586 return null; 587 } 588 589 int hour; 590 int minute = 0; 591 try { 592 hour = Integer.parseInt(m.group(1)); 593 if (m.group(3) != null) { 594 minute = Integer.parseInt(m.group(3)); 595 } 596 } catch (NumberFormatException impossible) { 597 throw new AssertionError(impossible); 598 } 599 600 if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { 601 return null; 602 } 603 604 char sign = id.charAt(3); 605 int raw = (hour * 3600000) + (minute * 60000); 606 if (sign == '-') { 607 raw = -raw; 608 } 609 610 String cleanId = String.format(Locale.ROOT, "GMT%c%02d:%02d", sign, hour, minute); 611 612 return new SimpleTimeZone(raw, cleanId); 613 } 614 615 /** 616 * Gets the available IDs according to the given time zone offset in milliseconds. 617 * 618 * @param rawOffset the given time zone GMT offset in milliseconds. 619 * @return an array of IDs, where the time zone for that ID has 620 * the specified GMT offset. For example, "America/Phoenix" and "America/Denver" 621 * both have GMT-07:00, but differ in daylight saving behavior. 622 * @see #getRawOffset() 623 */ getAvailableIDs(int rawOffset)624 public static synchronized String[] getAvailableIDs(int rawOffset) { 625 return ZoneInfoDB.getInstance().getAvailableIDs(rawOffset); 626 } 627 628 /** 629 * Gets all the available IDs supported. 630 * @return an array of IDs. 631 */ getAvailableIDs()632 public static synchronized String[] getAvailableIDs() { 633 return ZoneInfoDB.getInstance().getAvailableIDs(); 634 } 635 636 /** 637 * Gets the platform defined TimeZone ID. 638 **/ getSystemTimeZoneID(String javaHome, String country)639 private static native String getSystemTimeZoneID(String javaHome, 640 String country); 641 642 /** 643 * Gets the custom time zone ID based on the GMT offset of the 644 * platform. (e.g., "GMT+08:00") 645 */ getSystemGMTOffsetID()646 private static native String getSystemGMTOffsetID(); 647 648 /** 649 * Gets the default <code>TimeZone</code> for this host. 650 * The source of the default <code>TimeZone</code> 651 * may vary with implementation. 652 * @return a default <code>TimeZone</code>. 653 * @see #setDefault 654 */ getDefault()655 public static TimeZone getDefault() { 656 return (TimeZone) getDefaultRef().clone(); 657 } 658 659 /** 660 * Returns the reference to the default TimeZone object. This 661 * method doesn't create a clone. 662 */ getDefaultRef()663 static synchronized TimeZone getDefaultRef() { 664 if (defaultTimeZone == null) { 665 TimezoneGetter tzGetter = TimezoneGetter.getInstance(); 666 String zoneName = (tzGetter != null) ? tzGetter.getId() : null; 667 if (zoneName != null) { 668 zoneName = zoneName.trim(); 669 } 670 if (zoneName == null || zoneName.isEmpty()) { 671 try { 672 // On the host, we can find the configured timezone here. 673 zoneName = IoUtils.readFileAsString("/etc/timezone"); 674 } catch (IOException ex) { 675 // "vogar --mode device" can end up here. 676 // TODO: give libcore access to Android system properties and read "persist.sys.timezone". 677 zoneName = "GMT"; 678 } 679 } 680 defaultTimeZone = TimeZone.getTimeZone(zoneName); 681 } 682 return defaultTimeZone; 683 } 684 hasPermission()685 private static boolean hasPermission() { 686 boolean hasPermission = true; 687 SecurityManager sm = System.getSecurityManager(); 688 if (sm != null) { 689 try { 690 sm.checkPermission(new PropertyPermission 691 ("user.timezone", "write")); 692 } catch (SecurityException e) { 693 hasPermission = false; 694 } 695 } 696 return hasPermission; 697 } 698 699 /** 700 * Sets the <code>TimeZone</code> that is 701 * returned by the <code>getDefault</code> method. If <code>zone</code> 702 * is null, reset the default to the value it had originally when the 703 * VM first started. 704 * @param timeZone the new default time zone 705 * @see #getDefault 706 */ 707 // Android changed s/zone/timeZone setDefault(TimeZone timeZone)708 public synchronized static void setDefault(TimeZone timeZone) 709 { 710 if (hasPermission()) { 711 defaultTimeZone = timeZone != null ? (TimeZone) timeZone.clone() : null; 712 android.icu.util.TimeZone.clearCachedDefault(); 713 } 714 } 715 716 /** 717 * Returns true if this zone has the same rule and offset as another zone. 718 * That is, if this zone differs only in ID, if at all. Returns false 719 * if the other zone is null. 720 * @param other the <code>TimeZone</code> object to be compared with 721 * @return true if the other zone is not null and is the same as this one, 722 * with the possible exception of the ID 723 * @since 1.2 724 */ hasSameRules(TimeZone other)725 public boolean hasSameRules(TimeZone other) { 726 return other != null && getRawOffset() == other.getRawOffset() && 727 useDaylightTime() == other.useDaylightTime(); 728 } 729 730 /** 731 * Creates a copy of this <code>TimeZone</code>. 732 * 733 * @return a clone of this <code>TimeZone</code> 734 */ clone()735 public Object clone() 736 { 737 try { 738 TimeZone other = (TimeZone) super.clone(); 739 other.ID = ID; 740 return other; 741 } catch (CloneNotSupportedException e) { 742 throw new InternalError(); 743 } 744 } 745 746 /** 747 * The null constant as a TimeZone. 748 */ 749 static final TimeZone NO_TIMEZONE = null; 750 751 // =======================privates=============================== 752 753 /** 754 * The string identifier of this <code>TimeZone</code>. This is a 755 * programmatic identifier used internally to look up <code>TimeZone</code> 756 * objects from the system table and also to map them to their localized 757 * display names. <code>ID</code> values are unique in the system 758 * table but may not be for dynamically created zones. 759 * @serial 760 */ 761 private String ID; 762 private static volatile TimeZone defaultTimeZone; 763 } 764