2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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).
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.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
28 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
30 * The original version of this source code and documentation is copyrighted
31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32 * materials are provided under terms of a License Agreement between Taligent
33 * and Sun. This technology is protected by multiple US and International
34 * patents. This notice and attribution to Taligent may not be removed.
35 * Taligent is a registered trademark of Taligent, Inc.
41 import java.io.Serializable;
42 import java.lang.ref.SoftReference;
43 import java.util.concurrent.ConcurrentHashMap;
44 import org.apidesign.bck2brwsr.core.JavaScriptBody;
47 * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
51 * Typically, you get a <code>TimeZone</code> using <code>getDefault</code>
52 * which creates a <code>TimeZone</code> based on the time zone where the program
53 * is running. For example, for a program running in Japan, <code>getDefault</code>
54 * creates a <code>TimeZone</code> object based on Japanese Standard Time.
57 * You can also get a <code>TimeZone</code> using <code>getTimeZone</code>
58 * along with a time zone ID. For instance, the time zone ID for the
59 * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a
60 * U.S. Pacific Time <code>TimeZone</code> object with:
62 * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
64 * You can use the <code>getAvailableIDs</code> method to iterate through
65 * all the supported time zone IDs. You can then choose a
66 * supported ID to get a <code>TimeZone</code>.
67 * If the time zone you want is not represented by one of the
68 * supported IDs, then a custom time zone ID can be specified to
69 * produce a TimeZone. The syntax of a custom time zone ID is:
72 * <a name="CustomID"><i>CustomID:</i></a>
73 * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
74 * <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i>
75 * <code>GMT</code> <i>Sign</i> <i>Hours</i>
80 * <i>Digit</i> <i>Digit</i>
82 * <i>Digit</i> <i>Digit</i>
83 * <i>Digit:</i> one of
84 * <code>0 1 2 3 4 5 6 7 8 9</code>
87 * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be
88 * between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten
89 * hours and ten minutes ahead of GMT, respectively.
91 * The format is locale independent and digits must be taken from the
92 * Basic Latin block of the Unicode standard. No daylight saving time
93 * transition schedule can be specified with a custom time zone ID. If
94 * the specified string doesn't match the syntax, <code>"GMT"</code>
97 * When creating a <code>TimeZone</code>, the specified custom time
98 * zone ID is normalized in the following syntax:
100 * <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a>
101 * <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i>
102 * <i>Sign:</i> one of
104 * <i>TwoDigitHours:</i>
105 * <i>Digit</i> <i>Digit</i>
107 * <i>Digit</i> <i>Digit</i>
108 * <i>Digit:</i> one of
109 * <code>0 1 2 3 4 5 6 7 8 9</code>
110 * </pre></blockquote>
111 * For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00".
113 * <h4>Three-letter time zone IDs</h4>
115 * For compatibility with JDK 1.1.x, some other three-letter time zone IDs
116 * (such as "PST", "CTT", "AST") are also supported. However, <strong>their
117 * use is deprecated</strong> because the same abbreviation is often used
118 * for multiple time zones (for example, "CST" could be U.S. "Central Standard
119 * Time" and "China Standard Time"), and the Java platform can then only
120 * recognize one of them.
124 * @see GregorianCalendar
125 * @see SimpleTimeZone
126 * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
129 abstract public class TimeZone implements Serializable, Cloneable {
131 * Sole constructor. (For invocation by subclass constructors, typically
138 * A style specifier for <code>getDisplayName()</code> indicating
139 * a short name, such as "PST."
143 public static final int SHORT = 0;
146 * A style specifier for <code>getDisplayName()</code> indicating
147 * a long name, such as "Pacific Standard Time."
151 public static final int LONG = 1;
153 // Constants used internally; unit is milliseconds
154 private static final int ONE_MINUTE = 60*1000;
155 private static final int ONE_HOUR = 60*ONE_MINUTE;
156 private static final int ONE_DAY = 24*ONE_HOUR;
158 // Proclaim serialization compatibility with JDK 1.1
159 static final long serialVersionUID = 3581463369166924961L;
162 * Gets the time zone offset, for current date, modified in case of
163 * daylight savings. This is the offset to add to UTC to get local time.
165 * This method returns a historically correct offset if an
166 * underlying <code>TimeZone</code> implementation subclass
167 * supports historical Daylight Saving Time schedule and GMT
170 * @param era the era of the given date.
171 * @param year the year in the given date.
172 * @param month the month in the given date.
173 * Month is 0-based. e.g., 0 for January.
174 * @param day the day-in-month of the given date.
175 * @param dayOfWeek the day-of-week of the given date.
176 * @param milliseconds the milliseconds in day in <em>standard</em>
179 * @return the offset in milliseconds to add to GMT to get local time.
181 * @see Calendar#ZONE_OFFSET
182 * @see Calendar#DST_OFFSET
184 public abstract int getOffset(int era, int year, int month, int day,
185 int dayOfWeek, int milliseconds);
188 * Returns the offset of this time zone from UTC at the specified
189 * date. If Daylight Saving Time is in effect at the specified
190 * date, the offset value is adjusted with the amount of daylight
193 * This method returns a historically correct offset value if an
194 * underlying TimeZone implementation subclass supports historical
195 * Daylight Saving Time schedule and GMT offset changes.
197 * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT
198 * @return the amount of time in milliseconds to add to UTC to get local time.
200 * @see Calendar#ZONE_OFFSET
201 * @see Calendar#DST_OFFSET
204 public int getOffset(long date) {
205 if (inDaylightTime(new Date(date))) {
206 return getRawOffset() + getDSTSavings();
208 return getRawOffset();
212 * Gets the raw GMT offset and the amount of daylight saving of this
213 * time zone at the given time.
214 * @param date the milliseconds (since January 1, 1970,
215 * 00:00:00.000 GMT) at which the time zone offset and daylight
216 * saving amount are found
217 * @param offset an array of int where the raw GMT offset
218 * (offset[0]) and daylight saving amount (offset[1]) are stored,
219 * or null if those values are not needed. The method assumes that
220 * the length of the given array is two or larger.
221 * @return the total amount of the raw GMT offset and daylight
222 * saving at the specified date.
224 * @see Calendar#ZONE_OFFSET
225 * @see Calendar#DST_OFFSET
227 int getOffsets(long date, int[] offsets) {
228 int rawoffset = getRawOffset();
230 if (inDaylightTime(new Date(date))) {
231 dstoffset = getDSTSavings();
233 if (offsets != null) {
234 offsets[0] = rawoffset;
235 offsets[1] = dstoffset;
237 return rawoffset + dstoffset;
241 * Sets the base time zone offset to GMT.
242 * This is the offset to add to UTC to get local time.
244 * If an underlying <code>TimeZone</code> implementation subclass
245 * supports historical GMT offset changes, the specified GMT
246 * offset is set as the latest GMT offset and the difference from
247 * the known latest GMT offset value is used to adjust all
248 * historical GMT offset values.
250 * @param offsetMillis the given base time zone offset to GMT.
252 abstract public void setRawOffset(int offsetMillis);
255 * Returns the amount of time in milliseconds to add to UTC to get
256 * standard time in this time zone. Because this value is not
257 * affected by daylight saving time, it is called <I>raw
260 * If an underlying <code>TimeZone</code> implementation subclass
261 * supports historical GMT offset changes, the method returns the
262 * raw offset value of the current date. In Honolulu, for example,
263 * its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and
264 * this method always returns -36000000 milliseconds (i.e., -10
267 * @return the amount of raw offset time in milliseconds to add to UTC.
268 * @see Calendar#ZONE_OFFSET
270 public abstract int getRawOffset();
273 * Gets the ID of this time zone.
274 * @return the ID of this time zone.
276 public String getID()
282 * Sets the time zone ID. This does not change any other data in
283 * the time zone object.
284 * @param ID the new time zone ID.
286 public void setID(String ID)
289 throw new NullPointerException();
295 * Returns a long standard time name of this {@code TimeZone} suitable for
296 * presentation to the user in the default locale.
298 * <p>This method is equivalent to:
300 * getDisplayName(false, {@link #LONG},
301 * Locale.getDefault({@link Locale.Category#DISPLAY}))
302 * </blockquote></pre>
304 * @return the human-readable name of this time zone in the default locale.
306 * @see #getDisplayName(boolean, int, Locale)
307 * @see Locale#getDefault(Locale.Category)
308 * @see Locale.Category
310 public final String getDisplayName() {
311 return getDisplayName(false, LONG,
312 Locale.getDefault(Locale.Category.DISPLAY));
316 * Returns a long standard time name of this {@code TimeZone} suitable for
317 * presentation to the user in the specified {@code locale}.
319 * <p>This method is equivalent to:
321 * getDisplayName(false, {@link #LONG}, locale)
322 * </blockquote></pre>
324 * @param locale the locale in which to supply the display name.
325 * @return the human-readable name of this time zone in the given locale.
326 * @exception NullPointerException if {@code locale} is {@code null}.
328 * @see #getDisplayName(boolean, int, Locale)
330 public final String getDisplayName(Locale locale) {
331 return getDisplayName(false, LONG, locale);
335 * Returns a name in the specified {@code style} of this {@code TimeZone}
336 * suitable for presentation to the user in the default locale. If the
337 * specified {@code daylight} is {@code true}, a Daylight Saving Time name
338 * is returned (even if this {@code TimeZone} doesn't observe Daylight Saving
339 * Time). Otherwise, a Standard Time name is returned.
341 * <p>This method is equivalent to:
343 * getDisplayName(daylight, style,
344 * Locale.getDefault({@link Locale.Category#DISPLAY}))
345 * </blockquote></pre>
347 * @param daylight {@code true} specifying a Daylight Saving Time name, or
348 * {@code false} specifying a Standard Time name
349 * @param style either {@link #LONG} or {@link #SHORT}
350 * @return the human-readable name of this time zone in the default locale.
351 * @exception IllegalArgumentException if {@code style} is invalid.
353 * @see #getDisplayName(boolean, int, Locale)
354 * @see Locale#getDefault(Locale.Category)
355 * @see Locale.Category
356 * @see java.text.DateFormatSymbols#getZoneStrings()
358 public final String getDisplayName(boolean daylight, int style) {
359 return getDisplayName(daylight, style,
360 Locale.getDefault(Locale.Category.DISPLAY));
364 * Returns a name in the specified {@code style} of this {@code TimeZone}
365 * suitable for presentation to the user in the specified {@code
366 * locale}. If the specified {@code daylight} is {@code true}, a Daylight
367 * Saving Time name is returned (even if this {@code TimeZone} doesn't
368 * observe Daylight Saving Time). Otherwise, a Standard Time name is
371 * <p>When looking up a time zone name, the {@linkplain
372 * ResourceBundle.Control#getCandidateLocales(String,Locale) default
373 * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
374 * from the specified {@code locale} is used. (No {@linkplain
375 * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
376 * <code>Locale</code>} search is performed.) If a time zone name in any
377 * {@code Locale} of the search path, including {@link Locale#ROOT}, is
378 * found, the name is returned. Otherwise, a string in the
379 * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
381 * @param daylight {@code true} specifying a Daylight Saving Time name, or
382 * {@code false} specifying a Standard Time name
383 * @param style either {@link #LONG} or {@link #SHORT}
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 * @exception IllegalArgumentException if {@code style} is invalid.
387 * @exception NullPointerException if {@code locale} is {@code null}.
389 * @see java.text.DateFormatSymbols#getZoneStrings()
391 public String getDisplayName(boolean daylight, int style, Locale locale) {
392 if (style != SHORT && style != LONG) {
393 throw new IllegalArgumentException("Illegal style: " + style);
397 String[] names = getDisplayNames(id, locale);
399 if (id.startsWith("GMT")) {
400 char sign = id.charAt(3);
401 if (sign == '+' || sign == '-') {
405 int offset = getRawOffset();
407 offset += getDSTSavings();
409 // return ZoneInfoFile.toCustomID(offset);
412 int index = daylight ? 3 : 1;
413 if (style == SHORT) {
419 private static class DisplayNames {
420 // Cache for managing display names per timezone per locale
422 // Map(key=id, value=SoftReference(Map(key=locale, value=displaynames)))
423 private static final Map<String, SoftReference<Map<Locale, String[]>>> CACHE =
424 new ConcurrentHashMap<String, SoftReference<Map<Locale, String[]>>>();
427 private static final String[] getDisplayNames(String id, Locale locale) {
428 Map<String, SoftReference<Map<Locale, String[]>>> displayNames = DisplayNames.CACHE;
430 SoftReference<Map<Locale, String[]>> ref = displayNames.get(id);
432 Map<Locale, String[]> perLocale = ref.get();
433 if (perLocale != null) {
434 String[] names = perLocale.get(locale);
438 names = null; // TimeZoneNameUtility.retrieveDisplayNames(id, locale);
440 perLocale.put(locale, names);
446 String[] names = null; // TimeZoneNameUtility.retrieveDisplayNames(id, locale);
448 Map<Locale, String[]> perLocale = new ConcurrentHashMap<Locale, String[]>();
449 perLocale.put(locale, names);
450 ref = new SoftReference<Map<Locale, String[]>>(perLocale);
451 displayNames.put(id, ref);
457 * Returns the amount of time to be added to local standard time
458 * to get local wall clock time.
460 * <p>The default implementation returns 3600000 milliseconds
461 * (i.e., one hour) if a call to {@link #useDaylightTime()}
462 * returns {@code true}. Otherwise, 0 (zero) is returned.
464 * <p>If an underlying {@code TimeZone} implementation subclass
465 * supports historical and future Daylight Saving Time schedule
466 * changes, this method returns the amount of saving time of the
467 * last known Daylight Saving Time rule that can be a future
470 * <p>If the amount of saving time at any given time stamp is
471 * required, construct a {@link Calendar} with this {@code
472 * TimeZone} and the time stamp, and call {@link Calendar#get(int)
473 * Calendar.get}{@code (}{@link Calendar#DST_OFFSET}{@code )}.
475 * @return the amount of saving time in milliseconds
477 * @see #inDaylightTime(Date)
478 * @see #getOffset(long)
479 * @see #getOffset(int,int,int,int,int,int)
480 * @see Calendar#ZONE_OFFSET
482 public int getDSTSavings() {
483 if (useDaylightTime()) {
490 * Queries if this {@code TimeZone} uses Daylight Saving Time.
492 * <p>If an underlying {@code TimeZone} implementation subclass
493 * supports historical and future Daylight Saving Time schedule
494 * changes, this method refers to the last known Daylight Saving Time
495 * rule that can be a future prediction and may not be the same as
496 * the current rule. Consider calling {@link #observesDaylightTime()}
497 * if the current rule should also be taken into account.
499 * @return {@code true} if this {@code TimeZone} uses Daylight Saving Time,
500 * {@code false}, otherwise.
501 * @see #inDaylightTime(Date)
502 * @see Calendar#DST_OFFSET
504 public abstract boolean useDaylightTime();
507 * Returns {@code true} if this {@code TimeZone} is currently in
508 * Daylight Saving Time, or if a transition from Standard Time to
509 * Daylight Saving Time occurs at any future time.
511 * <p>The default implementation returns {@code true} if
512 * {@code useDaylightTime()} or {@code inDaylightTime(new Date())}
513 * returns {@code true}.
515 * @return {@code true} if this {@code TimeZone} is currently in
516 * Daylight Saving Time, or if a transition from Standard Time to
517 * Daylight Saving Time occurs at any future time; {@code false}
520 * @see #useDaylightTime()
521 * @see #inDaylightTime(Date)
522 * @see Calendar#DST_OFFSET
524 public boolean observesDaylightTime() {
525 return useDaylightTime() || inDaylightTime(new Date());
529 * Queries if the given {@code date} is in Daylight Saving Time in
532 * @param date the given Date.
533 * @return {@code true} if the given date is in Daylight Saving Time,
534 * {@code false}, otherwise.
536 abstract public boolean inDaylightTime(Date date);
539 * Gets the <code>TimeZone</code> for the given ID.
541 * @param ID the ID for a <code>TimeZone</code>, either an abbreviation
542 * such as "PST", a full name such as "America/Los_Angeles", or a custom
543 * ID such as "GMT-8:00". Note that the support of abbreviations is
544 * for JDK 1.1.x compatibility only and full names should be used.
546 * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
547 * cannot be understood.
549 public static synchronized TimeZone getTimeZone(String ID) {
550 return getTimeZone(ID, true);
553 private static TimeZone getTimeZone(String ID, boolean fallback) {
554 // TimeZone tz = ZoneInfo.getTimeZone(ID);
556 // tz = parseCustomTimeZone(ID);
557 // if (tz == null && fallback) {
558 // tz = new ZoneInfo(GMT_ID, 0);
562 return TimeZone.NO_TIMEZONE;
566 * Gets the available IDs according to the given time zone offset in milliseconds.
568 * @param rawOffset the given time zone GMT offset in milliseconds.
569 * @return an array of IDs, where the time zone for that ID has
570 * the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
571 * both have GMT-07:00, but differ in daylight saving behavior.
572 * @see #getRawOffset()
574 public static synchronized String[] getAvailableIDs(int rawOffset) {
575 return new String[0];//ZoneInfo.getAvailableIDs(rawOffset);
579 * Gets all the available IDs supported.
580 * @return an array of IDs.
582 public static synchronized String[] getAvailableIDs() {
583 return new String[0];//return ZoneInfo.getAvailableIDs();
587 * Gets the platform defined TimeZone ID.
589 private static native String getSystemTimeZoneID(String javaHome,
593 * Gets the custom time zone ID based on the GMT offset of the
594 * platform. (e.g., "GMT+08:00")
596 private static native String getSystemGMTOffsetID();
599 * Gets the default <code>TimeZone</code> for this host.
600 * The source of the default <code>TimeZone</code>
601 * may vary with implementation.
602 * @return a default <code>TimeZone</code>.
605 public static TimeZone getDefault() {
606 return (TimeZone) getDefaultRef().clone();
610 * Returns the reference to the default TimeZone object. This
611 * method doesn't create a clone.
613 static TimeZone getDefaultRef() {
614 TimeZone defaultZone = null;//defaultZoneTL.get();
615 if (defaultZone == null) {
616 defaultZone = defaultTimeZone;
617 if (defaultZone == null) {
618 // Need to initialize the default time zone.
619 defaultZone = new TimeZone() {
621 public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) {
622 return getRawOffset();
626 public void setRawOffset(int offsetMillis) {
630 public int getRawOffset() {
631 int minutesOff = dateTimezoneOffset();
632 return -60 * 1000 * minutesOff;
635 @JavaScriptBody(args = { }, body = "return new Date().getTimezoneOffset();")
636 private native int dateTimezoneOffset();
639 public boolean useDaylightTime() {
644 public boolean inDaylightTime(Date date) {
654 private static boolean hasPermission() {
655 boolean hasPermission = false;
656 return hasPermission;
660 * Sets the <code>TimeZone</code> that is
661 * returned by the <code>getDefault</code> method. If <code>zone</code>
662 * is null, reset the default to the value it had originally when the
664 * @param zone the new default time zone
667 public static void setDefault(TimeZone zone)
669 if (hasPermission()) {
670 synchronized (TimeZone.class) {
671 defaultTimeZone = zone;
672 // defaultZoneTL.set(null);
675 //defaultZoneTL.set(zone);
680 * Returns true if this zone has the same rule and offset as another zone.
681 * That is, if this zone differs only in ID, if at all. Returns false
682 * if the other zone is null.
683 * @param other the <code>TimeZone</code> object to be compared with
684 * @return true if the other zone is not null and is the same as this one,
685 * with the possible exception of the ID
688 public boolean hasSameRules(TimeZone other) {
689 return other != null && getRawOffset() == other.getRawOffset() &&
690 useDaylightTime() == other.useDaylightTime();
694 * Creates a copy of this <code>TimeZone</code>.
696 * @return a clone of this <code>TimeZone</code>
698 public Object clone()
701 TimeZone other = (TimeZone) super.clone();
704 } catch (CloneNotSupportedException e) {
705 throw new InternalError();
710 * The null constant as a TimeZone.
712 static final TimeZone NO_TIMEZONE = null;
714 // =======================privates===============================
717 * The string identifier of this <code>TimeZone</code>. This is a
718 * programmatic identifier used internally to look up <code>TimeZone</code>
719 * objects from the system table and also to map them to their localized
720 * display names. <code>ID</code> values are unique in the system
721 * table but may not be for dynamically created zones.
725 private static volatile TimeZone defaultTimeZone;
727 static final String GMT_ID = "GMT";
728 private static final int GMT_ID_LENGTH = 3;
731 * Parses a custom time zone identifier and returns a corresponding zone.
732 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
734 * @param id a string of the <a href="#CustomID">custom ID form</a>.
735 * @return a newly created TimeZone with the given offset and
736 * no daylight saving time, or null if the id cannot be parsed.
738 private static final TimeZone parseCustomTimeZone(String id) {