Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/tzdb/ZoneRulesBuilder.java
32287 views
/*1* Copyright (c) 2012, 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*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package build.tools.tzdb;6263import static build.tools.tzdb.Utils.*;6465import java.util.ArrayList;66import java.util.Collections;67import java.util.HashMap;68import java.util.List;69import java.util.Map;70import java.util.Objects;7172/**73* A mutable builder used to create all the rules for a historic time-zone.74* <p>75* The rules of a time-zone describe how the offset changes over time.76* The rules are created by building windows on the time-line within which77* the different rules apply. The rules may be one of two kinds:78* <p><ul>79* <li>Fixed savings - A single fixed amount of savings from the standard offset will apply.</li>80* <li>Rules - A set of one or more rules describe how daylight savings changes during the window.</li>81* </ul><p>82*83* <h4>Implementation notes</h4>84* This class is a mutable builder used to create zone instances.85* It must only be used from a single thread.86* The created instances are immutable and thread-safe.87*88* @since 1.889*/90public class ZoneRulesBuilder {9192/**93* The list of windows.94*/95private List<TZWindow> windowList = new ArrayList<>();9697//-----------------------------------------------------------------------98/**99* Constructs an instance of the builder that can be used to create zone rules.100* <p>101* The builder is used by adding one or more windows representing portions102* of the time-line. The standard offset from UTC/Greenwich will be constant103* within a window, although two adjacent windows can have the same standard offset.104* <p>105* Within each window, there can either be a106* {@link #setFixedSavingsToWindow fixed savings amount} or a107* {@link #addRuleToWindow list of rules}.108*/109public ZoneRulesBuilder() {110}111112//-----------------------------------------------------------------------113/**114* Adds a window to the builder that can be used to filter a set of rules.115* <p>116* This method defines and adds a window to the zone where the standard offset is specified.117* The window limits the effect of subsequent additions of transition rules118* or fixed savings. If neither rules or fixed savings are added to the window119* then the window will default to no savings.120* <p>121* Each window must be added sequentially, as the start instant of the window122* is derived from the until instant of the previous window.123*124* @param standardOffset the standard offset, not null125* @param until the date-time that the offset applies until, not null126* @param untilDefinition the time type for the until date-time, not null127* @return this, for chaining128* @throws IllegalStateException if the window order is invalid129*/130public ZoneRulesBuilder addWindow(131ZoneOffset standardOffset,132LocalDateTime until,133TimeDefinition untilDefinition) {134Objects.requireNonNull(standardOffset, "standardOffset");135Objects.requireNonNull(until, "until");136Objects.requireNonNull(untilDefinition, "untilDefinition");137TZWindow window = new TZWindow(standardOffset, until, untilDefinition);138if (windowList.size() > 0) {139TZWindow previous = windowList.get(windowList.size() - 1);140window.validateWindowOrder(previous);141}142windowList.add(window);143return this;144}145146/**147* Adds a window that applies until the end of time to the builder that can be148* used to filter a set of rules.149* <p>150* This method defines and adds a window to the zone where the standard offset is specified.151* The window limits the effect of subsequent additions of transition rules152* or fixed savings. If neither rules or fixed savings are added to the window153* then the window will default to no savings.154* <p>155* This must be added after all other windows.156* No more windows can be added after this one.157*158* @param standardOffset the standard offset, not null159* @return this, for chaining160* @throws IllegalStateException if a forever window has already been added161*/162public ZoneRulesBuilder addWindowForever(ZoneOffset standardOffset) {163return addWindow(standardOffset, LocalDateTime.MAX, TimeDefinition.WALL);164}165166//-----------------------------------------------------------------------167/**168* Sets the previously added window to have fixed savings.169* <p>170* Setting a window to have fixed savings simply means that a single daylight171* savings amount applies throughout the window. The window could be small,172* such as a single summer, or large, such as a multi-year daylight savings.173* <p>174* A window can either have fixed savings or rules but not both.175*176* @param fixedSavingAmountSecs the amount of saving to use for the whole window, not null177* @return this, for chaining178* @throws IllegalStateException if no window has yet been added179* @throws IllegalStateException if the window already has rules180*/181public ZoneRulesBuilder setFixedSavingsToWindow(int fixedSavingAmountSecs) {182if (windowList.isEmpty()) {183throw new IllegalStateException("Must add a window before setting the fixed savings");184}185TZWindow window = windowList.get(windowList.size() - 1);186window.setFixedSavings(fixedSavingAmountSecs);187return this;188}189190//-----------------------------------------------------------------------191/**192* Adds a single transition rule to the current window.193* <p>194* This adds a rule such that the offset, expressed as a daylight savings amount,195* changes at the specified date-time.196*197* @param transitionDateTime the date-time that the transition occurs as defined by timeDefintion, not null198* @param timeDefinition the definition of how to convert local to actual time, not null199* @param savingAmountSecs the amount of saving from the standard offset after the transition in seconds200* @return this, for chaining201* @throws IllegalStateException if no window has yet been added202* @throws IllegalStateException if the window already has fixed savings203* @throws IllegalStateException if the window has reached the maximum capacity of 2000 rules204*/205public ZoneRulesBuilder addRuleToWindow(206LocalDateTime transitionDateTime,207TimeDefinition timeDefinition,208int savingAmountSecs) {209Objects.requireNonNull(transitionDateTime, "transitionDateTime");210return addRuleToWindow(211transitionDateTime.getYear(), transitionDateTime.getYear(),212transitionDateTime.getMonth(), transitionDateTime.getDayOfMonth(),213-1, transitionDateTime.getTime(), false, timeDefinition, savingAmountSecs);214}215216/**217* Adds a single transition rule to the current window.218* <p>219* This adds a rule such that the offset, expressed as a daylight savings amount,220* changes at the specified date-time.221*222* @param year the year of the transition, from MIN_YEAR to MAX_YEAR223* @param month the month of the transition, not null224* @param dayOfMonthIndicator the day-of-month of the transition, adjusted by dayOfWeek,225* from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month226* @param time the time that the transition occurs as defined by timeDefintion, not null227* @param timeEndOfDay whether midnight is at the end of day228* @param timeDefinition the definition of how to convert local to actual time, not null229* @param savingAmountSecs the amount of saving from the standard offset after the transition in seconds230* @return this, for chaining231* @throws DateTimeException if a date-time field is out of range232* @throws IllegalStateException if no window has yet been added233* @throws IllegalStateException if the window already has fixed savings234* @throws IllegalStateException if the window has reached the maximum capacity of 2000 rules235*/236public ZoneRulesBuilder addRuleToWindow(237int year,238int month,239int dayOfMonthIndicator,240LocalTime time,241boolean timeEndOfDay,242TimeDefinition timeDefinition,243int savingAmountSecs) {244return addRuleToWindow(year, year, month, dayOfMonthIndicator, -1, time, timeEndOfDay, timeDefinition, savingAmountSecs);245}246247/**248* Adds a multi-year transition rule to the current window.249* <p>250* This adds a rule such that the offset, expressed as a daylight savings amount,251* changes at the specified date-time for each year in the range.252*253* @param startYear the start year of the rule, from MIN_YEAR to MAX_YEAR254* @param endYear the end year of the rule, from MIN_YEAR to MAX_YEAR255* @param month the month of the transition, from 1 to 12256* @param dayOfMonthIndicator the day-of-month of the transition, adjusted by dayOfWeek,257* from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month258* @param dayOfWeek the day-of-week to adjust to, -1 if day-of-month should not be adjusted259* @param time the time that the transition occurs as defined by timeDefintion, not null260* @param timeEndOfDay whether midnight is at the end of day261* @param timeDefinition the definition of how to convert local to actual time, not null262* @param savingAmountSecs the amount of saving from the standard offset after the transition in seconds263* @return this, for chaining264* @throws DateTimeException if a date-time field is out of range265* @throws IllegalArgumentException if the day of month indicator is invalid266* @throws IllegalArgumentException if the end of day midnight flag does not match the time267* @throws IllegalStateException if no window has yet been added268* @throws IllegalStateException if the window already has fixed savings269* @throws IllegalStateException if the window has reached the maximum capacity of 2000 rules270*/271public ZoneRulesBuilder addRuleToWindow(272int startYear,273int endYear,274int month,275int dayOfMonthIndicator,276int dayOfWeek,277LocalTime time,278boolean timeEndOfDay,279TimeDefinition timeDefinition,280int savingAmountSecs) {281Objects.requireNonNull(time, "time");282Objects.requireNonNull(timeDefinition, "timeDefinition");283if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {284throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");285}286if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {287throw new IllegalArgumentException("Time must be midnight when end of day flag is true");288}289if (windowList.isEmpty()) {290throw new IllegalStateException("Must add a window before adding a rule");291}292TZWindow window = windowList.get(windowList.size() - 1);293window.addRule(startYear, endYear, month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay, timeDefinition, savingAmountSecs);294return this;295}296297//-----------------------------------------------------------------------298/**299* Completes the build converting the builder to a set of time-zone rules.300* <p>301* Calling this method alters the state of the builder.302* Further rules should not be added to this builder once this method is called.303*304* @param zoneId the time-zone ID, not null305* @return the zone rules, not null306* @throws IllegalStateException if no windows have been added307* @throws IllegalStateException if there is only one rule defined as being forever for any given window308*/309public ZoneRules toRules(String zoneId) {310Objects.requireNonNull(zoneId, "zoneId");311if (windowList.isEmpty()) {312throw new IllegalStateException("No windows have been added to the builder");313}314315final List<ZoneOffsetTransition> standardTransitionList = new ArrayList<>(4);316final List<ZoneOffsetTransition> transitionList = new ArrayList<>(256);317final List<ZoneOffsetTransitionRule> lastTransitionRuleList = new ArrayList<>(2);318319// initialize the standard offset calculation320final TZWindow firstWindow = windowList.get(0);321ZoneOffset loopStandardOffset = firstWindow.standardOffset;322int loopSavings = 0;323if (firstWindow.fixedSavingAmountSecs != null) {324loopSavings = firstWindow.fixedSavingAmountSecs;325}326final ZoneOffset firstWallOffset = ZoneOffset.ofTotalSeconds(loopStandardOffset.getTotalSeconds() + loopSavings);327LocalDateTime loopWindowStart = LocalDateTime.of(YEAR_MIN_VALUE, 1, 1, 0, 0);328ZoneOffset loopWindowOffset = firstWallOffset;329330// build the windows and rules to interesting data331for (TZWindow window : windowList) {332// tidy the state333window.tidy(loopWindowStart.getYear());334335// calculate effective savings at the start of the window336Integer effectiveSavings = window.fixedSavingAmountSecs;337if (effectiveSavings == null) {338// apply rules from this window together with the standard offset and339// savings from the last window to find the savings amount applicable340// at start of this window341effectiveSavings = 0;342for (TZRule rule : window.ruleList) {343if (rule.toEpochSecond(loopStandardOffset, loopSavings) > loopWindowStart.toEpochSecond(loopWindowOffset)) {344// previous savings amount found, which could be the savings amount at345// the instant that the window starts (hence isAfter)346break;347}348effectiveSavings = rule.savingAmountSecs;349}350}351352// check if standard offset changed, and update it353if (loopStandardOffset.equals(window.standardOffset) == false) {354standardTransitionList.add(355new ZoneOffsetTransition(356LocalDateTime.ofEpochSecond(loopWindowStart.toEpochSecond(loopWindowOffset), 0, loopStandardOffset),357loopStandardOffset, window.standardOffset));358loopStandardOffset = window.standardOffset;359}360361// check if the start of the window represents a transition362ZoneOffset effectiveWallOffset = ZoneOffset.ofTotalSeconds(loopStandardOffset.getTotalSeconds() + effectiveSavings);363if (loopWindowOffset.equals(effectiveWallOffset) == false) {364transitionList.add(new ZoneOffsetTransition(loopWindowStart, loopWindowOffset, effectiveWallOffset));365}366loopSavings = effectiveSavings;367368// apply rules within the window369for (TZRule rule : window.ruleList) {370if (rule.isTransition(loopSavings)) {371ZoneOffsetTransition trans = rule.toTransition(loopStandardOffset, loopSavings);372if (trans.toEpochSecond() < loopWindowStart.toEpochSecond(loopWindowOffset) == false &&373trans.toEpochSecond() < window.createDateTimeEpochSecond(loopSavings)) {374transitionList.add(trans);375loopSavings = rule.savingAmountSecs;376}377}378}379380// calculate last rules381for (TZRule lastRule : window.lastRuleList) {382lastTransitionRuleList.add(lastRule.toTransitionRule(loopStandardOffset, loopSavings));383loopSavings = lastRule.savingAmountSecs;384}385386// finally we can calculate the true end of the window, passing it to the next window387loopWindowOffset = window.createWallOffset(loopSavings);388loopWindowStart = LocalDateTime.ofEpochSecond(389window.createDateTimeEpochSecond(loopSavings), 0, loopWindowOffset);390}391392return new ZoneRules(393firstWindow.standardOffset, firstWallOffset, standardTransitionList,394transitionList, lastTransitionRuleList);395}396397//-----------------------------------------------------------------------398/**399* A definition of a window in the time-line.400* The window will have one standard offset and will either have a401* fixed DST savings or a set of rules.402*/403class TZWindow {404/** The standard offset during the window, not null. */405private final ZoneOffset standardOffset;406/** The end local time, not null. */407private final LocalDateTime windowEnd;408/** The type of the end time, not null. */409private final TimeDefinition timeDefinition;410411/** The fixed amount of the saving to be applied during this window. */412private Integer fixedSavingAmountSecs;413/** The rules for the current window. */414private List<TZRule> ruleList = new ArrayList<>();415/** The latest year that the last year starts at. */416private int maxLastRuleStartYear = YEAR_MIN_VALUE;417/** The last rules. */418private List<TZRule> lastRuleList = new ArrayList<>();419420/**421* Constructor.422*423* @param standardOffset the standard offset applicable during the window, not null424* @param windowEnd the end of the window, relative to the time definition, null if forever425* @param timeDefinition the time definition for calculating the true end, not null426*/427TZWindow(428ZoneOffset standardOffset,429LocalDateTime windowEnd,430TimeDefinition timeDefinition) {431super();432this.windowEnd = windowEnd;433this.timeDefinition = timeDefinition;434this.standardOffset = standardOffset;435}436437/**438* Sets the fixed savings amount for the window.439*440* @param fixedSavingAmount the amount of daylight saving to apply throughout the window, may be null441* @throws IllegalStateException if the window already has rules442*/443void setFixedSavings(int fixedSavingAmount) {444if (ruleList.size() > 0 || lastRuleList.size() > 0) {445throw new IllegalStateException("Window has DST rules, so cannot have fixed savings");446}447this.fixedSavingAmountSecs = fixedSavingAmount;448}449450/**451* Adds a rule to the current window.452*453* @param startYear the start year of the rule, from MIN_YEAR to MAX_YEAR454* @param endYear the end year of the rule, from MIN_YEAR to MAX_YEAR455* @param month the month of the transition, not null456* @param dayOfMonthIndicator the day-of-month of the transition, adjusted by dayOfWeek,457* from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month458* @param dayOfWeek the day-of-week to adjust to, null if day-of-month should not be adjusted459* @param time the time that the transition occurs as defined by timeDefintion, not null460* @param timeEndOfDay whether midnight is at the end of day461* @param timeDefinition the definition of how to convert local to actual time, not null462* @param savingAmountSecs the amount of saving from the standard offset in seconds463* @throws IllegalStateException if the window already has fixed savings464* @throws IllegalStateException if the window has reached the maximum capacity of 2000 rules465*/466void addRule(467int startYear,468int endYear,469int month,470int dayOfMonthIndicator,471int dayOfWeek,472LocalTime time,473boolean timeEndOfDay,474TimeDefinition timeDefinition,475int savingAmountSecs) {476477if (fixedSavingAmountSecs != null) {478throw new IllegalStateException("Window has a fixed DST saving, so cannot have DST rules");479}480if (ruleList.size() >= 2000) {481throw new IllegalStateException("Window has reached the maximum number of allowed rules");482}483boolean lastRule = false;484if (endYear == YEAR_MAX_VALUE) {485lastRule = true;486endYear = startYear;487}488int year = startYear;489while (year <= endYear) {490TZRule rule = new TZRule(year, month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay, timeDefinition, savingAmountSecs);491if (lastRule) {492lastRuleList.add(rule);493} else {494ruleList.add(rule);495}496maxLastRuleStartYear = Math.max(startYear, maxLastRuleStartYear);497year++;498}499}500501/**502* Validates that this window is after the previous one.503*504* @param previous the previous window, not null505* @throws IllegalStateException if the window order is invalid506*/507void validateWindowOrder(TZWindow previous) {508if (windowEnd.compareTo(previous.windowEnd) < 0) {509throw new IllegalStateException("Windows must be added in date-time order: " +510windowEnd + " < " + previous.windowEnd);511}512}513514/**515* Adds rules to make the last rules all start from the same year.516* Also add one more year to avoid weird case where penultimate year has odd offset.517*518* @param windowStartYear the window start year519* @throws IllegalStateException if there is only one rule defined as being forever520*/521void tidy(int windowStartYear) {522if (lastRuleList.size() == 1) {523throw new IllegalStateException("Cannot have only one rule defined as being forever");524}525526// handle last rules527if (windowEnd.equals(LocalDateTime.MAX)) {528// setup at least one real rule, which closes off other windows nicely529maxLastRuleStartYear = Math.max(maxLastRuleStartYear, windowStartYear) + 1;530for (TZRule lastRule : lastRuleList) {531addRule(lastRule.year, maxLastRuleStartYear, lastRule.month, lastRule.dayOfMonthIndicator,532lastRule.dayOfWeek, lastRule.time, lastRule.timeEndOfDay, lastRule.timeDefinition, lastRule.savingAmountSecs);533lastRule.year = maxLastRuleStartYear + 1;534}535if (maxLastRuleStartYear == YEAR_MAX_VALUE) {536lastRuleList.clear();537} else {538maxLastRuleStartYear++;539}540} else {541// convert all within the endYear limit542int endYear = windowEnd.getYear();543for (TZRule lastRule : lastRuleList) {544addRule(lastRule.year, endYear + 1, lastRule.month, lastRule.dayOfMonthIndicator,545lastRule.dayOfWeek, lastRule.time, lastRule.timeEndOfDay, lastRule.timeDefinition, lastRule.savingAmountSecs);546}547lastRuleList.clear();548maxLastRuleStartYear = YEAR_MAX_VALUE;549}550551// ensure lists are sorted552Collections.sort(ruleList);553Collections.sort(lastRuleList);554555// default fixed savings to zero556if (ruleList.size() == 0 && fixedSavingAmountSecs == null) {557fixedSavingAmountSecs = 0;558}559}560561/**562* Checks if the window is empty.563*564* @return true if the window is only a standard offset565*/566boolean isSingleWindowStandardOffset() {567return windowEnd.equals(LocalDateTime.MAX) && timeDefinition == TimeDefinition.WALL &&568fixedSavingAmountSecs == null && lastRuleList.isEmpty() && ruleList.isEmpty();569}570571/**572* Creates the wall offset for the local date-time at the end of the window.573*574* @param savingsSecs the amount of savings in use in seconds575* @return the created date-time epoch second in the wall offset, not null576*/577ZoneOffset createWallOffset(int savingsSecs) {578return ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingsSecs);579}580581/**582* Creates the offset date-time for the local date-time at the end of the window.583*584* @param savingsSecs the amount of savings in use in seconds585* @return the created date-time epoch second in the wall offset, not null586*/587long createDateTimeEpochSecond(int savingsSecs) {588ZoneOffset wallOffset = createWallOffset(savingsSecs);589LocalDateTime ldt = timeDefinition.createDateTime(windowEnd, standardOffset, wallOffset);590return ldt.toEpochSecond(wallOffset);591}592}593594//-----------------------------------------------------------------------595/**596* A definition of the way a local time can be converted to an offset time.597*/598class TZRule implements Comparable<TZRule> {599private int year;600private int month;601private int dayOfMonthIndicator;602private int dayOfWeek;603private LocalTime time;604private boolean timeEndOfDay; // Whether the local time is end of day.605private TimeDefinition timeDefinition; // The type of the time.606private int savingAmountSecs; // The amount of the saving to be applied after this point.607608/**609* Constructor.610*611* @param year the year612* @param month the month, value from 1 to 12613* @param dayOfMonthIndicator the day-of-month of the transition, adjusted by dayOfWeek,614* from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month615* @param dayOfWeek the day-of-week, -1 if day-of-month is exact616* @param time the time, not null617* @param timeEndOfDay whether midnight is at the end of day618* @param timeDefinition the time definition, not null619* @param savingAfterSecs the savings amount in seconds620*/621TZRule(int year, int month, int dayOfMonthIndicator,622int dayOfWeek, LocalTime time, boolean timeEndOfDay,623TimeDefinition timeDefinition, int savingAfterSecs) {624this.year = year;625this.month = month;626this.dayOfMonthIndicator = dayOfMonthIndicator;627this.dayOfWeek = dayOfWeek;628this.time = time;629this.timeEndOfDay = timeEndOfDay;630this.timeDefinition = timeDefinition;631this.savingAmountSecs = savingAfterSecs;632}633634/**635* Converts this to a transition.636*637* @param standardOffset the active standard offset, not null638* @param savingsBeforeSecs the active savings in seconds639* @return the transition, not null640*/641ZoneOffsetTransition toTransition(ZoneOffset standardOffset, int savingsBeforeSecs) {642// copy of code in ZoneOffsetTransitionRule to avoid infinite loop643LocalDate date = toLocalDate();644LocalDateTime ldt = LocalDateTime.of(date, time);645ZoneOffset wallOffset = ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingsBeforeSecs);646LocalDateTime dt = timeDefinition.createDateTime(ldt, standardOffset, wallOffset);647ZoneOffset offsetAfter = ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingAmountSecs);648return new ZoneOffsetTransition(dt, wallOffset, offsetAfter);649}650651/**652* Returns the apoch second of this rules with the specified653* active standard offset and active savings654*655* @param standardOffset the active standard offset, not null656* @param savingsBeforeSecs the active savings in seconds657* @return the transition epoch second658*/659long toEpochSecond(ZoneOffset standardOffset, int savingsBeforeSecs) {660LocalDateTime ldt = LocalDateTime.of(toLocalDate(), time);661ZoneOffset wallOffset = ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingsBeforeSecs);662return timeDefinition.createDateTime(ldt, standardOffset, wallOffset)663.toEpochSecond(wallOffset);664}665666/**667* Tests if this a real transition with the active savings in seconds668*669* @param savingsBeforeSecs the active savings in seconds670* @return true, if savings in seconds changes671*/672boolean isTransition(int savingsBeforeSecs) {673return savingAmountSecs != savingsBeforeSecs;674}675676/**677* Converts this to a transition rule.678*679* @param standardOffset the active standard offset, not null680* @param savingsBeforeSecs the active savings before the transition in seconds681* @return the transition, not null682*/683ZoneOffsetTransitionRule toTransitionRule(ZoneOffset standardOffset, int savingsBeforeSecs) {684// optimize stored format685if (dayOfMonthIndicator < 0) {686if (month != 2) { // not Month.FEBRUARY687dayOfMonthIndicator = maxLengthOfMonth(month) - 6;688}689}690if (timeEndOfDay && dayOfMonthIndicator > 0 &&691(dayOfMonthIndicator == 28 && month == 2) == false) {692LocalDate date = LocalDate.of(2004, month, dayOfMonthIndicator).plusDays(1); // leap-year693month = date.getMonth();694dayOfMonthIndicator = date.getDayOfMonth();695if (dayOfWeek != -1) {696dayOfWeek = plusDayOfWeek(dayOfWeek, 1);697}698timeEndOfDay = false;699}700// build rule701return new ZoneOffsetTransitionRule(702month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay, timeDefinition,703standardOffset,704ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingsBeforeSecs),705ZoneOffset.ofTotalSeconds(standardOffset.getTotalSeconds() + savingAmountSecs));706}707708public int compareTo(TZRule other) {709int cmp = year - other.year;710cmp = (cmp == 0 ? month - other.month : cmp);711if (cmp == 0) {712// convert to date to handle dow/domIndicator/timeEndOfDay713LocalDate thisDate = toLocalDate();714LocalDate otherDate = other.toLocalDate();715cmp = thisDate.compareTo(otherDate);716}717cmp = (cmp == 0 ? time.compareTo(other.time) : cmp);718return cmp;719}720721private LocalDate toLocalDate() {722LocalDate date;723if (dayOfMonthIndicator < 0) {724int monthLen = lengthOfMonth(month, isLeapYear(year));725date = LocalDate.of(year, month, monthLen + 1 + dayOfMonthIndicator);726if (dayOfWeek != -1) {727date = previousOrSame(date, dayOfWeek);728}729} else {730date = LocalDate.of(year, month, dayOfMonthIndicator);731if (dayOfWeek != -1) {732date = nextOrSame(date, dayOfWeek);733}734}735if (timeEndOfDay) {736date = date.plusDays(1);737}738return date;739}740}741742}743744745