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