rt/emul/compact/src/main/java/java/util/TimeZone.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1340 41046f76a76a
permissions -rw-r--r--
Set of JDK classes needed to run javac
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
import sun.security.action.GetPropertyAction;
jtulach@1334
    47
import sun.util.TimeZoneNameUtility;
jtulach@1334
    48
import sun.util.calendar.ZoneInfo;
jtulach@1334
    49
import sun.util.calendar.ZoneInfoFile;
jtulach@1334
    50
jtulach@1334
    51
/**
jtulach@1334
    52
 * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
jtulach@1334
    53
 * savings.
jtulach@1334
    54
 *
jtulach@1334
    55
 * <p>
jtulach@1334
    56
 * Typically, you get a <code>TimeZone</code> using <code>getDefault</code>
jtulach@1334
    57
 * which creates a <code>TimeZone</code> based on the time zone where the program
jtulach@1334
    58
 * is running. For example, for a program running in Japan, <code>getDefault</code>
jtulach@1334
    59
 * creates a <code>TimeZone</code> object based on Japanese Standard Time.
jtulach@1334
    60
 *
jtulach@1334
    61
 * <p>
jtulach@1334
    62
 * You can also get a <code>TimeZone</code> using <code>getTimeZone</code>
jtulach@1334
    63
 * along with a time zone ID. For instance, the time zone ID for the
jtulach@1334
    64
 * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a
jtulach@1334
    65
 * U.S. Pacific Time <code>TimeZone</code> object with:
jtulach@1334
    66
 * <blockquote><pre>
jtulach@1334
    67
 * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
jtulach@1334
    68
 * </pre></blockquote>
jtulach@1334
    69
 * You can use the <code>getAvailableIDs</code> method to iterate through
jtulach@1334
    70
 * all the supported time zone IDs. You can then choose a
jtulach@1334
    71
 * supported ID to get a <code>TimeZone</code>.
jtulach@1334
    72
 * If the time zone you want is not represented by one of the
jtulach@1334
    73
 * supported IDs, then a custom time zone ID can be specified to
jtulach@1334
    74
 * produce a TimeZone. The syntax of a custom time zone ID is:
jtulach@1334
    75
 *
jtulach@1334
    76
 * <blockquote><pre>
jtulach@1334
    77
 * <a name="CustomID"><i>CustomID:</i></a>
jtulach@1334
    78
 *         <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
jtulach@1334
    79
 *         <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i>
jtulach@1334
    80
 *         <code>GMT</code> <i>Sign</i> <i>Hours</i>
jtulach@1334
    81
 * <i>Sign:</i> one of
jtulach@1334
    82
 *         <code>+ -</code>
jtulach@1334
    83
 * <i>Hours:</i>
jtulach@1334
    84
 *         <i>Digit</i>
jtulach@1334
    85
 *         <i>Digit</i> <i>Digit</i>
jtulach@1334
    86
 * <i>Minutes:</i>
jtulach@1334
    87
 *         <i>Digit</i> <i>Digit</i>
jtulach@1334
    88
 * <i>Digit:</i> one of
jtulach@1334
    89
 *         <code>0 1 2 3 4 5 6 7 8 9</code>
jtulach@1334
    90
 * </pre></blockquote>
jtulach@1334
    91
 *
jtulach@1334
    92
 * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be
jtulach@1334
    93
 * between 00 to 59.  For example, "GMT+10" and "GMT+0010" mean ten
jtulach@1334
    94
 * hours and ten minutes ahead of GMT, respectively.
jtulach@1334
    95
 * <p>
jtulach@1334
    96
 * The format is locale independent and digits must be taken from the
jtulach@1334
    97
 * Basic Latin block of the Unicode standard. No daylight saving time
jtulach@1334
    98
 * transition schedule can be specified with a custom time zone ID. If
jtulach@1334
    99
 * the specified string doesn't match the syntax, <code>"GMT"</code>
jtulach@1334
   100
 * is used.
jtulach@1334
   101
 * <p>
jtulach@1334
   102
 * When creating a <code>TimeZone</code>, the specified custom time
jtulach@1334
   103
 * zone ID is normalized in the following syntax:
jtulach@1334
   104
 * <blockquote><pre>
jtulach@1334
   105
 * <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a>
jtulach@1334
   106
 *         <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i>
jtulach@1334
   107
 * <i>Sign:</i> one of
jtulach@1334
   108
 *         <code>+ -</code>
jtulach@1334
   109
 * <i>TwoDigitHours:</i>
jtulach@1334
   110
 *         <i>Digit</i> <i>Digit</i>
jtulach@1334
   111
 * <i>Minutes:</i>
jtulach@1334
   112
 *         <i>Digit</i> <i>Digit</i>
jtulach@1334
   113
 * <i>Digit:</i> one of
jtulach@1334
   114
 *         <code>0 1 2 3 4 5 6 7 8 9</code>
jtulach@1334
   115
 * </pre></blockquote>
jtulach@1334
   116
 * For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00".
jtulach@1334
   117
 *
jtulach@1334
   118
 * <h4>Three-letter time zone IDs</h4>
jtulach@1334
   119
 *
jtulach@1334
   120
 * For compatibility with JDK 1.1.x, some other three-letter time zone IDs
jtulach@1334
   121
 * (such as "PST", "CTT", "AST") are also supported. However, <strong>their
jtulach@1334
   122
 * use is deprecated</strong> because the same abbreviation is often used
jtulach@1334
   123
 * for multiple time zones (for example, "CST" could be U.S. "Central Standard
jtulach@1334
   124
 * Time" and "China Standard Time"), and the Java platform can then only
jtulach@1334
   125
 * recognize one of them.
jtulach@1334
   126
 *
jtulach@1334
   127
 *
jtulach@1334
   128
 * @see          Calendar
jtulach@1334
   129
 * @see          GregorianCalendar
jtulach@1334
   130
 * @see          SimpleTimeZone
jtulach@1334
   131
 * @author       Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
jtulach@1334
   132
 * @since        JDK1.1
jtulach@1334
   133
 */
jtulach@1334
   134
