Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/calendar/zi/RuleRec.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.StringTokenizer;2627/**28* RuleRec class represents one record of the Rule set.29*30* @since 1.431*/32class RuleRec {33private int fromYear;34private int toYear;35private String type;36private Month inMonth;37private RuleDay onDay;38private Time atTime;39private int save;40private String letters;41private String line;42private boolean isLastRule;4344int getFromYear() {45return fromYear;46}4748int getToYear() {49return toYear;50}5152Month getMonth() {53return inMonth;54}5556int getMonthNum() {57return inMonth.value();58}5960RuleDay getDay() {61return onDay;62}6364Time getTime() {65return atTime;66}6768int getSave() {69return save;70}7172String getLine() {73return line;74}7576/**77* Sets the line from the text file.78* @param line the text of the line79*/80void setLine(String line) {81this.line = line;82}8384/**85* @return true if the rule type is "odd".86*/87boolean isOdd() {88return "odd".equals(type);89}9091/**92* @return true if the rule type is "even".93*/94boolean isEven() {95return "even".equals(type);96}9798/**99* Determines if this rule record is the last DST schedule rule.100*101* @return true if this rule record has "max" as TO (year).102*/103boolean isLastRule() {104return isLastRule;105}106107/**108* Determines if the unadjusted until time of the specified ZoneRec109* is the same as the transition time of this rule in the same110* year as the ZoneRec until year.111*112* @param zrec ZoneRec to compare to113* @param save the amount of daylight saving in milliseconds114* @param gmtOffset the GMT offset value in milliseconds115* @return true if the unadjusted until time is the same as rule's116* transition time.117*/118boolean isSameTransition(ZoneRec zrec, int save, int gmtOffset) {119long until, transition;120121if (zrec.getUntilTime().getType() != atTime.getType()) {122until = zrec.getLocalUntilTime(save, gmtOffset);123transition = Time.getLocalTime(zrec.getUntilYear(),124getMonth(),125getDay(),126save,127gmtOffset,128atTime);129} else {130until = zrec.getLocalUntilTime();131transition = Time.getLocalTime(zrec.getUntilYear(),132getMonth(),133getDay(),134atTime.getTime());135}136137return until == transition;138}139140/**141* Parses a Rule line and returns a RuleRec object.142*143* @param tokens a StringTokenizer object that should contain a144* token for the "FROM" field and the rest.145* @return a RuleRec object.146*/147static RuleRec parse(StringTokenizer tokens) {148RuleRec rec = new RuleRec();149try {150// FROM151String token = tokens.nextToken();152try {153rec.fromYear = Integer.parseInt(token);154} catch (NumberFormatException e) {155// it's not integer156if ("min".equals(token) || "minimum".equals(token)) {157rec.fromYear = Zoneinfo.getMinYear();158} else if ("max".equals(token) || "maximum".equals(token)) {159rec.fromYear = Zoneinfo.getMaxYear();160} else {161Main.panic("invalid year value: "+token);162}163}164165// TO166token = tokens.nextToken();167rec.isLastRule = false;168try {169rec.toYear = Integer.parseInt(token);170} catch (NumberFormatException e) {171// it's not integer172if ("min".equals(token) || "minimum".equals(token)) {173rec.fromYear = Zoneinfo.getMinYear();174} else if ("max".equals(token) || "maximum".equals(token)) {175rec.toYear = Integer.MAX_VALUE;176rec.isLastRule = true;177} else if ("only".equals(token)) {178rec.toYear = rec.fromYear;179} else {180Main.panic("invalid year value: "+token);181}182}183184// TYPE185rec.type = tokens.nextToken();186187// IN188rec.inMonth = Month.parse(tokens.nextToken());189190// ON191rec.onDay = RuleDay.parse(tokens.nextToken());192193// AT194rec.atTime = Time.parse(tokens.nextToken());195196// SAVE197rec.save = (int) Time.parse(tokens.nextToken()).getTime();198199// LETTER/S200rec.letters = tokens.nextToken();201} catch (Exception e) {202e.printStackTrace();203}204return rec;205}206207/**208* Calculates the transition time of the given year under this rule.209* @param year the year value210* @param gmtOffset the GMT offset value in milliseconds211* @param save the amount of daylight save time212* @return the transition time in milliseconds of the given year in UTC.213*/214long getTransitionTime(int year, int gmtOffset, int save) {215long time = Time.getLocalTime(year, getMonth(),216getDay(), atTime.getTime());217if (atTime.isSTD()) {218time -= gmtOffset;219} else if (atTime.isWall()) {220time -= gmtOffset + save;221}222return time;223}224225private static int getInt(StringTokenizer tokens) {226String token = tokens.nextToken();227return Integer.parseInt(token);228}229}230231232