jdk/src/java.base/share/classes/java/util/SimpleTimeZone.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8375
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3738
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3738
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3738
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3738
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3738
diff changeset
    23
 * questions.
2
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.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.util.calendar.CalendarSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.util.calendar.CalendarUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.util.calendar.BaseCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.util.calendar.Gregorian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>SimpleTimeZone</code> is a concrete subclass of <code>TimeZone</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * that represents a time zone for use with a Gregorian calendar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The class holds an offset from GMT, called <em>raw offset</em>, and start
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * and end rules for a daylight saving time schedule.  Since it only holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * single values for each, it cannot handle historical changes in the offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * from GMT and the daylight saving schedule, except that the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * #setStartYear setStartYear} method can specify the year when the daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * saving time schedule starts in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * To construct a <code>SimpleTimeZone</code> with a daylight saving time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * schedule, the schedule can be described with a set of rules,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <em>start-rule</em> and <em>end-rule</em>. A day when daylight saving time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * starts or ends is specified by a combination of <em>month</em>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <em>day-of-month</em>, and <em>day-of-week</em> values. The <em>month</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * value is represented by a Calendar {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * value, such as {@link Calendar#MARCH}. The <em>day-of-week</em> value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * represented by a Calendar {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * such as {@link Calendar#SUNDAY SUNDAY}. The meanings of value combinations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * are as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <li><b>Exact day of month</b><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * To specify an exact day of month, set the <em>month</em> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <em>day-of-month</em> to an exact value, and <em>day-of-week</em> to zero. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * example, to specify March 1, set the <em>month</em> to {@link Calendar#MARCH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * MARCH}, <em>day-of-month</em> to 1, and <em>day-of-week</em> to 0.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <li><b>Day of week on or after day of month</b><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * To specify a day of week on or after an exact day of month, set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <em>month</em> to an exact month value, <em>day-of-month</em> to the day on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * or after which the rule is applied, and <em>day-of-week</em> to a negative {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Calendar#DAY_OF_WEEK DAY_OF_WEEK} field value. For example, to specify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * second Sunday of April, set <em>month</em> to {@link Calendar#APRIL APRIL},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <em>day-of-month</em> to 8, and <em>day-of-week</em> to <code>-</code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Calendar#SUNDAY SUNDAY}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <li><b>Day of week on or before day of month</b><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * To specify a day of the week on or before an exact day of the month, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <em>day-of-month</em> and <em>day-of-week</em> to a negative value. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * example, to specify the last Wednesday on or before the 21st of March, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <em>month</em> to {@link Calendar#MARCH MARCH}, <em>day-of-month</em> is -21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * and <em>day-of-week</em> is <code>-</code>{@link Calendar#WEDNESDAY WEDNESDAY}. </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <li><b>Last day-of-week of month</b><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * To specify, the last day-of-week of the month, set <em>day-of-week</em> to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value and <em>day-of-month</em> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * -1. For example, to specify the last Sunday of October, set <em>month</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * to {@link Calendar#OCTOBER OCTOBER}, <em>day-of-week</em> to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * Calendar#SUNDAY SUNDAY} and <em>day-of-month</em> to -1.  </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * The time of the day at which daylight saving time starts or ends is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * specified by a millisecond value within the day. There are three kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <em>mode</em>s to specify the time: {@link #WALL_TIME}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * #STANDARD_TIME} and {@link #UTC_TIME}. For example, if daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * saving time ends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * at 2:00 am in the wall clock time, it can be specified by 7200000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * milliseconds in the {@link #WALL_TIME} mode. In this case, the wall clock time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * for an <em>end-rule</em> means the same thing as the daylight time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * The following are examples of parameters for constructing time zone objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *      // Base GMT offset: -8:00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *      // DST starts:      at 2:00am in standard time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *      //                  on the first Sunday in April
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *      // DST ends:        at 2:00am in daylight time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *      //                  on the last Sunday in October
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *      // Save:            1 hour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *      SimpleTimeZone(-28800000,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *                     "America/Los_Angeles",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *                     Calendar.APRIL, 1, -Calendar.SUNDAY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *                     7200000,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *                     Calendar.OCTOBER, -1, Calendar.SUNDAY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *                     7200000,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *                     3600000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *      // Base GMT offset: +1:00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *      // DST starts:      at 1:00am in UTC time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *      //                  on the last Sunday in March
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *      // DST ends:        at 1:00am in UTC time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *      //                  on the last Sunday in October
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *      // Save:            1 hour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *      SimpleTimeZone(3600000,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *                     "Europe/Paris",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *                     Calendar.MARCH, -1, Calendar.SUNDAY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *                     3600000, SimpleTimeZone.UTC_TIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *                     Calendar.OCTOBER, -1, Calendar.SUNDAY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *                     3600000, SimpleTimeZone.UTC_TIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *                     3600000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * These parameter rules are also applicable to the set rule methods, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <code>setStartRule</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * @see      Calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @see      GregorianCalendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @see      TimeZone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * @author   David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
public class SimpleTimeZone extends TimeZone {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Constructs a SimpleTimeZone with the given base time zone offset from GMT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * and time zone ID with no daylight saving time schedule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param rawOffset  The base time zone offset in milliseconds to GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param ID         The time zone name that is given to this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public SimpleTimeZone(int rawOffset, String ID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.rawOffset = rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        setID (ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        dstSavings = millisPerHour; // In case user sets rules later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Constructs a SimpleTimeZone with the given base time zone offset from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * GMT, time zone ID, and rules for starting and ending the daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Both <code>startTime</code> and <code>endTime</code> are specified to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * represented in the wall clock time. The amount of daylight saving is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * assumed to be 3600000 milliseconds (i.e., one hour). This constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *     SimpleTimeZone(rawOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *                    ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *                    startMonth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *                    startDay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                    startDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *                    startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *                    SimpleTimeZone.{@link #WALL_TIME},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *                    endMonth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *                    endDay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *                    endDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *                    endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *                    SimpleTimeZone.{@link #WALL_TIME},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *                    3600000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param rawOffset       The given base time zone offset from GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param ID              The time zone ID which is given to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param startMonth      The daylight saving time starting month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *                        a {@link Calendar#MONTH MONTH} field value (0-based. e.g., 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *                        for January).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param startDay        The day of the month on which the daylight saving time starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param startDayOfWeek  The daylight saving time starting day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param startTime       The daylight saving time starting time in local wall clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *                        time (in milliseconds within the day), which is local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *                        standard time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param endMonth        The daylight saving time ending month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *                        value (0-based. e.g., 9 for October).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param endDay          The day of the month on which the daylight saving time ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param endDayOfWeek    The daylight saving time ending day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param endTime         The daylight saving ending time in local wall clock time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *                        (in milliseconds within the day) which is local daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *                        time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception IllegalArgumentException if the month, day, dayOfWeek, or time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * parameters are out of range for the start or end rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public SimpleTimeZone(int rawOffset, String ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                          int startMonth, int startDay, int startDayOfWeek, int startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                          int endMonth, int endDay, int endDayOfWeek, int endTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        this(rawOffset, ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
             millisPerHour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Constructs a SimpleTimeZone with the given base time zone offset from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * GMT, time zone ID, and rules for starting and ending the daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Both <code>startTime</code> and <code>endTime</code> are assumed to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * represented in the wall clock time. This constructor is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *     SimpleTimeZone(rawOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *                    ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *                    startMonth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *                    startDay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *                    startDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *                    startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *                    SimpleTimeZone.{@link #WALL_TIME},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *                    endMonth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *                    endDay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *                    endDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *                    endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *                    SimpleTimeZone.{@link #WALL_TIME},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *                    dstSavings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param rawOffset       The given base time zone offset from GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param ID              The time zone ID which is given to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param startMonth      The daylight saving time starting month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *                        value (0-based. e.g., 0 for January).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param startDay        The day of the month on which the daylight saving time starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param startDayOfWeek  The daylight saving time starting day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param startTime       The daylight saving time starting time in local wall clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *                        time, which is local standard time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param endMonth        The daylight saving time ending month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *                        value (0-based. e.g., 9 for October).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param endDay          The day of the month on which the daylight saving time ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param endDayOfWeek    The daylight saving time ending day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param endTime         The daylight saving ending time in local wall clock time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *                        which is local daylight time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param dstSavings      The amount of time in milliseconds saved during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *                        daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @exception IllegalArgumentException if the month, day, dayOfWeek, or time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * parameters are out of range for the start or end rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public SimpleTimeZone(int rawOffset, String ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                          int startMonth, int startDay, int startDayOfWeek, int startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                          int endMonth, int endDay, int endDayOfWeek, int endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                          int dstSavings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        this(rawOffset, ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
             dstSavings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Constructs a SimpleTimeZone with the given base time zone offset from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * GMT, time zone ID, and rules for starting and ending the daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * This constructor takes the full set of the start and end rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * parameters, including modes of <code>startTime</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <code>endTime</code>. The mode specifies either {@link #WALL_TIME wall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * time} or {@link #STANDARD_TIME standard time} or {@link #UTC_TIME UTC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * time}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param rawOffset       The given base time zone offset from GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param ID              The time zone ID which is given to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param startMonth      The daylight saving time starting month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *                        value (0-based. e.g., 0 for January).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param startDay        The day of the month on which the daylight saving time starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param startDayOfWeek  The daylight saving time starting day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param startTime       The daylight saving time starting time in the time mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *                        specified by <code>startTimeMode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param startTimeMode   The mode of the start time specified by startTime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param endMonth        The daylight saving time ending month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *                        value (0-based. e.g., 9 for October).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param endDay          The day of the month on which the daylight saving time ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param endDayOfWeek    The daylight saving time ending day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param endTime         The daylight saving ending time in time time mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *                        specified by <code>endTimeMode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param endTimeMode     The mode of the end time specified by endTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param dstSavings      The amount of time in milliseconds saved during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *                        daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @exception IllegalArgumentException if the month, day, dayOfWeek, time more, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * time parameters are out of range for the start or end rule, or if a time mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * value is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see #WALL_TIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @see #STANDARD_TIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #UTC_TIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public SimpleTimeZone(int rawOffset, String ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                          int startMonth, int startDay, int startDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                          int startTime, int startTimeMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                          int endMonth, int endDay, int endDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                          int endTime, int endTimeMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                          int dstSavings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        setID(ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        this.rawOffset      = rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        this.startMonth     = startMonth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        this.startDay       = startDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        this.startDayOfWeek = startDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        this.startTime      = startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        this.startTimeMode  = startTimeMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        this.endMonth       = endMonth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        this.endDay         = endDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        this.endDayOfWeek   = endDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        this.endTime        = endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        this.endTimeMode    = endTimeMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        this.dstSavings     = dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // this.useDaylight is set by decodeRules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        decodeRules();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (dstSavings <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new IllegalArgumentException("Illegal daylight saving value: " + dstSavings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Sets the daylight saving time starting year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param year  The daylight saving starting year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public void setStartYear(int year)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        startYear = year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        invalidateCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Sets the daylight saving time start rule. For example, if daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * time starts on the first Sunday in April at 2 am in local wall clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * time, you can set the start rule by calling:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <pre><code>setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);</code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param startMonth      The daylight saving time starting month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *                        value (0-based. e.g., 0 for January).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param startDay        The day of the month on which the daylight saving time starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @param startDayOfWeek  The daylight saving time starting day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param startTime       The daylight saving time starting time in local wall clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *                        time, which is local standard time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public void setStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        this.startMonth = startMonth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        this.startDay = startDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        this.startDayOfWeek = startDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        this.startTime = startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        startTimeMode = WALL_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        decodeStartRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        invalidateCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Sets the daylight saving time start rule to a fixed date within a month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * This method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <pre><code>setStartRule(startMonth, startDay, 0, startTime)</code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param startMonth      The daylight saving time starting month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *                        value (0-based. e.g., 0 for January).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param startDay        The day of the month on which the daylight saving time starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param startTime       The daylight saving time starting time in local wall clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *                        time, which is local standard time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @exception IllegalArgumentException if the <code>startMonth</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <code>startDayOfMonth</code>, or <code>startTime</code> parameters are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public void setStartRule(int startMonth, int startDay, int startTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        setStartRule(startMonth, startDay, 0, startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Sets the daylight saving time start rule to a weekday before or after the given date within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * a month, e.g., the first Monday on or after the 8th.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param startMonth      The daylight saving time starting month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *                        value (0-based. e.g., 0 for January).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param startDay        The day of the month on which the daylight saving time starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param startDayOfWeek  The daylight saving time starting day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param startTime       The daylight saving time starting time in local wall clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *                        time, which is local standard time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param after           If true, this rule selects the first <code>dayOfWeek</code> on or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *                        <em>after</em> <code>dayOfMonth</code>.  If false, this rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *                        selects the last <code>dayOfWeek</code> on or <em>before</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *                        <code>dayOfMonth</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public void setStartRule(int startMonth, int startDay, int startDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                             int startTime, boolean after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // TODO: this method doesn't check the initial values of dayOfMonth or dayOfWeek.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            setStartRule(startMonth, startDay, -startDayOfWeek, startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            setStartRule(startMonth, -startDay, -startDayOfWeek, startTime);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Sets the daylight saving time end rule. For example, if daylight saving time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * ends on the last Sunday in October at 2 am in wall clock time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * you can set the end rule by calling:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * <code>setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param endMonth        The daylight saving time ending month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *                        value (0-based. e.g., 9 for October).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param endDay          The day of the month on which the daylight saving time ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @param endDayOfWeek    The daylight saving time ending day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *                        See the class description for the special cases of this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param endTime         The daylight saving ending time in local wall clock time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *                        (in milliseconds within the day) which is local daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *                        time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @exception IllegalArgumentException if the <code>endMonth</code>, <code>endDay</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void setEndRule(int endMonth, int endDay, int endDayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                           int endTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        this.endMonth = endMonth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        this.endDay = endDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        this.endDayOfWeek = endDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        this.endTime = endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        this.endTimeMode = WALL_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        decodeEndRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        invalidateCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Sets the daylight saving time end rule to a fixed date within a month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * This method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * <pre><code>setEndRule(endMonth, endDay, 0, endTime)</code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param endMonth        The daylight saving time ending month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *                        value (0-based. e.g., 9 for October).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param endDay          The day of the month on which the daylight saving time ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @param endTime         The daylight saving ending time in local wall clock time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *                        (in milliseconds within the day) which is local daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *                        time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * or <code>endTime</code> parameters are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public void setEndRule(int endMonth, int endDay, int endTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        setEndRule(endMonth, endDay, 0, endTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Sets the daylight saving time end rule to a weekday before or after the given date within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * a month, e.g., the first Monday on or after the 8th.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param endMonth        The daylight saving time ending month. Month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *                        a {@link Calendar#MONTH MONTH} field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *                        value (0-based. e.g., 9 for October).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param endDay          The day of the month on which the daylight saving time ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param endDayOfWeek    The daylight saving time ending day-of-week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param endTime         The daylight saving ending time in local wall clock time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *                        (in milliseconds within the day) which is local daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *                        time in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param after           If true, this rule selects the first <code>endDayOfWeek</code> on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *                        or <em>after</em> <code>endDay</code>.  If false, this rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *                        selects the last <code>endDayOfWeek</code> on or before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *                        <code>endDay</code> of the month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public void setEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime, boolean after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            setEndRule(endMonth, endDay, -endDayOfWeek, endTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            setEndRule(endMonth, -endDay, -endDayOfWeek, endTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Returns the offset of this time zone from UTC at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * time. If daylight saving time is in effect at the given time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * the offset value is adjusted with the amount of daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * saving.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param date the time at which the time zone offset is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return the amount of time in milliseconds to add to UTC to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * local time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public int getOffset(long date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return getOffsets(date, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @see TimeZone#getOffsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    int getOffsets(long date, int[] offsets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        int offset = rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
      computeOffset:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (useDaylight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                if (cacheStart != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    if (date >= cacheStart && date < cacheEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        offset += dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        break computeOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            BaseCalendar cal = date >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                gcal : (BaseCalendar) CalendarSystem.forName("julian");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            // Get the year in local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            cal.getCalendarDate(date + rawOffset, cdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            int year = cdate.getNormalizedYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if (year >= startYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                // Clear time elements for the transition calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                cdate.setTimeOfDay(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                offset = getOffset(cal, cdate, year, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (offsets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            offsets[0] = rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            offsets[1] = offset - rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Returns the difference in milliseconds between local time and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * UTC, taking into account both the raw offset and the effect of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * daylight saving, for the specified date and time.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * assumes that the start and end month are distinct.  It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * uses a default {@link GregorianCalendar} object as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * underlying calendar, such as for determining leap years.  Do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * not use the result of this method with a calendar other than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * default <code>GregorianCalendar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * <p><em>Note:  In general, clients should use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <code>Calendar.get(ZONE_OFFSET) + Calendar.get(DST_OFFSET)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * instead of calling this method.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param era       The era of the given date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param year      The year in the given date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param month     The month in the given date. Month is 0-based. e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *                  0 for January.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param day       The day-in-month of the given date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @param dayOfWeek The day-of-week of the given date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @param millis    The milliseconds in day in <em>standard</em> local time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @return          The milliseconds to add to UTC to get local time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @exception       IllegalArgumentException the <code>era</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *                  <code>month</code>, <code>day</code>, <code>dayOfWeek</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *                  or <code>millis</code> parameters are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public int getOffset(int era, int year, int month, int day, int dayOfWeek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                         int millis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (era != GregorianCalendar.AD && era != GregorianCalendar.BC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            throw new IllegalArgumentException("Illegal era " + era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        int y = year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (era == GregorianCalendar.BC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            // adjust y with the GregorianCalendar-style year numbering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            y = 1 - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // If the year isn't representable with the 64-bit long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // integer in milliseconds, convert the year to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // equivalent year. This is required to pass some JCK test cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // which are actually useless though because the specified years
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        // can't be supported by the Java time system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (y >= 292278994) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            y = 2800 + y % 2800;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        } else if (y <= -292269054) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            // y %= 28 also produces an equivalent year, but positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // year numbers would be convenient to use the UNIX cal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            y = (int) CalendarUtils.mod((long) y, 28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // convert year to its 1-based month value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        int m = month + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        // First, calculate time as a Gregorian date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        BaseCalendar cal = gcal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        cdate.setDate(y, m, day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        long time = cal.getTime(cdate); // normalize cdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        time += millis - rawOffset; // UTC time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // If the time value represents a time before the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        // Gregorian cutover, recalculate time using the Julian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // calendar system. For the Julian calendar system, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        // normalized year numbering is ..., -2 (BCE 2), -1 (BCE 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        // 1, 2 ... which is different from the GregorianCalendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        // style year numbering (..., -1, 0 (BCE 1), 1, 2, ...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (time < GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            cal = (BaseCalendar) CalendarSystem.forName("julian");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            cdate.setNormalizedDate(y, m, day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            time = cal.getTime(cdate) + millis - rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if ((cdate.getNormalizedYear() != y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            || (cdate.getMonth() != m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            || (cdate.getDayOfMonth() != day)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            // The validation should be cdate.getDayOfWeek() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            // dayOfWeek. However, we don't check dayOfWeek for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            // compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            || (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            || (millis < 0 || millis >= (24*60*60*1000))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (!useDaylight || year < startYear || era != GregorianCalendar.CE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return getOffset(cal, cdate, y, time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    private int getOffset(BaseCalendar cal, BaseCalendar.Date cdate, int year, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (cacheStart != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                if (time >= cacheStart && time < cacheEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    return rawOffset + dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                if (year == cacheYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    return rawOffset;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        long start = getStart(cal, cdate, year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        long end = getEnd(cal, cdate, year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        int offset = rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (start <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (time >= start && time < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                offset += dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                cacheYear = year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                cacheStart = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                cacheEnd = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (time < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                // TODO: support Gregorian cutover. The previous year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                // may be in the other calendar system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                start = getStart(cal, cdate, year - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                if (time >= start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    offset += dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            } else if (time >= start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                // TODO: support Gregorian cutover. The next year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                // may be in the other calendar system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                end = getEnd(cal, cdate, year + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                if (time < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    offset += dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (start <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    // The start and end transitions are in multiple years.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    cacheYear = (long) startYear - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    cacheStart = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    cacheEnd = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    private long getStart(BaseCalendar cal, BaseCalendar.Date cdate, int year) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        int time = startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (startTimeMode != UTC_TIME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            time -= rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return getTransition(cal, cdate, startMode, year, startMonth, startDay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                             startDayOfWeek, time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    private long getEnd(BaseCalendar cal, BaseCalendar.Date cdate, int year) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        int time = endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (endTimeMode != UTC_TIME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            time -= rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (endTimeMode == WALL_TIME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            time -= dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        return getTransition(cal, cdate, endMode, year, endMonth, endDay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                        endDayOfWeek, time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    private long getTransition(BaseCalendar cal, BaseCalendar.Date cdate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                               int mode, int year, int month, int dayOfMonth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                               int dayOfWeek, int timeOfDay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        cdate.setNormalizedYear(year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        cdate.setMonth(month + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        case DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            cdate.setDayOfMonth(dayOfMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        case DOW_IN_MONTH_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            cdate.setDayOfMonth(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            if (dayOfMonth < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                cdate.setDayOfMonth(cal.getMonthLength(cdate));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(dayOfMonth, dayOfWeek, cdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        case DOW_GE_DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            cdate.setDayOfMonth(dayOfMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(1, dayOfWeek, cdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        case DOW_LE_DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            cdate.setDayOfMonth(dayOfMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(-1, dayOfWeek, cdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return cal.getTime(cdate) + timeOfDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Gets the GMT offset for this time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @return the GMT offset value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @see #setRawOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    public int getRawOffset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // The given date will be taken into account while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        // we have the historical time zone data in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        return rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Sets the base time zone offset to GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * This is the offset to add to UTC to get local time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @see #getRawOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    public void setRawOffset(int offsetMillis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        this.rawOffset = offsetMillis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * Sets the amount of time in milliseconds that the clock is advanced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * during daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @param millisSavedDuringDST the number of milliseconds the time is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * advanced with respect to standard time when the daylight saving time rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * are in effect. A positive number, typically one hour (3600000).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @see #getDSTSavings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    public void setDSTSavings(int millisSavedDuringDST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (millisSavedDuringDST <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            throw new IllegalArgumentException("Illegal daylight saving value: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                                               + millisSavedDuringDST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        dstSavings = millisSavedDuringDST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Returns the amount of time in milliseconds that the clock is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * advanced during daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @return the number of milliseconds the time is advanced with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * respect to standard time when the daylight saving rules are in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * effect, or 0 (zero) if this time zone doesn't observe daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @see #setDSTSavings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public int getDSTSavings() {
8375
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   828
        return useDaylight ? dstSavings : 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Queries if this time zone uses daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @return true if this time zone uses daylight saving time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    public boolean useDaylightTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return useDaylight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
8375
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   842
     * Returns {@code true} if this {@code SimpleTimeZone} observes
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   843
     * Daylight Saving Time. This method is equivalent to {@link
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   844
     * #useDaylightTime()}.
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   845
     *
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   846
     * @return {@code true} if this {@code SimpleTimeZone} observes
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   847
     * Daylight Saving Time; {@code false} otherwise.
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   848
     * @since 1.7
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   849
     */
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   850
    @Override
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   851
    public boolean observesDaylightTime() {
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   852
        return useDaylightTime();
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   853
    }
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   854
7b37813b7461 6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents: 5506
diff changeset
   855
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * Queries if the given date is in daylight saving time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @return true if daylight saving time is in effective at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * given date; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    public boolean inDaylightTime(Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return (getOffset(date.getTime()) != rawOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Returns a clone of this <code>SimpleTimeZone</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @return a clone of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Generates the hash code for the SimpleDateFormat object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @return the hash code for this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public synchronized int hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Compares the equality of two <code>SimpleTimeZone</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param obj  The <code>SimpleTimeZone</code> object to be compared with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @return     True if the given <code>obj</code> is the same as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *             <code>SimpleTimeZone</code> object; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    public boolean equals(Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        if (!(obj instanceof SimpleTimeZone)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        SimpleTimeZone that = (SimpleTimeZone) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        return getID().equals(that.getID()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            hasSameRules(that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Returns <code>true</code> if this zone has the same rules and offset as another zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @param other the TimeZone object to be compared with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @return <code>true</code> if the given zone is a SimpleTimeZone and has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * same rules and offset as this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public boolean hasSameRules(TimeZone other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if (!(other instanceof SimpleTimeZone)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        SimpleTimeZone that = (SimpleTimeZone) other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        return rawOffset == that.rawOffset &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            useDaylight == that.useDaylight &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            (!useDaylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
             // Only check rules if using DST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
             || (dstSavings == that.dstSavings &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                 startMode == that.startMode &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                 startMonth == that.startMonth &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                 startDay == that.startDay &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                 startDayOfWeek == that.startDayOfWeek &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                 startTime == that.startTime &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                 startTimeMode == that.startTimeMode &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                 endMode == that.endMode &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                 endMonth == that.endMonth &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                 endDay == that.endDay &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                 endDayOfWeek == that.endDayOfWeek &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                 endTime == that.endTime &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                 endTimeMode == that.endTimeMode &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                 startYear == that.startYear));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Returns a string representation of this time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @return a string representation of this time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        return getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            "[id=" + getID() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            ",offset=" + rawOffset +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            ",dstSavings=" + dstSavings +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            ",useDaylight=" + useDaylight +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            ",startYear=" + startYear +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            ",startMode=" + startMode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            ",startMonth=" + startMonth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            ",startDay=" + startDay +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            ",startDayOfWeek=" + startDayOfWeek +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            ",startTime=" + startTime +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            ",startTimeMode=" + startTimeMode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            ",endMode=" + endMode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            ",endMonth=" + endMonth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            ",endDay=" + endDay +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            ",endDayOfWeek=" + endDayOfWeek +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            ",endTime=" + endTime +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            ",endTimeMode=" + endTimeMode + ']';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    // =======================privates===============================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * The month in which daylight saving time starts.  This value must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * between <code>Calendar.JANUARY</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * <code>Calendar.DECEMBER</code> inclusive.  This value must not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <code>endMonth</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    private int startMonth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * This field has two possible interpretations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <dt><code>startMode == DOW_IN_MONTH</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * <code>startDay</code> indicates the day of the month of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * <code>startMonth</code> on which daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * saving time starts, from 1 to 28, 30, or 31, depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * <code>startMonth</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * <dt><code>startMode != DOW_IN_MONTH</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * <code>startDay</code> indicates which <code>startDayOfWeek</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * month <code>startMonth</code> daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * saving time starts on.  For example, a value of +1 and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * <code>startDayOfWeek</code> of <code>Calendar.SUNDAY</code> indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * first Sunday of <code>startMonth</code>.  Likewise, +2 would indicate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * second Sunday, and -1 the last Sunday.  A value of 0 is illegal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    private int startDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * The day of the week on which daylight saving time starts.  This value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * must be between <code>Calendar.SUNDAY</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * <code>Calendar.SATURDAY</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * <p>If <code>useDaylight</code> is false or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * <code>startMode == DAY_OF_MONTH</code>, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    private int startDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * The time in milliseconds after midnight at which daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * time starts.  This value is expressed as wall time, standard time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * or UTC time, depending on the setting of <code>startTimeMode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    private int startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * The format of startTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    private int startTimeMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * The month in which daylight saving time ends.  This value must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * between <code>Calendar.JANUARY</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * <code>Calendar.UNDECIMBER</code>.  This value must not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * <code>startMonth</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    private int endMonth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * This field has two possible interpretations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * <dt><code>endMode == DOW_IN_MONTH</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * <code>endDay</code> indicates the day of the month of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * <code>endMonth</code> on which daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * saving time ends, from 1 to 28, 30, or 31, depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * <code>endMonth</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * <dt><code>endMode != DOW_IN_MONTH</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * <code>endDay</code> indicates which <code>endDayOfWeek</code> in th
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * month <code>endMonth</code> daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * saving time ends on.  For example, a value of +1 and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * <code>endDayOfWeek</code> of <code>Calendar.SUNDAY</code> indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * first Sunday of <code>endMonth</code>.  Likewise, +2 would indicate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * second Sunday, and -1 the last Sunday.  A value of 0 is illegal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    private int endDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * The day of the week on which daylight saving time ends.  This value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * must be between <code>Calendar.SUNDAY</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * <code>Calendar.SATURDAY</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <p>If <code>useDaylight</code> is false or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * <code>endMode == DAY_OF_MONTH</code>, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    private int endDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * The time in milliseconds after midnight at which daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * time ends.  This value is expressed as wall time, standard time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * or UTC time, depending on the setting of <code>endTimeMode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    private int endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * The format of endTime, either <code>WALL_TIME</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * <code>STANDARD_TIME</code>, or <code>UTC_TIME</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    private int endTimeMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * The year in which daylight saving time is first observed.  This is an {@link GregorianCalendar#AD AD}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * value.  If this value is less than 1 then daylight saving time is observed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * for all <code>AD</code> years.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    private int startYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * The offset in milliseconds between this zone and GMT.  Negative offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * are to the west of Greenwich.  To obtain local <em>standard</em> time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * add the offset to GMT time.  To obtain local wall time it may also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * necessary to add <code>dstSavings</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    private int rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * A boolean value which is true if and only if this zone uses daylight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * saving time.  If this value is false, several other fields are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    private boolean useDaylight=false; // indicate if this time zone uses DST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    private static final int millisPerHour = 60*60*1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    private static final int millisPerDay  = 24*millisPerHour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * This field was serialized in JDK 1.1, so we have to keep it that way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * to maintain serialization compatibility. However, there's no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * recreate the array each time we create a new time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @serial An array of bytes containing the values {31, 28, 31, 30, 31, 30,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * 31, 31, 30, 31, 30, 31}.  This is ignored as of the Java 2 platform v1.2, however, it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * be streamed out for compatibility with JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    private final byte monthLength[] = staticMonthLength;
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
  1128
    private static final byte staticMonthLength[] = {31,28,31,30,31,30,31,31,30,31,30,31};
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
  1129
    private static final byte staticLeapMonthLength[] = {31,29,31,30,31,30,31,31,30,31,30,31};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Variables specifying the mode of the start rule.  Takes the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <dt><code>DOM_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * Exact day of week; e.g., March 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * <dt><code>DOW_IN_MONTH_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * Day of week in month; e.g., last Sunday in March.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * <dt><code>DOW_GE_DOM_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * Day of week after day of month; e.g., Sunday on or after March 15.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * <dt><code>DOW_LE_DOM_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * Day of week before day of month; e.g., Sunday on or before March 15.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * The setting of this field affects the interpretation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <code>startDay</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * @since 1.1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    private int startMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * Variables specifying the mode of the end rule.  Takes the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <dt><code>DOM_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Exact day of week; e.g., March 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * <dt><code>DOW_IN_MONTH_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * Day of week in month; e.g., last Sunday in March.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <dt><code>DOW_GE_DOM_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * Day of week after day of month; e.g., Sunday on or after March 15.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * <dt><code>DOW_LE_DOM_MODE</code></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * Day of week before day of month; e.g., Sunday on or before March 15.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * The setting of this field affects the interpretation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * <code>endDay</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * @since 1.1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    private int endMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * A positive value indicating the amount of time saved during DST in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * Typically one hour (3600000); sometimes 30 minutes (1800000).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * <p>If <code>useDaylight</code> is false, this value is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * @since 1.1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    private int dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    private static final Gregorian gcal = CalendarSystem.getGregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * Cache values representing a single period of daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * time. When the cache values are valid, cacheStart is the start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * time (inclusive) of daylight saving time and cacheEnd is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * end time (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * cacheYear has a year value if both cacheStart and cacheEnd are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * in the same year. cacheYear is set to startYear - 1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * cacheStart and cacheEnd are in different years. cacheStart is 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * if the cache values are void. cacheYear is a long to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * Integer.MIN_VALUE - 1 (JCK requirement).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    private transient long cacheYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    private transient long cacheStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    private transient long cacheEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * Constants specifying values of startMode and endMode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    private static final int DOM_MODE          = 1; // Exact day of month, "Mar 1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    private static final int DOW_IN_MONTH_MODE = 2; // Day of week in month, "lastSun"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    private static final int DOW_GE_DOM_MODE   = 3; // Day of week after day of month, "Sun>=15"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    private static final int DOW_LE_DOM_MODE   = 4; // Day of week before day of month, "Sun<=21"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * Constant for a mode of start or end time specified as wall clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * time.  Wall clock time is standard time for the onset rule, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * daylight time for the end rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    public static final int WALL_TIME = 0; // Zero for backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * Constant for a mode of start or end time specified as standard time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    public static final int STANDARD_TIME = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * Constant for a mode of start or end time specified as UTC. European
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * Union rules are specified as UTC time, for example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    public static final int UTC_TIME = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    // Proclaim compatibility with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    static final long serialVersionUID = -403250971215465050L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    // the internal serial version which says which version was written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    // - 0 (default) for version up to JDK 1.1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    // - 1 for version from JDK 1.1.4, which includes 3 new fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    // - 2 for JDK 1.3, which includes 2 new fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    static final int currentSerialVersion = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * The version of the serialized data on the stream.  Possible values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * <dt><b>0</b> or not present on stream</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * JDK 1.1.3 or earlier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * <dt><b>1</b></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * JDK 1.1.4 or later.  Includes three new fields: <code>startMode</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * <code>endMode</code>, and <code>dstSavings</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * <dt><b>2</b></dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * JDK 1.3 or later.  Includes two new fields: <code>startTimeMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * and <code>endTimeMode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * When streaming out this class, the most recent format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * and the highest allowable <code>serialVersionOnStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @since 1.1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    private int serialVersionOnStream = currentSerialVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
  1281
    private synchronized void invalidateCache() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        cacheYear = startYear - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        cacheStart = cacheEnd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    //----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    // Rule representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    // We represent the following flavors of rules:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    //       5        the fifth of the month
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    //       lastSun  the last Sunday in the month
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    //       lastMon  the last Monday in the month
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    //       Sun>=8   first Sunday on or after the eighth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    //       Sun<=25  last Sunday on or before the 25th
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    // This is further complicated by the fact that we need to remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    // backward compatible with the 1.1 FCS.  Finally, we need to minimize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    // API changes.  In order to satisfy these requirements, we support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    // three representation systems, and we translate between them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    // INTERNAL REPRESENTATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    // This is the format SimpleTimeZone objects take after construction or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    // streaming in is complete.  Rules are represented directly, using an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    // unencoded format.  We will discuss the start rule only below; the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    // rule is analogous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    //   startMode      Takes on enumerated values DAY_OF_MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    //                  DOW_IN_MONTH, DOW_AFTER_DOM, or DOW_BEFORE_DOM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    //   startDay       The day of the month, or for DOW_IN_MONTH mode, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    //                  value indicating which DOW, such as +1 for first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    //                  +2 for second, -1 for last, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    //   startDayOfWeek The day of the week.  Ignored for DAY_OF_MONTH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    // ENCODED REPRESENTATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    // This is the format accepted by the constructor and by setStartRule()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    // and setEndRule().  It uses various combinations of positive, negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    // and zero values to encode the different rules.  This representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    // allows us to specify all the different rule flavors without altering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    // the API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    //   MODE              startMonth    startDay    startDayOfWeek
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    //   DOW_IN_MONTH_MODE >=0           !=0         >0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    //   DOM_MODE          >=0           >0          ==0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    //   DOW_GE_DOM_MODE   >=0           >0          <0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    //   DOW_LE_DOM_MODE   >=0           <0          <0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    //   (no DST)          don't care    ==0         don't care
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    // STREAMED REPRESENTATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    // We must retain binary compatibility with the 1.1 FCS.  The 1.1 code only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    // handles DOW_IN_MONTH_MODE and non-DST mode, the latter indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    // flag useDaylight.  When we stream an object out, we translate into an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    // approximate DOW_IN_MONTH_MODE representation so the object can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    // and used by 1.1 code.  Following that, we write out the full
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    // representation separately so that contemporary code can recognize and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    // parse it.  The full representation is written in a "packed" format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    // consisting of a version number, a length, and an array of bytes.  Future
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    // versions of this class may specify different versions.  If they wish to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    // include additional data, they should do so by storing them after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    // packed representation below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    //----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * Given a set of encoded rules in startDay and startDayOfMonth, decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * them and set the startMode appropriately.  Do the same for endDay and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * endDayOfMonth.  Upon entry, the day of week variables may be zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * negative, in order to indicate special modes.  The day of month
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * variables may also be negative.  Upon exit, the mode variables will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * set, and the day of week and day of month variables will be positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * This method also recognizes a startDay or endDay of zero as indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * no DST.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    private void decodeRules()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        decodeStartRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        decodeEndRule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * Decode the start rule and validate the parameters.  The parameters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * expected to be in encoded form, which represents the various rule modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * by negating or zeroing certain values.  Representation formats are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *            DOW_IN_MONTH  DOM    DOW>=DOM  DOW<=DOM  no DST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *            ------------  -----  --------  --------  ----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * month       0..11        same    same      same     don't care
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * day        -5..5         1..31   1..31    -1..-31   0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * dayOfWeek   1..7         0      -1..-7    -1..-7    don't care
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * time        0..ONEDAY    same    same      same     don't care
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * The range for month does not include UNDECIMBER since this class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * really specific to GregorianCalendar, which does not use that month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * The range for time includes ONEDAY (vs. ending at ONEDAY-1) because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * end rule is an exclusive limit point.  That is, the range of times that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * are in DST include those >= the start and < the end.  For this reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * it should be possible to specify an end of ONEDAY in order to include the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * entire day.  Although this is equivalent to time 0 of the following day,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * it's not always possible to specify that, for example, on December 31.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * While arguably the start range should still be 0..ONEDAY-1, we keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * the start and end ranges the same for consistency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    private void decodeStartRule() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        useDaylight = (startDay != 0) && (endDay != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        if (startDay != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            if (startMonth < Calendar.JANUARY || startMonth > Calendar.DECEMBER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                        "Illegal start month " + startMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            }
3738
837612df050f 6851214: (tz) New Jordan rule creates a failure for SimpleTimeZone parsing post tzdata2009h
peytoia
parents: 2
diff changeset
  1386
            if (startTime < 0 || startTime > millisPerDay) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                        "Illegal start time " + startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            if (startDayOfWeek == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                startMode = DOM_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                if (startDayOfWeek > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    startMode = DOW_IN_MONTH_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                    startDayOfWeek = -startDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                    if (startDay > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                        startMode = DOW_GE_DOM_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                        startDay = -startDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                        startMode = DOW_LE_DOM_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                if (startDayOfWeek > Calendar.SATURDAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                           "Illegal start day of week " + startDayOfWeek);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            if (startMode == DOW_IN_MONTH_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                if (startDay < -5 || startDay > 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                            "Illegal start day of week in month " + startDay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            } else if (startDay < 1 || startDay > staticMonthLength[startMonth]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                        "Illegal start day " + startDay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * Decode the end rule and validate the parameters.  This method is exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * analogous to decodeStartRule().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @see decodeStartRule
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    private void decodeEndRule() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        useDaylight = (startDay != 0) && (endDay != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        if (endDay != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            if (endMonth < Calendar.JANUARY || endMonth > Calendar.DECEMBER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                        "Illegal end month " + endMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            }
3738
837612df050f 6851214: (tz) New Jordan rule creates a failure for SimpleTimeZone parsing post tzdata2009h
peytoia
parents: 2
diff changeset
  1433
            if (endTime < 0 || endTime > millisPerDay) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                        "Illegal end time " + endTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            if (endDayOfWeek == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                endMode = DOM_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                if (endDayOfWeek > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    endMode = DOW_IN_MONTH_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                    endDayOfWeek = -endDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                    if (endDay > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                        endMode = DOW_GE_DOM_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                        endDay = -endDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                        endMode = DOW_LE_DOM_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                if (endDayOfWeek > Calendar.SATURDAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                           "Illegal end day of week " + endDayOfWeek);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            if (endMode == DOW_IN_MONTH_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                if (endDay < -5 || endDay > 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                            "Illegal end day of week in month " + endDay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            } else if (endDay < 1 || endDay > staticMonthLength[endMonth]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                        "Illegal end day " + endDay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * Make rules compatible to 1.1 FCS code.  Since 1.1 FCS code only understands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * day-of-week-in-month rules, we must modify other modes of rules to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * approximate equivalent in 1.1 FCS terms.  This method is used when streaming
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * out objects of this class.  After it is called, the rules will be modified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * with a possible loss of information.  startMode and endMode will NOT be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * altered, even though semantically they should be set to DOW_IN_MONTH_MODE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * since the rule modification is only intended to be temporary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    private void makeRulesCompatible()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        switch (startMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        case DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            startDay = 1 + (startDay / 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            startDayOfWeek = Calendar.SUNDAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        case DOW_GE_DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            // that is, Sun>=1 == firstSun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            if (startDay != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                startDay = 1 + (startDay / 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        case DOW_LE_DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            if (startDay >= 30) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                startDay = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                startDay = 1 + (startDay / 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        switch (endMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        case DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            endDay = 1 + (endDay / 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            endDayOfWeek = Calendar.SUNDAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        case DOW_GE_DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            // that is, Sun>=1 == firstSun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            if (endDay != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                endDay = 1 + (endDay / 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        case DOW_LE_DOM_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            if (endDay >= 30) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                endDay = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                endDay = 1 + (endDay / 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
         * Adjust the start and end times to wall time.  This works perfectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
         * well unless it pushes into the next or previous day.  If that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
         * happens, we attempt to adjust the day rule somewhat crudely.  The day
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
         * rules have been forced into DOW_IN_MONTH mode already, so we change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
         * the day of week to move forward or back by a day.  It's possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
         * make a more refined adjustment of the original rules first, but in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
         * most cases this extra effort will go to waste once we adjust the day
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
         * rules anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        switch (startTimeMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        case UTC_TIME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            startTime += rawOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        while (startTime < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            startTime += millisPerDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            startDayOfWeek = 1 + ((startDayOfWeek+5) % 7); // Back 1 day
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        while (startTime >= millisPerDay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            startTime -= millisPerDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            startDayOfWeek = 1 + (startDayOfWeek % 7); // Forward 1 day
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        switch (endTimeMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        case UTC_TIME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            endTime += rawOffset + dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        case STANDARD_TIME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            endTime += dstSavings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        while (endTime < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            endTime += millisPerDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            endDayOfWeek = 1 + ((endDayOfWeek+5) % 7); // Back 1 day
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        while (endTime >= millisPerDay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            endTime -= millisPerDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            endDayOfWeek = 1 + (endDayOfWeek % 7); // Forward 1 day
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * Pack the start and end rules into an array of bytes.  Only pack
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * data which is not preserved by makeRulesCompatible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    private byte[] packRules()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        byte[] rules = new byte[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        rules[0] = (byte)startDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        rules[1] = (byte)startDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        rules[2] = (byte)endDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        rules[3] = (byte)endDayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        // As of serial version 2, include time modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        rules[4] = (byte)startTimeMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        rules[5] = (byte)endTimeMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        return rules;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * Given an array of bytes produced by packRules, interpret them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * as the start and end rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    private void unpackRules(byte[] rules)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        startDay       = rules[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        startDayOfWeek = rules[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        endDay         = rules[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        endDayOfWeek   = rules[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        // As of serial version 2, include time modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        if (rules.length >= 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            startTimeMode = rules[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            endTimeMode   = rules[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * Pack the start and end times into an array of bytes.  This is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * as of serial version 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    private int[] packTimes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        int[] times = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        times[0] = startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        times[1] = endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        return times;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Unpack the start and end times from an array of bytes.  This is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * as of serial version 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    private void unpackTimes(int[] times) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        startTime = times[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        endTime = times[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * Save the state of this object to a stream (i.e., serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * @serialData We write out two formats, a JDK 1.1 compatible format, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * <code>DOW_IN_MONTH_MODE</code> rules, in the required section, followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * by the full rules, in packed format, in the optional section.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * optional section will be ignored by JDK 1.1 code upon stream in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * <p> Contents of the optional section: The length of a byte array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * emitted (int); this is 4 as of this release. The byte array of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * length is emitted. The contents of the byte array are the true values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * the fields <code>startDay</code>, <code>startDayOfWeek</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * <code>endDay</code>, and <code>endDayOfWeek</code>.  The values of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * fields in the required section are approximate values suited to the rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * mode <code>DOW_IN_MONTH_MODE</code>, which is the only mode recognized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    private void writeObject(ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
         throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        // Construct a binary rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        byte[] rules = packRules();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        int[] times = packTimes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        // Convert to 1.1 FCS rules.  This step may cause us to lose information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        makeRulesCompatible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        // Write out the 1.1 FCS rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        stream.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        // Write out the binary rules in the optional data area of the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        stream.writeInt(rules.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        stream.write(rules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        stream.writeObject(times);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        // Recover the original rules.  This recovers the information lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        // by makeRulesCompatible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        unpackRules(rules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        unpackTimes(times);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * Reconstitute this object from a stream (i.e., deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * We handle both JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * binary formats and full formats with a packed byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    private void readObject(ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        if (serialVersionOnStream < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            // Fix a bug in the 1.1 SimpleTimeZone code -- namely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            // startDayOfWeek and endDayOfWeek were usually uninitialized.  We can't do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            // too much, so we assume SUNDAY, which actually works most of the time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            if (startDayOfWeek == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                startDayOfWeek = Calendar.SUNDAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            if (endDayOfWeek == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                endDayOfWeek = Calendar.SUNDAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            // The variables dstSavings, startMode, and endMode are post-1.1, so they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            // won't be present if we're reading from a 1.1 stream.  Fix them up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            startMode = endMode = DOW_IN_MONTH_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            dstSavings = millisPerHour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            // For 1.1.4, in addition to the 3 new instance variables, we also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            // store the actual rules (which have not be made compatible with 1.1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            // in the optional area.  Read them in here and parse them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            int length = stream.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            byte[] rules = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            stream.readFully(rules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            unpackRules(rules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        if (serialVersionOnStream >= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            int[] times = (int[]) stream.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            unpackTimes(times);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        serialVersionOnStream = currentSerialVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
}