abstract public class TimeZone implements Serializable, Cloneable {
jtulach@1334
   135
    /**
jtulach@1334
   136
     * Sole constructor.  (For invocation by subclass constructors, typically
jtulach@1334
   137
     * implicit.)
jtulach@1334
   138
     */
jtulach@1334
   139
    public TimeZone() {
jtulach@1334
   140
    }
jtulach@1334
   141
jtulach@1334
   142
    /**
jtulach@1334
   143
     * A style specifier for <code>getDisplayName()</code> indicating
jtulach@1334
   144
     * a short name, such as "PST."
jtulach@1334
   145
     * @see #LONG
jtulach@1334
   146
     * @since 1.2
jtulach@1334
   147
     */
jtulach@1334
   148
    public static final int SHORT = 0;
jtulach@1334
   149
jtulach@1334
   150
    /**
jtulach@1334
   151
     * A style specifier for <code>getDisplayName()</code> indicating
jtulach@1334
   152
     * a long name, such as "Pacific Standard Time."
jtulach@1334
   153
     * @see #SHORT
jtulach@1334
   154
     * @since 1.2
jtulach@1334
   155
     */
jtulach@1334
   156
    public static final int LONG  = 1;
jtulach@1334
   157
jtulach@1334
   158
    // Constants used internally; unit is milliseconds
jtulach@1334
   159
    private static final int ONE_MINUTE = 60*1000;
jtulach@1334
   160
    private static final int ONE_HOUR   = 60*ONE_MINUTE;
jtulach@1334
   161
    private static final int ONE_DAY    = 24*ONE_HOUR;
jtulach@1334
   162
jtulach@1334
   163
    // Proclaim serialization compatibility with JDK 1.1
jtulach@1334
   164
    static final long serialVersionUID = 3581463369166924961L;
jtulach@1334
   165
jtulach@1334
   166
    /**
jtulach@1334
   167
     * Gets the time zone offset, for current date, modified in case of
jtulach@1334
   168
     * daylight savings. This is the offset to add to UTC to get local time.
jtulach@1334
   169
     * <p>
jtulach@1334
   170
     * This method returns a historically correct offset if an
jtulach@1334
   171
     * underlying <code>TimeZone</code> implementation subclass
jtulach@1334
   172
     * supports historical Daylight Saving Time schedule and GMT
jtulach@1334
   173
     * offset changes.
jtulach@1334
   174
     *
jtulach@1334
   175
     * @param era the era of the given date.
jtulach@1334
   176
     * @param year the year in the given date.
jtulach@1334
   177
     * @param month the month in the given date.
jtulach@1334
   178
     * Month is 0-based. e.g., 0 for January.
jtulach@1334
   179
     * @param day the day-in-month of the given date.
jtulach@1334
   180
     * @param dayOfWeek the day-of-week of the given date.
jtulach@1334
   181
     * @param milliseconds the milliseconds in day in <em>standard</em>
jtulach@1334
   182
     * local time.
jtulach@1334
   183
     *
jtulach@1334
   184
     * @return the offset in milliseconds to add to GMT to get local time.
jtulach@1334
   185
     *
jtulach@1334
   186
     * @see Calendar#ZONE_OFFSET
jtulach@1334
   187
     * @see Calendar#DST_OFFSET
jtulach@1334
   188
     */
jtulach@1334
   189
    public abstract int getOffset(int era, int year, int month, int day,
jtulach@1334
   190
                                  int dayOfWeek, int milliseconds);
jtulach@1334
   191
jtulach@1334
   192
    /**
jtulach@1334
   193
     * Returns the offset of this time zone from UTC at the specified
jtulach@1334
   194
     * date. If Daylight Saving Time is in effect at the specified
jtulach@1334
   195
     * date, the offset value is adjusted with the amount of daylight
jtulach@1334
   196
     * saving.
jtulach@1334
   197
     * <p>
jtulach@1334
   198
     * This method returns a historically correct offset value if an
jtulach@1334
   199
     * underlying TimeZone implementation subclass supports historical
jtulach@1334
   200
     * Daylight Saving Time schedule and GMT offset changes.
jtulach@1334
   201
     *
jtulach@1334
   202
     * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT
jtulach@1334
   203
     * @return the amount of time in milliseconds to add to UTC to get local time.
jtulach@1334
   204
     *
jtulach@1334
   205
     * @see Calendar#ZONE_OFFSET
jtulach@1334
   206
     * @see Calendar#DST_OFFSET
jtulach@1334
   207
     * @since 1.4
jtulach@1334
   208
     */
jtulach@1334
   209
    public int getOffset(long date) {
jtulach@1334
   210
        if (inDaylightTime(new Date(date))) {
jtulach@1334
   211
            return getRawOffset() + getDSTSavings();
jtulach@1334
   212
        }
jtulach@1334
   213
        return getRawOffset();
jtulach@1334
   214
    }
jtulach@1334
   215
jtulach@1334
   216
    /**
jtulach@1334
   217
     * Gets the raw GMT offset and the amount of daylight saving of this
jtulach@1334
   218
     * time zone at the given time.
jtulach@1334
   219
     * @param date the milliseconds (since January 1, 1970,
jtulach@1334
   220
     * 00:00:00.000 GMT) at which the time zone offset and daylight
jtulach@1334
   221
     * saving amount are found
jtulach@1334
   222
     * @param offset an array of int where the raw GMT offset
jtulach@1334
   223
     * (offset[0]) and daylight saving amount (offset[1]) are stored,
jtulach@1334
   224
     * or null if those values are not needed. The method assumes that
jtulach@1334
   225
     * the length of the given array is two or larger.
jtulach@1334
   226
     * @return the total amount of the raw GMT offset and daylight
jtulach@1334
   227
     * saving at the specified date.
jtulach@1334
   228
     *
jtulach@1334
   229
     * @see Calendar#ZONE_OFFSET
jtulach@1334
   230
     * @see Calendar#DST_OFFSET
jtulach@1334
   231
     */
jtulach@1334
   232
    int getOffsets(long date, int[] offsets) {
jtulach@1334
   233
        int rawoffset = getRawOffset();
jtulach@1334
   234
        int dstoffset = 0;
jtulach@1334
   235
        if (inDaylightTime(new Date(date))) {
jtulach@1334
   236
            dstoffset = getDSTSavings();
jtulach@1334
   237
        }
jtulach@1334
   238
        if (offsets != null) {
jtulach@1334
   239
            offsets[0] = rawoffset;
jtulach@1334
   240
            offsets[1] = dstoffset;
jtulach@1334
   241
        }
jtulach@1334
   242
        return rawoffset + dstoffset;
jtulach@1334
   243
    }
jtulach@1334
   244
jtulach@1334
   245
    /**
jtulach@1334
   246
     * Sets the base time zone offset to GMT.
jtulach@1334
   247
     * This is the offset to add to UTC to get local time.
jtulach@1334
   248
     * <p>
jtulach@1334
   249
     * If an underlying <code>TimeZone</code> implementation subclass
jtulach@1334
   250
     * supports historical GMT offset changes, the specified GMT
jtulach@1334
   251
     * offset is set as the latest GMT offset and the difference from
jtulach@1334
   252
     * the known latest GMT offset value is used to adjust all
jtulach@1334
   253
     * historical GMT offset values.
jtulach@1334
   254
     *
jtulach@1334
   255
     * @param offsetMillis the given base time zone offset to GMT.
jtulach@1334
   256
     */
jtulach@1334
   257
    abstract public void setRawOffset(int offsetMillis);
jtulach@1334
   258
jtulach@1334
   259
    /**
jtulach@1334
   260
     * Returns the amount of time in milliseconds to add to UTC to get
jtulach@1334
   261
     * standard time in this time zone. Because this value is not
jtulach@1334
   262
     * affected by daylight saving time, it is called <I>raw
jtulach@1334
   263
     * offset</I>.
jtulach@1334
   264
     * <p>
jtulach@1334
   265
     * If an underlying <code>TimeZone</code> implementation subclass
jtulach@1334
   266
     * supports historical GMT offset changes, the method returns the
jtulach@1334
   267
     * raw offset value of the current date. In Honolulu, for example,
jtulach@1334
   268
     * its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and
jtulach@1334
   269
     * this method always returns -36000000 milliseconds (i.e., -10
jtulach@1334
   270
     * hours).
jtulach@1334
   271
     *
jtulach@1334
   272
     * @return the amount of raw offset time in milliseconds to add to UTC.
jtulach@1334
   273
     * @see Calendar#ZONE_OFFSET
jtulach@1334
   274
     */
jtulach@1334
   275
    public abstract int getRawOffset();
jtulach@1334
   276
jtulach@1334
   277
    /**
jtulach@1334
   278
     * Gets the ID of this time zone.
jtulach@1334
   279
     * @return the ID of this time zone.
jtulach@1334
   280
     */
jtulach@1334
   281
    public String getID()
jtulach@1334
   282
    {
jtulach@1334
   283
        return ID;
jtulach@1334
   284
    }
jtulach@1334
   285
jtulach@1334
   286
    /**
jtulach@1334
   287
     * Sets the time zone ID. This does not change any other data in
jtulach@1334
   288
     * the time zone object.
jtulach@1334
   289
     * @param ID the new time zone ID.
jtulach@1334
   290
     */
jtulach@1334
   291
    public void setID(String ID)
jtulach@1334
   292
    {
jtulach@1334
   293
        if (ID == null) {
jtulach@1334
   294
            throw new NullPointerException();
jtulach@1334
   295
        }
jtulach@1334
   296
        this.ID = ID;
jtulach@1334
   297
    }
jtulach@1334
   298
jtulach@1334
   299
    /**
jtulach@1334
   300
     * Returns a long standard time name of this {@code TimeZone} suitable for
jtulach@1334
   301
     * presentation to the user in the default locale.
jtulach@1334
   302
     *
jtulach@1334
   303
     * <p>This method is equivalent to:
jtulach@1334
   304
     * <pre><blockquote>
jtulach@1334
   305
     * getDisplayName(false, {@link #LONG},
jtulach@1334
   306
     *                Locale.getDefault({@link Locale.Category#DISPLAY}))
jtulach@1334
   307
     * </blockquote></pre>
jtulach@1334
   308
     *
jtulach@1334
   309
     * @return the human-readable name of this time zone in the default locale.
jtulach@1334
   310
     * @since 1.2
jtulach@1334
   311
     * @see #getDisplayName(boolean, int, Locale)
jtulach@1334
   312
     * @see Locale#getDefault(Locale.Category)
jtulach@1334
   313
     * @see Locale.Category
jtulach@1334
   314
     */
jtulach@1334
   315
    public final String getDisplayName() {
jtulach@1334
   316
        return getDisplayName(false, LONG,
jtulach@1334
   317
                              Locale.getDefault(Locale.Category.DISPLAY));
jtulach@1334
   318
    }
jtulach@1334
   319
jtulach@1334
   320
    /**
jtulach@1334
   321
     * Returns a long standard time name of this {@code TimeZone} suitable for
jtulach@1334
   322
     * presentation to the user in the specified {@code locale}.
jtulach@1334
   323
     *
jtulach@1334
   324
     * <p>This method is equivalent to:
jtulach@1334
   325
     * <pre><blockquote>
jtulach@1334
   326
     * getDisplayName(false, {@link #LONG}, locale)
jtulach@1334
   327
     * </blockquote></pre>
jtulach@1334
   328
     *
jtulach@1334
   329
     * @param locale the locale in which to supply the display name.
jtulach@1334
   330
     * @return the human-readable name of this time zone in the given locale.
jtulach@1334
   331
     * @exception NullPointerException if {@code locale} is {@code null}.
jtulach@1334
   332
     * @since 1.2
jtulach@1334
   333
     * @see #getDisplayName(boolean, int, Locale)
jtulach@1334
   334
     */
jtulach@1334
   335
    public final String getDisplayName(Locale locale) {
jtulach@1334
   336
        return getDisplayName(false, LONG, locale);
jtulach@1334
   337
    }
jtulach@1334
   338
jtulach@1334
   339
    /**
jtulach@1334
   340
     * Returns a name in the specified {@code style} of this {@code TimeZone}
jtulach@1334
   341
     * suitable for presentation to the user in the default locale. If the
jtulach@1334
   342
     * specified {@code daylight} is {@code true}, a Daylight Saving Time name
jtulach@1334
   343
     * is returned (even if this {@code TimeZone} doesn't observe Daylight Saving
jtulach@1334
   344
     * Time). Otherwise, a Standard Time name is returned.
jtulach@1334
   345
     *
jtulach@1334
   346
     * <p>This method is equivalent to:
jtulach@1334
   347
     * <pre><blockquote>
jtulach@1334
   348
     * getDisplayName(daylight, style,
jtulach@1334
   349
     *                Locale.getDefault({@link Locale.Category#DISPLAY}))
jtulach@1334
   350
     * </blockquote></pre>
jtulach@1334
   351
     *
jtulach@1334
   352
     * @param daylight {@code true} specifying a Daylight Saving Time name, or
jtulach@1334
   353
     *                 {@code false} specifying a Standard Time name
jtulach@1334
   354
     * @param style either {@link #LONG} or {@link #SHORT}
jtulach@1334
   355
     * @return the human-readable name of this time zone in the default locale.
jtulach@1334
   356
     * @exception IllegalArgumentException if {@code style} is invalid.
jtulach@1334
   357
     * @since 1.2
jtulach@1334
   358
     * @see #getDisplayName(boolean, int, Locale)
jtulach@1334
   359
     * @see Locale#getDefault(Locale.Category)
jtulach@1334
   360
     * @see Locale.Category
jtulach@1334
   361
     * @see java.text.DateFormatSymbols#getZoneStrings()
jtulach@1334
   362
     */
jtulach@1334
   363
    public final String getDisplayName(boolean daylight, int style) {
jtulach@1334
   364
        return getDisplayName(daylight, style,
jtulach@1334
   365
                              Locale.getDefault(Locale.Category.DISPLAY));
jtulach@1334
   366
    }
jtulach@1334
   367
jtulach@1334
   368
    /**
jtulach@1334
   369
     * Returns a name in the specified {@code style} of this {@code TimeZone}
jtulach@1334
   370
     * suitable for presentation to the user in the specified {@code
jtulach@1334
   371
     * locale}. If the specified {@code daylight} is {@code true}, a Daylight
jtulach@1334
   372
     * Saving Time name is returned (even if this {@code TimeZone} doesn't
jtulach@1334
   373
     * observe Daylight Saving Time). Otherwise, a Standard Time name is
jtulach@1334
   374
     * returned.
jtulach@1334
   375
     *
jtulach@1334
   376
     * <p>When looking up a time zone name, the {@linkplain
jtulach@1334
   377
     * ResourceBundle.Control#getCandidateLocales(String,Locale) default
jtulach@1334
   378
     * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
jtulach@1334
   379
     * from the specified {@code locale} is used. (No {@linkplain
jtulach@1334
   380
     * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
jtulach@1334
   381
     * <code>Locale</code>} search is performed.) If a time zone name in any
jtulach@1334
   382
     * {@code Locale} of the search path, including {@link Locale#ROOT}, is
jtulach@1334
   383
     * found, the name is returned. Otherwise, a string in the
jtulach@1334
   384
     * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
jtulach@1334
   385
     *
jtulach@1334
   386
     * @param daylight {@code true} specifying a Daylight Saving Time name, or
jtulach@1334
   387
     *                 {@code false} specifying a Standard Time name
jtulach@1334
   388
     * @param style either {@link #LONG} or {@link #SHORT}
jtulach@1334
   389
     * @param locale   the locale in which to supply the display name.
jtulach@1334
   390
     * @return the human-readable name of this time zone in the given locale.
jtulach@1334
   391
     * @exception IllegalArgumentException if {@code style} is invalid.
jtulach@1334
   392
     * @exception NullPointerException if {@code locale} is {@code null}.
jtulach@1334
   393
     * @since 1.2
jtulach@1334
   394
     * @see java.text.DateFormatSymbols#getZoneStrings()
jtulach@1334
   395
     */
jtulach@1334
   396
    public String getDisplayName(boolean daylight, int style, Locale locale) {
jtulach@1334
   397
        if (style != SHORT && style != LONG) {
jtulach@1334
   398
            throw new IllegalArgumentException("Illegal style: " + style);
jtulach@1334
   399
        }
jtulach@1334
   400
jtulach@1334
   401
        String id = getID();
jtulach@1334
   402
        String[] names = getDisplayNames(id, locale);
jtulach@1334
   403
        if (names == null) {
jtulach@1334
   404
            if (id.startsWith("GMT")) {
jtulach@1334
   405
                char sign = id.charAt(3);
jtulach@1334
   406
                if (sign == '+' || sign == '-') {
jtulach@1334
   407
                    return id;
jtulach@1334
   408
                }
jtulach@1334
   409
            }
jtulach@1334
   410
            int offset = getRawOffset();
jtulach@1334
   411
            if (daylight) {
jtulach@1334
   412
                offset += getDSTSavings();
jtulach@1334
   413
            }
jtulach@1334
   414
            return ZoneInfoFile.toCustomID(offset);
jtulach@1334
   415
        }
jtulach@1334
   416
jtulach@1334
   417
        int index = daylight ? 3 : 1;
jtulach@1334
   418
        if (style == SHORT) {
jtulach@1334
   419
            index++;
jtulach@1334
   420
        }
jtulach@1334
   421
        return names[index];
jtulach@1334
   422
    }
jtulach@1334
   423
jtulach@1334
   424
    private static class DisplayNames {
jtulach@1334
   425
        // Cache for managing display names per timezone per locale
jtulach@1334
   426
        // The structure is:
jtulach@1334
   427
        //   Map(key=id, value=SoftReference(Map(key=locale, value=displaynames)))
jtulach@1334
   428
        private static final Map<String, SoftReference<Map<Locale, String[]>>> CACHE =
jtulach@1334
   429
            new ConcurrentHashMap<String, SoftReference<Map<Locale, String[]>>>();
jtulach@1334
   430
    }
jtulach@1334
   431
jtulach@1334
   432
    private static final String[] getDisplayNames(String id, Locale locale) {
jtulach@1334
   433
        Map<String, SoftReference<Map<Locale, String[]>>> displayNames = DisplayNames.CACHE;
jtulach@1334
   434
jtulach@1334
   435
        SoftReference<Map<Locale, String[]>> ref = displayNames.get(id);
jtulach@1334
   436
        if (ref != null) {
jtulach@1334
   437
            Map<Locale, String[]> perLocale = ref.get();
jtulach@1334
   438
            if (perLocale != null) {
jtulach@1334
   439
                String[] names = perLocale.get(locale);
jtulach@1334
   440
                if (names != null) {
jtulach@1334
   441
                    return names;
jtulach@1334
   442
                }
jtulach@1334
   443
                names = TimeZoneNameUtility.retrieveDisplayNames(id, locale);
jtulach@1334
   444
                if (names != null) {
jtulach@1334
   445
                    perLocale.put(locale, names);
jtulach@1334
   446
                }
jtulach@1334
   447
                return names;
jtulach@1334
   448
            }
jtulach@1334
   449
        }
jtulach@1334
   450
jtulach@1334
   451
        String[] names = TimeZoneNameUtility.retrieveDisplayNames(id, locale);
jtulach@1334
   452
        if (names != null) {
jtulach@1334
   453
            Map<Locale, String[]> perLocale = new ConcurrentHashMap<Locale, String[]>();
jtulach@1334
   454
            perLocale.put(locale, names);
jtulach@1334
   455
            ref = new SoftReference<Map<Locale, String[]>>(perLocale);
jtulach@1334
   456
            displayNames.put(id, ref);
jtulach@1334
   457
        }
jtulach@1334
   458
        return names;
jtulach@1334
   459
    }
jtulach@1334
   460
jtulach@1334
   461
    /**
jtulach@1334
   462
     * Returns the amount of time to be added to local standard time
jtulach@1334
   463
     * to get local wall clock time.
jtulach@1334
   464
     *
jtulach@1334
   465
     * <p>The default implementation returns 3600000 milliseconds
jtulach@1334
   466
     * (i.e., one hour) if a call to {@link #useDaylightTime()}
jtulach@1334
   467
     * returns {@code true}. Otherwise, 0 (zero) is returned.
jtulach@1334
   468
     *
jtulach@1334
   469
     * <p>If an underlying {@code TimeZone} implementation subclass
jtulach@1334
   470
     * supports historical and future Daylight Saving Time schedule
jtulach@1334
   471
     * changes, this method returns the amount of saving time of the
jtulach@1334
   472
     * last known Daylight Saving Time rule that can be a future
jtulach@1334
   473
     * prediction.
jtulach@1334
   474
     *
jtulach@1334
   475
     * <p>If the amount of saving time at any given time stamp is
jtulach@1334
   476
     * required, construct a {@link Calendar} with this {@code
jtulach@1334
   477
     * TimeZone} and the time stamp, and call {@link Calendar#get(int)
jtulach@1334
   478
     * Calendar.get}{@code (}{@link Calendar#DST_OFFSET}{@code )}.
jtulach@1334
   479
     *
jtulach@1334
   480
     * @return the amount of saving time in milliseconds
jtulach@1334
   481
     * @since 1.4
jtulach@1334
   482
     * @see #inDaylightTime(Date)
jtulach@1334
   483
     * @see #getOffset(long)
jtulach@1334
   484
     * @see #getOffset(int,int,int,int,int,int)
jtulach@1334
   485
     * @see Calendar#ZONE_OFFSET
jtulach@1334
   486
     */
jtulach@1334
   487
    public int getDSTSavings() {
jtulach@1334
   488
        if (useDaylightTime()) {
jtulach@1334
   489
            return 3600000;
jtulach@1334
   490
        }
jtulach@1334
   491
        return 0;
jtulach@1334
   492
    }
jtulach@1334
   493
jtulach@1334
   494
    /**
jtulach@1334
   495
     * Queries if this {@code TimeZone} uses Daylight Saving Time.
jtulach@1334
   496
     *
jtulach@1334
   497
     * <p>If an underlying {@code TimeZone} implementation subclass
jtulach@1334
   498
     * supports historical and future Daylight Saving Time schedule
jtulach@1334
   499
     * changes, this method refers to the last known Daylight Saving Time
jtulach@1334
   500
     * rule that can be a future prediction and may not be the same as
jtulach@1334
   501
     * the current rule. Consider calling {@link #observesDaylightTime()}
jtulach@1334
   502
     * if the current rule should also be taken into account.
jtulach@1334
   503
     *
jtulach@1334
   504
     * @return {@code true} if this {@code TimeZone} uses Daylight Saving Time,
jtulach@1334
   505
     *         {@code false}, otherwise.
jtulach@1334
   506
     * @see #inDaylightTime(Date)
jtulach@1334
   507
     * @see Calendar#DST_OFFSET
jtulach@1334
   508
     */
jtulach@1334
   509
    public abstract boolean useDaylightTime();
jtulach@1334
   510
jtulach@1334
   511
    /**
jtulach@1334
   512
     * Returns {@code true} if this {@code TimeZone} is currently in
jtulach@1334
   513
     * Daylight Saving Time, or if a transition from Standard Time to
jtulach@1334
   514
     * Daylight Saving Time occurs at any future time.
jtulach@1334
   515
     *
jtulach@1334
   516
     * <p>The default implementation returns {@code true} if
jtulach@1334
   517
     * {@code useDaylightTime()} or {@code inDaylightTime(new Date())}
jtulach@1334
   518
     * returns {@code true}.
jtulach@1334
   519
     *
jtulach@1334
   520
     * @return {@code true} if this {@code TimeZone} is currently in
jtulach@1334
   521
     * Daylight Saving Time, or if a transition from Standard Time to
jtulach@1334
   522
     * Daylight Saving Time occurs at any future time; {@code false}
jtulach@1334
   523
     * otherwise.
jtulach@1334
   524
     * @since 1.7
jtulach@1334
   525
     * @see #useDaylightTime()
jtulach@1334
   526
     * @see #inDaylightTime(Date)
jtulach@1334
   527
     * @see Calendar#DST_OFFSET
jtulach@1334
   528
     */
jtulach@1334
   529
    public boolean observesDaylightTime() {
jtulach@1334
   530
        return useDaylightTime() || inDaylightTime(new Date());
jtulach@1334
   531
    }
jtulach@1334
   532
jtulach@1334
   533
    /**
jtulach@1334
   534
     * Queries if the given {@code date} is in Daylight Saving Time in
jtulach@1334
   535
     * this time zone.
jtulach@1334
   536
     *
jtulach@1334
   537
     * @param date the given Date.
jtulach@1334
   538
     * @return {@code true} if the given date is in Daylight Saving Time,
jtulach@1334
   539
     *         {@code false}, otherwise.
jtulach@1334
   540
     */
jtulach@1334
   541
    abstract public boolean inDaylightTime(Date date);
jtulach@1334
   542
jtulach@1334
   543
    /**
jtulach@1334
   544
     * Gets the <code>TimeZone</code> for the given ID.
jtulach@1334
   545
     *
jtulach@1334
   546
     * @param ID the ID for a <code>TimeZone</code>, either an abbreviation
jtulach@1334
   547
     * such as "PST", a full name such as "America/Los_Angeles", or a custom
jtulach@1334
   548
     * ID such as "GMT-8:00". Note that the support of abbreviations is
jtulach@1334
   549
     * for JDK 1.1.x compatibility only and full names should be used.
jtulach@1334
   550
     *
jtulach@1334
   551
     * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
jtulach@1334
   552
     * cannot be understood.
jtulach@1334
   553
     */
jtulach@1334
   554
    public static synchronized TimeZone getTimeZone(String ID) {
jtulach@1334
   555
        return getTimeZone(ID, true);
jtulach@1334
   556
    }
jtulach@1334
   557
jtulach@1334
   558
    private static TimeZone getTimeZone(String ID, boolean fallback) {
jtulach@1334
   559
        TimeZone tz = ZoneInfo.getTimeZone(ID);
jtulach@1334
   560
        if (tz == null) {
jtulach@1334
   561
            tz = parseCustomTimeZone(ID);
jtulach@1334
   562
            if (tz == null && fallback) {
jtulach@1334
   563
                tz = new ZoneInfo(GMT_ID, 0);
jtulach@1334
   564
            }
jtulach@1334
   565
        }
jtulach@1334
   566
        return tz;
jtulach@1334
   567
    }
jtulach@1334
   568
jtulach@1334
   569
    /**
jtulach@1334
   570
     * Gets the available IDs according to the given time zone offset in milliseconds.
jtulach@1334
   571
     *
jtulach@1334
   572
     * @param rawOffset the given time zone GMT offset in milliseconds.
jtulach@1334
   573
     * @return an array of IDs, where the time zone for that ID has
jtulach@1334
   574
     * the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
jtulach@1334
   575
     * both have GMT-07:00, but differ in daylight saving behavior.
jtulach@1334
   576
     * @see #getRawOffset()
jtulach@1334
   577
     */
jtulach@1334
   578
    public static synchronized String[] getAvailableIDs(int rawOffset) {
jtulach@1334
   579
        return ZoneInfo.getAvailableIDs(rawOffset);
jtulach@1334
   580
    }
jtulach@1334
   581
jtulach@1334
   582
    /**
jtulach@1334
   583
     * Gets all the available IDs supported.
jtulach@1334
   584
     * @return an array of IDs.
jtulach@1334
   585
     */
jtulach@1334
   586
    public static synchronized String[] getAvailableIDs() {
jtulach@1334
   587
        return ZoneInfo.getAvailableIDs();
jtulach@1334
   588
    }
jtulach@1334
   589
jtulach@1334
   590
    /**
jtulach@1334
   591
     * Gets the platform defined TimeZone ID.
jtulach@1334
   592
     **/
jtulach@1334
   593
    private static native String getSystemTimeZoneID(String javaHome,
jtulach@1334
   594
                                                     String country);
jtulach@1334
   595
jtulach@1334
   596
    /**
jtulach@1334
   597
     * Gets the custom time zone ID based on the GMT offset of the
jtulach@1334
   598
     * platform. (e.g., "GMT+08:00")
jtulach@1334
   599
     */
jtulach@1334
   600
    private static native String getSystemGMTOffsetID();
jtulach@1334
   601
jtulach@1334
   602
    /**
jtulach@1334
   603
     * Gets the default <code>TimeZone</code> for this host.
jtulach@1334
   604
     * The source of the default <code>TimeZone</code>
jtulach@1334
   605
     * may vary with implementation.
jtulach@1334
   606
     * @return a default <code>TimeZone</code>.
jtulach@1334
   607
     * @see #setDefault
jtulach@1334
   608
     */
jtulach@1334
   609
    public static TimeZone getDefault() {
jtulach@1334
   610
        return (TimeZone) getDefaultRef().clone();
jtulach@1334
   611
    }
jtulach@1334
   612
jtulach@1334
   613
    /**
jtulach@1334
   614
     * Returns the reference to the default TimeZone object. This
jtulach@1334
   615
     * method doesn't create a clone.
jtulach@1334
   616
     */
jtulach@1334
   617
    static TimeZone getDefaultRef() {
jtulach@1334
   618
        TimeZone defaultZone = defaultZoneTL.get();
jtulach@1334
   619
        if (defaultZone == null) {
jtulach@1334
   620
            defaultZone = defaultTimeZone;
jtulach@1334
   621
            if (defaultZone == null) {
jtulach@1334
   622
                // Need to initialize the default time zone.
jtulach@1334
   623
                defaultZone = setDefaultZone();
jtulach@1334
   624
                assert defaultZone != null;
jtulach@1334
   625
            }
jtulach@1334
   626
        }
jtulach@1334
   627
        // Don't clone here.
jtulach@1334
   628
        return defaultZone;
jtulach@1334
   629
    }
jtulach@1334
   630
jtulach@1334
   631
    private static synchronized TimeZone setDefaultZone() {
jtulach@1334
   632
        TimeZone tz = null;
jtulach@1334
   633
        // get the time zone ID from the system properties
jtulach@1334
   634
        String zoneID = AccessController.doPrivileged(
jtulach@1334
   635
                new GetPropertyAction("user.timezone"));
jtulach@1334
   636
jtulach@1334
   637
        // if the time zone ID is not set (yet), perform the
jtulach@1334
   638
        // platform to Java time zone ID mapping.
jtulach@1334
   639
        if (zoneID == null || zoneID.equals("")) {
jtulach@1334
   640
            String country = AccessController.doPrivileged(
jtulach@1334
   641
                    new GetPropertyAction("user.country"));
jtulach@1334
   642
            String javaHome = AccessController.doPrivileged(
jtulach@1334
   643
                    new GetPropertyAction("java.home"));
jtulach@1334
   644
            try {
jtulach@1334
   645
                zoneID = getSystemTimeZoneID(javaHome, country);
jtulach@1334
   646
                if (zoneID == null) {
jtulach@1334
   647
                    zoneID = GMT_ID;
jtulach@1334
   648
                }
jtulach@1334
   649
            } catch (NullPointerException e) {
jtulach@1334
   650
                zoneID = GMT_ID;
jtulach@1334
   651
            }
jtulach@1334
   652
        }
jtulach@1334
   653
jtulach@1334
   654
        // Get the time zone for zoneID. But not fall back to
jtulach@1334
   655
        // "GMT" here.
jtulach@1334
   656
        tz = getTimeZone(zoneID, false);
jtulach@1334
   657
jtulach@1334
   658
        if (tz == null) {
jtulach@1334
   659
            // If the given zone ID is unknown in Java, try to
jtulach@1334
   660
            // get the GMT-offset-based time zone ID,
jtulach@1334
   661
            // a.k.a. custom time zone ID (e.g., "GMT-08:00").
jtulach@1334
   662
            String gmtOffsetID = getSystemGMTOffsetID();
jtulach@1334
   663
            if (gmtOffsetID != null) {
jtulach@1334
   664
                zoneID = gmtOffsetID;
jtulach@1334
   665
            }
jtulach@1334
   666
            tz = getTimeZone(zoneID, true);
jtulach@1334
   667
        }
jtulach@1334
   668
        assert tz != null;
jtulach@1334
   669
jtulach@1334
   670
        final String id = zoneID;
jtulach@1334
   671
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
jtulach@1334
   672
                public Object run() {
jtulach@1334
   673
                    System.setProperty("user.timezone", id);
jtulach@1334
   674
                    return null;
jtulach@1334
   675
                }
jtulach@1334
   676
            });
