Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/calendar/zi/Time.java
38855 views
/*1* Copyright (c) 2000, 2013, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.util.Locale;26import sun.util.calendar.CalendarDate;27import sun.util.calendar.CalendarSystem;28import sun.util.calendar.Gregorian;2930/**31* Time class represents the "AT" field and other time related information.32*33* @since 1.434*/35class Time {3637static final Gregorian gcal = CalendarSystem.getGregorianCalendar();3839// type is wall clock time40private static final int WALL = 1;4142// type is standard time43private static final int STD = 2;4445// type is UTC46private static final int UTC = 3;4748// type of representing time49private int type;5051/**52* Time from the EPOCH in milliseconds53*/54private long time;5556/**57* Current time in milliseconds58*/59private static final long currentTime = System.currentTimeMillis();6061Time() {62time = 0L;63}6465Time(long time) {66this.time = time;67}6869void setType(int type) {70this.type = type;71}7273long getTime() {74return time;75}7677int getType() {78return type;79}8081static long getCurrentTime() {82return currentTime;83}8485/**86* @return true if the time is represented in wall-clock time.87*/88boolean isWall() {89return type == WALL;90}9192/**93* @return true if the time is represented in standard time.94*/95boolean isSTD() {96return type == STD;97}9899/**100* @return true if the time is represented in UTC time.101*/102boolean isUTC() {103return type == UTC;104}105106/**107* Converts the type to a string that represents the type in the108* SimpleTimeZone time mode. (e.g., "SimpleTimeZone.WALL_TIME").109* @return the converted string or null if the type is undefined.110*/111String getTypeForSimpleTimeZone() {112String stz = "SimpleTimeZone.";113if (isWall()) {114return stz+"WALL_TIME";115}116else if (isSTD()) {117return stz+"STANDARD_TIME";118}119else if (isUTC()) {120return stz+"UTC_TIME";121}122else {123return null;124}125}126127/**128* Converts the given Gregorian calendar field values to local time.129* Local time is represented by the amount of milliseconds from130* January 1, 1970 0:00 GMT.131* @param year the year value132* @param month the Month value133* @param day the day represented by {@link RuleDay}134* @param save the amount of daylight time in milliseconds135* @param gmtOffset the GMT offset in milliseconds136* @param time the time of the day represented by {@link Time}137* @return local time138*/139static long getLocalTime(int year, Month month, RuleDay day, int save,140int gmtOffset, Time time) {141long t = time.getTime();142143if (time.isSTD())144t = time.getTime() + save;145else if (time.isUTC())146t = time.getTime() + save + gmtOffset;147148return getLocalTime(year, month, day, t);149}150151/**152* Converts the given Gregorian calendar field values to local time.153* Local time is represented by the amount of milliseconds from154* January 1, 1970 0:00 GMT.155* @param year the year value156* @param month the Month value157* @param day the day value158* @param time the time of the day in milliseconds159* @return local time160*/161static long getLocalTime(int year, Month month, int day, long time) {162CalendarDate date = gcal.newCalendarDate(null);163date.setDate(year, month.value(), day);164long millis = gcal.getTime(date);165return millis + time;166}167168/**169* Equivalent to <code>getLocalTime(year, month, day, (long)time)</code>.170* @param year the year value171* @param month the Month value172* @param day the day value173* @param time the time of the day in milliseconds174* @return local time175*/176static long getLocalTime(int year, Month month, int day, int time) {177return getLocalTime(year, month, day, (long)time);178}179180/**181* Equivalent to {@link #getLocalTime(int, Month, RuleDay, int)182* getLocalTime(year, month, day, (int) time)}.183* @param year the year value184* @param month the Month value185* @param day the day represented by {@link RuleDay}186* @param time the time of the day represented by {@link Time}187* @return local time188*/189static long getLocalTime(int year, Month month, RuleDay day, long time) {190return getLocalTime(year, month, day, (int) time);191}192193/**194* Converts the given Gregorian calendar field values to local time.195* Local time is represented by the amount of milliseconds from196* January 1, 1970 0:00 GMT.197* @param year the year value198* @param month the Month value199* @param day the day represented by {@link RuleDay}200* @param time the time of the day represented by {@link Time}201* @return local time202*/203static long getLocalTime(int year, Month month, RuleDay day, int time) {204CalendarDate cdate = gcal.newCalendarDate(null);205int monthValue = month.value();206207if (day.isLast()) { // e.g., "lastSun"208cdate.setDate(year, monthValue, 1);209cdate.setDayOfMonth(gcal.getMonthLength(cdate));210cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);211} else if (day.isLater()) { // e.g., "Sun>=1"212cdate.setDate(year, monthValue, day.getDay());213cdate = gcal.getNthDayOfWeek(1, day.getDayOfWeekNum(), cdate);214} else if (day.isExact()) {215cdate.setDate(year, monthValue, day.getDay());216} else if (day.isEarlier()) { // e.g., "Sun<=15"217cdate.setDate(year, monthValue, day.getDay());218cdate = gcal.getNthDayOfWeek(-1, day.getDayOfWeekNum(), cdate);219} else {220Main.panic("invalid day type: " + day);221}222return gcal.getTime(cdate) + time;223}224225/**226* Parses the given "AT" field and constructs a Time object.227* @param the "AT" field string228* @return the Time object229*/230static Time parse(String time) {231int sign;232int index = 0;233Time tm;234235if (time.charAt(0) == '-') {236sign = -1;237index++;238} else {239sign = 1;240}241int val = 0;242int num = 0;243int countDelim = 0;244while (index < time.length()) {245char c = time.charAt(index++);246if (c == ':') {247val = val * 60 + num;248countDelim++;249num = 0;250continue;251}252int d = Character.digit(c, 10);253if (d == -1) {254--index;255break;256}257num = num * 10 + d;258}259val = val * 60 + num;260// convert val to second261for (; countDelim < 2; countDelim++) {262val *= 60;263}264tm = new Time((long)val * 1000 * sign);265if (index < time.length()) {266char c = time.charAt(index++);267if (c == 's') {268tm.setType(Time.STD);269} else if (c == 'u' || c == 'g' || c == 'z') {270tm.setType(Time.UTC);271} else if (c == 'w') {272tm.setType(Time.WALL);273} else {274Main.panic("unknown time mode: "+c);275}276} else {277tm.setType(Time.WALL);278}279return tm;280}281282/**283* Converts the given milliseconds string to a "[+-]hh:mm" string.284* @param ms the milliseconds string285*/286static String toGMTFormat(String ms) {287long sec = Long.parseLong(ms) / 1000;288char sign;289if (sec < 0) {290sign = '-';291sec = -sec;292} else {293sign = '+';294}295return String.format((Locale)null, "%c%02d:%02d",296sign, sec/3600, (sec%3600)/60);297}298299/**300* Converts the given millisecond value to a string for a301* SimpleTimeZone parameter.302* @param ms the millisecond value303* @return the string in a human readable form304*/305static String toFormedString(int ms) {306StringBuilder s = new StringBuilder();307boolean minus = false;308309if (ms < 0) {310s.append("-");311minus = true;312ms = -ms;313} else if (ms == 0) {314return "0";315}316317int hour = ms / (60 * 60 * 1000);318ms %= (60 * 60 * 1000);319int minute = ms / (60 * 1000);320321if (hour != 0) {322if (minus && minute != 0) {323s.append("(");324}325s.append(Integer.toString(hour) + "*ONE_HOUR");326}327328if (minute != 0) {329if (hour != 0) {330s.append("+");331}332s.append(Integer.toString(minute) + "*ONE_MINUTE");333if (minus && hour != 0) {334s.append(")");335}336}337338return s.toString();339}340}341342343