Path: blob/master/test/jdk/sun/util/calendar/zi/Time.java
41153 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Locale;24import sun.util.calendar.CalendarDate;25import sun.util.calendar.CalendarSystem;26import sun.util.calendar.Gregorian;2728/**29* Time class represents the "AT" field and other time related information.30*31* @since 1.432*/33class Time {3435static final Gregorian gcal = CalendarSystem.getGregorianCalendar();3637// type is wall clock time38private static final int WALL = 1;3940// type is standard time41private static final int STD = 2;4243// type is UTC44private static final int UTC = 3;4546// type of representing time47private int type;4849/**50* Time from the EPOCH in milliseconds51*/52private long time;5354/**55* Current time in milliseconds56*/57private static final long currentTime = System.currentTimeMillis();5859Time() {60time = 0L;61}6263Time(long time) {64this.time = time;65}6667void setType(int type) {68this.type = type;69}7071long getTime() {72return time;73}7475int getType() {76return type;77}7879static long getCurrentTime() {80return currentTime;81}8283/**84* @return true if the time is represented in wall-clock time.85*/86boolean isWall() {87return type == WALL;88}8990/**91* @return true if the time is represented in standard time.92*/93boolean isSTD() {94return type == STD;95}9697/**98* @return true if the time is represented in UTC time.99*/100boolean isUTC() {101return type == UTC;102}103104/**105* Converts the type to a string that represents the type in the106* SimpleTimeZone time mode. (e.g., "SimpleTimeZone.WALL_TIME").107* @return the converted string or null if the type is undefined.108*/109String getTypeForSimpleTimeZone() {110String stz = "SimpleTimeZone.";111if (isWall()) {112return stz+"WALL_TIME";113}114else if (isSTD()) {115return stz+"STANDARD_TIME";116}117else if (isUTC()) {118return stz+"UTC_TIME";119}120else {121return null;122}123}124125/**126* Converts the given Gregorian calendar field values to local time.127* Local time is represented by the amount of milliseconds from128* January 1, 1970 0:00 GMT.129* @param year the year value130* @param month the Month value131* @param day the day represented by {@link RuleDay}132* @param save the amount of daylight time in milliseconds133* @param gmtOffset the GMT offset in milliseconds134* @param time the time of the day represented by {@link Time}135* @return local time136*/137static long getLocalTime(int year, Month month, RuleDay day, int save,138int gmtOffset, Time time) {139long t = time.getTime();140141if (time.isSTD())142t = time.getTime() + save;143else if (time.isUTC())144t = time.getTime() + save + gmtOffset;145146return getLocalTime(year, month, day, t);147}148149/**150* Converts the given Gregorian calendar field values to local time.151* Local time is represented by the amount of milliseconds from152* January 1, 1970 0:00 GMT.153* @param year the year value154* @param month the Month value155* @param day the day value156* @param time the time of the day in milliseconds157* @return local time158*/159static long getLocalTime(int year, Month month, int day, long time) {160CalendarDate date = gcal.newCalendarDate(null);161date.setDate(year, month.value(), day);162long millis = gcal.getTime(date);163return millis + time;164}165166/**167* Equivalent to <code>getLocalTime(year, month, day, (long)time)</code>.168* @param year the year value169* @param month the Month value170* @param day the day value171* @param time the time of the day in milliseconds172* @return local time173*/174static long getLocalTime(int year, Month month, int day, int time) {175return getLocalTime(year, month, day, (long)time);176}177178/**179* Equivalent to {@link #getLocalTime(int, Month, RuleDay, int)180* getLocalTime(year, month, day, (int) time)}.181* @param year the year value182* @param month the Month value183* @param day the day represented by {@link RuleDay}184* @param time the time of the day represented by {@link Time}185* @return local time186*/187static long getLocalTime(int year, Month month, RuleDay day, long time) {188return getLocalTime(year, month, day, (int) time);189}190191/**192* Converts the given Gregorian calendar field values to local time.193* Local time is represented by the amount of milliseconds from194* January 1, 1970 0:00 GMT.195* @param year the year value196* @param month the Month value197* @param day the day represented by {@link RuleDay}198* @param time the time of the day represented by {@link Time}199* @return local time200*/201static long getLocalTime(int year, Month month, RuleDay day, int time) {202CalendarDate cdate = gcal.newCalendarDate(null);203int monthValue = month.value();204205if (day.isLast()) { // e.g., "lastSun"206cdate.setDate(year, monthValue, 1);207cdate.setDayOfMonth(gcal.getMonthLength(cdate));208cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);209} else if (day.isLater()) { // e.g., "Sun>=1"210cdate.setDate(year, monthValue, day.getDay());211cdate = gcal.getNthDayOfWeek(1, day.getDayOfWeekNum(), cdate);212} else if (day.isExact()) {213cdate.setDate(year, monthValue, day.getDay());214} else if (day.isEarlier()) { // e.g., "Sun<=15"215cdate.setDate(year, monthValue, day.getDay());216cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);217} else {218Main.panic("invalid day type: " + day);219}220return gcal.getTime(cdate) + time;221}222223/**224* Parses the given "AT" field and constructs a Time object.225* @param the "AT" field string226* @return the Time object227*/228static Time parse(String time) {229int sign;230int index = 0;231Time tm;232233if (time.charAt(0) == '-') {234sign = -1;235index++;236} else {237sign = 1;238}239int val = 0;240int num = 0;241int countDelim = 0;242while (index < time.length()) {243char c = time.charAt(index++);244if (c == ':') {245val = val * 60 + num;246countDelim++;247num = 0;248continue;249}250int d = Character.digit(c, 10);251if (d == -1) {252--index;253break;254}255num = num * 10 + d;256}257val = val * 60 + num;258// convert val to second259for (; countDelim < 2; countDelim++) {260val *= 60;261}262tm = new Time((long)val * 1000 * sign);263if (index < time.length()) {264char c = time.charAt(index++);265if (c == 's') {266tm.setType(Time.STD);267} else if (c == 'u' || c == 'g' || c == 'z') {268tm.setType(Time.UTC);269} else if (c == 'w') {270tm.setType(Time.WALL);271} else {272Main.panic("unknown time mode: "+c);273}274} else {275tm.setType(Time.WALL);276}277return tm;278}279280/**281* Converts the given milliseconds string to a "[+-]hh:mm" string.282* @param ms the milliseconds string283*/284static String toGMTFormat(String ms) {285long sec = Long.parseLong(ms) / 1000;286char sign;287if (sec < 0) {288sign = '-';289sec = -sec;290} else {291sign = '+';292}293return String.format((Locale)null, "%c%02d:%02d",294sign, sec/3600, (sec%3600)/60);295}296297/**298* Converts the given millisecond value to a string for a299* SimpleTimeZone parameter.300* @param ms the millisecond value301* @return the string in a human readable form302*/303static String toFormedString(int ms) {304StringBuilder s = new StringBuilder();305boolean minus = false;306307if (ms < 0) {308s.append("-");309minus = true;310ms = -ms;311} else if (ms == 0) {312return "0";313}314315int hour = ms / (60 * 60 * 1000);316ms %= (60 * 60 * 1000);317int minute = ms / (60 * 1000);318319if (hour != 0) {320if (minus && minute != 0) {321s.append("(");322}323s.append(Integer.toString(hour) + "*ONE_HOUR");324}325326if (minute != 0) {327if (hour != 0) {328s.append("+");329}330s.append(Integer.toString(minute) + "*ONE_MINUTE");331if (minus && hour != 0) {332s.append(")");333}334}335336return s.toString();337}338}339340341