jtulach@1334
   677
jtulach@1334
   678
        defaultTimeZone = tz;
jtulach@1334
   679
        return tz;
jtulach@1334
   680
    }
jtulach@1334
   681
jtulach@1334
   682
    private static boolean hasPermission() {
jtulach@1334
   683
        boolean hasPermission = true;
jtulach@1334
   684
        SecurityManager sm = System.getSecurityManager();
jtulach@1334
   685
        if (sm != null) {
jtulach@1334
   686
            try {
jtulach@1334
   687
                sm.checkPermission(new PropertyPermission
jtulach@1334
   688
                                   ("user.timezone", "write"));
jtulach@1334
   689
            } catch (SecurityException e) {
jtulach@1334
   690
                hasPermission = false;
jtulach@1334
   691
            }
jtulach@1334
   692
        }
jtulach@1334
   693
        return hasPermission;
jtulach@1334
   694
    }
jtulach@1334
   695
jtulach@1334
   696
    /**
jtulach@1334
   697
     * Sets the <code>TimeZone</code> that is
jtulach@1334
   698
     * returned by the <code>getDefault</code> method.  If <code>zone</code>
jtulach@1334
   699
     * is null, reset the default to the value it had originally when the
jtulach@1334
   700
     * VM first started.
jtulach@1334
   701
     * @param zone the new default time zone
jtulach@1334
   702
     * @see #getDefault
jtulach@1334
   703
     */
