jdk/src/share/classes/sun/util/calendar/ZoneInfo.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1315 ad5950a1098c
permissions -rw-r--r--
Initial load
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 2000-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
package sun.util.calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.SimpleTimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>ZoneInfo</code> is an implementation subclass of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * java.util.TimeZone TimeZone} that represents GMT offsets and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * daylight saving time transitions of a time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The daylight saving time transitions are described in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * #transitions transitions} table consisting of a chronological
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * sequence of transitions of GMT offset and/or daylight saving time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * changes. Since all transitions are represented in UTC, in theory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>ZoneInfo</code> can be used with any calendar systems except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * for the {@link #getOffset(int,int,int,int,int,int) getOffset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * method that takes Gregorian calendar date fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This table covers transitions from 1900 until 2037 (as of version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * 1.4), Before 1900, it assumes that there was no daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * time and the <code>getOffset</code> methods always return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link #getRawOffset} value. No Local Mean Time is supported. If a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * specified date is beyond the transition table and this time zone is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * supposed to observe daylight saving time in 2037, it delegates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * operations to a {@link java.util.SimpleTimeZone SimpleTimeZone}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * object created using the daylight saving time schedule as of 2037.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * The date items, transitions, GMT offset(s), etc. are read from a database
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * file. See {@link ZoneInfoFile} for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see java.util.SimpleTimeZone
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class ZoneInfo extends TimeZone {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int UTC_TIME = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int STANDARD_TIME = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int WALL_TIME = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final long OFFSET_MASK = 0x0fL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final long DST_MASK = 0xf0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int DST_NSHIFT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // this bit field is reserved for abbreviation support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final long ABBR_MASK = 0xf00L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int TRANSITION_NSHIFT = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final CalendarSystem gcal = CalendarSystem.getGregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * The raw GMT offset in milliseconds between this zone and GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Negative offsets are to the west of Greenwich.  To obtain local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <em>standard</em> time, add the offset to GMT time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private int rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Difference in milliseconds from the original GMT offset in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * the raw offset value has been modified by calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * #setRawOffset}. The initial value is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private int rawOffsetDiff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * A CRC32 value of all pairs of transition time (in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * in <code>long</code>) in local time and its GMT offset (in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * seconds in <code>int</code>) in the chronological order. Byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * values of each <code>long</code> and <code>int</code> are taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * in the big endian order (i.e., MSB to LSB).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private int checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The amount of time in milliseconds saved during daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * time. If <code>useDaylight</code> is false, this value is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private int dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * This array describes transitions of GMT offsets of this time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * zone, including both raw offset changes and daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * time changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * A long integer consists of four bit fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <li>The most significant 52-bit field represents transition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * time in milliseconds from Gregorian January 1 1970, 00:00:00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * GMT.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <li>The next 4-bit field is reserved and must be 0.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <li>The next 4-bit field is an index value to {@link #offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * offsets[]} for the amount of daylight saving at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * transition. If this value is zero, it means that no daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * saving, not the index value zero.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <li>The least significant 4-bit field is an index value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * {@link #offsets offsets[]} for <em>total</em> GMT offset at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * transition.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * If this time zone doesn't observe daylight saving time and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * never changed any GMT offsets in the past, this value is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private long[] transitions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * This array holds all unique offset values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * milliseconds. Index values to this array are stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * transitions array elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private int[] offsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * SimpleTimeZone parameter values. It has to have either 8 for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * {@link java.util.SimpleTimeZone#SimpleTimeZone(int, String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * int, int , int , int , int , int , int , int , int) the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * 11-argument SimpleTimeZone constructor} or 10 for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * java.util.SimpleTimeZone#SimpleTimeZone(int, String, int, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * int , int , int , int , int , int , int, int, int) the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * 13-argument SimpleTimeZone constructor} parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private int[] simpleTimeZoneParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * True if the raw GMT offset value would change after the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * zone data has been generated; false, otherwise. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * value is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private boolean willGMTOffsetChange = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * True if the object has been modified after its instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    transient private boolean dirty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final long serialVersionUID = 2653134537216586139L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * A constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public ZoneInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * A Constructor for CustomID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public ZoneInfo(String ID, int rawOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this(ID, rawOffset, 0, 0, null, null, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Constructs a ZoneInfo instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param ID time zone name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param rawOffset GMT offset in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param dstSavings daylight saving value in milliseconds or 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * (zero) if this time zone doesn't observe Daylight Saving Time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param checksum CRC32 value with all transitions table entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param transitions transition table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param offsets offset value table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param simpleTimeZoneParams parameter values for constructing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * SimpleTimeZone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param willGMTOffsetChange the value of willGMTOffsetChange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    ZoneInfo(String ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
             int rawOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
             int dstSavings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
             int checksum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
             long[] transitions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
             int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
             int[] simpleTimeZoneParams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
             boolean willGMTOffsetChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        setID(ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        this.rawOffset = rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this.dstSavings = dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.checksum = checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this.transitions = transitions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.offsets = offsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        this.simpleTimeZoneParams = simpleTimeZoneParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this.willGMTOffsetChange = willGMTOffsetChange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Returns the difference in milliseconds between local time and UTC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * of given time, taking into account both the raw offset and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * effect of daylight savings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param date the milliseconds in UTC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return the milliseconds to add to UTC to get local wall time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public int getOffset(long date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return getOffsets(date, null, UTC_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public int getOffsets(long utc, int[] offsets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return getOffsets(utc, offsets, UTC_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public int getOffsetsByStandard(long standard, int[] offsets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return getOffsets(standard, offsets, STANDARD_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public int getOffsetsByWall(long wall, int[] offsets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return getOffsets(wall, offsets, WALL_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private int getOffsets(long date, int[] offsets, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // if dst is never observed, there is no transition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (transitions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            int offset = getLastRawOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (offsets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                offsets[0] = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                offsets[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        date -= rawOffsetDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int index = getTransitionIndex(date, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // prior to the transition table, returns the raw offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // should support LMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int offset = getLastRawOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (offsets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                offsets[0] = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                offsets[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (index < transitions.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            long val = transitions[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            int offset = this.offsets[(int)(val & OFFSET_MASK)] + rawOffsetDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (offsets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                int dst = (int)((val >>> DST_NSHIFT) & 0xfL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                int save = (dst == 0) ? 0 : this.offsets[dst];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                offsets[0] = offset - save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                offsets[1] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // beyond the transitions, delegate to SimpleTimeZone if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // is a rule; otherwise, return rawOffset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        SimpleTimeZone tz = getLastRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (tz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            int rawoffset = tz.getRawOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            long msec = date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (type != UTC_TIME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                msec -= rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            int dstoffset = tz.inDaylightTime(new Date(msec)) ? tz.getDSTSavings() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (offsets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                offsets[0] = rawoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                offsets[1] = dstoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return rawoffset + dstoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int offset = getLastRawOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (offsets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            offsets[0] = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            offsets[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private final int getTransitionIndex(long date, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int high = transitions.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            int mid = (low + high) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            long val = transitions[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            long midVal = val >> TRANSITION_NSHIFT; // sign extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (type != UTC_TIME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                midVal += offsets[(int)(val & OFFSET_MASK)]; // wall time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (type == STANDARD_TIME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                int dstIndex = (int)((val >>> DST_NSHIFT) & 0xfL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (dstIndex != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    midVal -= offsets[dstIndex]; // make it standard time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (midVal < date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else if (midVal > date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // if beyond the transitions, returns that index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (low >= transitions.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return low - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Returns the difference in milliseconds between local time and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * UTC, taking into account both the raw offset and the effect of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * daylight savings, for the specified date and time.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * assumes that the start and end month are distinct.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * assumes a Gregorian calendar for calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <em>Note: In general, clients should use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * {@link Calendar#ZONE_OFFSET Calendar.get(ZONE_OFFSET)} +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * {@link Calendar#DST_OFFSET Calendar.get(DST_OFFSET)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * instead of calling this method.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param era       The era of the given date. The value must be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *                  GregorianCalendar.AD or GregorianCalendar.BC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param year      The year in the given date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param month     The month in the given date. Month is 0-based. e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *                  0 for January.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param day       The day-in-month of the given date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param dayOfWeek The day-of-week of the given date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param millis    The milliseconds in day in <em>standard</em> local time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return The milliseconds to add to UTC to get local time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public int getOffset(int era, int year, int month, int day,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                         int dayOfWeek, int milliseconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (milliseconds < 0 || milliseconds >= AbstractCalendar.DAY_IN_MILLIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (era == java.util.GregorianCalendar.BC) { // BC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            year = 1 - year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        } else if (era != java.util.GregorianCalendar.AD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        CalendarDate date = gcal.newCalendarDate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        date.setDate(year, month + 1, day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (gcal.validate(date) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // bug-for-bug compatible argument checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (dayOfWeek < java.util.GregorianCalendar.SUNDAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            || dayOfWeek > java.util.GregorianCalendar.SATURDAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (transitions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return getLastRawOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        long dateInMillis = gcal.getTime(date) + milliseconds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        dateInMillis -= (long) rawOffset; // make it UTC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return getOffsets(dateInMillis, null, UTC_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Sets the base time zone offset from GMT. This operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * modifies all the transitions of this ZoneInfo object, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * historical ones, if applicable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param offsetMillis the base time zone offset to GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @see getRawOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public synchronized void setRawOffset(int offsetMillis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (offsetMillis == rawOffset + rawOffsetDiff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        rawOffsetDiff = offsetMillis - rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (lastRule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            lastRule.setRawOffset(offsetMillis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        dirty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Returns the GMT offset of the current date. This GMT offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * value is not modified during Daylight Saving Time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return the GMT offset value in milliseconds to add to UTC time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * to get local standard time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public int getRawOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (!willGMTOffsetChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return rawOffset + rawOffsetDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int[] offsets = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        getOffsets(System.currentTimeMillis(), offsets, UTC_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return offsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public boolean isDirty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return dirty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private int getLastRawOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return rawOffset + rawOffsetDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Queries if this time zone uses Daylight Saving Time in the last known rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public boolean useDaylightTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return (simpleTimeZoneParams != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Queries if the specified date is in Daylight Saving Time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public boolean inDaylightTime(Date date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (transitions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        long utc = date.getTime() - rawOffsetDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        int index = getTransitionIndex(utc, UTC_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // before transitions in the transition table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // the time is in the table range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (index < transitions.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return (transitions[index] & DST_MASK) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // beyond the transition table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        SimpleTimeZone tz = getLastRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (tz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return tz.inDaylightTime(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Returns the amount of time in milliseconds that the clock is advanced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * during daylight saving time is in effect in its last daylight saving time rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @return the number of milliseconds the time is advanced with respect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * standard time when daylight saving time is in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public int getDSTSavings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
//    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
//     * @return the last year in the transition table or -1 if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
//     * time zone doesn't observe any daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
//     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
//    public int getMaxTransitionYear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
//      if (transitions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
//          return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
//      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
//      long val = transitions[transitions.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
//      int offset = this.offsets[(int)(val & OFFSET_MASK)] + rawOffsetDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
//      val = (val >> TRANSITION_NSHIFT) + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
//      CalendarDate lastDate = Gregorian.getCalendarDate(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
//      return lastDate.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Returns a string representation of this time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            "[id=\"" + getID() + "\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            ",offset=" + getLastRawOffset() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            ",dstSavings=" + dstSavings +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            ",useDaylight=" + useDaylightTime() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            ",transitions=" + ((transitions != null) ? transitions.length : 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            ",lastRule=" + (lastRule == null ? getLastRuleInstance() : lastRule) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Gets all available IDs supported in the Java run-time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @return an array of time zone IDs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public static String[] getAvailableIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        List<String> idList = ZoneInfoFile.getZoneIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        List<String> excluded = ZoneInfoFile.getExcludedZones();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (excluded != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            // List all zones from the idList and excluded lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            List<String> list = new ArrayList<String>(idList.size() + excluded.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            list.addAll(idList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            list.addAll(excluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            idList = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        String[] ids = new String[idList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return idList.toArray(ids);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Gets all available IDs that have the same value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * specified raw GMT offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param rawOffset the GMT offset in milliseconds. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * value should not include any daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @return an array of time zone IDs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public static String[] getAvailableIDs(int rawOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        String[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        List<String> matched = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        List<String> IDs = ZoneInfoFile.getZoneIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        int[] rawOffsets = ZoneInfoFile.getRawOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        for (int index = 0; index < rawOffsets.length; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (rawOffsets[index] == rawOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                byte[] indices = ZoneInfoFile.getRawOffsetIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                for (int i = 0; i < indices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    if (indices[i] == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        matched.add(IDs.get(i++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        while (i < indices.length && indices[i] == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                            matched.add(IDs.get(i++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        break loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        // We need to add any zones from the excluded zone list that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // currently have the same GMT offset as the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // rawOffset. The zones returned by this method may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // correct as of return to the caller if any GMT offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // transition is happening during this GMT offset checking...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        List<String> excluded = ZoneInfoFile.getExcludedZones();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (excluded != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            for (String id : excluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                TimeZone zi = getTimeZone(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if (zi != null && zi.getRawOffset() == rawOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    matched.add(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        result = new String[matched.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        matched.toArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Gets the ZoneInfo for the given ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param ID the ID for a ZoneInfo. See TimeZone for detail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return the specified ZoneInfo object, or null if there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * time zone of the ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public static TimeZone getTimeZone(String ID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        ZoneInfo zi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        zi = ZoneInfoFile.getZoneInfo(ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (zi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            // if we can't create an object for the ID, try aliases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                Map<String, String> map = getAliasTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                String alias = ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                while ((alias = map.get(alias)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    zi = ZoneInfoFile.getZoneInfo(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    if (zi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        zi.setID(ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        zi = ZoneInfoFile.addToCache(ID, zi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        zi = (ZoneInfo) zi.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                // ignore exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return zi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    private transient SimpleTimeZone lastRule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Returns a SimpleTimeZone object representing the last GMT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * offset and DST schedule or null if this time zone doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * observe DST.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    private synchronized SimpleTimeZone getLastRule() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (lastRule == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            lastRule = getLastRuleInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return lastRule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Returns a SimpleTimeZone object that represents the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * known daylight saving time rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @return a SimpleTimeZone object or null if this time zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * doesn't observe DST.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public SimpleTimeZone getLastRuleInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (simpleTimeZoneParams == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (simpleTimeZoneParams.length == 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            return new SimpleTimeZone(getLastRawOffset(), getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                      simpleTimeZoneParams[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                      simpleTimeZoneParams[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                      simpleTimeZoneParams[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                      simpleTimeZoneParams[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                      simpleTimeZoneParams[4],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                      simpleTimeZoneParams[5],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                      simpleTimeZoneParams[6],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                      simpleTimeZoneParams[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                      simpleTimeZoneParams[8],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                      simpleTimeZoneParams[9],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                      dstSavings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        return new SimpleTimeZone(getLastRawOffset(), getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                                  simpleTimeZoneParams[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                                  simpleTimeZoneParams[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                  simpleTimeZoneParams[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                                  simpleTimeZoneParams[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                                  simpleTimeZoneParams[4],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                                  simpleTimeZoneParams[5],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                                  simpleTimeZoneParams[6],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                                  simpleTimeZoneParams[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                                  dstSavings);
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
     * Returns a copy of this <code>ZoneInfo</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        ZoneInfo zi = (ZoneInfo) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        zi.lastRule = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        return zi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Returns a hash code value calculated from the GMT offset and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * transitions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @return a hash code of this time zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return getLastRawOffset() ^ checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Compares the equity of two ZoneInfo objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param obj the object to be compared with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @return true if given object is same as this ZoneInfo object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (!(obj instanceof ZoneInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        ZoneInfo that = (ZoneInfo) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        return (getID().equals(that.getID())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                && (getLastRawOffset() == that.getLastRawOffset())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                && (checksum == that.checksum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Returns true if this zone has the same raw GMT offset value and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * transition table as another zone info. If the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * TimeZone object is not a ZoneInfo instance, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * true if the specified TimeZone object has the same raw GMT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * offset value with no daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param other the ZoneInfo object to be compared with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @return true if the given <code>TimeZone</code> has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * GMT offset and transition information; false, otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public boolean hasSameRules(TimeZone other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (!(other instanceof ZoneInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            if (getRawOffset() != other.getRawOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            // if both have the same raw offset and neither observes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            // DST, they have the same rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            if ((transitions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                && (useDaylightTime() == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                && (other.useDaylightTime() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        if (getLastRawOffset() != ((ZoneInfo)other).getLastRawOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return (checksum == ((ZoneInfo)other).checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    private static SoftReference<Map> aliasTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Returns a Map from alias time zone IDs to their standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * time zone IDs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @return the Map that holds the mappings from alias time zone IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *    to their standard time zone IDs, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *    <code>ZoneInfoMappings</code> file is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public synchronized static Map<String, String> getAliasTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        Map<String, String> aliases = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        SoftReference<Map> cache = aliasTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            aliases = cache.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (aliases != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                return aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        aliases = ZoneInfoFile.getZoneAliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (aliases != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            aliasTable = new SoftReference<Map>(aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    private void readObject(ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        // We don't know how this object from 1.4.x or earlier has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // been mutated. So it should always be marked as `dirty'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        dirty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
}