Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/calendar/zi/Timezone.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.ArrayList;26import java.util.List;2728/**29* Timezone represents all information of a single point of time to30* generate its time zone database.31*32* @since 1.433*/34class Timezone {35/**36* zone name of this time zone37*/38private String name;3940/**41* transition time values in UTC (millisecond)42*/43private List<Long> transitions;4445/**46* All offset values in millisecond47* @see sun.util.calendar.ZoneInfo48*/49private List<Integer> offsets;5051/**52* Indices of GMT offset values (both raw and raw+saving)53* at transitions54*/55private List<Integer> gmtOffsets;5657/**58* Indices of regular or "direct" saving time values59* at transitions60*/61private List<Integer> dstOffsets;6263/**64* Zone records of this time zone65*/66private List<ZoneRec> usedZoneRecs;6768/**69* Rule records referred to by this time zone70*/71private List<RuleRec> usedRuleRecs;7273/**74* Type of DST rules in this time zone75*/76private int dstType;77static final int UNDEF_DST = 0; // DST type not set yet78static final int NO_DST = 1; // never observed DST79static final int LAST_DST = 2; // last rule ends in DST (all year round DST-only)80static final int X_DST = 3; // used to observe DST81static final int DST = 4; // observing DST regularly8283/**84* Raw GMT offset of this time zone in the last rule85*/86private int rawOffset;8788/**89* The CRC32 value of the transitions data90*/91private int crc32;9293/**94* The last ZoneRec95*/96private ZoneRec lastZoneRec;9798/**99* The last DST rules. lastRules[0] is the DST start100* rule. lastRules[1] is the DST end rules.101*/102private List<RuleRec> lastRules;103104/**105* The amount of DST saving value (millisecond) in the last DST106* rule.107*/108private int lastSaving;109110/**111* true if the raw offset will change in the future time.112*/113private boolean willRawOffsetChange = false;114115116/**117* Constracts a Timezone object with the given zone name.118* @param name the zone name119*/120Timezone(String name) {121this.name = name;122}123124/**125* @return the number of transitions126*/127int getNTransitions() {128if (transitions == null) {129return 0;130}131return transitions.size();132}133134/**135* @return the zone name136*/137String getName() {138return name;139}140141/**142* Returns the list of all rule records that have been referred to143* by this time zone.144* @return the rule records list145*/146List<RuleRec> getRules() {147return usedRuleRecs;148}149150/**151* Returns the list of all zone records that have been referred to152* by this time zone.153* @return the zone records list154*/155List<ZoneRec> getZones() {156return usedZoneRecs;157}158159/**160* @return the transition table (list)161*/162List<Long> getTransitions() {163return transitions;164}165166/**167* @return the offsets list168*/169List<Integer> getOffsets() {170return offsets;171}172173/**174* @return the DST saving offsets list175*/176List<Integer> getDstOffsets() {177return dstOffsets;178}179180/**181* @return the GMT offsets list182*/183List<Integer> getGmtOffsets() {184return gmtOffsets;185}186187/**188* @return the checksum (crc32) value of the trasition table189*/190int getCRC32() {191return crc32;192}193194/**195* @return true if the GMT offset of this time zone would change196* after the time zone database has been generated, false, otherwise.197*/198boolean willGMTOffsetChange() {199return willRawOffsetChange;200}201202/**203* @return the last known GMT offset value in milliseconds204*/205int getRawOffset() {206return rawOffset;207}208209/**210* Sets time zone's GMT offset to <code>offset</code>.211* @param offset the GMT offset value in milliseconds212*/213void setRawOffset(int offset) {214rawOffset = offset;215}216217/**218* Sets time zone's GMT offset value to <code>offset</code>. If219* <code>startTime</code> is future time, then the {@link220* #willRawOffsetChange} value is set to true.221* @param offset the GMT offset value in milliseconds222* @param startTime the UTC time at which the GMT offset is in effective223*/224void setRawOffset(int offset, long startTime) {225// if this rawOffset is for the future time, let the run-time226// look for the current GMT offset.227if (startTime > Time.getCurrentTime()) {228willRawOffsetChange = true;229}230setRawOffset(offset);231}232233/**234* Adds the specified transition information to the end of the transition table.235* @param time the UTC time at which this transition happens236* @param offset the total amount of the offset from GMT in milliseconds237* @param dstOffset the amount of time in milliseconds saved at this transition238*/239void addTransition(long time, int offset, int dstOffset) {240if (transitions == null) {241transitions = new ArrayList<Long>();242offsets = new ArrayList<Integer>();243dstOffsets = new ArrayList<Integer>();244}245transitions.add(time);246offsets.add(offset);247dstOffsets.add(dstOffset);248}249250/**251* Sets the type of historical daylight saving time252* observation. For example, China used to observed daylight253* saving time, but it no longer does. Then, X_DST is set to the254* China time zone.255* @param type the type of daylight saving time256*/257void setDSTType(int type) {258dstType = type;259}260261/**262* @return the type of historical daylight saving time263* observation.264*/265int getDSTType() {266return dstType;267}268269/**270* Adds the specified zone record to the zone records list.271* @param rec the zone record272*/273void addUsedRec(ZoneRec rec) {274if (usedZoneRecs == null) {275usedZoneRecs = new ArrayList<ZoneRec>();276}277usedZoneRecs.add(rec);278}279280/**281* Adds the specified rule record to the rule records list.282* @param rec the rule record283*/284void addUsedRec(RuleRec rec) {285if (usedRuleRecs == null) {286usedRuleRecs = new ArrayList<RuleRec>();287}288// if the last used rec is the same as the given rec, avoid289// putting the same rule.290int n = usedRuleRecs.size();291for (int i = 0; i < n; i++) {292if (usedRuleRecs.get(i).equals(rec)) {293return;294}295}296usedRuleRecs.add(rec);297}298299/**300* Sets the last zone record for this time zone.301* @param the last zone record302*/303void setLastZoneRec(ZoneRec zrec) {304lastZoneRec = zrec;305}306307/**308* @return the last zone record for this time zone.309*/310ZoneRec getLastZoneRec() {311return lastZoneRec;312}313314/**315* Sets the last rule records for this time zone. Those are used316* for generating SimpleTimeZone parameters.317* @param rules the last rule records318*/319void setLastRules(List<RuleRec> rules) {320int n = rules.size();321if (n > 0) {322lastRules = rules;323RuleRec rec = rules.get(0);324int offset = rec.getSave();325if (offset > 0) {326setLastDSTSaving(offset);327} else {328System.err.println("\t No DST starting rule in the last rules.");329}330}331}332333/**334* @return the last rule records for this time zone.335*/336List<RuleRec> getLastRules() {337return lastRules;338}339340/**341* Sets the last daylight saving amount.342* @param the daylight saving amount343*/344void setLastDSTSaving(int offset) {345lastSaving = offset;346}347348/**349* @return the last daylight saving amount.350*/351int getLastDSTSaving() {352return lastSaving;353}354355/**356* Calculates the CRC32 value from the transition table and sets357* the value to <code>crc32</code>.358*/359void checksum() {360if (transitions == null) {361crc32 = 0;362return;363}364Checksum sum = new Checksum();365for (int i = 0; i < transitions.size(); i++) {366int offset = offsets.get(i);367// adjust back to make the transition in local time368sum.update(transitions.get(i) + offset);369sum.update(offset);370sum.update(dstOffsets.get(i));371}372crc32 = (int)sum.getValue();373}374375/**376* Removes unnecessary transitions for Java time zone support.377*/378void optimize() {379// if there is only one offset, delete all transitions. This380// could happen if only time zone abbreviations changed.381if (gmtOffsets.size() == 1) {382transitions = null;383usedRuleRecs = null;384setDSTType(NO_DST);385return;386}387for (int i = 0; i < (transitions.size() - 2); i++) { // don't remove the last one388if (transitions.get(i) == transitions.get(i+1)) {389transitions.remove(i);390offsets.remove(i);391dstOffsets.remove(i);392i--;393}394}395396for (int i = 0; i < (transitions.size() - 2); i++) { // don't remove the last one397if (offsets.get(i) == offsets.get(i+1)398&& dstOffsets.get(i) == dstOffsets.get(i+1)) {399transitions.remove(i+1);400offsets.remove(i+1);401dstOffsets.remove(i+1);402i--;403}404}405}406407/**408* Stores the specified offset value from GMT in the GMT offsets409* table and returns its index. The offset value includes the base410* GMT offset and any additional daylight saving if applicable. If411* the same value as the specified offset is already in the table,412* its index is returned.413* @param offset the offset value in milliseconds414* @return the index to the offset value in the GMT offsets table.415*/416int getOffsetIndex(int offset) {417return getOffsetIndex(offset, 0);418}419420/**421* Stores the specified daylight saving value in the GMT offsets422* table and returns its index. If the same value as the specified423* offset is already in the table, its index is returned. If 0 is424* specified, it's not stored in the table and -1 is returned.425* @param offset the offset value in milliseconds426* @return the index to the specified offset value in the GMT427* offsets table, or -1 if 0 is specified.428*/429int getDstOffsetIndex(int offset) {430if (offset == 0) {431return -1;432}433return getOffsetIndex(offset, 1);434}435436private int getOffsetIndex(int offset, int index) {437if (gmtOffsets == null) {438gmtOffsets = new ArrayList<Integer>();439}440for (int i = index; i < gmtOffsets.size(); i++) {441if (offset == gmtOffsets.get(i)) {442return i;443}444}445if (gmtOffsets.size() < index) {446gmtOffsets.add(0);447}448gmtOffsets.add(offset);449return gmtOffsets.size() - 1;450}451}452453454