jtulach@1334
   704
    public static void setDefault(TimeZone zone)
jtulach@1334
   705
    {
jtulach@1334
   706
        if (hasPermission()) {
jtulach@1334
   707
            synchronized (TimeZone.class) {
jtulach@1334
   708
                defaultTimeZone = zone;
jtulach@1334
   709
                defaultZoneTL.set(null);
jtulach@1334
   710
            }
jtulach@1334
   711
        } else {
jtulach@1334
   712
            defaultZoneTL.set(zone);
jtulach@1334
   713
        }
jtulach@1334
   714
    }
jtulach@1334
   715
jtulach@1334
   716
    /**
jtulach@1334
   717
     * Returns true if this zone has the same rule and offset as another zone.
jtulach@1334
   718
     * That is, if this zone differs only in ID, if at all.  Returns false
jtulach@1334
   719
     * if the other zone is null.
jtulach@1334
   720
     * @param other the <code>TimeZone</code> object to be compared with
jtulach@1334
   721
     * @return true if the other zone is not null and is the same as this one,
jtulach@1334
   722
     * with the possible exception of the ID
jtulach@1334
   723
     * @since 1.2
jtulach@1334
   724
     */
jtulach@1334
   725
    public boolean hasSameRules(TimeZone other) {
jtulach@1334
   726
        return other != null && getRawOffset() == other.getRawOffset() &&
jtulach@1334
   727
            useDaylightTime() == other.useDaylightTime();
jtulach@1334
   728
    }
