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