jdk/src/share/classes/sun/util/calendar/AbstractCalendar.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 5506 202f599c92aa
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.util.calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The <code>AbstractCalendar</code> class provides a framework for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * implementing a concrete calendar system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p><a name="fixed_date"></a><B>Fixed Date</B><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * For implementing a concrete calendar system, each calendar must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * have the common date numbering, starting from midnight the onset of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Monday, January 1, 1 (Gregorian). It is called a <I>fixed date</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * in this class. January 1, 1 (Gregorian) is fixed date 1. (See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Nachum Dershowitz and Edward M. Reingold, <I>CALENDRICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * CALCULATION The Millennium Edition</I>, Section 1.2 for details.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Masayoshi Okutsu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class AbstractCalendar extends CalendarSystem {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // The constants assume no leap seconds support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final int SECOND_IN_MILLIS = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final int MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final int HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final int DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // The number of days between January 1, 1 and January 1, 1970 (Gregorian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final int EPOCH_OFFSET = 719163;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Era[] eras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected AbstractCalendar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public Era getEra(String eraName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (eras != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            for (int i = 0; i < eras.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                if (eras[i].equals(eraName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    return eras[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public Era[] getEras() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Era[] e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (eras != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            e = new Era[eras.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.arraycopy(eras, 0, e, 0, eras.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void setEra(CalendarDate date, String eraName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (eras == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return; // should report an error???
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (int i = 0; i < eras.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            Era e = eras[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (e != null && e.getName().equals(eraName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                date.setEra(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throw new IllegalArgumentException("unknown era name: " + eraName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected void setEras(Era[] eras) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.eras = eras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public CalendarDate getCalendarDate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return getCalendarDate(System.currentTimeMillis(), newCalendarDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public CalendarDate getCalendarDate(long millis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return getCalendarDate(millis, newCalendarDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public CalendarDate getCalendarDate(long millis, TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        CalendarDate date = newCalendarDate(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return getCalendarDate(millis, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public CalendarDate getCalendarDate(long millis, CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int ms = 0;             // time of day
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int zoneOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int saving = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        long days = 0;          // fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // adjust to local time if `date' has time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        TimeZone zi = date.getZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (zi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int[] offsets = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (zi instanceof ZoneInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                zoneOffset = ((ZoneInfo)zi).getOffsets(millis, offsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                zoneOffset = zi.getOffset(millis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                offsets[0] = zi.getRawOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                offsets[1] = zoneOffset - offsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // We need to calculate the given millis and time zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // offset separately for java.util.GregorianCalendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // compatibility. (i.e., millis + zoneOffset could cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // overflow or underflow, which must be avoided.) Usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // days should be 0 and ms is in the range of -13:00 to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // +14:00. However, we need to deal with extreme cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            days = zoneOffset / DAY_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            ms = zoneOffset % DAY_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            saving = offsets[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        date.setZoneOffset(zoneOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        date.setDaylightSaving(saving);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        days += millis / DAY_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        ms += (int) (millis % DAY_IN_MILLIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (ms >= DAY_IN_MILLIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // at most ms is (DAY_IN_MILLIS - 1) * 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            ms -= DAY_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            ++days;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // at most ms is (1 - DAY_IN_MILLIS) * 2. Adding one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // DAY_IN_MILLIS results in still negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            while (ms < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                ms += DAY_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                --days;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // convert to fixed date (offset from Jan. 1, 1 (Gregorian))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        days += EPOCH_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // calculate date fields from the fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        getCalendarDateFromFixedDate(date, days);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // calculate time fields from the time of day
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        setTimeOfDay(date, ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        date.setLeapYear(isLeapYear(date));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        date.setNormalized(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public long getTime(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        long gd = getFixedDate(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        long ms = (gd - EPOCH_OFFSET) * DAY_IN_MILLIS + getTimeOfDay(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        int zoneOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        TimeZone zi = date.getZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (zi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (date.isNormalized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return ms - date.getZoneOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // adjust time zone and daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            int[] offsets = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (date.isStandardTime()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // 1) 2:30am during starting-DST transition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                //    intrepreted as 2:30am ST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                // 2) 5:00pm during DST is still interpreted as 5:00pm ST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // 3) 1:30am during ending-DST transition is interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                //    as 1:30am ST (after transition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (zi instanceof ZoneInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    ((ZoneInfo)zi).getOffsetsByStandard(ms, offsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    zoneOffset = offsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    zoneOffset = zi.getOffset(ms - zi.getRawOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // 1) 2:30am during starting-DST transition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                //    intrepreted as 3:30am DT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                // 2) 5:00pm during DST is intrepreted as 5:00pm DT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                // 3) 1:30am during ending-DST transition is interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                //    as 1:30am DT/0:30am ST (before transition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (zi instanceof ZoneInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    zoneOffset = ((ZoneInfo)zi).getOffsetsByWall(ms, offsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    zoneOffset = zi.getOffset(ms - zi.getRawOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        ms -= zoneOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        getCalendarDate(ms, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return ms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected long getTimeOfDay(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        long fraction = date.getTimeOfDay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (fraction != CalendarDate.TIME_UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return fraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        fraction = getTimeOfDayValue(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        date.setTimeOfDay(fraction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return fraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public long getTimeOfDayValue(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        long fraction = date.getHours();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        fraction *= 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        fraction += date.getMinutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        fraction *= 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        fraction += date.getSeconds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        fraction *= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        fraction += date.getMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return fraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public CalendarDate setTimeOfDay(CalendarDate cdate, int fraction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (fraction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        boolean normalizedState = cdate.isNormalized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int time = fraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int hours = time / HOUR_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        time %= HOUR_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int minutes = time / MINUTE_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        time %= MINUTE_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int seconds = time / SECOND_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        time %= SECOND_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        cdate.setHours(hours);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        cdate.setMinutes(minutes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        cdate.setSeconds(seconds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        cdate.setMillis(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        cdate.setTimeOfDay(fraction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (hours < 24 && normalizedState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // If this time of day setting doesn't affect the date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // then restore the normalized state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            cdate.setNormalized(normalizedState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return cdate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns 7 in this default implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public int getWeekLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    protected abstract boolean isLeapYear(CalendarDate date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public CalendarDate getNthDayOfWeek(int nth, int dayOfWeek, CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        CalendarDate ndate = (CalendarDate) date.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        normalize(ndate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        long fd = getFixedDate(ndate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        long nfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (nth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            nfd = 7 * nth + getDayOfWeekDateBefore(fd, dayOfWeek);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            nfd = 7 * nth + getDayOfWeekDateAfter(fd, dayOfWeek);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        getCalendarDateFromFixedDate(ndate, nfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return ndate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns a date of the given day of week before the given fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param fixedDate the fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param dayOfWeek the day of week
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return the calculated date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    static long getDayOfWeekDateBefore(long fixedDate, int dayOfWeek) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return getDayOfWeekDateOnOrBefore(fixedDate - 1, dayOfWeek);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns a date of the given day of week that is closest to and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * after the given fixed date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param fixedDate the fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param dayOfWeek the day of week
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return the calculated date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    static long getDayOfWeekDateAfter(long fixedDate, int dayOfWeek) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return getDayOfWeekDateOnOrBefore(fixedDate + 7, dayOfWeek);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Returns a date of the given day of week on or before the given fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param fixedDate the fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param dayOfWeek the day of week
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return the calculated date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    // public for java.util.GregorianCalendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public static long getDayOfWeekDateOnOrBefore(long fixedDate, int dayOfWeek) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        long fd = fixedDate - (dayOfWeek - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (fd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return fixedDate - (fd % 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return fixedDate - CalendarUtils.mod(fd, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns the fixed date calculated with the specified calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * date. If the specified date is not normalized, its date fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * are normalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param date a <code>CalendarDate</code> with which the fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * date is calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @return the calculated fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @see AbstractCalendar.html#fixed_date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    protected abstract long getFixedDate(CalendarDate date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Calculates calendar fields from the specified fixed date. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * method stores the calculated calendar field values in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>CalendarDate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param date a <code>CalendarDate</code> to stored the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * calculated calendar fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param fixedDate a fixed date to calculate calendar fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see AbstractCalendar.html#fixed_date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    protected abstract void getCalendarDateFromFixedDate(CalendarDate date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                                         long fixedDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean validateTime(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int t = date.getHours();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (t < 0 || t >= 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        t = date.getMinutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (t < 0 || t >= 60) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        t = date.getSeconds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // TODO: Leap second support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (t < 0 || t >= 60) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        t = date.getMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (t < 0 || t >= 1000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    int normalizeTime(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        long fraction = getTimeOfDay(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        long days = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (fraction >= DAY_IN_MILLIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            days = fraction / DAY_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            fraction %= DAY_IN_MILLIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } else if (fraction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            days = CalendarUtils.floorDivide(fraction, DAY_IN_MILLIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (days != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                fraction -= DAY_IN_MILLIS * days; // mod(fraction, DAY_IN_MILLIS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (days != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            date.setTimeOfDay(fraction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        date.setMillis((int)(fraction % 1000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        fraction /= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        date.setSeconds((int)(fraction % 60));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        fraction /= 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        date.setMinutes((int)(fraction % 60));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        date.setHours((int)(fraction / 60));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return (int)days;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}