jtulach@1334
   729
jtulach@1334
   730
    /**
jtulach@1334
   731
     * Creates a copy of this <code>TimeZone</code>.
jtulach@1334
   732
     *
jtulach@1334
   733
     * @return a clone of this <code>TimeZone</code>
jtulach@1334
   734
     */
jtulach@1334
   735
    public Object clone()
jtulach@1334
   736
    {
jtulach@1334
   737
        try {
jtulach@1334
   738
            TimeZone other = (TimeZone) super.clone();
jtulach@1334
   739
            other.ID = ID;
jtulach@1334
   740
            return other;
jtulach@1334
   741
        } catch (CloneNotSupportedException e) {
jtulach@1334
   742
            throw new InternalError();
jtulach@1334
   743
        }
jtulach@1334
   744
    }
jtulach@1334
   745
jtulach@1334
   746
    /**
jtulach@1334
   747
     * The null constant as a TimeZone.
jtulach@1334
   748
     */
jtulach@1334
   749
    static final TimeZone NO_TIMEZONE = null;
jtulach@1334
   750
jtulach@1334
   751
    // =======================privates===============================
jtulach@1334
   752
jtulach@1334
   753
    /**
jtulach@1334
   754
     * The string identifier of this <code>TimeZone</code>.  This is a
jtulach@1334
   755
     * programmatic identifier used internally to look up <code>TimeZone</code>
jtulach@1334
   756
     * objects from the system table and also to map them to their localized
jtulach@1334
   757
     * display names.  <code>ID</code> values are unique in the system
jtulach@1334
   758
     * table but may not be for dynamically created zones.
jtulach@1334
   759
     * @serial
jtulach@1334
   760
     */
