-
Notifications
You must be signed in to change notification settings - Fork 668
/
ZoneId.java
761 lines (692 loc) · 33 KB
/
ZoneId.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.time.zone.ZoneRules;
import java.time.zone.ZoneRulesException;
import java.time.zone.ZoneRulesProvider;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import static java.util.Map.entry;
/**
* A time-zone ID, such as {@code Europe/Paris}.
* <p>
* A {@code ZoneId} is used to identify the rules used to convert between
* an {@link Instant} and a {@link LocalDateTime}.
* There are two distinct types of ID:
* <ul>
* <li>Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses
* the same offset for all local date-times
* <li>Geographical regions - an area where a specific set of rules for finding
* the offset from UTC/Greenwich apply
* </ul>
* Most fixed offsets are represented by {@link ZoneOffset}.
* Calling {@link #normalized()} on any {@code ZoneId} will ensure that a
* fixed offset ID will be represented as a {@code ZoneOffset}.
* <p>
* The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.
* This class is simply an ID used to obtain the underlying rules.
* This approach is taken because rules are defined by governments and change
* frequently, whereas the ID is stable.
* <p>
* The distinction has other effects. Serializing the {@code ZoneId} will only send
* the ID, whereas serializing the rules sends the entire data set.
* Similarly, a comparison of two IDs only examines the ID, whereas
* a comparison of two rules examines the entire data set.
*
* <h3>Time-zone IDs</h3>
* The ID is unique within the system.
* There are three types of ID.
* <p>
* The simplest type of ID is that from {@code ZoneOffset}.
* This consists of 'Z' and IDs starting with '+' or '-'.
* <p>
* The next type of ID are offset-style IDs with some form of prefix,
* such as 'GMT+2' or 'UTC+01:00'.
* The recognised prefixes are 'UTC', 'GMT' and 'UT'.
* The offset is the suffix and will be normalized during creation.
* These IDs can be normalized to a {@code ZoneOffset} using {@code normalized()}.
* <p>
* The third type of ID are region-based IDs. A region-based ID must be of
* two or more characters, and not start with 'UTC', 'GMT', 'UT' '+' or '-'.
* Region-based IDs are defined by configuration, see {@link ZoneRulesProvider}.
* The configuration focuses on providing the lookup from the ID to the
* underlying {@code ZoneRules}.
* <p>
* Time-zone rules are defined by governments and change frequently.
* There are a number of organizations, known here as groups, that monitor
* time-zone changes and collate them.
* The default group is the IANA Time Zone Database (TZDB).
* Other organizations include IATA (the airline industry body) and Microsoft.
* <p>
* Each group defines its own format for the region ID it provides.
* The TZDB group defines IDs such as 'Europe/London' or 'America/New_York'.
* TZDB IDs take precedence over other groups.
* <p>
* It is strongly recommended that the group name is included in all IDs supplied by
* groups other than TZDB to avoid conflicts. For example, IATA airline time-zone
* region IDs are typically the same as the three letter airport code.
* However, the airport of Utrecht has the code 'UTC', which is obviously a conflict.
* The recommended format for region IDs from groups other than TZDB is 'group~region'.
* Thus if IATA data were defined, Utrecht airport would be 'IATA~UTC'.
*
* <h3>Serialization</h3>
* This class can be serialized and stores the string zone ID in the external form.
* The {@code ZoneOffset} subclass uses a dedicated format that only stores the
* offset from UTC/Greenwich.
* <p>
* A {@code ZoneId} can be deserialized in a Java Runtime where the ID is unknown.
* For example, if a server-side Java Runtime has been updated with a new zone ID, but
* the client-side Java Runtime has not been updated. In this case, the {@code ZoneId}
* object will exist, and can be queried using {@code getId}, {@code equals},
* {@code hashCode}, {@code toString}, {@code getDisplayName} and {@code normalized}.
* However, any call to {@code getRules} will fail with {@code ZoneRulesException}.
* This approach is designed to allow a {@link ZonedDateTime} to be loaded and
* queried, but not modified, on a Java Runtime with incomplete time-zone information.
*
* <p>
* This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
* ({@code ==}), identity hash code, or synchronization) on instances of
* {@code ZoneId} may have unpredictable results and should be avoided.
* The {@code equals} method should be used for comparisons.
*
* @implSpec
* This abstract class has two implementations, both of which are immutable and thread-safe.
* One implementation models region-based IDs, the other is {@code ZoneOffset} modelling
* offset-based IDs. This difference is visible in serialization.
*
* @since 1.8
*/
// 时区ID,包括基于时间偏移的时区ID[ZoneOffset]和基于地理时区的时区ID[ZoneRegion]
public abstract class ZoneId implements Serializable {
/**
* A map of zone overrides to enable the short time-zone names to be used.
* <p>
* Use of short zone IDs has been deprecated in {@code java.util.TimeZone}.
* This map allows the IDs to continue to be used via the
* {@link #of(String, Map)} factory method.
* <p>
* This map contains a mapping of the IDs that is in line with TZDB 2005r and
* later, where 'EST', 'MST' and 'HST' map to IDs which do not include daylight
* savings.
* <p>
* This maps as follows:
* <ul>
* <li>EST - -05:00</li>
* <li>HST - -10:00</li>
* <li>MST - -07:00</li>
* <li>ACT - Australia/Darwin</li>
* <li>AET - Australia/Sydney</li>
* <li>AGT - America/Argentina/Buenos_Aires</li>
* <li>ART - Africa/Cairo</li>
* <li>AST - America/Anchorage</li>
* <li>BET - America/Sao_Paulo</li>
* <li>BST - Asia/Dhaka</li>
* <li>CAT - Africa/Harare</li>
* <li>CNT - America/St_Johns</li>
* <li>CST - America/Chicago</li>
* <li>CTT - Asia/Shanghai</li>
* <li>EAT - Africa/Addis_Ababa</li>
* <li>ECT - Europe/Paris</li>
* <li>IET - America/Indiana/Indianapolis</li>
* <li>IST - Asia/Kolkata</li>
* <li>JST - Asia/Tokyo</li>
* <li>MIT - Pacific/Apia</li>
* <li>NET - Asia/Yerevan</li>
* <li>NST - Pacific/Auckland</li>
* <li>PLT - Asia/Karachi</li>
* <li>PNT - America/Phoenix</li>
* <li>PRT - America/Puerto_Rico</li>
* <li>PST - America/Los_Angeles</li>
* <li>SST - Pacific/Guadalcanal</li>
* <li>VST - Asia/Ho_Chi_Minh</li>
* </ul>
* The map is unmodifiable.
*/
// 兼容旧的时区ID映射
public static final Map<String, String> SHORT_IDS = Map.ofEntries(
entry("ACT", "Australia/Darwin"),
entry("AET", "Australia/Sydney"),
entry("AGT", "America/Argentina/Buenos_Aires"),
entry("ART", "Africa/Cairo"),
entry("AST", "America/Anchorage"),
entry("BET", "America/Sao_Paulo"),
entry("BST", "Asia/Dhaka"),
entry("CAT", "Africa/Harare"),
entry("CNT", "America/St_Johns"),
entry("CST", "America/Chicago"),
entry("CTT", "Asia/Shanghai"),
entry("EAT", "Africa/Addis_Ababa"),
entry("ECT", "Europe/Paris"),
entry("IET", "America/Indiana/Indianapolis"),
entry("IST", "Asia/Kolkata"),
entry("JST", "Asia/Tokyo"),
entry("MIT", "Pacific/Apia"),
entry("NET", "Asia/Yerevan"),
entry("NST", "Pacific/Auckland"), entry("PLT", "Asia/Karachi"), entry("PNT", "America/Phoenix"), entry("PRT", "America/Puerto_Rico"), entry("PST", "America/Los_Angeles"), entry("SST", "Pacific/Guadalcanal"), entry("VST", "Asia/Ho_Chi_Minh"), entry("EST", "-05:00"), entry("MST", "-07:00"), entry("HST", "-10:00"));
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Constructor only accessible within the package.
*/
ZoneId() {
if(getClass() != ZoneOffset.class && getClass() != ZoneRegion.class) {
throw new AssertionError("Invalid subclass");
}
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 视图 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Gets the system default time-zone.
* <p>
* This queries {@link TimeZone#getDefault()} to find the default time-zone
* and converts it to a {@code ZoneId}. If the system default time-zone is changed,
* then the result of this method will also change.
*
* @return the zone ID, not null
*
* @throws DateTimeException if the converted zone ID has an invalid format
* @throws ZoneRulesException if the converted zone region ID cannot be found
*/
/*
* 返回系统默认的时区ID
*
* 注:获取到的时区ID可能是ZoneOffset(基于时间偏移的时区ID),也可能是ZoneRegion(基于地理时区的时区ID)
*/
public static ZoneId systemDefault() {
return TimeZone.getDefault().toZoneId();
}
/**
* Gets the set of available zone IDs.
* <p>
* This set includes the string form of all available region-based IDs.
* Offset-based zone IDs are not included in the returned set.
* The ID can be passed to {@link #of(String)} to create a {@code ZoneId}.
* <p>
* The set of zone IDs can increase over time, although in a typical application
* the set of IDs is fixed. Each call to this method is thread-safe.
*
* @return a modifiable copy of the set of zone IDs, not null
*/
// 返回当前所有可用的[基于地理时区的时区ID]字符串
public static Set<String> getAvailableZoneIds() {
return new HashSet<String>(ZoneRulesProvider.getAvailableZoneIds());
}
/*▲ 视图 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Obtains an instance of {@code ZoneId} from an ID ensuring that the
* ID is valid and available for use.
* <p>
* This method parses the ID producing a {@code ZoneId} or {@code ZoneOffset}.
* A {@code ZoneOffset} is returned if the ID is 'Z', or starts with '+' or '-'.
* The result will always be a valid ID for which {@link ZoneRules} can be obtained.
* <p>
* Parsing matches the zone ID step by step as follows.
* <ul>
* <li>If the zone ID equals 'Z', the result is {@code ZoneOffset.UTC}.
* <li>If the zone ID consists of a single letter, the zone ID is invalid
* and {@code DateTimeException} is thrown.
* <li>If the zone ID starts with '+' or '-', the ID is parsed as a
* {@code ZoneOffset} using {@link ZoneOffset#of(String)}.
* <li>If the zone ID equals 'GMT', 'UTC' or 'UT' then the result is a {@code ZoneId}
* with the same ID and rules equivalent to {@code ZoneOffset.UTC}.
* <li>If the zone ID starts with 'UTC+', 'UTC-', 'GMT+', 'GMT-', 'UT+' or 'UT-'
* then the ID is a prefixed offset-based ID. The ID is split in two, with
* a two or three letter prefix and a suffix starting with the sign.
* The suffix is parsed as a {@link ZoneOffset#of(String) ZoneOffset}.
* The result will be a {@code ZoneId} with the specified UTC/GMT/UT prefix
* and the normalized offset ID as per {@link ZoneOffset#getId()}.
* The rules of the returned {@code ZoneId} will be equivalent to the
* parsed {@code ZoneOffset}.
* <li>All other IDs are parsed as region-based zone IDs. Region IDs must
* match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>
* otherwise a {@code DateTimeException} is thrown. If the zone ID is not
* in the configured set of IDs, {@code ZoneRulesException} is thrown.
* The detailed format of the region ID depends on the group supplying the data.
* The default set of data is supplied by the IANA Time Zone Database (TZDB).
* This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
* This is compatible with most IDs from {@link java.util.TimeZone}.
* </ul>
*
* @param zoneId the time-zone ID, not null
* @return the zone ID, not null
* @throws DateTimeException if the zone ID has an invalid format
* @throws ZoneRulesException if the zone ID is a region ID that cannot be found
*/
/*
* 根据给定的字符串构造一个时区ID。
* 返回的时区ID可能是[基于时间偏移的时区ID],也可能是[基于地理时区的时区ID]。
*
* 例如:
* "+8" ==> 返回ZoneOffset对象,即[基于时间偏移的时区ID]
* "Asia/Shanghai" ==> 返回ZoneRegion对象,即[基于地理时区的时区ID]
*
* zoneId允许的名称包括:
* Z(表示在UTC时区)
* +h
* +hh
* +hh:mm
* -hh:mm
* +hhmm
* -hhmm
* +hh:mm:ss
* -hh:mm:ss
* +hhmmss
* -hhmmss
* GMT
* UTC
* UT
* GMT+8
* UTC-8
* Etc/GMT-8
* [地理时区名称]
*/
public static ZoneId of(String zoneId) {
return of(zoneId, true);
}
/**
* Obtains an instance of {@code ZoneId} using its ID using a map
* of aliases to supplement the standard zone IDs.
* <p>
* Many users of time-zones use short abbreviations, such as PST for
* 'Pacific Standard Time' and PDT for 'Pacific Daylight Time'.
* These abbreviations are not unique, and so cannot be used as IDs.
* This method allows a map of string to time-zone to be setup and reused
* within an application.
*
* @param zoneId the time-zone ID, not null
* @param aliasMap a map of alias zone IDs (typically abbreviations) to real zone IDs, not null
*
* @return the zone ID, not null
*
* @throws DateTimeException if the zone ID has an invalid format
* @throws ZoneRulesException if the zone ID is a region ID that cannot be found
*/
// 根据给定的时区ID别名构造ZoneId对象,aliasMap存储了别名到规范名称的映射
public static ZoneId of(String aliasZoneId, Map<String, String> aliasMap) {
Objects.requireNonNull(aliasZoneId, "zoneId");
Objects.requireNonNull(aliasMap, "aliasMap");
String id = Objects.requireNonNullElse(aliasMap.get(aliasZoneId), aliasZoneId);
return of(id);
}
/**
* Obtains an instance of {@code ZoneId} wrapping an offset.
* <p>
* If the prefix is "GMT", "UTC", or "UT" a {@code ZoneId}
* with the prefix and the non-zero offset is returned.
* If the prefix is empty {@code ""} the {@code ZoneOffset} is returned.
*
* @param prefix the time-zone ID, not null
* @param offset the offset, not null
* @return the zone ID, not null
* @throws IllegalArgumentException if the prefix is not one of
* "GMT", "UTC", or "UT", or ""
*/
// 处理以"UTC"/"GMT"/"UT"开头的时区ID名称,然后将其结合时区偏移量offset,来构造ZoneId对象
public static ZoneId ofOffset(String prefix, ZoneOffset offset) {
Objects.requireNonNull(prefix, "prefix");
Objects.requireNonNull(offset, "offset");
if (prefix.length() == 0) {
return offset;
}
if(!prefix.equals("GMT") && !prefix.equals("UTC") && !prefix.equals("UT")) {
throw new IllegalArgumentException("prefix should be GMT, UTC or UT, is: " + prefix);
}
if(offset.getTotalSeconds() != 0) {
prefix = prefix.concat(offset.getId());
}
// 获取与offset对应的"时区规则集"
ZoneRules rules = offset.getRules();
return new ZoneRegion(prefix, rules);
}
/**
* Obtains an instance of {@code ZoneId} from a temporal object.
* <p>
* This obtains a zone based on the specified temporal.
* A {@code TemporalAccessor} represents an arbitrary set of date and time information,
* which this factory converts to an instance of {@code ZoneId}.
* <p>
* A {@code TemporalAccessor} represents some form of date and time information.
* This factory converts the arbitrary temporal object to an instance of {@code ZoneId}.
* <p>
* The conversion will try to obtain the zone in a way that favours region-based
* zones over offset-based zones using {@link TemporalQueries#zone()}.
* <p>
* This method matches the signature of the functional interface {@link TemporalQuery}
* allowing it to be used as a query via method reference, {@code ZoneId::from}.
*
* @param temporal the temporal object to convert, not null
* @return the zone ID, not null
* @throws DateTimeException if unable to convert to a {@code ZoneId}
*/
// 从temporal中查询ZoneId部件的信息(宽松模式,如果在时间量中无法直接查找到ZoneId属性,则回退为查找ZoneOffset属性)
public static ZoneId from(TemporalAccessor temporal) {
ZoneId obj = temporal.query(TemporalQueries.zone());
if(obj == null) {
throw new DateTimeException("Unable to obtain ZoneId from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName());
}
return obj;
}
/*▲ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 杂项 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Gets the time-zone rules for this ID allowing calculations to be performed.
* <p>
* The rules provide the functionality associated with a time-zone,
* such as finding the offset for a given instant or local date-time.
* <p>
* A time-zone can be invalid if it is deserialized in a Java Runtime which
* does not have the same rules loaded as the Java Runtime that stored it.
* In this case, calling this method will throw a {@code ZoneRulesException}.
* <p>
* The rules are supplied by {@link ZoneRulesProvider}. An advanced provider may
* support dynamic updates to the rules without restarting the Java Runtime.
* If so, then the result of this method may change over time.
* Each individual call will be still remain thread-safe.
* <p>
* {@link ZoneOffset} will always return a set of rules where the offset never changes.
*
* @return the rules, not null
*
* @throws ZoneRulesException if no rules are available for this ID
*/
// 返回与当前时区ID对应的"时区规则集"
public abstract ZoneRules getRules();
/**
* Gets the unique time-zone ID.
* <p>
* This ID uniquely defines this object.
* The format of an offset based ID is defined by {@link ZoneOffset#getId()}.
*
* @return the time-zone unique ID, not null
*/
// 返回当前时区ID的字符串表示,该字符串是唯一的
public abstract String getId();
/**
* Gets the textual representation of the zone, such as 'British Time' or
* '+02:00'.
* <p>
* This returns the textual name used to identify the time-zone ID,
* suitable for presentation to the user.
* The parameters control the style of the returned text and the locale.
* <p>
* If no textual mapping is found then the {@link #getId() full ID} is returned.
*
* @param style the length of the text required, not null
* @param locale the locale to use, not null
* @return the text value of the zone, not null
*/
// 返回当前时区ID的本地字符串表示
public String getDisplayName(TextStyle style, Locale locale) {
return new DateTimeFormatterBuilder().appendZoneText(style).toFormatter(locale).format(toTemporal());
}
/**
* Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.
* <p>
* The returns a normalized {@code ZoneId} that can be used in place of this ID.
* The result will have {@code ZoneRules} equivalent to those returned by this object,
* however the ID returned by {@code getId()} may be different.
* <p>
* The normalization checks if the rules of this {@code ZoneId} have a fixed offset.
* If they do, then the {@code ZoneOffset} equal to that offset is returned.
* Otherwise {@code this} is returned.
*
* @return the time-zone unique ID, not null
*/
// 获取标准化时区ID
public ZoneId normalized() {
try {
// 返回与当前时区ID对应的"时区规则集"
ZoneRules rules = getRules();
// 如果当前时区的"实际偏移"规则是固定的
if(rules.isFixedOffset()) {
/*
* 获取zone时区在Instant.EPOCH时刻的"实际偏移"。
* 这里可以返回一个准确的"实际偏移"。
*/
return rules.getOffset(Instant.EPOCH);
}
} catch(ZoneRulesException ex) {
// invalid ZoneRegion is not important to this method
}
return this;
}
/*▲ 杂项 ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* Parses the ID, taking a flag to indicate whether {@code ZoneRulesException}
* should be thrown or not, used in deserialization.
*
* @param zoneId the time-zone ID, not null
* @param checkAvailable whether to check if the zone ID is available
*
* @return the zone ID, not null
*
* @throws DateTimeException if the ID format is invalid
* @throws ZoneRulesException if checking availability and the ID cannot be found
*/
// 根据给定的字符串构造一个时区ID,checkAvailable用来指示当无法找到zoneId对应的时区规则集时是否抛异常
static ZoneId of(String zoneId, boolean checkAvailable) {
Objects.requireNonNull(zoneId, "zoneId");
if(zoneId.length()<=1 || zoneId.startsWith("+") || zoneId.startsWith("-")) {
// 根据给定的时间偏移构造基于时间偏移的时区ID
return ZoneOffset.of(zoneId);
}
if(zoneId.startsWith("UTC") || zoneId.startsWith("GMT")) {
return ofWithPrefix(zoneId, 3, checkAvailable);
}
if(zoneId.startsWith("UT")) {
return ofWithPrefix(zoneId, 2, checkAvailable);
}
return ZoneRegion.ofId(zoneId, checkAvailable);
}
/**
* Parse once a prefix is established.
*
* @param zoneId the time-zone ID, not null
* @param prefixLength the length of the prefix, 2 or 3
*
* @return the zone ID, not null
*
* @throws DateTimeException if the zone ID has an invalid format
*/
// 处理带有"UTC"/"GMT"/"UT"前缀的时区ID名称,会尝试从zoneId中解析出时区偏移,并进一步构造ZoneId对象
private static ZoneId ofWithPrefix(String zoneId, int prefixLength, boolean checkAvailable) {
String prefix = zoneId.substring(0, prefixLength);
if(zoneId.length() == prefixLength) {
return ofOffset(prefix, ZoneOffset.UTC);
}
if(zoneId.charAt(prefixLength) != '+' && zoneId.charAt(prefixLength) != '-') {
return ZoneRegion.ofId(zoneId, checkAvailable); // drop through to ZoneRulesProvider
}
try {
// 根据给定的时间偏移构造基于时间偏移的时区ID
ZoneOffset offset = ZoneOffset.of(zoneId.substring(prefixLength));
return ofOffset(prefix, offset);
} catch(DateTimeException ex) {
throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId, ex);
}
}
/**
* Converts this zone to a {@code TemporalAccessor}.
* <p>
* A {@code ZoneId} can be fully represented as a {@code TemporalAccessor}.
* However, the interface is not implemented by this class as most of the
* methods on the interface have no meaning to {@code ZoneId}.
* <p>
* The returned temporal has no supported fields, with the query method
* supporting the return of the zone using {@link TemporalQueries#zoneId()}.
*
* @return a temporal equivalent to this zone, not null
*/
// 构造一个时间量访问器,以允许访问当前时区ID对象
private TemporalAccessor toTemporal() {
return new TemporalAccessor() {
// 判断当前时间量是否支持指定的时间量字段
@Override
public boolean isSupported(TemporalField field) {
return false;
}
// 以long形式返回时间量字段field的值
@Override
public long getLong(TemporalField field) {
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
// 使用指定的时间量查询器,从当前时间量中查询目标信息
@SuppressWarnings("unchecked")
@Override
public <R> R query(TemporalQuery<R> query) {
if(query == TemporalQueries.zoneId()) {
return (R) ZoneId.this;
}
return TemporalAccessor.super.query(query);
}
};
}
/**
* Outputs this zone as a {@code String}, using the ID.
*
* @return a string representation of this time-zone ID, not null
*/
@Override
public String toString() {
return getId();
}
/**
* Checks if this time-zone ID is equal to another time-zone ID.
* <p>
* The comparison is based on the ID.
*
* @param obj the object to check, null returns false
* @return true if this is equal to the other time-zone ID
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ZoneId) {
ZoneId other = (ZoneId) obj;
return getId().equals(other.getId());
}
return false;
}
/**
* A hash code for this time-zone ID.
*
* @return a suitable hash code
*/
@Override
public int hashCode() {
return getId().hashCode();
}
/*▼ 序列化 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Serialization version.
*/
private static final long serialVersionUID = 8352817235686L;
abstract void write(DataOutput out) throws IOException;
/**
* Writes the object using a
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
*
* @return the instance of {@code Ser}, not null
*
* @serialData <pre>
* out.writeByte(7); // identifies a ZoneId (not ZoneOffset)
* out.writeUTF(getId());
* </pre>
* <p>
* When read back in, the {@code ZoneId} will be created as though using
* {@link #of(String)}, but without any exception in the case where the
* ID has a valid format, but is not in the known set of region-based IDs.
*/
// this is here for serialization Javadoc
private Object writeReplace() {
return new Ser(Ser.ZONE_REGION_TYPE, this);
}
/**
* Defend against malicious streams.
*
* @param s the stream to read
*
* @throws InvalidObjectException always
*/
private void readObject(ObjectInputStream s) throws InvalidObjectException {
throw new InvalidObjectException("Deserialization via serialization delegate");
}
/*▲ 序列化 ████████████████████████████████████████████████████████████████████████████████┛ */
}