-
Notifications
You must be signed in to change notification settings - Fork 668
/
ZoneRegion.java
287 lines (253 loc) · 11.2 KB
/
ZoneRegion.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
/*
* Copyright (c) 2012, 2013, 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.
*/
/*
* 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.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.time.zone.ZoneRules;
import java.time.zone.ZoneRulesException;
import java.time.zone.ZoneRulesProvider;
import java.util.Objects;
/**
* A geographical region where the same time-zone rules apply.
* <p>
* Time-zone information is categorized as a set of rules defining when and
* how the offset from UTC/Greenwich changes. These rules are accessed using
* identifiers based on geographical regions, such as countries or states.
* The most common region classification is the Time Zone Database (TZDB),
* which defines regions such as 'Europe/Paris' and 'Asia/Tokyo'.
* <p>
* The region identifier, modeled by this class, is distinct from the
* underlying rules, modeled by {@link ZoneRules}.
* The rules are defined by governments and change frequently.
* By contrast, the region identifier is well-defined and long-lived.
* This separation also allows rules to be shared between regions if appropriate.
*
* @implSpec
* This class is immutable and thread-safe.
*
* @since 1.8
*/
// 基于地理时区的时区ID,可以处理夏令时
final class ZoneRegion extends ZoneId implements Serializable {
/**
* The time-zone ID, not null.
*/
/*
* 时区ID的字符串表示
*
* 此处的ID是基于地理时区的,比如:
* Asia/Shanghai(并不等同于GMT/UTC+8时区)
* America/Los_Angeles(有夏令时的时区)
* GMT0
* GMT
* UTC
* Etc/UTC
* Etc/GMT0
* Etc/GMT
* Etc/GMT+0
* Etc/GMT-0
* Etc/GMT-8(相当于GMT/UTC+8)
* Etc/GMT+8(相当于GMT/UTC-8)
*
* 参见:ZoneId#getAvailableZoneIds()
*/
private final String id;
/**
* The time-zone rules, null if zone ID was loaded leniently.
*/
// 时区规则集
private final transient ZoneRules rules;
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Constructor.
*
* @param id the time-zone ID, not null
* @param rules the rules, null for lazy lookup
*/
ZoneRegion(String id, ZoneRules rules) {
this.id = id;
this.rules = rules;
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Obtains an instance of {@code ZoneId} from an identifier.
*
* @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来构造ZoneRegion对象。
*
* checkAvailable: 当无法找到zoneId对应的时区规则集时是否抛异常
*/
static ZoneRegion ofId(String zoneId, boolean checkAvailable) {
Objects.requireNonNull(zoneId, "zoneId");
checkName(zoneId);
ZoneRules rules = null;
try {
/* always attempt load for better behavior after deserialization */
// 加载时区规则集
rules = ZoneRulesProvider.getRules(zoneId, true);
} catch(ZoneRulesException ex) {
if(checkAvailable) {
throw ex;
}
}
return new ZoneRegion(zoneId, rules);
}
/*▲ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 部件 ████████████████████████████████████████████████████████████████████████████████┓ */
// 返回时区ID的字符串表示
@Override
public String getId() {
return id;
}
// 返回与当前时区ID对应的"时区规则集"
@Override
public ZoneRules getRules() {
/*
* additional query for group provider when null allows for possibility
* that the provider was updated after the ZoneId was created
*/
return (rules != null ? rules : ZoneRulesProvider.getRules(id, false));
}
/*▲ 部件 ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* Checks that the given string is a legal ZondId name.
*
* @param zoneId the time-zone ID, not null
*
* @throws DateTimeException if the ID format is invalid
*/
private static void checkName(String zoneId) {
int n = zoneId.length();
if(n<2) {
throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
}
for (int i = 0; i < n; i++) {
char c = zoneId.charAt(i);
if (c >= 'a' && c <= 'z') continue;
if (c >= 'A' && c<='Z')
continue;
if(c == '/' && i != 0)
continue;
if(c >= '0' && c<='9' && i != 0)
continue;
if(c == '~' && i != 0)
continue;
if(c == '.' && i != 0)
continue;
if(c == '_' && i != 0)
continue;
if(c == '+' && i != 0)
continue;
if(c == '-' && i != 0)
continue;
throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
}
}
/*▼ 序列化 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Serialization version.
*/
private static final long serialVersionUID = 8386373296231747096L;
/**
* Writes the object using a
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
* @serialData
* <pre>
* out.writeByte(7); // identifies a ZoneId (not ZoneOffset)
* out.writeUTF(zoneId);
* </pre>
*
* @return the instance of {@code Ser}, not null
*/
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");
}
@Override
void write(DataOutput out) throws IOException {
out.writeByte(Ser.ZONE_REGION_TYPE);
writeExternal(out);
}
void writeExternal(DataOutput out) throws IOException {
out.writeUTF(id);
}
static ZoneId readExternal(DataInput in) throws IOException {
String id = in.readUTF();
return ZoneId.of(id, false);
}
/*▲ 序列化 ████████████████████████████████████████████████████████████████████████████████┛ */
}