jtulach@1334
   761
    private String           ID;
jtulach@1334
   762
    private static volatile TimeZone defaultTimeZone;
jtulach@1334
   763
    private static final InheritableThreadLocal<TimeZone> defaultZoneTL
jtulach@1334
   764
                                        = new InheritableThreadLocal<TimeZone>();
jtulach@1334
   765
jtulach@1334
   766
    static final String         GMT_ID        = "GMT";
jtulach@1334
   767
    private static final int    GMT_ID_LENGTH = 3;
jtulach@1334
   768
jtulach@1334
   769
    /**
jtulach@1334
   770
     * Parses a custom time zone identifier and returns a corresponding zone.
jtulach@1334
   771
     * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
jtulach@1334
   772
     *
jtulach@1334
   773
     * @param id a string of the <a href="#CustomID">custom ID form</a>.
jtulach@1334
   774
     * @return a newly created TimeZone with the given offset and
jtulach@1334
   775
     * no daylight saving time, or null if the id cannot be parsed.
jtulach@1334
   776
     */
jtulach@1334
   777
    private static final TimeZone parseCustomTimeZone(String id) {
jtulach@1334
   778
        int length;
jtulach@1334
   779
jtulach@1334
   780
        // Error if the length of id isn't long enough or id doesn't
jtulach@1334
   781
        // start with "GMT".
jtulach@1334
   782
        if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
jtulach@1334
   783
            id.indexOf(GMT_ID) != 0) {
jtulach@1334
   784
            return null;
jtulach@1334
   785
        }
