|
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) 2007-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; |
|
63 |
|
64 import static java.time.temporal.ChronoField.OFFSET_SECONDS; |
|
65 |
|
66 import java.io.DataInput; |
|
67 import java.io.DataOutput; |
|
68 import java.io.IOException; |
|
69 import java.io.InvalidObjectException; |
|
70 import java.io.ObjectStreamException; |
|
71 import java.io.Serializable; |
|
72 import java.time.temporal.ChronoField; |
|
73 import java.time.temporal.Queries; |
|
74 import java.time.temporal.Temporal; |
|
75 import java.time.temporal.TemporalAccessor; |
|
76 import java.time.temporal.TemporalAdjuster; |
|
77 import java.time.temporal.TemporalField; |
|
78 import java.time.temporal.TemporalQuery; |
|
79 import java.time.temporal.ValueRange; |
|
80 import java.time.zone.ZoneRules; |
|
81 import java.util.Objects; |
|
82 import java.util.concurrent.ConcurrentHashMap; |
|
83 import java.util.concurrent.ConcurrentMap; |
|
84 |
|
85 /** |
|
86 * A time-zone offset from Greenwich/UTC, such as {@code +02:00}. |
|
87 * <p> |
|
88 * A time-zone offset is the period of time that a time-zone differs from Greenwich/UTC. |
|
89 * This is usually a fixed number of hours and minutes. |
|
90 * <p> |
|
91 * Different parts of the world have different time-zone offsets. |
|
92 * The rules for how offsets vary by place and time of year are captured in the |
|
93 * {@link ZoneId} class. |
|
94 * <p> |
|
95 * For example, Paris is one hour ahead of Greenwich/UTC in winter and two hours |
|
96 * ahead in summer. The {@code ZoneId} instance for Paris will reference two |
|
97 * {@code ZoneOffset} instances - a {@code +01:00} instance for winter, |
|
98 * and a {@code +02:00} instance for summer. |
|
99 * <p> |
|
100 * In 2008, time-zone offsets around the world extended from -12:00 to +14:00. |
|
101 * To prevent any problems with that range being extended, yet still provide |
|
102 * validation, the range of offsets is restricted to -18:00 to 18:00 inclusive. |
|
103 * <p> |
|
104 * This class is designed for use with the ISO calendar system. |
|
105 * The fields of hours, minutes and seconds make assumptions that are valid for the |
|
106 * standard ISO definitions of those fields. This class may be used with other |
|
107 * calendar systems providing the definition of the time fields matches those |
|
108 * of the ISO calendar system. |
|
109 * <p> |
|
110 * Instances of {@code ZoneOffset} must be compared using {@link #equals}. |
|
111 * Implementations may choose to cache certain common offsets, however |
|
112 * applications must not rely on such caching. |
|
113 * |
|
114 * <h3>Specification for implementors</h3> |
|
115 * This class is immutable and thread-safe. |
|
116 * |
|
117 * @since 1.8 |
|
118 */ |
|
119 public final class ZoneOffset |
|
120 extends ZoneId |
|
121 implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable { |
|
122 |
|
123 /** Cache of time-zone offset by offset in seconds. */ |
|
124 private static final ConcurrentMap<Integer, ZoneOffset> SECONDS_CACHE = new ConcurrentHashMap<>(16, 0.75f, 4); |
|
125 /** Cache of time-zone offset by ID. */ |
|
126 private static final ConcurrentMap<String, ZoneOffset> ID_CACHE = new ConcurrentHashMap<>(16, 0.75f, 4); |
|
127 |
|
128 /** |
|
129 * The number of seconds per hour. |
|
130 */ |
|
131 private static final int SECONDS_PER_HOUR = 60 * 60; |
|
132 /** |
|
133 * The number of seconds per minute. |
|
134 */ |
|
135 private static final int SECONDS_PER_MINUTE = 60; |
|
136 /** |
|
137 * The number of minutes per hour. |
|
138 */ |
|
139 private static final int MINUTES_PER_HOUR = 60; |
|
140 /** |
|
141 * The abs maximum seconds. |
|
142 */ |
|
143 private static final int MAX_SECONDS = 18 * SECONDS_PER_HOUR; |
|
144 /** |
|
145 * Serialization version. |
|
146 */ |
|
147 private static final long serialVersionUID = 2357656521762053153L; |
|
148 |
|
149 /** |
|
150 * The time-zone offset for UTC, with an ID of 'Z'. |
|
151 */ |
|
152 public static final ZoneOffset UTC = ZoneOffset.ofTotalSeconds(0); |
|
153 /** |
|
154 * Constant for the maximum supported offset. |
|
155 */ |
|
156 public static final ZoneOffset MIN = ZoneOffset.ofTotalSeconds(-MAX_SECONDS); |
|
157 /** |
|
158 * Constant for the maximum supported offset. |
|
159 */ |
|
160 public static final ZoneOffset MAX = ZoneOffset.ofTotalSeconds(MAX_SECONDS); |
|
161 |
|
162 /** |
|
163 * The total offset in seconds. |
|
164 */ |
|
165 private final int totalSeconds; |
|
166 /** |
|
167 * The string form of the time-zone offset. |
|
168 */ |
|
169 private final transient String id; |
|
170 |
|
171 //----------------------------------------------------------------------- |
|
172 /** |
|
173 * Obtains an instance of {@code ZoneOffset} using the ID. |
|
174 * <p> |
|
175 * This method parses the string ID of a {@code ZoneOffset} to |
|
176 * return an instance. The parsing accepts all the formats generated by |
|
177 * {@link #getId()}, plus some additional formats: |
|
178 * <p><ul> |
|
179 * <li>{@code Z} - for UTC |
|
180 * <li>{@code +h} |
|
181 * <li>{@code +hh} |
|
182 * <li>{@code +hh:mm} |
|
183 * <li>{@code -hh:mm} |
|
184 * <li>{@code +hhmm} |
|
185 * <li>{@code -hhmm} |
|
186 * <li>{@code +hh:mm:ss} |
|
187 * <li>{@code -hh:mm:ss} |
|
188 * <li>{@code +hhmmss} |
|
189 * <li>{@code -hhmmss} |
|
190 * </ul><p> |
|
191 * Note that ± means either the plus or minus symbol. |
|
192 * <p> |
|
193 * The ID of the returned offset will be normalized to one of the formats |
|
194 * described by {@link #getId()}. |
|
195 * <p> |
|
196 * The maximum supported range is from +18:00 to -18:00 inclusive. |
|
197 * |
|
198 * @param offsetId the offset ID, not null |
|
199 * @return the zone-offset, not null |
|
200 * @throws DateTimeException if the offset ID is invalid |
|
201 */ |
|
202 @SuppressWarnings("fallthrough") |
|
203 public static ZoneOffset of(String offsetId) { |
|
204 Objects.requireNonNull(offsetId, "offsetId"); |
|
205 // "Z" is always in the cache |
|
206 ZoneOffset offset = ID_CACHE.get(offsetId); |
|
207 if (offset != null) { |
|
208 return offset; |
|
209 } |
|
210 |
|
211 // parse - +h, +hh, +hhmm, +hh:mm, +hhmmss, +hh:mm:ss |
|
212 final int hours, minutes, seconds; |
|
213 switch (offsetId.length()) { |
|
214 case 2: |
|
215 offsetId = offsetId.charAt(0) + "0" + offsetId.charAt(1); // fallthru |
|
216 case 3: |
|
217 hours = parseNumber(offsetId, 1, false); |
|
218 minutes = 0; |
|
219 seconds = 0; |
|
220 break; |
|
221 case 5: |
|
222 hours = parseNumber(offsetId, 1, false); |
|
223 minutes = parseNumber(offsetId, 3, false); |
|
224 seconds = 0; |
|
225 break; |
|
226 case 6: |
|
227 hours = parseNumber(offsetId, 1, false); |
|
228 minutes = parseNumber(offsetId, 4, true); |
|
229 seconds = 0; |
|
230 break; |
|
231 case 7: |
|
232 hours = parseNumber(offsetId, 1, false); |
|
233 minutes = parseNumber(offsetId, 3, false); |
|
234 seconds = parseNumber(offsetId, 5, false); |
|
235 break; |
|
236 case 9: |
|
237 hours = parseNumber(offsetId, 1, false); |
|
238 minutes = parseNumber(offsetId, 4, true); |
|
239 seconds = parseNumber(offsetId, 7, true); |
|
240 break; |
|
241 default: |
|
242 throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid"); |
|
243 } |
|
244 char first = offsetId.charAt(0); |
|
245 if (first != '+' && first != '-') { |
|
246 throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid: Plus/minus not found when expected"); |
|
247 } |
|
248 if (first == '-') { |
|
249 return ofHoursMinutesSeconds(-hours, -minutes, -seconds); |
|
250 } else { |
|
251 return ofHoursMinutesSeconds(hours, minutes, seconds); |
|
252 } |
|
253 } |
|
254 |
|
255 /** |
|
256 * Parse a two digit zero-prefixed number. |
|
257 * |
|
258 * @param offsetId the offset ID, not null |
|
259 * @param pos the position to parse, valid |
|
260 * @param precededByColon should this number be prefixed by a precededByColon |
|
261 * @return the parsed number, from 0 to 99 |
|
262 */ |
|
263 private static int parseNumber(CharSequence offsetId, int pos, boolean precededByColon) { |
|
264 if (precededByColon && offsetId.charAt(pos - 1) != ':') { |
|
265 throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid: Colon not found when expected"); |
|
266 } |
|
267 char ch1 = offsetId.charAt(pos); |
|
268 char ch2 = offsetId.charAt(pos + 1); |
|
269 if (ch1 < '0' || ch1 > '9' || ch2 < '0' || ch2 > '9') { |
|
270 throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid: Non numeric characters found"); |
|
271 } |
|
272 return (ch1 - 48) * 10 + (ch2 - 48); |
|
273 } |
|
274 |
|
275 //----------------------------------------------------------------------- |
|
276 /** |
|
277 * Obtains an instance of {@code ZoneOffset} using an offset in hours. |
|
278 * |
|
279 * @param hours the time-zone offset in hours, from -18 to +18 |
|
280 * @return the zone-offset, not null |
|
281 * @throws DateTimeException if the offset is not in the required range |
|
282 */ |
|
283 public static ZoneOffset ofHours(int hours) { |
|
284 return ofHoursMinutesSeconds(hours, 0, 0); |
|
285 } |
|
286 |
|
287 /** |
|
288 * Obtains an instance of {@code ZoneOffset} using an offset in |
|
289 * hours and minutes. |
|
290 * <p> |
|
291 * The sign of the hours and minutes components must match. |
|
292 * Thus, if the hours is negative, the minutes must be negative or zero. |
|
293 * If the hours is zero, the minutes may be positive, negative or zero. |
|
294 * |
|
295 * @param hours the time-zone offset in hours, from -18 to +18 |
|
296 * @param minutes the time-zone offset in minutes, from 0 to ±59, sign matches hours |
|
297 * @return the zone-offset, not null |
|
298 * @throws DateTimeException if the offset is not in the required range |
|
299 */ |
|
300 public static ZoneOffset ofHoursMinutes(int hours, int minutes) { |
|
301 return ofHoursMinutesSeconds(hours, minutes, 0); |
|
302 } |
|
303 |
|
304 /** |
|
305 * Obtains an instance of {@code ZoneOffset} using an offset in |
|
306 * hours, minutes and seconds. |
|
307 * <p> |
|
308 * The sign of the hours, minutes and seconds components must match. |
|
309 * Thus, if the hours is negative, the minutes and seconds must be negative or zero. |
|
310 * |
|
311 * @param hours the time-zone offset in hours, from -18 to +18 |
|
312 * @param minutes the time-zone offset in minutes, from 0 to ±59, sign matches hours and seconds |
|
313 * @param seconds the time-zone offset in seconds, from 0 to ±59, sign matches hours and minutes |
|
314 * @return the zone-offset, not null |
|
315 * @throws DateTimeException if the offset is not in the required range |
|
316 */ |
|
317 public static ZoneOffset ofHoursMinutesSeconds(int hours, int minutes, int seconds) { |
|
318 validate(hours, minutes, seconds); |
|
319 int totalSeconds = totalSeconds(hours, minutes, seconds); |
|
320 return ofTotalSeconds(totalSeconds); |
|
321 } |
|
322 |
|
323 //----------------------------------------------------------------------- |
|
324 /** |
|
325 * Obtains an instance of {@code ZoneOffset} from a temporal object. |
|
326 * <p> |
|
327 * A {@code TemporalAccessor} represents some form of date and time information. |
|
328 * This factory converts the arbitrary temporal object to an instance of {@code ZoneOffset}. |
|
329 * <p> |
|
330 * The conversion extracts the {@link ChronoField#OFFSET_SECONDS offset-seconds} field. |
|
331 * <p> |
|
332 * This method matches the signature of the functional interface {@link TemporalQuery} |
|
333 * allowing it to be used in queries via method reference, {@code ZoneOffset::from}. |
|
334 * |
|
335 * @param temporal the temporal object to convert, not null |
|
336 * @return the zone-offset, not null |
|
337 * @throws DateTimeException if unable to convert to an {@code ZoneOffset} |
|
338 */ |
|
339 public static ZoneOffset from(TemporalAccessor temporal) { |
|
340 if (temporal instanceof ZoneOffset) { |
|
341 return (ZoneOffset) temporal; |
|
342 } |
|
343 try { |
|
344 return ofTotalSeconds(temporal.get(OFFSET_SECONDS)); |
|
345 } catch (DateTimeException ex) { |
|
346 throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal.getClass(), ex); |
|
347 } |
|
348 } |
|
349 |
|
350 //----------------------------------------------------------------------- |
|
351 /** |
|
352 * Validates the offset fields. |
|
353 * |
|
354 * @param hours the time-zone offset in hours, from -18 to +18 |
|
355 * @param minutes the time-zone offset in minutes, from 0 to ±59 |
|
356 * @param seconds the time-zone offset in seconds, from 0 to ±59 |
|
357 * @throws DateTimeException if the offset is not in the required range |
|
358 */ |
|
359 private static void validate(int hours, int minutes, int seconds) { |
|
360 if (hours < -18 || hours > 18) { |
|
361 throw new DateTimeException("Zone offset hours not in valid range: value " + hours + |
|
362 " is not in the range -18 to 18"); |
|
363 } |
|
364 if (hours > 0) { |
|
365 if (minutes < 0 || seconds < 0) { |
|
366 throw new DateTimeException("Zone offset minutes and seconds must be positive because hours is positive"); |
|
367 } |
|
368 } else if (hours < 0) { |
|
369 if (minutes > 0 || seconds > 0) { |
|
370 throw new DateTimeException("Zone offset minutes and seconds must be negative because hours is negative"); |
|
371 } |
|
372 } else if ((minutes > 0 && seconds < 0) || (minutes < 0 && seconds > 0)) { |
|
373 throw new DateTimeException("Zone offset minutes and seconds must have the same sign"); |
|
374 } |
|
375 if (Math.abs(minutes) > 59) { |
|
376 throw new DateTimeException("Zone offset minutes not in valid range: abs(value) " + |
|
377 Math.abs(minutes) + " is not in the range 0 to 59"); |
|
378 } |
|
379 if (Math.abs(seconds) > 59) { |
|
380 throw new DateTimeException("Zone offset seconds not in valid range: abs(value) " + |
|
381 Math.abs(seconds) + " is not in the range 0 to 59"); |
|
382 } |
|
383 if (Math.abs(hours) == 18 && (Math.abs(minutes) > 0 || Math.abs(seconds) > 0)) { |
|
384 throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00"); |
|
385 } |
|
386 } |
|
387 |
|
388 /** |
|
389 * Calculates the total offset in seconds. |
|
390 * |
|
391 * @param hours the time-zone offset in hours, from -18 to +18 |
|
392 * @param minutes the time-zone offset in minutes, from 0 to ±59, sign matches hours and seconds |
|
393 * @param seconds the time-zone offset in seconds, from 0 to ±59, sign matches hours and minutes |
|
394 * @return the total in seconds |
|
395 */ |
|
396 private static int totalSeconds(int hours, int minutes, int seconds) { |
|
397 return hours * SECONDS_PER_HOUR + minutes * SECONDS_PER_MINUTE + seconds; |
|
398 } |
|
399 |
|
400 //----------------------------------------------------------------------- |
|
401 /** |
|
402 * Obtains an instance of {@code ZoneOffset} specifying the total offset in seconds |
|
403 * <p> |
|
404 * The offset must be in the range {@code -18:00} to {@code +18:00}, which corresponds to -64800 to +64800. |
|
405 * |
|
406 * @param totalSeconds the total time-zone offset in seconds, from -64800 to +64800 |
|
407 * @return the ZoneOffset, not null |
|
408 * @throws DateTimeException if the offset is not in the required range |
|
409 */ |
|
410 public static ZoneOffset ofTotalSeconds(int totalSeconds) { |
|
411 if (Math.abs(totalSeconds) > MAX_SECONDS) { |
|
412 throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00"); |
|
413 } |
|
414 if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) { |
|
415 Integer totalSecs = totalSeconds; |
|
416 ZoneOffset result = SECONDS_CACHE.get(totalSecs); |
|
417 if (result == null) { |
|
418 result = new ZoneOffset(totalSeconds); |
|
419 SECONDS_CACHE.putIfAbsent(totalSecs, result); |
|
420 result = SECONDS_CACHE.get(totalSecs); |
|
421 ID_CACHE.putIfAbsent(result.getId(), result); |
|
422 } |
|
423 return result; |
|
424 } else { |
|
425 return new ZoneOffset(totalSeconds); |
|
426 } |
|
427 } |
|
428 |
|
429 //----------------------------------------------------------------------- |
|
430 /** |
|
431 * Constructor. |
|
432 * |
|
433 * @param totalSeconds the total time-zone offset in seconds, from -64800 to +64800 |
|
434 */ |
|
435 private ZoneOffset(int totalSeconds) { |
|
436 super(); |
|
437 this.totalSeconds = totalSeconds; |
|
438 id = buildId(totalSeconds); |
|
439 } |
|
440 |
|
441 private static String buildId(int totalSeconds) { |
|
442 if (totalSeconds == 0) { |
|
443 return "Z"; |
|
444 } else { |
|
445 int absTotalSeconds = Math.abs(totalSeconds); |
|
446 StringBuilder buf = new StringBuilder(); |
|
447 int absHours = absTotalSeconds / SECONDS_PER_HOUR; |
|
448 int absMinutes = (absTotalSeconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR; |
|
449 buf.append(totalSeconds < 0 ? "-" : "+") |
|
450 .append(absHours < 10 ? "0" : "").append(absHours) |
|
451 .append(absMinutes < 10 ? ":0" : ":").append(absMinutes); |
|
452 int absSeconds = absTotalSeconds % SECONDS_PER_MINUTE; |
|
453 if (absSeconds != 0) { |
|
454 buf.append(absSeconds < 10 ? ":0" : ":").append(absSeconds); |
|
455 } |
|
456 return buf.toString(); |
|
457 } |
|
458 } |
|
459 |
|
460 //----------------------------------------------------------------------- |
|
461 /** |
|
462 * Gets the total zone offset in seconds. |
|
463 * <p> |
|
464 * This is the primary way to access the offset amount. |
|
465 * It returns the total of the hours, minutes and seconds fields as a |
|
466 * single offset that can be added to a time. |
|
467 * |
|
468 * @return the total zone offset amount in seconds |
|
469 */ |
|
470 public int getTotalSeconds() { |
|
471 return totalSeconds; |
|
472 } |
|
473 |
|
474 /** |
|
475 * Gets the normalized zone offset ID. |
|
476 * <p> |
|
477 * The ID is minor variation to the standard ISO-8601 formatted string |
|
478 * for the offset. There are three formats: |
|
479 * <p><ul> |
|
480 * <li>{@code Z} - for UTC (ISO-8601) |
|
481 * <li>{@code +hh:mm} or {@code -hh:mm} - if the seconds are zero (ISO-8601) |
|
482 * <li>{@code +hh:mm:ss} or {@code -hh:mm:ss} - if the seconds are non-zero (not ISO-8601) |
|
483 * </ul><p> |
|
484 * |
|
485 * @return the zone offset ID, not null |
|
486 */ |
|
487 @Override |
|
488 public String getId() { |
|
489 return id; |
|
490 } |
|
491 |
|
492 /** |
|
493 * Gets the associated time-zone rules. |
|
494 * <p> |
|
495 * The rules will always return this offset when queried. |
|
496 * The implementation class is immutable, thread-safe and serializable. |
|
497 * |
|
498 * @return the rules, not null |
|
499 */ |
|
500 @Override |
|
501 public ZoneRules getRules() { |
|
502 return ZoneRules.of(this); |
|
503 } |
|
504 |
|
505 //----------------------------------------------------------------------- |
|
506 /** |
|
507 * Checks if the specified field is supported. |
|
508 * <p> |
|
509 * This checks if this offset can be queried for the specified field. |
|
510 * If false, then calling the {@link #range(TemporalField) range} and |
|
511 * {@link #get(TemporalField) get} methods will throw an exception. |
|
512 * <p> |
|
513 * If the field is a {@link ChronoField} then the query is implemented here. |
|
514 * The {@code OFFSET_SECONDS} field returns true. |
|
515 * All other {@code ChronoField} instances will return false. |
|
516 * <p> |
|
517 * If the field is not a {@code ChronoField}, then the result of this method |
|
518 * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)} |
|
519 * passing {@code this} as the argument. |
|
520 * Whether the field is supported is determined by the field. |
|
521 * |
|
522 * @param field the field to check, null returns false |
|
523 * @return true if the field is supported on this offset, false if not |
|
524 */ |
|
525 @Override |
|
526 public boolean isSupported(TemporalField field) { |
|
527 if (field instanceof ChronoField) { |
|
528 return field == OFFSET_SECONDS; |
|
529 } |
|
530 return field != null && field.doIsSupported(this); |
|
531 } |
|
532 |
|
533 /** |
|
534 * Gets the range of valid values for the specified field. |
|
535 * <p> |
|
536 * The range object expresses the minimum and maximum valid values for a field. |
|
537 * This offset is used to enhance the accuracy of the returned range. |
|
538 * If it is not possible to return the range, because the field is not supported |
|
539 * or for some other reason, an exception is thrown. |
|
540 * <p> |
|
541 * If the field is a {@link ChronoField} then the query is implemented here. |
|
542 * The {@link #isSupported(TemporalField) supported fields} will return |
|
543 * appropriate range instances. |
|
544 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
|
545 * <p> |
|
546 * If the field is not a {@code ChronoField}, then the result of this method |
|
547 * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)} |
|
548 * passing {@code this} as the argument. |
|
549 * Whether the range can be obtained is determined by the field. |
|
550 * |
|
551 * @param field the field to query the range for, not null |
|
552 * @return the range of valid values for the field, not null |
|
553 * @throws DateTimeException if the range for the field cannot be obtained |
|
554 */ |
|
555 @Override // override for Javadoc |
|
556 public ValueRange range(TemporalField field) { |
|
557 return TemporalAccessor.super.range(field); |
|
558 } |
|
559 |
|
560 /** |
|
561 * Gets the value of the specified field from this offset as an {@code int}. |
|
562 * <p> |
|
563 * This queries this offset for the value for the specified field. |
|
564 * The returned value will always be within the valid range of values for the field. |
|
565 * If it is not possible to return the value, because the field is not supported |
|
566 * or for some other reason, an exception is thrown. |
|
567 * <p> |
|
568 * If the field is a {@link ChronoField} then the query is implemented here. |
|
569 * The {@code OFFSET_SECONDS} field returns the value of the offset. |
|
570 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
|
571 * <p> |
|
572 * If the field is not a {@code ChronoField}, then the result of this method |
|
573 * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)} |
|
574 * passing {@code this} as the argument. Whether the value can be obtained, |
|
575 * and what the value represents, is determined by the field. |
|
576 * |
|
577 * @param field the field to get, not null |
|
578 * @return the value for the field |
|
579 * @throws DateTimeException if a value for the field cannot be obtained |
|
580 * @throws ArithmeticException if numeric overflow occurs |
|
581 */ |
|
582 @Override // override for Javadoc and performance |
|
583 public int get(TemporalField field) { |
|
584 if (field == OFFSET_SECONDS) { |
|
585 return totalSeconds; |
|
586 } else if (field instanceof ChronoField) { |
|
587 throw new DateTimeException("Unsupported field: " + field.getName()); |
|
588 } |
|
589 return range(field).checkValidIntValue(getLong(field), field); |
|
590 } |
|
591 |
|
592 /** |
|
593 * Gets the value of the specified field from this offset as a {@code long}. |
|
594 * <p> |
|
595 * This queries this offset for the value for the specified field. |
|
596 * If it is not possible to return the value, because the field is not supported |
|
597 * or for some other reason, an exception is thrown. |
|
598 * <p> |
|
599 * If the field is a {@link ChronoField} then the query is implemented here. |
|
600 * The {@code OFFSET_SECONDS} field returns the value of the offset. |
|
601 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
|
602 * <p> |
|
603 * If the field is not a {@code ChronoField}, then the result of this method |
|
604 * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)} |
|
605 * passing {@code this} as the argument. Whether the value can be obtained, |
|
606 * and what the value represents, is determined by the field. |
|
607 * |
|
608 * @param field the field to get, not null |
|
609 * @return the value for the field |
|
610 * @throws DateTimeException if a value for the field cannot be obtained |
|
611 * @throws ArithmeticException if numeric overflow occurs |
|
612 */ |
|
613 @Override |
|
614 public long getLong(TemporalField field) { |
|
615 if (field == OFFSET_SECONDS) { |
|
616 return totalSeconds; |
|
617 } else if (field instanceof ChronoField) { |
|
618 throw new DateTimeException("Unsupported field: " + field.getName()); |
|
619 } |
|
620 return field.doGet(this); |
|
621 } |
|
622 |
|
623 //----------------------------------------------------------------------- |
|
624 /** |
|
625 * Queries this offset using the specified query. |
|
626 * <p> |
|
627 * This queries this offset using the specified query strategy object. |
|
628 * The {@code TemporalQuery} object defines the logic to be used to |
|
629 * obtain the result. Read the documentation of the query to understand |
|
630 * what the result of this method will be. |
|
631 * <p> |
|
632 * The result of this method is obtained by invoking the |
|
633 * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the |
|
634 * specified query passing {@code this} as the argument. |
|
635 * |
|
636 * @param <R> the type of the result |
|
637 * @param query the query to invoke, not null |
|
638 * @return the query result, null may be returned (defined by the query) |
|
639 * @throws DateTimeException if unable to query (defined by the query) |
|
640 * @throws ArithmeticException if numeric overflow occurs (defined by the query) |
|
641 */ |
|
642 @SuppressWarnings("unchecked") |
|
643 @Override |
|
644 public <R> R query(TemporalQuery<R> query) { |
|
645 if (query == Queries.offset() || query == Queries.zone()) { |
|
646 return (R) this; |
|
647 } |
|
648 return TemporalAccessor.super.query(query); |
|
649 } |
|
650 |
|
651 /** |
|
652 * Adjusts the specified temporal object to have the same offset as this object. |
|
653 * <p> |
|
654 * This returns a temporal object of the same observable type as the input |
|
655 * with the offset changed to be the same as this. |
|
656 * <p> |
|
657 * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} |
|
658 * passing {@link ChronoField#OFFSET_SECONDS} as the field. |
|
659 * <p> |
|
660 * In most cases, it is clearer to reverse the calling pattern by using |
|
661 * {@link Temporal#with(TemporalAdjuster)}: |
|
662 * <pre> |
|
663 * // these two lines are equivalent, but the second approach is recommended |
|
664 * temporal = thisOffset.adjustInto(temporal); |
|
665 * temporal = temporal.with(thisOffset); |
|
666 * </pre> |
|
667 * <p> |
|
668 * This instance is immutable and unaffected by this method call. |
|
669 * |
|
670 * @param temporal the target object to be adjusted, not null |
|
671 * @return the adjusted object, not null |
|
672 * @throws DateTimeException if unable to make the adjustment |
|
673 * @throws ArithmeticException if numeric overflow occurs |
|
674 */ |
|
675 @Override |
|
676 public Temporal adjustInto(Temporal temporal) { |
|
677 return temporal.with(OFFSET_SECONDS, totalSeconds); |
|
678 } |
|
679 |
|
680 //----------------------------------------------------------------------- |
|
681 /** |
|
682 * Compares this offset to another offset in descending order. |
|
683 * <p> |
|
684 * The offsets are compared in the order that they occur for the same time |
|
685 * of day around the world. Thus, an offset of {@code +10:00} comes before an |
|
686 * offset of {@code +09:00} and so on down to {@code -18:00}. |
|
687 * <p> |
|
688 * The comparison is "consistent with equals", as defined by {@link Comparable}. |
|
689 * |
|
690 * @param other the other date to compare to, not null |
|
691 * @return the comparator value, negative if less, postive if greater |
|
692 * @throws NullPointerException if {@code other} is null |
|
693 */ |
|
694 @Override |
|
695 public int compareTo(ZoneOffset other) { |
|
696 return other.totalSeconds - totalSeconds; |
|
697 } |
|
698 |
|
699 //----------------------------------------------------------------------- |
|
700 /** |
|
701 * Checks if this offset is equal to another offset. |
|
702 * <p> |
|
703 * The comparison is based on the amount of the offset in seconds. |
|
704 * This is equivalent to a comparison by ID. |
|
705 * |
|
706 * @param obj the object to check, null returns false |
|
707 * @return true if this is equal to the other offset |
|
708 */ |
|
709 @Override |
|
710 public boolean equals(Object obj) { |
|
711 if (this == obj) { |
|
712 return true; |
|
713 } |
|
714 if (obj instanceof ZoneOffset) { |
|
715 return totalSeconds == ((ZoneOffset) obj).totalSeconds; |
|
716 } |
|
717 return false; |
|
718 } |
|
719 |
|
720 /** |
|
721 * A hash code for this offset. |
|
722 * |
|
723 * @return a suitable hash code |
|
724 */ |
|
725 @Override |
|
726 public int hashCode() { |
|
727 return totalSeconds; |
|
728 } |
|
729 |
|
730 //----------------------------------------------------------------------- |
|
731 /** |
|
732 * Outputs this offset as a {@code String}, using the normalized ID. |
|
733 * |
|
734 * @return a string representation of this offset, not null |
|
735 */ |
|
736 @Override |
|
737 public String toString() { |
|
738 return id; |
|
739 } |
|
740 |
|
741 // ----------------------------------------------------------------------- |
|
742 /** |
|
743 * Writes the object using a |
|
744 * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. |
|
745 * <pre> |
|
746 * out.writeByte(8); // identifies this as a ZoneOffset |
|
747 * int offsetByte = totalSeconds % 900 == 0 ? totalSeconds / 900 : 127; |
|
748 * out.writeByte(offsetByte); |
|
749 * if (offsetByte == 127) { |
|
750 * out.writeInt(totalSeconds); |
|
751 * } |
|
752 * </pre> |
|
753 * |
|
754 * @return the instance of {@code Ser}, not null |
|
755 */ |
|
756 private Object writeReplace() { |
|
757 return new Ser(Ser.ZONE_OFFSET_TYPE, this); |
|
758 } |
|
759 |
|
760 /** |
|
761 * Defend against malicious streams. |
|
762 * @return never |
|
763 * @throws InvalidObjectException always |
|
764 */ |
|
765 private Object readResolve() throws ObjectStreamException { |
|
766 throw new InvalidObjectException("Deserialization via serialization delegate"); |
|
767 } |
|
768 |
|
769 @Override |
|
770 void write(DataOutput out) throws IOException { |
|
771 out.writeByte(Ser.ZONE_OFFSET_TYPE); |
|
772 writeExternal(out); |
|
773 } |
|
774 |
|
775 void writeExternal(DataOutput out) throws IOException { |
|
776 final int offsetSecs = totalSeconds; |
|
777 int offsetByte = offsetSecs % 900 == 0 ? offsetSecs / 900 : 127; // compress to -72 to +72 |
|
778 out.writeByte(offsetByte); |
|
779 if (offsetByte == 127) { |
|
780 out.writeInt(offsetSecs); |
|
781 } |
|
782 } |
|
783 |
|
784 static ZoneOffset readExternal(DataInput in) throws IOException { |
|
785 int offsetByte = in.readByte(); |
|
786 return (offsetByte == 127 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(offsetByte * 900)); |
|
787 } |
|
788 |
|
789 } |