Path: blob/master/test/jdk/sun/util/calendar/zi/RuleRec.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.StringTokenizer;2425/**26* RuleRec class represents one record of the Rule set.27*28* @since 1.429*/30class RuleRec {31private int fromYear;32private int toYear;33private String type;34private Month inMonth;35private RuleDay onDay;36private Time atTime;37private int save;38private String letters;39private String line;40private boolean isLastRule;4142int getFromYear() {43return fromYear;44}4546int getToYear() {47return toYear;48}4950Month getMonth() {51return inMonth;52}5354int getMonthNum() {55return inMonth.value();56}5758RuleDay getDay() {59return onDay;60}6162Time getTime() {63return atTime;64}6566int getSave() {67return save;68}6970String getLine() {71return line;72}7374/**75* Sets the line from the text file.76* @param line the text of the line77*/78void setLine(String line) {79this.line = line;80}8182/**83* @return true if the rule type is "odd".84*/85boolean isOdd() {86return "odd".equals(type);87}8889/**90* @return true if the rule type is "even".91*/92boolean isEven() {93return "even".equals(type);94}9596/**97* Determines if this rule record is the last DST schedule rule.98*99* @return true if this rule record has "max" as TO (year).100*/101boolean isLastRule() {102return isLastRule;103}104105/**106* Determines if the unadjusted until time of the specified ZoneRec107* is the same as the transition time of this rule in the same108* year as the ZoneRec until year.109*110* @param zrec ZoneRec to compare to111* @param save the amount of daylight saving in milliseconds112* @param gmtOffset the GMT offset value in milliseconds113* @return true if the unadjusted until time is the same as rule's114* transition time.115*/116boolean isSameTransition(ZoneRec zrec, int save, int gmtOffset) {117long until, transition;118119if (zrec.getUntilTime().getType() != atTime.getType()) {120until = zrec.getLocalUntilTime(save, gmtOffset);121transition = Time.getLocalTime(zrec.getUntilYear(),122getMonth(),123getDay(),124save,125gmtOffset,126atTime);127} else {128until = zrec.getLocalUntilTime();129transition = Time.getLocalTime(zrec.getUntilYear(),130getMonth(),131getDay(),132atTime.getTime());133}134135return until == transition;136}137138/**139* Parses a Rule line and returns a RuleRec object.140*141* @param tokens a StringTokenizer object that should contain a142* token for the "FROM" field and the rest.143* @return a RuleRec object.144*/145static RuleRec parse(StringTokenizer tokens) {146RuleRec rec = new RuleRec();147try {148// FROM149String token = tokens.nextToken();150try {151rec.fromYear = Integer.parseInt(token);152} catch (NumberFormatException e) {153// it's not integer154if ("min".equals(token) || "minimum".equals(token)) {155rec.fromYear = Zoneinfo.getMinYear();156} else if ("max".equals(token) || "maximum".equals(token)) {157rec.fromYear = Zoneinfo.getMaxYear();158} else {159Main.panic("invalid year value: "+token);160}161}162163// TO164token = tokens.nextToken();165rec.isLastRule = false;166try {167rec.toYear = Integer.parseInt(token);168} catch (NumberFormatException e) {169// it's not integer170if ("min".equals(token) || "minimum".equals(token)) {171rec.fromYear = Zoneinfo.getMinYear();172} else if ("max".equals(token) || "maximum".equals(token)) {173rec.toYear = Integer.MAX_VALUE;174rec.isLastRule = true;175} else if ("only".equals(token)) {176rec.toYear = rec.fromYear;177} else {178Main.panic("invalid year value: "+token);179}180}181182// TYPE183rec.type = tokens.nextToken();184185// IN186rec.inMonth = Month.parse(tokens.nextToken());187188// ON189rec.onDay = RuleDay.parse(tokens.nextToken());190191// AT192rec.atTime = Time.parse(tokens.nextToken());193194// SAVE195rec.save = (int) Time.parse(tokens.nextToken()).getTime();196197// LETTER/S198rec.letters = tokens.nextToken();199} catch (Exception e) {200e.printStackTrace();201}202return rec;203}204205/**206* Calculates the transition time of the given year under this rule.207* @param year the year value208* @param gmtOffset the GMT offset value in milliseconds209* @param save the amount of daylight save time210* @return the transition time in milliseconds of the given year in UTC.211*/212long getTransitionTime(int year, int gmtOffset, int save) {213long time = Time.getLocalTime(year, getMonth(),214getDay(), atTime.getTime());215if (atTime.isSTD()) {216time -= gmtOffset;217} else if (atTime.isWall()) {218time -= gmtOffset + save;219}220return time;221}222223private static int getInt(StringTokenizer tokens) {224String token = tokens.nextToken();225return Integer.parseInt(token);226}227}228229230