jtulach@1334
   786
jtulach@1334
   787
        ZoneInfo zi;
jtulach@1334
   788
jtulach@1334
   789
        // First, we try to find it in the cache with the given
jtulach@1334
   790
        // id. Even the id is not normalized, the returned ZoneInfo
jtulach@1334
   791
        // should have its normalized id.
jtulach@1334
   792
        zi = ZoneInfoFile.getZoneInfo(id);
jtulach@1334
   793
        if (zi != null) {
jtulach@1334
   794
            return zi;
jtulach@1334
   795
        }
jtulach@1334
   796
jtulach@1334
   797
        int index = GMT_ID_LENGTH;
jtulach@1334
   798
        boolean negative = false;
jtulach@1334
   799
        char c = id.charAt(index++);
jtulach@1334
   800
        if (c == '-') {
jtulach@1334
   801
            negative = true;
jtulach@1334
   802
        } else if (c != '+') {
jtulach@1334
   803
            return null;
jtulach@1334
   804
        }
jtulach@1334
   805
jtulach@1334
   806
        int hours = 0;
jtulach@1334
   807
        int num = 0;
jtulach@1334
   808
        int countDelim = 0;
jtulach@1334
   809
        int len = 0;
jtulach@1334
   810
        while (index < length) {
jtulach@1334
   811
            c = id.charAt(index++);
jtulach@1334
   812
            if (c == ':') {
jtulach@1334
   813
                if (countDelim > 0) {
jtulach@1334
   814
                    return null;
jtulach@1334
   815
                }
jtulach@1334
   816
                if (len > 2) {
jtulach@1334
   817
                    return null;
jtulach@1334
   818
                }
jtulach@1334
   819
                hours = num;
jtulach@1334
   820
                countDelim++;
jtulach@1334
   821
                num = 0;
jtulach@1334
   822
                len = 0;
jtulach@1334
   823
                continue;
jtulach@1334
   824
            }
jtulach@1334
   825
            if (c < '0' || c > '9') {
jtulach@1334
   826
                return null;
jtulach@1334
   827
            }
jtulach@1334
   828
            num = num * 10 + (c - '0');
jtulach@1334
   829
            len++;
jtulach@1334
   830
        }
