Class StandardZoneRules
- java.lang.Object
-
- org.threeten.bp.zone.ZoneRules
-
- org.threeten.bp.zone.StandardZoneRules
-
- All Implemented Interfaces:
java.io.Serializable
final class StandardZoneRules extends ZoneRules implements java.io.Serializable
The rules describing how the zone offset varies through the year and historically.This class is used by the TZDB time-zone rules.
Specification for implementors
This class is immutable and thread-safe.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.threeten.bp.zone.ZoneRules
ZoneRules.Fixed
-
-
Field Summary
Fields Modifier and Type Field Description private static intLAST_CACHED_YEARThe last year to have its transitions cached.private ZoneOffsetTransitionRule[]lastRulesThe last rule.private java.util.concurrent.ConcurrentMap<java.lang.Integer,ZoneOffsetTransition[]>lastRulesCacheThe map of recent transitions.private long[]savingsInstantTransitionsThe transitions between instants (epoch seconds), sorted.private LocalDateTime[]savingsLocalTransitionsThe transitions between local date-times, sorted.private static longserialVersionUIDSerialization version.private ZoneOffset[]standardOffsetsThe standard offsets.private long[]standardTransitionsThe transitions between standard offsets (epoch seconds), sorted.private ZoneOffset[]wallOffsetsThe wall offsets.
-
Constructor Summary
Constructors Modifier Constructor Description privateStandardZoneRules(long[] standardTransitions, ZoneOffset[] standardOffsets, long[] savingsInstantTransitions, ZoneOffset[] wallOffsets, ZoneOffsetTransitionRule[] lastRules)Constructor.(package private)StandardZoneRules(ZoneOffset baseStandardOffset, ZoneOffset baseWallOffset, java.util.List<ZoneOffsetTransition> standardOffsetTransitionList, java.util.List<ZoneOffsetTransition> transitionList, java.util.List<ZoneOffsetTransitionRule> lastRules)Creates an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)Checks if this set of rules equals another.private java.lang.ObjectfindOffsetInfo(LocalDateTime dt, ZoneOffsetTransition trans)Finds the offset info for a local date-time and transition.private ZoneOffsetTransition[]findTransitionArray(int year)Finds the appropriate transition array for the given year.private intfindYear(long epochSecond, ZoneOffset offset)DurationgetDaylightSavings(Instant instant)Gets the amount of daylight savings in use for the specified instant in this zone.ZoneOffsetgetOffset(Instant instant)Gets the offset applicable at the specified instant in these rules.ZoneOffsetgetOffset(LocalDateTime localDateTime)Gets a suitable offset for the specified local date-time in these rules.private java.lang.ObjectgetOffsetInfo(LocalDateTime dt)ZoneOffsetgetStandardOffset(Instant instant)Gets the standard offset for the specified instant in this zone.ZoneOffsetTransitiongetTransition(LocalDateTime localDateTime)Gets the offset transition applicable at the specified local date-time in these rules.java.util.List<ZoneOffsetTransitionRule>getTransitionRules()Gets the list of transition rules for years beyond those defined in the transition list.java.util.List<ZoneOffsetTransition>getTransitions()Gets the complete list of fully defined transitions.java.util.List<ZoneOffset>getValidOffsets(LocalDateTime localDateTime)Gets the offset applicable at the specified local date-time in these rules.inthashCode()Returns a suitable hash code given the definition of#equals.booleanisDaylightSavings(Instant instant)Checks if the specified instant is in daylight savings.booleanisFixedOffset()Checks of the zone rules are fixed, such that the offset never varies.booleanisValidOffset(LocalDateTime localDateTime, ZoneOffset offset)Checks if the offset date-time is valid for these rules.ZoneOffsetTransitionnextTransition(Instant instant)Gets the next transition after the specified instant.ZoneOffsetTransitionpreviousTransition(Instant instant)Gets the previous transition before the specified instant.(package private) static StandardZoneRulesreadExternal(java.io.DataInput in)Reads the state from the stream.java.lang.StringtoString()Returns a string describing this object.(package private) voidwriteExternal(java.io.DataOutput out)Writes the state to the stream.private java.lang.ObjectwriteReplace()Uses a serialization delegate.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialization version.- See Also:
- Constant Field Values
-
LAST_CACHED_YEAR
private static final int LAST_CACHED_YEAR
The last year to have its transitions cached.- See Also:
- Constant Field Values
-
standardTransitions
private final long[] standardTransitions
The transitions between standard offsets (epoch seconds), sorted.
-
standardOffsets
private final ZoneOffset[] standardOffsets
The standard offsets.
-
savingsInstantTransitions
private final long[] savingsInstantTransitions
The transitions between instants (epoch seconds), sorted.
-
savingsLocalTransitions
private final LocalDateTime[] savingsLocalTransitions
The transitions between local date-times, sorted. This is a paired array, where the first entry is the start of the transition and the second entry is the end of the transition.
-
wallOffsets
private final ZoneOffset[] wallOffsets
The wall offsets.
-
lastRules
private final ZoneOffsetTransitionRule[] lastRules
The last rule.
-
lastRulesCache
private final java.util.concurrent.ConcurrentMap<java.lang.Integer,ZoneOffsetTransition[]> lastRulesCache
The map of recent transitions.
-
-
Constructor Detail
-
StandardZoneRules
StandardZoneRules(ZoneOffset baseStandardOffset, ZoneOffset baseWallOffset, java.util.List<ZoneOffsetTransition> standardOffsetTransitionList, java.util.List<ZoneOffsetTransition> transitionList, java.util.List<ZoneOffsetTransitionRule> lastRules)
Creates an instance.- Parameters:
baseStandardOffset- the standard offset to use before legal rules were set, not nullbaseWallOffset- the wall offset to use before legal rules were set, not nullstandardOffsetTransitionList- the list of changes to the standard offset, not nulltransitionList- the list of transitions, not nulllastRules- the recurring last rules, size 15 or less, not null
-
StandardZoneRules
private StandardZoneRules(long[] standardTransitions, ZoneOffset[] standardOffsets, long[] savingsInstantTransitions, ZoneOffset[] wallOffsets, ZoneOffsetTransitionRule[] lastRules)Constructor.- Parameters:
standardTransitions- the standard transitions, not nullstandardOffsets- the standard offsets, not nullsavingsInstantTransitions- the standard transitions, not nullwallOffsets- the wall offsets, not nulllastRules- the recurring last rules, size 15 or less, not null
-
-
Method Detail
-
writeReplace
private java.lang.Object writeReplace()
Uses a serialization delegate.- Returns:
- the replacing object, not null
-
writeExternal
void writeExternal(java.io.DataOutput out) throws java.io.IOExceptionWrites the state to the stream.- Parameters:
out- the output stream, not null- Throws:
java.io.IOException- if an error occurs
-
readExternal
static StandardZoneRules readExternal(java.io.DataInput in) throws java.io.IOException, java.lang.ClassNotFoundException
Reads the state from the stream.- Parameters:
in- the input stream, not null- Returns:
- the created object, not null
- Throws:
java.io.IOException- if an error occursjava.lang.ClassNotFoundException
-
isFixedOffset
public boolean isFixedOffset()
Description copied from class:ZoneRulesChecks of the zone rules are fixed, such that the offset never varies.- Specified by:
isFixedOffsetin classZoneRules- Returns:
- true if the time-zone is fixed and the offset never changes
-
getOffset
public ZoneOffset getOffset(Instant instant)
Description copied from class:ZoneRulesGets the offset applicable at the specified instant in these rules.The mapping from an instant to an offset is simple, there is only one valid offset for each instant. This method returns that offset.
-
getOffset
public ZoneOffset getOffset(LocalDateTime localDateTime)
Description copied from class:ZoneRulesGets a suitable offset for the specified local date-time in these rules.The mapping from a local date-time to an offset is not straightforward. There are three cases:
- Normal, with one valid offset. For the vast majority of the year, the normal case applies, where there is a single valid offset for the local date-time.
- Gap, with zero valid offsets. This is when clocks jump forward typically due to the spring daylight savings change from "winter" to "summer". In a gap there are local date-time values with no valid offset.
- Overlap, with two valid offsets. This is when clocks are set back typically due to the autumn daylight savings change from "summer" to "winter". In an overlap there are local date-time values with two valid offsets.
Thus, for any given local date-time there can be zero, one or two valid offsets. This method returns the single offset in the Normal case, and in the Gap or Overlap case it returns the offset before the transition.
Since, in the case of Gap and Overlap, the offset returned is a "best" value, rather than the "correct" value, it should be treated with care. Applications that care about the correct offset should use a combination of this method,
ZoneRules.getValidOffsets(LocalDateTime)andZoneRules.getTransition(LocalDateTime).
-
getValidOffsets
public java.util.List<ZoneOffset> getValidOffsets(LocalDateTime localDateTime)
Description copied from class:ZoneRulesGets the offset applicable at the specified local date-time in these rules.The mapping from a local date-time to an offset is not straightforward. There are three cases:
- Normal, with one valid offset. For the vast majority of the year, the normal case applies, where there is a single valid offset for the local date-time.
- Gap, with zero valid offsets. This is when clocks jump forward typically due to the spring daylight savings change from "winter" to "summer". In a gap there are local date-time values with no valid offset.
- Overlap, with two valid offsets. This is when clocks are set back typically due to the autumn daylight savings change from "summer" to "winter". In an overlap there are local date-time values with two valid offsets.
Thus, for any given local date-time there can be zero, one or two valid offsets. This method returns that list of valid offsets, which is a list of size 0, 1 or 2. In the case where there are two offsets, the earlier offset is returned at index 0 and the later offset at index 1.
There are various ways to handle the conversion from a
LocalDateTime. One technique, using this method, would be:List
validOffsets = rules.getOffset(localDT); if (validOffsets.size() == 1) { // Normal case: only one valid offset zoneOffset = validOffsets.get(0); } else { // Gap or Overlap: determine what to do from transition (which will be non-null) ZoneOffsetTransition trans = rules.getTransition(localDT); } In theory, it is possible for there to be more than two valid offsets. This would happen if clocks to be put back more than once in quick succession. This has never happened in the history of time-zones and thus has no special handling. However, if it were to happen, then the list would return more than 2 entries.
- Specified by:
getValidOffsetsin classZoneRules- Parameters:
localDateTime- the local date-time to query for valid offsets, not null, but null may be ignored if the rules have a single offset for all instants- Returns:
- the list of valid offsets, may be immutable, not null
-
getTransition
public ZoneOffsetTransition getTransition(LocalDateTime localDateTime)
Description copied from class:ZoneRulesGets the offset transition applicable at the specified local date-time in these rules.The mapping from a local date-time to an offset is not straightforward. There are three cases:
- Normal, with one valid offset. For the vast majority of the year, the normal case applies, where there is a single valid offset for the local date-time.
- Gap, with zero valid offsets. This is when clocks jump forward typically due to the spring daylight savings change from "winter" to "summer". In a gap there are local date-time values with no valid offset.
- Overlap, with two valid offsets. This is when clocks are set back typically due to the autumn daylight savings change from "summer" to "winter". In an overlap there are local date-time values with two valid offsets.
A transition is used to model the cases of a Gap or Overlap. The Normal case will return null.
There are various ways to handle the conversion from a
LocalDateTime. One technique, using this method, would be:ZoneOffsetTransition trans = rules.getTransition(localDT); if (trans == null) { // Gap or Overlap: determine what to do from transition } else { // Normal case: only one valid offset zoneOffset = rule.getOffset(localDT); }- Specified by:
getTransitionin classZoneRules- Parameters:
localDateTime- the local date-time to query for offset transition, not null, but null may be ignored if the rules have a single offset for all instants- Returns:
- the offset transition, null if the local date-time is not in transition
-
getOffsetInfo
private java.lang.Object getOffsetInfo(LocalDateTime dt)
-
findOffsetInfo
private java.lang.Object findOffsetInfo(LocalDateTime dt, ZoneOffsetTransition trans)
Finds the offset info for a local date-time and transition.- Parameters:
dt- the date-time, not nulltrans- the transition, not null- Returns:
- the offset info, not null
-
isValidOffset
public boolean isValidOffset(LocalDateTime localDateTime, ZoneOffset offset)
Description copied from class:ZoneRulesChecks if the offset date-time is valid for these rules.To be valid, the local date-time must not be in a gap and the offset must match the valid offsets.
- Specified by:
isValidOffsetin classZoneRules- Parameters:
localDateTime- the date-time to check, not null, but null may be ignored if the rules have a single offset for all instantsoffset- the offset to check, null returns false- Returns:
- true if the offset date-time is valid for these rules
-
findTransitionArray
private ZoneOffsetTransition[] findTransitionArray(int year)
Finds the appropriate transition array for the given year.- Parameters:
year- the year, not null- Returns:
- the transition array, not null
-
getStandardOffset
public ZoneOffset getStandardOffset(Instant instant)
Description copied from class:ZoneRulesGets the standard offset for the specified instant in this zone.This provides access to historic information on how the standard offset has changed over time. The standard offset is the offset before any daylight saving time is applied. This is typically the offset applicable during winter.
- Specified by:
getStandardOffsetin classZoneRules- Parameters:
instant- the instant to find the offset information for, not null, but null may be ignored if the rules have a single offset for all instants- Returns:
- the standard offset, not null
-
getDaylightSavings
public Duration getDaylightSavings(Instant instant)
Description copied from class:ZoneRulesGets the amount of daylight savings in use for the specified instant in this zone.This provides access to historic information on how the amount of daylight savings has changed over time. This is the difference between the standard offset and the actual offset. Typically the amount is zero during winter and one hour during summer. Time-zones are second-based, so the nanosecond part of the duration will be zero.
- Specified by:
getDaylightSavingsin classZoneRules- Parameters:
instant- the instant to find the daylight savings for, not null, but null may be ignored if the rules have a single offset for all instants- Returns:
- the difference between the standard and actual offset, not null
-
isDaylightSavings
public boolean isDaylightSavings(Instant instant)
Description copied from class:ZoneRulesChecks if the specified instant is in daylight savings.This checks if the standard and actual offsets are the same at the specified instant.
- Specified by:
isDaylightSavingsin classZoneRules- Parameters:
instant- the instant to find the offset information for, not null, but null may be ignored if the rules have a single offset for all instants- Returns:
- the standard offset, not null
-
nextTransition
public ZoneOffsetTransition nextTransition(Instant instant)
Description copied from class:ZoneRulesGets the next transition after the specified instant.This returns details of the next transition after the specified instant. For example, if the instant represents a point where "Summer" daylight savings time applies, then the method will return the transition to the next "Winter" time.
- Specified by:
nextTransitionin classZoneRules- Parameters:
instant- the instant to get the next transition after, not null, but null may be ignored if the rules have a single offset for all instants- Returns:
- the next transition after the specified instant, null if this is after the last transition
-
previousTransition
public ZoneOffsetTransition previousTransition(Instant instant)
Description copied from class:ZoneRulesGets the previous transition before the specified instant.This returns details of the previous transition after the specified instant. For example, if the instant represents a point where "summer" daylight saving time applies, then the method will return the transition from the previous "winter" time.
- Specified by:
previousTransitionin classZoneRules- Parameters:
instant- the instant to get the previous transition after, not null, but null may be ignored if the rules have a single offset for all instants- Returns:
- the previous transition after the specified instant, null if this is before the first transition
-
findYear
private int findYear(long epochSecond, ZoneOffset offset)
-
getTransitions
public java.util.List<ZoneOffsetTransition> getTransitions()
Description copied from class:ZoneRulesGets the complete list of fully defined transitions.The complete set of transitions for this rules instance is defined by this method and
ZoneRules.getTransitionRules(). This method returns those transitions that have been fully defined. These are typically historical, but may be in the future.The list will be empty for fixed offset rules and for any time-zone where there has only ever been a single offset. The list will also be empty if the transition rules are unknown.
- Specified by:
getTransitionsin classZoneRules- Returns:
- an immutable list of fully defined transitions, not null
-
getTransitionRules
public java.util.List<ZoneOffsetTransitionRule> getTransitionRules()
Description copied from class:ZoneRulesGets the list of transition rules for years beyond those defined in the transition list.The complete set of transitions for this rules instance is defined by this method and
ZoneRules.getTransitions(). This method returns instances ofZoneOffsetTransitionRulethat define an algorithm for when transitions will occur.For any given
ZoneRules, this list contains the transition rules for years beyond those years that have been fully defined. These rules typically refer to future daylight saving time rule changes.If the zone defines daylight savings into the future, then the list will normally be of size two and hold information about entering and exiting daylight savings. If the zone does not have daylight savings, or information about future changes is uncertain, then the list will be empty.
The list will be empty for fixed offset rules and for any time-zone where there is no daylight saving time. The list will also be empty if the transition rules are unknown.
- Specified by:
getTransitionRulesin classZoneRules- Returns:
- an immutable list of transition rules, not null
-
equals
public boolean equals(java.lang.Object obj)
Description copied from class:ZoneRulesChecks if this set of rules equals another.Two rule sets are equal if they will always result in the same output for any given input instant or local date-time. Rules from two different groups may return false even if they are in fact the same.
This definition should result in implementations comparing their entire state.
-
hashCode
public int hashCode()
Description copied from class:ZoneRulesReturns a suitable hash code given the definition of#equals.
-
toString
public java.lang.String toString()
Returns a string describing this object.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string for debugging, not null
-
-