Path: blob/master/test/jdk/sun/util/calendar/zi/ZoneRec.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.io.BufferedReader;24import java.io.FileReader;25import java.io.FileNotFoundException;26import java.io.IOException;27import java.util.ArrayList;28import java.util.HashMap;29import java.util.HashSet;30import java.util.List;31import java.util.Map;32import java.util.Set;33import java.util.StringTokenizer;3435/**36* ZoneRec hold information of time zone corresponding to each text37* line of the "Zone" part.38*39* @since 1.440*/41class ZoneRec {42private int gmtOffset;43private String ruleName;44private int directSave;45private Rule ruleRef;46private String format;47private boolean hasUntil;48private int untilYear;49private Month untilMonth;50private RuleDay untilDay;51private Time untilTime;52private long untilInMillis;53private String line;5455/**56* @return the "UNTIL" value in milliseconds57*/58Time getUntilTime() {59return untilTime;60}6162/**63* @return the GMT offset value in milliseconds64*/65int getGmtOffset() {66return gmtOffset;67}6869/**70* @return the rule name to which this zone record refers71*/72String getRuleName() {73return ruleName;74}7576/**77* @return the amount of saving time directly defined in the78* "RULES/SAVE" field.79*/80int getDirectSave() {81return directSave;82}8384/**85* @return true if this zone record has a reference to a rule86*/87boolean hasRuleReference() {88return ruleRef != null;89}9091/**92* Returns the "FORMAT" field string of this zone record. This93* @return the "FORMAT" field94*/95String getFormat() {96return format;97}9899/**100* @return the year in the "UNTIL" field101*/102int getUntilYear() {103return untilYear;104}105106/**107* Returns the "UNTIL" field value in milliseconds from Janurary108* 1, 1970 0:00 GMT.109* @param currentSave the amount of daylight saving in110* milliseconds that is used to adjust wall-clock time.111* @return the milliseconds value of the "UNTIL" field112*/113long getUntilTime(int currentSave) {114if (untilTime.isWall()) {115return untilInMillis - currentSave;116}117return untilInMillis;118}119120/**121* Returns the "UNTIL" time in milliseconds without adjusting GMT122* offsets or daylight saving.123* @return local "UNTIL" time in milliseconds124*/125long getLocalUntilTime() {126return Time.getLocalTime(untilYear,127untilMonth,128untilDay,129untilTime.getTime());130}131132/**133* Returns the "UNTIL" time in milliseconds with adjusting GMT offsets and daylight saving.134* @return the "UNTIL" time after the adjustment135*/136long getLocalUntilTime(int save, int gmtOffset) {137return Time.getLocalTime(untilYear,138untilMonth,139untilDay,140save,141gmtOffset,142untilTime);143}144145/**146* @return the text line of this zone record147*/148String getLine() {149return line;150}151152/**153* Sets the specified text line to this zone record154*/155void setLine(String line) {156this.line = line;157}158159/**160* @return true if this zone record has the "UNTIL" field161*/162boolean hasUntil() {163return this.hasUntil;164}165166/**167* Adjusts the "UNTIL" time to GMT offset if this zone record has168* it. <code>untilTime</code> is not adjusted to daylight saving169* in this method.170*/171void adjustTime() {172if (!hasUntil()) {173return;174}175if (untilTime.isSTD() || untilTime.isWall()) {176// adjust to gmt offset only here. adjust to real177// wall-clock time when tracking rules178untilInMillis -= gmtOffset;179}180}181182/**183* @return the reference to the Rule object184*/185Rule getRuleRef() {186return ruleRef;187}188189/**190* Resolves the reference to a Rule and adjusts its "UNTIL" time191* to GMT offset.192*/193void resolve(Zoneinfo zi) {194if (ruleName != null && (!"-".equals(ruleName))) {195ruleRef = zi.getRule(ruleName);196}197adjustTime();198}199200/**201* Parses a Zone text line that is described by a StringTokenizer.202* @param tokens represents tokens of a Zone text line203* @return the zone record produced by parsing the text204*/205static ZoneRec parse(StringTokenizer tokens) {206ZoneRec rec = new ZoneRec();207try {208rec.gmtOffset = (int) Time.parse(tokens.nextToken()).getTime();209String token = tokens.nextToken();210char c = token.charAt(0);211if (c >= '0' && c <= '9') {212rec.directSave = (int) Time.parse(token).getTime();213} else {214rec.ruleName = token;215}216rec.format = tokens.nextToken();217if (tokens.hasMoreTokens()) {218rec.hasUntil = true;219rec.untilYear = Integer.parseInt(tokens.nextToken());220if (tokens.hasMoreTokens()) {221rec.untilMonth = Month.parse(tokens.nextToken());222} else {223rec.untilMonth = Month.JANUARY;224}225if (tokens.hasMoreTokens()) {226rec.untilDay = RuleDay.parse(tokens.nextToken());227} else {228rec.untilDay = new RuleDay(1);229}230if (tokens.hasMoreTokens()) {231rec.untilTime = Time.parse(tokens.nextToken());232} else {233rec.untilTime = Time.parse("0:00");234}235rec.untilInMillis = rec.getLocalUntilTime();236}237} catch (Exception e) {238// TODO: error reporting239e.printStackTrace();240}241return rec;242}243244private static void panic(String msg) {245Main.panic(msg);246}247}248249250