001: /*
002: * @(#)ZoneRec.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.tools.javazic;
028:
029: import java.io.BufferedReader;
030: import java.io.FileReader;
031: import java.io.FileNotFoundException;
032: import java.io.IOException;
033: import java.util.ArrayList;
034: import java.util.HashMap;
035: import java.util.HashSet;
036: import java.util.StringTokenizer;
037:
038: /**
039: * ZoneRec hold information of time zone corresponding to each text
040: * line of the "Zone" part.
041: *
042: * @since 1.4
043: */
044: class ZoneRec {
045: private int gmtOffset;
046: private String ruleName;
047: private int directSave;
048: private Rule ruleRef;
049: private String format;
050: private boolean hasUntil;
051: private int untilYear;
052: private int untilMonth;
053: private RuleDay untilDay;
054: private Time untilTime;
055: private long untilInMillis;
056: private String line;
057:
058: /**
059: * @return the "UNTIL" value in milliseconds
060: */
061: Time getUntilTime() {
062: return untilTime;
063: }
064:
065: /**
066: * @return the GMT offset value in milliseconds
067: */
068: int getGmtOffset() {
069: return gmtOffset;
070: }
071:
072: /**
073: * @return the rule name to which this zone record refers
074: */
075: String getRuleName() {
076: return ruleName;
077: }
078:
079: /**
080: * @return the amount of saving time directly defined in the
081: * "RULES/SAVE" field.
082: */
083: int getDirectSave() {
084: return directSave;
085: }
086:
087: /**
088: * @return true if this zone record has a reference to a rule
089: */
090: boolean hasRuleReference() {
091: return ruleRef != null;
092: }
093:
094: /**
095: * Returns the "FORMAT" field string of this zone record. This
096: * @return the "FORMAT" field
097: */
098: String getFormat() {
099: return format;
100: }
101:
102: /**
103: * @return the year in the "UNTIL" field
104: */
105: int getUntilYear() {
106: return untilYear;
107: }
108:
109: /**
110: * Returns the "UNTIL" field value in milliseconds from Janurary
111: * 1, 1970 0:00 GMT.
112: * @param currentSave the amount of daylight saving in
113: * milliseconds that is used to adjust wall-clock time.
114: * @return the milliseconds value of the "UNTIL" field
115: */
116: long getUntilTime(int currentSave) {
117: if (untilTime.isWall()) {
118: return untilInMillis - currentSave;
119: }
120: return untilInMillis;
121: }
122:
123: /**
124: * Returns the "UNTIL" time in milliseconds without adjusting GMT
125: * offsets or daylight saving.
126: * @return local "UNTIL" time in milliseconds
127: */
128: long getLocalUntilTime() {
129: return Time.getLocalTime(untilYear, untilMonth, untilDay,
130: untilTime.getTime());
131: }
132:
133: /**
134: * Returns the "UNTIL" time in milliseconds with adjusting GMT offsets and daylight saving.
135: * @return the "UNTIL" time after the adjustment
136: */
137: long getLocalUntilTime(int save, int gmtOffset) {
138: return Time.getLocalTime(untilYear, untilMonth, untilDay, save,
139: gmtOffset, untilTime);
140: }
141:
142: /**
143: * @return the text line of this zone record
144: */
145: String getLine() {
146: return line;
147: }
148:
149: /**
150: * Sets the specified text line to this zone record
151: */
152: void setLine(String line) {
153: this .line = line;
154: }
155:
156: /**
157: * @return true if this zone record has the "UNTIL" field
158: */
159: boolean hasUntil() {
160: return this .hasUntil;
161: }
162:
163: /**
164: * Adjusts the "UNTIL" time to GMT offset if this zone record has
165: * it. <code>untilTime</code> is not adjusted to daylight saving
166: * in this method.
167: */
168: void adjustTime() {
169: if (!hasUntil()) {
170: return;
171: }
172: if (untilTime.isSTD() || untilTime.isWall()) {
173: // adjust to gmt offset only here. adjust to real
174: // wall-clock time when tracking rules
175: untilInMillis -= gmtOffset;
176: }
177: }
178:
179: /**
180: * @return the reference to the Rule object
181: */
182: Rule getRuleRef() {
183: return ruleRef;
184: }
185:
186: /**
187: * Resolves the reference to a Rule and adjusts its "UNTIL" time
188: * to GMT offset.
189: */
190: void resolve(Zoneinfo zi) {
191: if (ruleName != null && (!"-".equals(ruleName))) {
192: ruleRef = zi.getRule(ruleName);
193: }
194: adjustTime();
195: }
196:
197: /**
198: * Parses a Zone text line that is described by a StringTokenizer.
199: * @param tokens represents tokens of a Zone text line
200: * @return the zone record produced by parsing the text
201: */
202: static ZoneRec parse(StringTokenizer tokens) {
203: ZoneRec rec = new ZoneRec();
204: try {
205: rec.gmtOffset = (int) Time.parse(tokens.nextToken())
206: .getTime();
207: String token = tokens.nextToken();
208: char c = token.charAt(0);
209: if (c >= '0' && c <= '9') {
210: rec.directSave = (int) Time.parse(token).getTime();
211: } else {
212: rec.ruleName = token;
213: }
214: rec.format = tokens.nextToken();
215: if (tokens.hasMoreTokens()) {
216: rec.hasUntil = true;
217: rec.untilYear = Integer.parseInt(tokens.nextToken());
218: if (tokens.hasMoreTokens()) {
219: rec.untilMonth = Month.parse(tokens.nextToken());
220: } else {
221: rec.untilMonth = Month.parse("Jan");
222: }
223: if (tokens.hasMoreTokens()) {
224: rec.untilDay = RuleDay.parse(tokens.nextToken());
225: } else {
226: rec.untilDay = new RuleDay(1);
227: }
228: if (tokens.hasMoreTokens()) {
229: rec.untilTime = Time.parse(tokens.nextToken());
230: } else {
231: rec.untilTime = Time.parse("0:00");
232: }
233: rec.untilInMillis = rec.getLocalUntilTime();
234: }
235: } catch (Exception e) {
236: // TODO: error reporting
237: e.printStackTrace();
238: }
239: return rec;
240: }
241:
242: private static void panic(String msg) {
243: Main.panic(msg);
244: }
245: }
|