author | rriggs |
Wed, 11 Sep 2013 10:16:21 -0400 | |
changeset 19841 | 15c8e97d6a14 |
parent 17474 | 8c100beabcc0 |
child 20873 | e91d5b1cb8e6 |
permissions | -rw-r--r-- |
15289 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* This file is available under and governed by the GNU General Public |
|
28 |
* License version 2 only, as published by the Free Software Foundation. |
|
29 |
* However, the following notice accompanied the original version of this |
|
30 |
* file: |
|
31 |
* |
|
32 |
* Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos |
|
33 |
* |
|
34 |
* All rights reserved. |
|
35 |
* |
|
36 |
* Redistribution and use in source and binary forms, with or without |
|
37 |
* modification, are permitted provided that the following conditions are met: |
|
38 |
* |
|
39 |
* * Redistributions of source code must retain the above copyright notice, |
|
40 |
* this list of conditions and the following disclaimer. |
|
41 |
* |
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice, |
|
43 |
* this list of conditions and the following disclaimer in the documentation |
|
44 |
* and/or other materials provided with the distribution. |
|
45 |
* |
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors |
|
47 |
* may be used to endorse or promote products derived from this software |
|
48 |
* without specific prior written permission. |
|
49 |
* |
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
61 |
*/ |
|
62 |
package java.time.zone; |
|
63 |
||
64 |
import java.io.DataInput; |
|
65 |
import java.io.DataOutput; |
|
66 |
import java.io.IOException; |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
67 |
import java.io.InvalidObjectException; |
15289 | 68 |
import java.io.Serializable; |
69 |
import java.time.Duration; |
|
70 |
import java.time.Instant; |
|
71 |
import java.time.LocalDate; |
|
72 |
import java.time.LocalDateTime; |
|
73 |
import java.time.ZoneId; |
|
74 |
import java.time.ZoneOffset; |
|
15658 | 75 |
import java.time.Year; |
15289 | 76 |
import java.util.ArrayList; |
77 |
import java.util.Arrays; |
|
78 |
import java.util.Collections; |
|
79 |
import java.util.List; |
|
80 |
import java.util.Objects; |
|
81 |
import java.util.concurrent.ConcurrentHashMap; |
|
82 |
import java.util.concurrent.ConcurrentMap; |
|
83 |
||
84 |
/** |
|
85 |
* The rules defining how the zone offset varies for a single time-zone. |
|
86 |
* <p> |
|
87 |
* The rules model all the historic and future transitions for a time-zone. |
|
88 |
* {@link ZoneOffsetTransition} is used for known transitions, typically historic. |
|
89 |
* {@link ZoneOffsetTransitionRule} is used for future transitions that are based |
|
90 |
* on the result of an algorithm. |
|
91 |
* <p> |
|
92 |
* The rules are loaded via {@link ZoneRulesProvider} using a {@link ZoneId}. |
|
93 |
* The same rules may be shared internally between multiple zone IDs. |
|
94 |
* <p> |
|
95 |
* Serializing an instance of {@code ZoneRules} will store the entire set of rules. |
|
96 |
* It does not store the zone ID as it is not part of the state of this object. |
|
97 |
* <p> |
|
98 |
* A rule implementation may or may not store full information about historic |
|
99 |
* and future transitions, and the information stored is only as accurate as |
|
100 |
* that supplied to the implementation by the rules provider. |
|
101 |
* Applications should treat the data provided as representing the best information |
|
102 |
* available to the implementation of this rule. |
|
103 |
* |
|
17474 | 104 |
* @implSpec |
15289 | 105 |
* This class is immutable and thread-safe. |
106 |
* |
|
107 |
* @since 1.8 |
|
108 |
*/ |
|
109 |
public final class ZoneRules implements Serializable { |
|
110 |
||
111 |
/** |
|
112 |
* Serialization version. |
|
113 |
*/ |
|
114 |
private static final long serialVersionUID = 3044319355680032515L; |
|
115 |
/** |
|
116 |
* The last year to have its transitions cached. |
|
117 |
*/ |
|
118 |
private static final int LAST_CACHED_YEAR = 2100; |
|
119 |
||
120 |
/** |
|
121 |
* The transitions between standard offsets (epoch seconds), sorted. |
|
122 |
*/ |
|
123 |
private final long[] standardTransitions; |
|
124 |
/** |
|
125 |
* The standard offsets. |
|
126 |
*/ |
|
127 |
private final ZoneOffset[] standardOffsets; |
|
128 |
/** |
|
129 |
* The transitions between instants (epoch seconds), sorted. |
|
130 |
*/ |
|
131 |
private final long[] savingsInstantTransitions; |
|
132 |
/** |
|
133 |
* The transitions between local date-times, sorted. |
|
134 |
* This is a paired array, where the first entry is the start of the transition |
|
135 |
* and the second entry is the end of the transition. |
|
136 |
*/ |
|
137 |
private final LocalDateTime[] savingsLocalTransitions; |
|
138 |
/** |
|
139 |
* The wall offsets. |
|
140 |
*/ |
|
141 |
private final ZoneOffset[] wallOffsets; |
|
142 |
/** |
|
143 |
* The last rule. |
|
144 |
*/ |
|
145 |
private final ZoneOffsetTransitionRule[] lastRules; |
|
146 |
/** |
|
147 |
* The map of recent transitions. |
|
148 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
149 |
private final transient ConcurrentMap<Integer, ZoneOffsetTransition[]> lastRulesCache = |
15289 | 150 |
new ConcurrentHashMap<Integer, ZoneOffsetTransition[]>(); |
151 |
/** |
|
152 |
* The zero-length long array. |
|
153 |
*/ |
|
154 |
private static final long[] EMPTY_LONG_ARRAY = new long[0]; |
|
155 |
/** |
|
156 |
* The zero-length lastrules array. |
|
157 |
*/ |
|
158 |
private static final ZoneOffsetTransitionRule[] EMPTY_LASTRULES = |
|
159 |
new ZoneOffsetTransitionRule[0]; |
|
160 |
/** |
|
161 |
* The zero-length ldt array. |
|
162 |
*/ |
|
163 |
private static final LocalDateTime[] EMPTY_LDT_ARRAY = new LocalDateTime[0]; |
|
164 |
||
165 |
/** |
|
166 |
* Obtains an instance of a ZoneRules. |
|
167 |
* |
|
168 |
* @param baseStandardOffset the standard offset to use before legal rules were set, not null |
|
169 |
* @param baseWallOffset the wall offset to use before legal rules were set, not null |
|
170 |
* @param standardOffsetTransitionList the list of changes to the standard offset, not null |
|
171 |
* @param transitionList the list of transitions, not null |
|
172 |
* @param lastRules the recurring last rules, size 16 or less, not null |
|
173 |
* @return the zone rules, not null |
|
174 |
*/ |
|
175 |
public static ZoneRules of(ZoneOffset baseStandardOffset, |
|
176 |
ZoneOffset baseWallOffset, |
|
177 |
List<ZoneOffsetTransition> standardOffsetTransitionList, |
|
178 |
List<ZoneOffsetTransition> transitionList, |
|
179 |
List<ZoneOffsetTransitionRule> lastRules) { |
|
180 |
Objects.requireNonNull(baseStandardOffset, "baseStandardOffset"); |
|
181 |
Objects.requireNonNull(baseWallOffset, "baseWallOffset"); |
|
182 |
Objects.requireNonNull(standardOffsetTransitionList, "standardOffsetTransitionList"); |
|
183 |
Objects.requireNonNull(transitionList, "transitionList"); |
|
184 |
Objects.requireNonNull(lastRules, "lastRules"); |
|
185 |
return new ZoneRules(baseStandardOffset, baseWallOffset, |
|
186 |
standardOffsetTransitionList, transitionList, lastRules); |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Obtains an instance of ZoneRules that has fixed zone rules. |
|
191 |
* |
|
192 |
* @param offset the offset this fixed zone rules is based on, not null |
|
193 |
* @return the zone rules, not null |
|
194 |
* @see #isFixedOffset() |
|
195 |
*/ |
|
196 |
public static ZoneRules of(ZoneOffset offset) { |
|
197 |
Objects.requireNonNull(offset, "offset"); |
|
198 |
return new ZoneRules(offset); |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Creates an instance. |
|
203 |
* |
|
204 |
* @param baseStandardOffset the standard offset to use before legal rules were set, not null |
|
205 |
* @param baseWallOffset the wall offset to use before legal rules were set, not null |
|
206 |
* @param standardOffsetTransitionList the list of changes to the standard offset, not null |
|
207 |
* @param transitionList the list of transitions, not null |
|
208 |
* @param lastRules the recurring last rules, size 16 or less, not null |
|
209 |
*/ |
|
210 |
ZoneRules(ZoneOffset baseStandardOffset, |
|
211 |
ZoneOffset baseWallOffset, |
|
212 |
List<ZoneOffsetTransition> standardOffsetTransitionList, |
|
213 |
List<ZoneOffsetTransition> transitionList, |
|
214 |
List<ZoneOffsetTransitionRule> lastRules) { |
|
215 |
super(); |
|
216 |
||
217 |
// convert standard transitions |
|
218 |
||
219 |
this.standardTransitions = new long[standardOffsetTransitionList.size()]; |
|
220 |
||
221 |
this.standardOffsets = new ZoneOffset[standardOffsetTransitionList.size() + 1]; |
|
222 |
this.standardOffsets[0] = baseStandardOffset; |
|
223 |
for (int i = 0; i < standardOffsetTransitionList.size(); i++) { |
|
224 |
this.standardTransitions[i] = standardOffsetTransitionList.get(i).toEpochSecond(); |
|
225 |
this.standardOffsets[i + 1] = standardOffsetTransitionList.get(i).getOffsetAfter(); |
|
226 |
} |
|
227 |
||
228 |
// convert savings transitions to locals |
|
229 |
List<LocalDateTime> localTransitionList = new ArrayList<>(); |
|
230 |
List<ZoneOffset> localTransitionOffsetList = new ArrayList<>(); |
|
231 |
localTransitionOffsetList.add(baseWallOffset); |
|
232 |
for (ZoneOffsetTransition trans : transitionList) { |
|
233 |
if (trans.isGap()) { |
|
234 |
localTransitionList.add(trans.getDateTimeBefore()); |
|
235 |
localTransitionList.add(trans.getDateTimeAfter()); |
|
236 |
} else { |
|
237 |
localTransitionList.add(trans.getDateTimeAfter()); |
|
238 |
localTransitionList.add(trans.getDateTimeBefore()); |
|
239 |
} |
|
240 |
localTransitionOffsetList.add(trans.getOffsetAfter()); |
|
241 |
} |
|
242 |
this.savingsLocalTransitions = localTransitionList.toArray(new LocalDateTime[localTransitionList.size()]); |
|
243 |
this.wallOffsets = localTransitionOffsetList.toArray(new ZoneOffset[localTransitionOffsetList.size()]); |
|
244 |
||
245 |
// convert savings transitions to instants |
|
246 |
this.savingsInstantTransitions = new long[transitionList.size()]; |
|
247 |
for (int i = 0; i < transitionList.size(); i++) { |
|
248 |
this.savingsInstantTransitions[i] = transitionList.get(i).toEpochSecond(); |
|
249 |
} |
|
250 |
||
251 |
// last rules |
|
252 |
if (lastRules.size() > 16) { |
|
253 |
throw new IllegalArgumentException("Too many transition rules"); |
|
254 |
} |
|
255 |
this.lastRules = lastRules.toArray(new ZoneOffsetTransitionRule[lastRules.size()]); |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Constructor. |
|
260 |
* |
|
261 |
* @param standardTransitions the standard transitions, not null |
|
262 |
* @param standardOffsets the standard offsets, not null |
|
263 |
* @param savingsInstantTransitions the standard transitions, not null |
|
264 |
* @param wallOffsets the wall offsets, not null |
|
265 |
* @param lastRules the recurring last rules, size 15 or less, not null |
|
266 |
*/ |
|
267 |
private ZoneRules(long[] standardTransitions, |
|
268 |
ZoneOffset[] standardOffsets, |
|
269 |
long[] savingsInstantTransitions, |
|
270 |
ZoneOffset[] wallOffsets, |
|
271 |
ZoneOffsetTransitionRule[] lastRules) { |
|
272 |
super(); |
|
273 |
||
274 |
this.standardTransitions = standardTransitions; |
|
275 |
this.standardOffsets = standardOffsets; |
|
276 |
this.savingsInstantTransitions = savingsInstantTransitions; |
|
277 |
this.wallOffsets = wallOffsets; |
|
278 |
this.lastRules = lastRules; |
|
279 |
||
280 |
if (savingsInstantTransitions.length == 0) { |
|
281 |
this.savingsLocalTransitions = EMPTY_LDT_ARRAY; |
|
282 |
} else { |
|
283 |
// convert savings transitions to locals |
|
284 |
List<LocalDateTime> localTransitionList = new ArrayList<>(); |
|
285 |
for (int i = 0; i < savingsInstantTransitions.length; i++) { |
|
286 |
ZoneOffset before = wallOffsets[i]; |
|
287 |
ZoneOffset after = wallOffsets[i + 1]; |
|
288 |
ZoneOffsetTransition trans = new ZoneOffsetTransition(savingsInstantTransitions[i], before, after); |
|
289 |
if (trans.isGap()) { |
|
290 |
localTransitionList.add(trans.getDateTimeBefore()); |
|
291 |
localTransitionList.add(trans.getDateTimeAfter()); |
|
292 |
} else { |
|
293 |
localTransitionList.add(trans.getDateTimeAfter()); |
|
294 |
localTransitionList.add(trans.getDateTimeBefore()); |
|
295 |
} |
|
296 |
} |
|
297 |
this.savingsLocalTransitions = localTransitionList.toArray(new LocalDateTime[localTransitionList.size()]); |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Creates an instance of ZoneRules that has fixed zone rules. |
|
303 |
* |
|
304 |
* @param offset the offset this fixed zone rules is based on, not null |
|
305 |
* @return the zone rules, not null |
|
306 |
* @see #isFixedOffset() |
|
307 |
*/ |
|
308 |
private ZoneRules(ZoneOffset offset) { |
|
309 |
this.standardOffsets = new ZoneOffset[1]; |
|
310 |
this.standardOffsets[0] = offset; |
|
311 |
this.standardTransitions = EMPTY_LONG_ARRAY; |
|
312 |
this.savingsInstantTransitions = EMPTY_LONG_ARRAY; |
|
313 |
this.savingsLocalTransitions = EMPTY_LDT_ARRAY; |
|
314 |
this.wallOffsets = standardOffsets; |
|
315 |
this.lastRules = EMPTY_LASTRULES; |
|
316 |
} |
|
317 |
||
318 |
/** |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
319 |
* Defend against malicious streams. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
320 |
* @return never |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
321 |
* @throws InvalidObjectException always |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
322 |
*/ |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
323 |
private Object readResolve() throws InvalidObjectException { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
324 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
325 |
} |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
326 |
|
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
327 |
/** |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
328 |
* Writes the object using a |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
329 |
* <a href="../../../serialized-form.html#java.time.zone.Ser">dedicated serialized form</a>. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
330 |
* @serialData |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
331 |
* <pre style="font-size:1.0em">{@code |
15289 | 332 |
* |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
333 |
* out.writeByte(1); // identifies a ZoneRules |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
334 |
* out.writeInt(standardTransitions.length); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
335 |
* for (long trans : standardTransitions) { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
336 |
* Ser.writeEpochSec(trans, out); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
337 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
338 |
* for (ZoneOffset offset : standardOffsets) { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
339 |
* Ser.writeOffset(offset, out); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
340 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
341 |
* out.writeInt(savingsInstantTransitions.length); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
342 |
* for (long trans : savingsInstantTransitions) { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
343 |
* Ser.writeEpochSec(trans, out); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
344 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
345 |
* for (ZoneOffset offset : wallOffsets) { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
346 |
* Ser.writeOffset(offset, out); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
347 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
348 |
* out.writeByte(lastRules.length); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
349 |
* for (ZoneOffsetTransitionRule rule : lastRules) { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
350 |
* rule.writeExternal(out); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
351 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
352 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
353 |
* </pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
354 |
* <p> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
355 |
* Epoch second values used for offsets are encoded in a variable |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
356 |
* length form to make the common cases put fewer bytes in the stream. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
357 |
* <pre style="font-size:1.0em">{@code |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
358 |
* |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
359 |
* static void writeEpochSec(long epochSec, DataOutput out) throws IOException { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
360 |
* if (epochSec >= -4575744000L && epochSec < 10413792000L && epochSec % 900 == 0) { // quarter hours between 1825 and 2300 |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
361 |
* int store = (int) ((epochSec + 4575744000L) / 900); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
362 |
* out.writeByte((store >>> 16) & 255); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
363 |
* out.writeByte((store >>> 8) & 255); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
364 |
* out.writeByte(store & 255); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
365 |
* } else { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
366 |
* out.writeByte(255); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
367 |
* out.writeLong(epochSec); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
368 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
369 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
370 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
371 |
* </pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
372 |
* <p> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
373 |
* ZoneOffset values are encoded in a variable length form so the |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
374 |
* common cases put fewer bytes in the stream. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
375 |
* <pre style="font-size:1.0em">{@code |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
376 |
* |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
377 |
* static void writeOffset(ZoneOffset offset, DataOutput out) throws IOException { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
378 |
* final int offsetSecs = offset.getTotalSeconds(); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
379 |
* int offsetByte = offsetSecs % 900 == 0 ? offsetSecs / 900 : 127; // compress to -72 to +72 |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
380 |
* out.writeByte(offsetByte); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
381 |
* if (offsetByte == 127) { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
382 |
* out.writeInt(offsetSecs); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
383 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
384 |
* } |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
385 |
*} |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
17474
diff
changeset
|
386 |
* </pre> |
15289 | 387 |
* @return the replacing object, not null |
388 |
*/ |
|
389 |
private Object writeReplace() { |
|
390 |
return new Ser(Ser.ZRULES, this); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Writes the state to the stream. |
|
395 |
* |
|
396 |
* @param out the output stream, not null |
|
397 |
* @throws IOException if an error occurs |
|
398 |
*/ |
|
399 |
void writeExternal(DataOutput out) throws IOException { |
|
400 |
out.writeInt(standardTransitions.length); |
|
401 |
for (long trans : standardTransitions) { |
|
402 |
Ser.writeEpochSec(trans, out); |
|
403 |
} |
|
404 |
for (ZoneOffset offset : standardOffsets) { |
|
405 |
Ser.writeOffset(offset, out); |
|
406 |
} |
|
407 |
out.writeInt(savingsInstantTransitions.length); |
|
408 |
for (long trans : savingsInstantTransitions) { |
|
409 |
Ser.writeEpochSec(trans, out); |
|
410 |
} |
|
411 |
for (ZoneOffset offset : wallOffsets) { |
|
412 |
Ser.writeOffset(offset, out); |
|
413 |
} |
|
414 |
out.writeByte(lastRules.length); |
|
415 |
for (ZoneOffsetTransitionRule rule : lastRules) { |
|
416 |
rule.writeExternal(out); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
/** |
|
421 |
* Reads the state from the stream. |
|
422 |
* |
|
423 |
* @param in the input stream, not null |
|
424 |
* @return the created object, not null |
|
425 |
* @throws IOException if an error occurs |
|
426 |
*/ |
|
427 |
static ZoneRules readExternal(DataInput in) throws IOException, ClassNotFoundException { |
|
428 |
int stdSize = in.readInt(); |
|
429 |
long[] stdTrans = (stdSize == 0) ? EMPTY_LONG_ARRAY |
|
430 |
: new long[stdSize]; |
|
431 |
for (int i = 0; i < stdSize; i++) { |
|
432 |
stdTrans[i] = Ser.readEpochSec(in); |
|
433 |
} |
|
434 |
ZoneOffset[] stdOffsets = new ZoneOffset[stdSize + 1]; |
|
435 |
for (int i = 0; i < stdOffsets.length; i++) { |
|
436 |
stdOffsets[i] = Ser.readOffset(in); |
|
437 |
} |
|
438 |
int savSize = in.readInt(); |
|
439 |
long[] savTrans = (savSize == 0) ? EMPTY_LONG_ARRAY |
|
440 |
: new long[savSize]; |
|
441 |
for (int i = 0; i < savSize; i++) { |
|
442 |
savTrans[i] = Ser.readEpochSec(in); |
|
443 |
} |
|
444 |
ZoneOffset[] savOffsets = new ZoneOffset[savSize + 1]; |
|
445 |
for (int i = 0; i < savOffsets.length; i++) { |
|
446 |
savOffsets[i] = Ser.readOffset(in); |
|
447 |
} |
|
448 |
int ruleSize = in.readByte(); |
|
449 |
ZoneOffsetTransitionRule[] rules = (ruleSize == 0) ? |
|
450 |
EMPTY_LASTRULES : new ZoneOffsetTransitionRule[ruleSize]; |
|
451 |
for (int i = 0; i < ruleSize; i++) { |
|
452 |
rules[i] = ZoneOffsetTransitionRule.readExternal(in); |
|
453 |
} |
|
454 |
return new ZoneRules(stdTrans, stdOffsets, savTrans, savOffsets, rules); |
|
455 |
} |
|
456 |
||
457 |
/** |
|
458 |
* Checks of the zone rules are fixed, such that the offset never varies. |
|
459 |
* |
|
460 |
* @return true if the time-zone is fixed and the offset never changes |
|
461 |
*/ |
|
462 |
public boolean isFixedOffset() { |
|
463 |
return savingsInstantTransitions.length == 0; |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Gets the offset applicable at the specified instant in these rules. |
|
468 |
* <p> |
|
469 |
* The mapping from an instant to an offset is simple, there is only |
|
470 |
* one valid offset for each instant. |
|
471 |
* This method returns that offset. |
|
472 |
* |
|
473 |
* @param instant the instant to find the offset for, not null, but null |
|
474 |
* may be ignored if the rules have a single offset for all instants |
|
475 |
* @return the offset, not null |
|
476 |
*/ |
|
477 |
public ZoneOffset getOffset(Instant instant) { |
|
478 |
if (savingsInstantTransitions.length == 0) { |
|
479 |
return standardOffsets[0]; |
|
480 |
} |
|
481 |
long epochSec = instant.getEpochSecond(); |
|
482 |
// check if using last rules |
|
483 |
if (lastRules.length > 0 && |
|
484 |
epochSec > savingsInstantTransitions[savingsInstantTransitions.length - 1]) { |
|
485 |
int year = findYear(epochSec, wallOffsets[wallOffsets.length - 1]); |
|
486 |
ZoneOffsetTransition[] transArray = findTransitionArray(year); |
|
487 |
ZoneOffsetTransition trans = null; |
|
488 |
for (int i = 0; i < transArray.length; i++) { |
|
489 |
trans = transArray[i]; |
|
490 |
if (epochSec < trans.toEpochSecond()) { |
|
491 |
return trans.getOffsetBefore(); |
|
492 |
} |
|
493 |
} |
|
494 |
return trans.getOffsetAfter(); |
|
495 |
} |
|
496 |
||
497 |
// using historic rules |
|
498 |
int index = Arrays.binarySearch(savingsInstantTransitions, epochSec); |
|
499 |
if (index < 0) { |
|
500 |
// switch negative insert position to start of matched range |
|
501 |
index = -index - 2; |
|
502 |
} |
|
503 |
return wallOffsets[index + 1]; |
|
504 |
} |
|
505 |
||
506 |
/** |
|
507 |
* Gets a suitable offset for the specified local date-time in these rules. |
|
508 |
* <p> |
|
509 |
* The mapping from a local date-time to an offset is not straightforward. |
|
510 |
* There are three cases: |
|
511 |
* <p><ul> |
|
512 |
* <li>Normal, with one valid offset. For the vast majority of the year, the normal |
|
513 |
* case applies, where there is a single valid offset for the local date-time.</li> |
|
514 |
* <li>Gap, with zero valid offsets. This is when clocks jump forward typically |
|
515 |
* due to the spring daylight savings change from "winter" to "summer". |
|
516 |
* In a gap there are local date-time values with no valid offset.</li> |
|
517 |
* <li>Overlap, with two valid offsets. This is when clocks are set back typically |
|
518 |
* due to the autumn daylight savings change from "summer" to "winter". |
|
519 |
* In an overlap there are local date-time values with two valid offsets.</li> |
|
520 |
* </ul><p> |
|
521 |
* Thus, for any given local date-time there can be zero, one or two valid offsets. |
|
522 |
* This method returns the single offset in the Normal case, and in the Gap or Overlap |
|
523 |
* case it returns the offset before the transition. |
|
524 |
* <p> |
|
525 |
* Since, in the case of Gap and Overlap, the offset returned is a "best" value, rather |
|
526 |
* than the "correct" value, it should be treated with care. Applications that care |
|
527 |
* about the correct offset should use a combination of this method, |
|
528 |
* {@link #getValidOffsets(LocalDateTime)} and {@link #getTransition(LocalDateTime)}. |
|
529 |
* |
|
530 |
* @param localDateTime the local date-time to query, not null, but null |
|
531 |
* may be ignored if the rules have a single offset for all instants |
|
532 |
* @return the best available offset for the local date-time, not null |
|
533 |
*/ |
|
534 |
public ZoneOffset getOffset(LocalDateTime localDateTime) { |
|
535 |
Object info = getOffsetInfo(localDateTime); |
|
536 |
if (info instanceof ZoneOffsetTransition) { |
|
537 |
return ((ZoneOffsetTransition) info).getOffsetBefore(); |
|
538 |
} |
|
539 |
return (ZoneOffset) info; |
|
540 |
} |
|
541 |
||
542 |
/** |
|
543 |
* Gets the offset applicable at the specified local date-time in these rules. |
|
544 |
* <p> |
|
545 |
* The mapping from a local date-time to an offset is not straightforward. |
|
546 |
* There are three cases: |
|
547 |
* <p><ul> |
|
548 |
* <li>Normal, with one valid offset. For the vast majority of the year, the normal |
|
549 |
* case applies, where there is a single valid offset for the local date-time.</li> |
|
550 |
* <li>Gap, with zero valid offsets. This is when clocks jump forward typically |
|
551 |
* due to the spring daylight savings change from "winter" to "summer". |
|
552 |
* In a gap there are local date-time values with no valid offset.</li> |
|
553 |
* <li>Overlap, with two valid offsets. This is when clocks are set back typically |
|
554 |
* due to the autumn daylight savings change from "summer" to "winter". |
|
555 |
* In an overlap there are local date-time values with two valid offsets.</li> |
|
556 |
* </ul><p> |
|
557 |
* Thus, for any given local date-time there can be zero, one or two valid offsets. |
|
558 |
* This method returns that list of valid offsets, which is a list of size 0, 1 or 2. |
|
559 |
* In the case where there are two offsets, the earlier offset is returned at index 0 |
|
560 |
* and the later offset at index 1. |
|
561 |
* <p> |
|
562 |
* There are various ways to handle the conversion from a {@code LocalDateTime}. |
|
563 |
* One technique, using this method, would be: |
|
564 |
* <pre> |
|
565 |
* List<ZoneOffset> validOffsets = rules.getOffset(localDT); |
|
566 |
* if (validOffsets.size() == 1) { |
|
567 |
* // Normal case: only one valid offset |
|
568 |
* zoneOffset = validOffsets.get(0); |
|
569 |
* } else { |
|
570 |
* // Gap or Overlap: determine what to do from transition (which will be non-null) |
|
571 |
* ZoneOffsetTransition trans = rules.getTransition(localDT); |
|
572 |
* } |
|
573 |
* </pre> |
|
574 |
* <p> |
|
575 |
* In theory, it is possible for there to be more than two valid offsets. |
|
576 |
* This would happen if clocks to be put back more than once in quick succession. |
|
577 |
* This has never happened in the history of time-zones and thus has no special handling. |
|
578 |
* However, if it were to happen, then the list would return more than 2 entries. |
|
579 |
* |
|
580 |
* @param localDateTime the local date-time to query for valid offsets, not null, but null |
|
581 |
* may be ignored if the rules have a single offset for all instants |
|
582 |
* @return the list of valid offsets, may be immutable, not null |
|
583 |
*/ |
|
584 |
public List<ZoneOffset> getValidOffsets(LocalDateTime localDateTime) { |
|
585 |
// should probably be optimized |
|
586 |
Object info = getOffsetInfo(localDateTime); |
|
587 |
if (info instanceof ZoneOffsetTransition) { |
|
588 |
return ((ZoneOffsetTransition) info).getValidOffsets(); |
|
589 |
} |
|
590 |
return Collections.singletonList((ZoneOffset) info); |
|
591 |
} |
|
592 |
||
593 |
/** |
|
594 |
* Gets the offset transition applicable at the specified local date-time in these rules. |
|
595 |
* <p> |
|
596 |
* The mapping from a local date-time to an offset is not straightforward. |
|
597 |
* There are three cases: |
|
598 |
* <p><ul> |
|
599 |
* <li>Normal, with one valid offset. For the vast majority of the year, the normal |
|
600 |
* case applies, where there is a single valid offset for the local date-time.</li> |
|
601 |
* <li>Gap, with zero valid offsets. This is when clocks jump forward typically |
|
602 |
* due to the spring daylight savings change from "winter" to "summer". |
|
603 |
* In a gap there are local date-time values with no valid offset.</li> |
|
604 |
* <li>Overlap, with two valid offsets. This is when clocks are set back typically |
|
605 |
* due to the autumn daylight savings change from "summer" to "winter". |
|
606 |
* In an overlap there are local date-time values with two valid offsets.</li> |
|
607 |
* </ul><p> |
|
608 |
* A transition is used to model the cases of a Gap or Overlap. |
|
609 |
* The Normal case will return null. |
|
610 |
* <p> |
|
611 |
* There are various ways to handle the conversion from a {@code LocalDateTime}. |
|
612 |
* One technique, using this method, would be: |
|
613 |
* <pre> |
|
614 |
* ZoneOffsetTransition trans = rules.getTransition(localDT); |
|
615 |
* if (trans == null) { |
|
616 |
* // Gap or Overlap: determine what to do from transition |
|
617 |
* } else { |
|
618 |
* // Normal case: only one valid offset |
|
619 |
* zoneOffset = rule.getOffset(localDT); |
|
620 |
* } |
|
621 |
* </pre> |
|
622 |
* |
|
623 |
* @param localDateTime the local date-time to query for offset transition, not null, but null |
|
624 |
* may be ignored if the rules have a single offset for all instants |
|
625 |
* @return the offset transition, null if the local date-time is not in transition |
|
626 |
*/ |
|
627 |
public ZoneOffsetTransition getTransition(LocalDateTime localDateTime) { |
|
628 |
Object info = getOffsetInfo(localDateTime); |
|
629 |
return (info instanceof ZoneOffsetTransition ? (ZoneOffsetTransition) info : null); |
|
630 |
} |
|
631 |
||
632 |
private Object getOffsetInfo(LocalDateTime dt) { |
|
633 |
if (savingsInstantTransitions.length == 0) { |
|
634 |
return standardOffsets[0]; |
|
635 |
} |
|
636 |
// check if using last rules |
|
637 |
if (lastRules.length > 0 && |
|
638 |
dt.isAfter(savingsLocalTransitions[savingsLocalTransitions.length - 1])) { |
|
639 |
ZoneOffsetTransition[] transArray = findTransitionArray(dt.getYear()); |
|
640 |
Object info = null; |
|
641 |
for (ZoneOffsetTransition trans : transArray) { |
|
642 |
info = findOffsetInfo(dt, trans); |
|
643 |
if (info instanceof ZoneOffsetTransition || info.equals(trans.getOffsetBefore())) { |
|
644 |
return info; |
|
645 |
} |
|
646 |
} |
|
647 |
return info; |
|
648 |
} |
|
649 |
||
650 |
// using historic rules |
|
651 |
int index = Arrays.binarySearch(savingsLocalTransitions, dt); |
|
652 |
if (index == -1) { |
|
653 |
// before first transition |
|
654 |
return wallOffsets[0]; |
|
655 |
} |
|
656 |
if (index < 0) { |
|
657 |
// switch negative insert position to start of matched range |
|
658 |
index = -index - 2; |
|
659 |
} else if (index < savingsLocalTransitions.length - 1 && |
|
660 |
savingsLocalTransitions[index].equals(savingsLocalTransitions[index + 1])) { |
|
661 |
// handle overlap immediately following gap |
|
662 |
index++; |
|
663 |
} |
|
664 |
if ((index & 1) == 0) { |
|
665 |
// gap or overlap |
|
666 |
LocalDateTime dtBefore = savingsLocalTransitions[index]; |
|
667 |
LocalDateTime dtAfter = savingsLocalTransitions[index + 1]; |
|
668 |
ZoneOffset offsetBefore = wallOffsets[index / 2]; |
|
669 |
ZoneOffset offsetAfter = wallOffsets[index / 2 + 1]; |
|
670 |
if (offsetAfter.getTotalSeconds() > offsetBefore.getTotalSeconds()) { |
|
671 |
// gap |
|
672 |
return new ZoneOffsetTransition(dtBefore, offsetBefore, offsetAfter); |
|
673 |
} else { |
|
674 |
// overlap |
|
675 |
return new ZoneOffsetTransition(dtAfter, offsetBefore, offsetAfter); |
|
676 |
} |
|
677 |
} else { |
|
678 |
// normal (neither gap or overlap) |
|
679 |
return wallOffsets[index / 2 + 1]; |
|
680 |
} |
|
681 |
} |
|
682 |
||
683 |
/** |
|
684 |
* Finds the offset info for a local date-time and transition. |
|
685 |
* |
|
686 |
* @param dt the date-time, not null |
|
687 |
* @param trans the transition, not null |
|
688 |
* @return the offset info, not null |
|
689 |
*/ |
|
690 |
private Object findOffsetInfo(LocalDateTime dt, ZoneOffsetTransition trans) { |
|
691 |
LocalDateTime localTransition = trans.getDateTimeBefore(); |
|
692 |
if (trans.isGap()) { |
|
693 |
if (dt.isBefore(localTransition)) { |
|
694 |
return trans.getOffsetBefore(); |
|
695 |
} |
|
696 |
if (dt.isBefore(trans.getDateTimeAfter())) { |
|
697 |
return trans; |
|
698 |
} else { |
|
699 |
return trans.getOffsetAfter(); |
|
700 |
} |
|
701 |
} else { |
|
702 |
if (dt.isBefore(localTransition) == false) { |
|
703 |
return trans.getOffsetAfter(); |
|
704 |
} |
|
705 |
if (dt.isBefore(trans.getDateTimeAfter())) { |
|
706 |
return trans.getOffsetBefore(); |
|
707 |
} else { |
|
708 |
return trans; |
|
709 |
} |
|
710 |
} |
|
711 |
} |
|
712 |
||
713 |
/** |
|
714 |
* Finds the appropriate transition array for the given year. |
|
715 |
* |
|
716 |
* @param year the year, not null |
|
717 |
* @return the transition array, not null |
|
718 |
*/ |
|
719 |
private ZoneOffsetTransition[] findTransitionArray(int year) { |
|
720 |
Integer yearObj = year; // should use Year class, but this saves a class load |
|
721 |
ZoneOffsetTransition[] transArray = lastRulesCache.get(yearObj); |
|
722 |
if (transArray != null) { |
|
723 |
return transArray; |
|
724 |
} |
|
725 |
ZoneOffsetTransitionRule[] ruleArray = lastRules; |
|
726 |
transArray = new ZoneOffsetTransition[ruleArray.length]; |
|
727 |
for (int i = 0; i < ruleArray.length; i++) { |
|
728 |
transArray[i] = ruleArray[i].createTransition(year); |
|
729 |
} |
|
730 |
if (year < LAST_CACHED_YEAR) { |
|
731 |
lastRulesCache.putIfAbsent(yearObj, transArray); |
|
732 |
} |
|
733 |
return transArray; |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Gets the standard offset for the specified instant in this zone. |
|
738 |
* <p> |
|
739 |
* This provides access to historic information on how the standard offset |
|
740 |
* has changed over time. |
|
741 |
* The standard offset is the offset before any daylight saving time is applied. |
|
742 |
* This is typically the offset applicable during winter. |
|
743 |
* |
|
744 |
* @param instant the instant to find the offset information for, not null, but null |
|
745 |
* may be ignored if the rules have a single offset for all instants |
|
746 |
* @return the standard offset, not null |
|
747 |
*/ |
|
748 |
public ZoneOffset getStandardOffset(Instant instant) { |
|
749 |
if (savingsInstantTransitions.length == 0) { |
|
750 |
return standardOffsets[0]; |
|
751 |
} |
|
752 |
long epochSec = instant.getEpochSecond(); |
|
753 |
int index = Arrays.binarySearch(standardTransitions, epochSec); |
|
754 |
if (index < 0) { |
|
755 |
// switch negative insert position to start of matched range |
|
756 |
index = -index - 2; |
|
757 |
} |
|
758 |
return standardOffsets[index + 1]; |
|
759 |
} |
|
760 |
||
761 |
/** |
|
762 |
* Gets the amount of daylight savings in use for the specified instant in this zone. |
|
763 |
* <p> |
|
764 |
* This provides access to historic information on how the amount of daylight |
|
765 |
* savings has changed over time. |
|
766 |
* This is the difference between the standard offset and the actual offset. |
|
767 |
* Typically the amount is zero during winter and one hour during summer. |
|
768 |
* Time-zones are second-based, so the nanosecond part of the duration will be zero. |
|
769 |
* <p> |
|
770 |
* This default implementation calculates the duration from the |
|
771 |
* {@link #getOffset(java.time.Instant) actual} and |
|
772 |
* {@link #getStandardOffset(java.time.Instant) standard} offsets. |
|
773 |
* |
|
774 |
* @param instant the instant to find the daylight savings for, not null, but null |
|
775 |
* may be ignored if the rules have a single offset for all instants |
|
776 |
* @return the difference between the standard and actual offset, not null |
|
777 |
*/ |
|
778 |
public Duration getDaylightSavings(Instant instant) { |
|
779 |
if (savingsInstantTransitions.length == 0) { |
|
780 |
return Duration.ZERO; |
|
781 |
} |
|
782 |
ZoneOffset standardOffset = getStandardOffset(instant); |
|
783 |
ZoneOffset actualOffset = getOffset(instant); |
|
784 |
return Duration.ofSeconds(actualOffset.getTotalSeconds() - standardOffset.getTotalSeconds()); |
|
785 |
} |
|
786 |
||
787 |
/** |
|
788 |
* Checks if the specified instant is in daylight savings. |
|
789 |
* <p> |
|
790 |
* This checks if the standard offset and the actual offset are the same |
|
791 |
* for the specified instant. |
|
792 |
* If they are not, it is assumed that daylight savings is in operation. |
|
793 |
* <p> |
|
794 |
* This default implementation compares the {@link #getOffset(java.time.Instant) actual} |
|
795 |
* and {@link #getStandardOffset(java.time.Instant) standard} offsets. |
|
796 |
* |
|
797 |
* @param instant the instant to find the offset information for, not null, but null |
|
798 |
* may be ignored if the rules have a single offset for all instants |
|
799 |
* @return the standard offset, not null |
|
800 |
*/ |
|
801 |
public boolean isDaylightSavings(Instant instant) { |
|
802 |
return (getStandardOffset(instant).equals(getOffset(instant)) == false); |
|
803 |
} |
|
804 |
||
805 |
/** |
|
806 |
* Checks if the offset date-time is valid for these rules. |
|
807 |
* <p> |
|
808 |
* To be valid, the local date-time must not be in a gap and the offset |
|
809 |
* must match one of the valid offsets. |
|
810 |
* <p> |
|
811 |
* This default implementation checks if {@link #getValidOffsets(java.time.LocalDateTime)} |
|
812 |
* contains the specified offset. |
|
813 |
* |
|
814 |
* @param localDateTime the date-time to check, not null, but null |
|
815 |
* may be ignored if the rules have a single offset for all instants |
|
816 |
* @param offset the offset to check, null returns false |
|
817 |
* @return true if the offset date-time is valid for these rules |
|
818 |
*/ |
|
819 |
public boolean isValidOffset(LocalDateTime localDateTime, ZoneOffset offset) { |
|
820 |
return getValidOffsets(localDateTime).contains(offset); |
|
821 |
} |
|
822 |
||
823 |
/** |
|
824 |
* Gets the next transition after the specified instant. |
|
825 |
* <p> |
|
826 |
* This returns details of the next transition after the specified instant. |
|
827 |
* For example, if the instant represents a point where "Summer" daylight savings time |
|
828 |
* applies, then the method will return the transition to the next "Winter" time. |
|
829 |
* |
|
830 |
* @param instant the instant to get the next transition after, not null, but null |
|
831 |
* may be ignored if the rules have a single offset for all instants |
|
832 |
* @return the next transition after the specified instant, null if this is after the last transition |
|
833 |
*/ |
|
834 |
public ZoneOffsetTransition nextTransition(Instant instant) { |
|
835 |
if (savingsInstantTransitions.length == 0) { |
|
836 |
return null; |
|
837 |
} |
|
838 |
long epochSec = instant.getEpochSecond(); |
|
839 |
// check if using last rules |
|
840 |
if (epochSec >= savingsInstantTransitions[savingsInstantTransitions.length - 1]) { |
|
841 |
if (lastRules.length == 0) { |
|
842 |
return null; |
|
843 |
} |
|
844 |
// search year the instant is in |
|
845 |
int year = findYear(epochSec, wallOffsets[wallOffsets.length - 1]); |
|
846 |
ZoneOffsetTransition[] transArray = findTransitionArray(year); |
|
847 |
for (ZoneOffsetTransition trans : transArray) { |
|
848 |
if (epochSec < trans.toEpochSecond()) { |
|
849 |
return trans; |
|
850 |
} |
|
851 |
} |
|
852 |
// use first from following year |
|
853 |
if (year < Year.MAX_VALUE) { |
|
854 |
transArray = findTransitionArray(year + 1); |
|
855 |
return transArray[0]; |
|
856 |
} |
|
857 |
return null; |
|
858 |
} |
|
859 |
||
860 |
// using historic rules |
|
861 |
int index = Arrays.binarySearch(savingsInstantTransitions, epochSec); |
|
862 |
if (index < 0) { |
|
863 |
index = -index - 1; // switched value is the next transition |
|
864 |
} else { |
|
865 |
index += 1; // exact match, so need to add one to get the next |
|
866 |
} |
|
867 |
return new ZoneOffsetTransition(savingsInstantTransitions[index], wallOffsets[index], wallOffsets[index + 1]); |
|
868 |
} |
|
869 |
||
870 |
/** |
|
871 |
* Gets the previous transition before the specified instant. |
|
872 |
* <p> |
|
873 |
* This returns details of the previous transition after the specified instant. |
|
874 |
* For example, if the instant represents a point where "summer" daylight saving time |
|
875 |
* applies, then the method will return the transition from the previous "winter" time. |
|
876 |
* |
|
877 |
* @param instant the instant to get the previous transition after, not null, but null |
|
878 |
* may be ignored if the rules have a single offset for all instants |
|
879 |
* @return the previous transition after the specified instant, null if this is before the first transition |
|
880 |
*/ |
|
881 |
public ZoneOffsetTransition previousTransition(Instant instant) { |
|
882 |
if (savingsInstantTransitions.length == 0) { |
|
883 |
return null; |
|
884 |
} |
|
885 |
long epochSec = instant.getEpochSecond(); |
|
886 |
if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) { |
|
887 |
epochSec += 1; // allow rest of method to only use seconds |
|
888 |
} |
|
889 |
||
890 |
// check if using last rules |
|
891 |
long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1]; |
|
892 |
if (lastRules.length > 0 && epochSec > lastHistoric) { |
|
893 |
// search year the instant is in |
|
894 |
ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1]; |
|
895 |
int year = findYear(epochSec, lastHistoricOffset); |
|
896 |
ZoneOffsetTransition[] transArray = findTransitionArray(year); |
|
897 |
for (int i = transArray.length - 1; i >= 0; i--) { |
|
898 |
if (epochSec > transArray[i].toEpochSecond()) { |
|
899 |
return transArray[i]; |
|
900 |
} |
|
901 |
} |
|
902 |
// use last from preceeding year |
|
903 |
int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset); |
|
904 |
if (--year > lastHistoricYear) { |
|
905 |
transArray = findTransitionArray(year); |
|
906 |
return transArray[transArray.length - 1]; |
|
907 |
} |
|
908 |
// drop through |
|
909 |
} |
|
910 |
||
911 |
// using historic rules |
|
912 |
int index = Arrays.binarySearch(savingsInstantTransitions, epochSec); |
|
913 |
if (index < 0) { |
|
914 |
index = -index - 1; |
|
915 |
} |
|
916 |
if (index <= 0) { |
|
917 |
return null; |
|
918 |
} |
|
919 |
return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]); |
|
920 |
} |
|
921 |
||
922 |
private int findYear(long epochSecond, ZoneOffset offset) { |
|
923 |
// inline for performance |
|
924 |
long localSecond = epochSecond + offset.getTotalSeconds(); |
|
925 |
long localEpochDay = Math.floorDiv(localSecond, 86400); |
|
926 |
return LocalDate.ofEpochDay(localEpochDay).getYear(); |
|
927 |
} |
|
928 |
||
929 |
/** |
|
930 |
* Gets the complete list of fully defined transitions. |
|
931 |
* <p> |
|
932 |
* The complete set of transitions for this rules instance is defined by this method |
|
933 |
* and {@link #getTransitionRules()}. This method returns those transitions that have |
|
934 |
* been fully defined. These are typically historical, but may be in the future. |
|
935 |
* <p> |
|
936 |
* The list will be empty for fixed offset rules and for any time-zone where there has |
|
937 |
* only ever been a single offset. The list will also be empty if the transition rules are unknown. |
|
938 |
* |
|
939 |
* @return an immutable list of fully defined transitions, not null |
|
940 |
*/ |
|
941 |
public List<ZoneOffsetTransition> getTransitions() { |
|
942 |
List<ZoneOffsetTransition> list = new ArrayList<>(); |
|
943 |
for (int i = 0; i < savingsInstantTransitions.length; i++) { |
|
944 |
list.add(new ZoneOffsetTransition(savingsInstantTransitions[i], wallOffsets[i], wallOffsets[i + 1])); |
|
945 |
} |
|
946 |
return Collections.unmodifiableList(list); |
|
947 |
} |
|
948 |
||
949 |
/** |
|
950 |
* Gets the list of transition rules for years beyond those defined in the transition list. |
|
951 |
* <p> |
|
952 |
* The complete set of transitions for this rules instance is defined by this method |
|
953 |
* and {@link #getTransitions()}. This method returns instances of {@link ZoneOffsetTransitionRule} |
|
954 |
* that define an algorithm for when transitions will occur. |
|
955 |
* <p> |
|
956 |
* For any given {@code ZoneRules}, this list contains the transition rules for years |
|
957 |
* beyond those years that have been fully defined. These rules typically refer to future |
|
958 |
* daylight saving time rule changes. |
|
959 |
* <p> |
|
960 |
* If the zone defines daylight savings into the future, then the list will normally |
|
961 |
* be of size two and hold information about entering and exiting daylight savings. |
|
962 |
* If the zone does not have daylight savings, or information about future changes |
|
963 |
* is uncertain, then the list will be empty. |
|
964 |
* <p> |
|
965 |
* The list will be empty for fixed offset rules and for any time-zone where there is no |
|
966 |
* daylight saving time. The list will also be empty if the transition rules are unknown. |
|
967 |
* |
|
968 |
* @return an immutable list of transition rules, not null |
|
969 |
*/ |
|
970 |
public List<ZoneOffsetTransitionRule> getTransitionRules() { |
|
971 |
return Collections.unmodifiableList(Arrays.asList(lastRules)); |
|
972 |
} |
|
973 |
||
974 |
/** |
|
975 |
* Checks if this set of rules equals another. |
|
976 |
* <p> |
|
977 |
* Two rule sets are equal if they will always result in the same output |
|
978 |
* for any given input instant or local date-time. |
|
979 |
* Rules from two different groups may return false even if they are in fact the same. |
|
980 |
* <p> |
|
981 |
* This definition should result in implementations comparing their entire state. |
|
982 |
* |
|
983 |
* @param otherRules the other rules, null returns false |
|
984 |
* @return true if this rules is the same as that specified |
|
985 |
*/ |
|
986 |
@Override |
|
987 |
public boolean equals(Object otherRules) { |
|
988 |
if (this == otherRules) { |
|
989 |
return true; |
|
990 |
} |
|
991 |
if (otherRules instanceof ZoneRules) { |
|
992 |
ZoneRules other = (ZoneRules) otherRules; |
|
993 |
return Arrays.equals(standardTransitions, other.standardTransitions) && |
|
994 |
Arrays.equals(standardOffsets, other.standardOffsets) && |
|
995 |
Arrays.equals(savingsInstantTransitions, other.savingsInstantTransitions) && |
|
996 |
Arrays.equals(wallOffsets, other.wallOffsets) && |
|
997 |
Arrays.equals(lastRules, other.lastRules); |
|
998 |
} |
|
999 |
return false; |
|
1000 |
} |
|
1001 |
||
1002 |
/** |
|
1003 |
* Returns a suitable hash code given the definition of {@code #equals}. |
|
1004 |
* |
|
1005 |
* @return the hash code |
|
1006 |
*/ |
|
1007 |
@Override |
|
1008 |
public int hashCode() { |
|
1009 |
return Arrays.hashCode(standardTransitions) ^ |
|
1010 |
Arrays.hashCode(standardOffsets) ^ |
|
1011 |
Arrays.hashCode(savingsInstantTransitions) ^ |
|
1012 |
Arrays.hashCode(wallOffsets) ^ |
|
1013 |
Arrays.hashCode(lastRules); |
|
1014 |
} |
|
1015 |
||
1016 |
/** |
|
1017 |
* Returns a string describing this object. |
|
1018 |
* |
|
1019 |
* @return a string for debugging, not null |
|
1020 |
*/ |
|
1021 |
@Override |
|
1022 |
public String toString() { |
|
1023 |
return "ZoneRules[currentStandardOffset=" + standardOffsets[standardOffsets.length - 1] + "]"; |
|
1024 |
} |
|
1025 |
||
1026 |
} |