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