jdk/test/sun/util/calendar/zi/Time.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
child 23010 6dadb192ad81
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:
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     1
/*
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     4
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    10
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    15
 * accompanied this code).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    16
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    20
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    23
 * questions.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    24
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    25
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    26
import java.util.Locale;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    27
import sun.util.calendar.CalendarDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    28
import sun.util.calendar.CalendarSystem;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    29
import sun.util.calendar.Gregorian;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    30
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    31
/**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    32
 * Time class represents the "AT" field and other time related information.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    33
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    34
 * @since 1.4
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    35
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    36
class Time {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    37
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    38
    static final Gregorian gcal = CalendarSystem.getGregorianCalendar();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    39
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    40
    // type is wall clock time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    41
    private static final int WALL = 1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    42
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    43
    // type is standard time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    44
    private static final int STD = 2;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    45
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    46
    // type is UTC
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    47
    private static final int UTC = 3;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    48
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    49
    // type of representing time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    50
    private int type;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    51
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    52
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    53
     * Time from the EPOCH in milliseconds
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    54
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    55
    private long time;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    56
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    57
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    58
     * Current time in milliseconds
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    59
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    60
    private static final long currentTime = System.currentTimeMillis();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    61
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    62
    Time() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    63
        time = 0L;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    64
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    65
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    66
    Time(long time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    67
        this.time = time;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    68
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    69
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    70
    void setType(int type) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    71
        this.type = type;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    72
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    73
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    74
    long getTime() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    75
        return time;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    76
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    77
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    78
    int getType() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    79
        return type;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    80
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    81
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    82
    static long getCurrentTime() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    83
        return currentTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    84
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    85
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    86
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    87
     * @return true if the time is represented in wall-clock time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    88
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    89
    boolean isWall() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    90
        return type == WALL;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    91
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    92
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    93
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    94
     * @return true if the time is represented in standard time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    95
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    96
    boolean isSTD() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    97
        return type == STD;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    98
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    99
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   100
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   101
     * @return true if the time is represented in UTC time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   102
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   103
    boolean isUTC() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   104
        return type == UTC;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   105
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   106
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   107
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   108
     * Converts the type to a string that represents the type in the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   109
     * SimpleTimeZone time mode. (e.g., "SimpleTimeZone.WALL_TIME").
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   110
     * @return the converted string or null if the type is undefined.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   111
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   112
    String getTypeForSimpleTimeZone() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   113
        String  stz = "SimpleTimeZone.";
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   114
        if (isWall()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   115
            return stz+"WALL_TIME";
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   116
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   117
        else if (isSTD()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   118
            return stz+"STANDARD_TIME";
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   119
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   120
        else if (isUTC()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   121
            return stz+"UTC_TIME";
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   122
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   123
        else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   124
            return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   125
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   126
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   127
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   128
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   129
     * Converts the given Gregorian calendar field values to local time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   130
     * Local time is represented by the amount of milliseconds from
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   131
     * January 1, 1970 0:00 GMT.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   132
     * @param year the year value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   133
     * @param month the Month value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   134
     * @param day the day represented by {@link RuleDay}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   135
     * @param save the amount of daylight time in milliseconds
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   136
     * @param gmtOffset the GMT offset in milliseconds
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   137
     * @param time the time of the day represented by {@link Time}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   138
     * @return local time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   139
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   140
    static long getLocalTime(int year, Month month, RuleDay day, int save,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   141
                             int gmtOffset, Time time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   142
        long    t = time.getTime();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   143
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   144
        if (time.isSTD())
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   145
            t = time.getTime() + save;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   146
        else if (time.isUTC())
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   147
            t = time.getTime() + save + gmtOffset;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   148
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   149
        return getLocalTime(year, month, day, t);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   150
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   151
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   152
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   153
     * Converts the given Gregorian calendar field values to local time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   154
     * Local time is represented by the amount of milliseconds from
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   155
     * January 1, 1970 0:00 GMT.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   156
     * @param year the year value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   157
     * @param month the Month value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   158
     * @param day the day value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   159
     * @param time the time of the day in milliseconds
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   160
     * @return local time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   161
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   162
    static long getLocalTime(int year, Month month, int day, long time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   163
        CalendarDate date = gcal.newCalendarDate(null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   164
        date.setDate(year, month.value(), day);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   165
        long millis = gcal.getTime(date);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   166
        return millis + time;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   167
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   168
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   169
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   170
     * Equivalent to <code>getLocalTime(year, month, day, (long)time)</code>.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   171
     * @param year the year value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   172
     * @param month the Month value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   173
     * @param day the day value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   174
     * @param time the time of the day in milliseconds
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   175
     * @return local time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   176
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   177
    static long getLocalTime(int year, Month month, int day, int time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   178
        return getLocalTime(year, month, day, (long)time);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   179
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   180
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   181
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   182
     * Equivalent to {@link #getLocalTime(int, Month, RuleDay, int)
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   183
     * getLocalTime(year, month, day, (int) time)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   184
     * @param year the year value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   185
     * @param month the Month value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   186
     * @param day the day represented by {@link RuleDay}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   187
     * @param time the time of the day represented by {@link Time}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   188
     * @return local time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   189
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   190
    static long getLocalTime(int year, Month month, RuleDay day, long time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   191
        return getLocalTime(year, month, day, (int) time);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   192
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   193
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   194
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   195
     * Converts the given Gregorian calendar field values to local time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   196
     * Local time is represented by the amount of milliseconds from
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   197
     * January 1, 1970 0:00 GMT.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   198
     * @param year the year value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   199
     * @param month the Month value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   200
     * @param day the day represented by {@link RuleDay}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   201
     * @param time the time of the day represented by {@link Time}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   202
     * @return local time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   203
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   204
    static long getLocalTime(int year, Month month, RuleDay day, int time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   205
        CalendarDate cdate = gcal.newCalendarDate(null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   206
        int monthValue = month.value();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   207
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   208
        if (day.isLast()) {     // e.g., "lastSun"
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   209
            cdate.setDate(year, monthValue, 1);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   210
            cdate.setDayOfMonth(gcal.getMonthLength(cdate));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   211
            cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   212
        } else if (day.isLater()) { // e.g., "Sun>=1"
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   213
            cdate.setDate(year, monthValue, day.getDay());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   214
            cdate = gcal.getNthDayOfWeek(1, day.getDayOfWeekNum(), cdate);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   215
        } else if (day.isExact()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   216
            cdate.setDate(year, monthValue, day.getDay());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   217
        } else if (day.isEarlier()) {   // e.g., "Sun<=15"
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   218
            cdate.setDate(year, monthValue, day.getDay());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   219
            cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   220
        } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   221
            Main.panic("invalid day type: " + day);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   222
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   223
        return gcal.getTime(cdate) + time;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   224
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   225
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   226
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   227
     * Parses the given "AT" field and constructs a Time object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   228
     * @param the "AT" field string
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   229
     * @return the Time object
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   230
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   231
    static Time parse(String time) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   232
        int sign;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   233
        int index = 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   234
        Time tm;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   235
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   236
        if (time.charAt(0) == '-') {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   237
            sign = -1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   238
            index++;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   239
        } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   240
            sign = 1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   241
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   242
        int val = 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   243
        int num = 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   244
        int countDelim = 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   245
        while (index < time.length()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   246
            char c = time.charAt(index++);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   247
            if (c == ':') {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   248
                val = val * 60 + num;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   249
                countDelim++;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   250
                num = 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   251
                continue;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   252
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   253
            int d = Character.digit(c, 10);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   254
            if (d == -1) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   255
                --index;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   256
                break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   257
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   258
            num = num * 10 + d;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   259
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   260
        val = val * 60 + num;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   261
        // convert val to second
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   262
        for (; countDelim < 2; countDelim++) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   263
            val *= 60;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   264
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   265
        tm = new Time((long)val * 1000 * sign);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   266
        if (index < time.length()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   267
            char c = time.charAt(index++);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   268
            if (c == 's') {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   269
                tm.setType(Time.STD);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   270
            } else if (c == 'u' || c == 'g' || c == 'z') {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   271
                tm.setType(Time.UTC);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   272
            } else if (c == 'w') {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   273
                tm.setType(Time.WALL);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   274
            } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   275
                Main.panic("unknown time mode: "+c);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   276
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   277
        } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   278
            tm.setType(Time.WALL);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   279
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   280
        return tm;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   281
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   282
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   283
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   284
     * Converts the given milliseconds string to a "[+-]hh:mm" string.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   285
     * @param ms the milliseconds string
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   286
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   287
    static String toGMTFormat(String ms) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   288
        long sec = Long.parseLong(ms) / 1000;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   289
        char sign;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   290
        if (sec < 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   291
            sign = '-';
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   292
            sec = -sec;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   293
        } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   294
            sign = '+';
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   295
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   296
        return String.format((Locale)null, "%c%02d:%02d",
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   297
                             sign, sec/3600, (sec%3600)/60);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   298
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   299
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   300
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   301
     * Converts the given millisecond value to a string for a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   302
     * SimpleTimeZone parameter.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   303
     * @param ms the millisecond value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   304
     * @return the string in a human readable form
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   305
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   306
    static String toFormedString(int ms) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   307
        StringBuilder s = new StringBuilder();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   308
        boolean minus = false;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   309
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   310
        if (ms < 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   311
            s.append("-");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   312
            minus = true;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   313
            ms = -ms;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   314
        } else if (ms == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   315
            return "0";
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   316
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   317
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   318
        int hour = ms / (60 * 60 * 1000);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   319
        ms %= (60 * 60 * 1000);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   320
        int minute = ms / (60 * 1000);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   321
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   322
        if (hour != 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   323
            if (minus && minute != 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   324
                s.append("(");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   325
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   326
            s.append(Integer.toString(hour) + "*ONE_HOUR");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   327
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   328
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   329
        if (minute != 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   330
            if (hour != 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   331
                s.append("+");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   332
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   333
            s.append(Integer.toString(minute) + "*ONE_MINUTE");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   334
            if (minus && hour != 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   335
                s.append(")");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   336
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   337
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   338
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   339
        return s.toString();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   340
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   341
}