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