jtulach@1334
   831
        if (index != length) {
jtulach@1334
   832
            return null;
jtulach@1334
   833
        }
jtulach@1334
   834
        if (countDelim == 0) {
jtulach@1334
   835
            if (len <= 2) {
jtulach@1334
   836
                hours = num;
jtulach@1334
   837
                num = 0;
jtulach@1334
   838
            } else {
jtulach@1334
   839
                hours = num / 100;
jtulach@1334
   840
                num %= 100;
jtulach@1334
   841
            }
jtulach@1334
   842
        } else {
jtulach@1334
   843
            if (len != 2) {
jtulach@1334
   844
                return null;
jtulach@1334
   845
            }
jtulach@1334
   846
        }
jtulach@1334
   847
        if (hours > 23 || num > 59) {
jtulach@1334
   848
            return null;
jtulach@1334
   849
        }
jtulach@1334
   850
        int gmtOffset =  (hours * 60 + num) * 60 * 1000;
jtulach@1334
   851
jtulach@1334
   852
        if (gmtOffset == 0) {
jtulach@1334
   853
            zi = ZoneInfoFile.getZoneInfo(GMT_ID);
jtulach@1334
   854
            if (negative) {
jtulach@1334
   855
                zi.setID("GMT-00:00");
jtulach@1334
   856
            } else {
jtulach@1334
   857
                zi.setID("GMT+00:00");
jtulach@1334
   858
            }
jtulach@1334
   859
        } else {
jtulach@1334
   860
            zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
jtulach@1334
   861
        }
jtulach@1334
   862
        return zi;
jtulach@1334
   863
    }
jtulach@1334
   864
}