author | sherman |
Wed, 13 Feb 2013 11:49:34 -0800 | |
changeset 15665 | 2223468cd706 |
parent 15658 | 55b829ca2334 |
child 16852 | 60207b2b4b42 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15658 | 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.util.calendar; |
|
27 |
||
15658 | 28 |
import java.io.ByteArrayInputStream; |
29 |
import java.io.DataInput; |
|
30 |
import java.io.DataInputStream; |
|
31 |
import java.io.File; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.StreamCorruptedException; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
import java.time.DayOfWeek; |
|
37 |
import java.time.LocalDateTime; |
|
38 |
import java.time.LocalTime; |
|
39 |
import java.time.Month; |
|
40 |
import java.time.OffsetDateTime; |
|
41 |
import java.time.ZoneOffset; |
|
42 |
import java.time.zone.ZoneRules; |
|
43 |
import java.time.zone.ZoneOffsetTransition; |
|
44 |
import java.time.zone.ZoneOffsetTransitionRule; |
|
45 |
import java.time.zone.ZoneOffsetTransitionRule.TimeDefinition; |
|
46 |
||
47 |
import java.util.ArrayList; |
|
48 |
import java.util.Arrays; |
|
49 |
import java.util.Calendar; |
|
50 |
import java.util.Collections; |
|
51 |
import java.util.HashMap; |
|
52 |
import java.util.List; |
|
53 |
import java.util.Locale; |
|
54 |
import java.util.Map; |
|
55 |
import java.util.Map.Entry; |
|
56 |
import java.util.Objects; |
|
57 |
import java.util.Set; |
|
58 |
import java.util.SimpleTimeZone; |
|
59 |
import java.util.concurrent.ConcurrentHashMap; |
|
60 |
import java.util.zip.CRC32; |
|
61 |
import java.util.zip.ZipFile; |
|
2 | 62 |
|
63 |
/** |
|
15658 | 64 |
* Loads TZDB time-zone rules for j.u.TimeZone |
2 | 65 |
* <p> |
15658 | 66 |
* @since 1.8 |
2 | 67 |
*/ |
15658 | 68 |
public final class ZoneInfoFile { |
2 | 69 |
|
15658 | 70 |
/** |
71 |
* Gets all available IDs supported in the Java run-time. |
|
72 |
* |
|
73 |
* @return a set of time zone IDs. |
|
74 |
*/ |
|
75 |
public static Set<String> getZoneIds() { |
|
76 |
return zones.keySet(); |
|
77 |
} |
|
2 | 78 |
|
79 |
/** |
|
15658 | 80 |
* Gets all available IDs that have the same value as the |
81 |
* specified raw GMT offset. |
|
82 |
* |
|
83 |
* @param rawOffset the GMT offset in milliseconds. This |
|
84 |
* value should not include any daylight saving time. |
|
85 |
* @return an array of time zone IDs. |
|
2 | 86 |
*/ |
15658 | 87 |
public static String[] getZoneIds(int rawOffset) { |
88 |
List<String> ids = new ArrayList<>(); |
|
89 |
for (String id : zones.keySet()) { |
|
90 |
ZoneInfo zi = getZoneInfo0(id); |
|
91 |
if (zi.getRawOffset() == rawOffset) { |
|
92 |
ids.add(id); |
|
93 |
} |
|
94 |
} |
|
15665
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
95 |
// It appears the "zi" implementation returns the |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
96 |
// sorted list, though the specification does not |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
97 |
// specify it. Keep the same behavior for better |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
98 |
// compatibility. |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
99 |
String[] list = ids.toArray(new String[ids.size()]); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
100 |
Arrays.sort(list); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
101 |
return list; |
15658 | 102 |
} |
2 | 103 |
|
15658 | 104 |
public static ZoneInfo getZoneInfo(String zoneId) { |
105 |
if (!zones.containsKey(zoneId)) { |
|
106 |
return null; |
|
107 |
} |
|
108 |
// ZoneInfo is mutable, return the copy |
|
2 | 109 |
|
15658 | 110 |
ZoneInfo zi = getZoneInfo0(zoneId); |
111 |
zi = (ZoneInfo)zi.clone(); |
|
112 |
zi.setID(zoneId); |
|
113 |
return zi; |
|
114 |
} |
|
2 | 115 |
|
116 |
/** |
|
15658 | 117 |
* Returns a Map from alias time zone IDs to their standard |
118 |
* time zone IDs. |
|
119 |
* |
|
120 |
* @return an unmodified alias mapping |
|
2 | 121 |
*/ |
15658 | 122 |
public static Map<String, String> getAliasMap() { |
123 |
return aliases; |
|
124 |
} |
|
2 | 125 |
|
126 |
/** |
|
15658 | 127 |
* Gets the version of this tz data. |
128 |
* |
|
129 |
* @return the tzdb version |
|
2 | 130 |
*/ |
15658 | 131 |
public static String getVersion() { |
132 |
return versionId; |
|
2 | 133 |
} |
134 |
||
135 |
/** |
|
136 |
* Gets a ZoneInfo with the given GMT offset. The object |
|
137 |
* has its ID in the format of GMT{+|-}hh:mm. |
|
138 |
* |
|
15658 | 139 |
* @param originalId the given custom id (before normalized such as "GMT+9") |
140 |
* @param gmtOffset GMT offset <em>in milliseconds</em> |
|
2 | 141 |
* @return a ZoneInfo constructed with the given GMT offset |
142 |
*/ |
|
143 |
public static ZoneInfo getCustomTimeZone(String originalId, int gmtOffset) { |
|
144 |
String id = toCustomID(gmtOffset); |
|
15658 | 145 |
return new ZoneInfo(id, gmtOffset); |
146 |
/* |
|
2 | 147 |
ZoneInfo zi = getFromCache(id); |
148 |
if (zi == null) { |
|
149 |
zi = new ZoneInfo(id, gmtOffset); |
|
150 |
zi = addToCache(id, zi); |
|
151 |
if (!id.equals(originalId)) { |
|
152 |
zi = addToCache(originalId, zi); |
|
153 |
} |
|
154 |
} |
|
155 |
return (ZoneInfo) zi.clone(); |
|
15658 | 156 |
*/ |
2 | 157 |
} |
158 |
||
159 |
public static String toCustomID(int gmtOffset) { |
|
160 |
char sign; |
|
161 |
int offset = gmtOffset / 60000; |
|
162 |
||
163 |
if (offset >= 0) { |
|
164 |
sign = '+'; |
|
165 |
} else { |
|
166 |
sign = '-'; |
|
167 |
offset = -offset; |
|
168 |
} |
|
169 |
int hh = offset / 60; |
|
170 |
int mm = offset % 60; |
|
171 |
||
172 |
char[] buf = new char[] { 'G', 'M', 'T', sign, '0', '0', ':', '0', '0' }; |
|
173 |
if (hh >= 10) { |
|
174 |
buf[4] += hh / 10; |
|
175 |
} |
|
176 |
buf[5] += hh % 10; |
|
177 |
if (mm != 0) { |
|
178 |
buf[7] += mm / 10; |
|
179 |
buf[8] += mm % 10; |
|
180 |
} |
|
181 |
return new String(buf); |
|
182 |
} |
|
183 |
||
184 |
||
15658 | 185 |
/////////////////////////////////////////////////////////// |
2 | 186 |
|
15658 | 187 |
private static ZoneInfo getZoneInfo0(String zoneId) { |
2 | 188 |
try { |
189 |
||
15658 | 190 |
Object obj = zones.get(zoneId); |
191 |
if (obj instanceof byte[]) { |
|
192 |
byte[] bytes = (byte[]) obj; |
|
193 |
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); |
|
194 |
obj = getZoneInfo(dis, zoneId); |
|
195 |
zones.put(zoneId, obj); |
|
2 | 196 |
} |
15658 | 197 |
return (ZoneInfo)obj; |
198 |
} catch (Exception ex) { |
|
199 |
throw new RuntimeException("Invalid binary time-zone data: TZDB:" + |
|
200 |
zoneId + ", version: " + versionId, ex); |
|
2 | 201 |
} |
202 |
} |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
9025
diff
changeset
|
203 |
|
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
9025
diff
changeset
|
204 |
private ZoneInfoFile() { |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
9025
diff
changeset
|
205 |
} |
15658 | 206 |
|
207 |
private static String versionId; |
|
208 |
private final static Map<String, Object> zones = new ConcurrentHashMap<>(); |
|
209 |
private static Map<String, String> aliases = new HashMap<>(); |
|
210 |
||
211 |
// Flag for supporting JDK backward compatible IDs, such as "EST". |
|
212 |
private static final boolean USE_OLDMAPPING; |
|
213 |
||
214 |
static { |
|
215 |
String oldmapping = AccessController.doPrivileged( |
|
216 |
new sun.security.action.GetPropertyAction("sun.timezone.ids.oldmapping", "false")).toLowerCase(Locale.ROOT); |
|
217 |
USE_OLDMAPPING = (oldmapping.equals("yes") || oldmapping.equals("true")); |
|
218 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
|
219 |
public Object run() { |
|
220 |
try { |
|
221 |
||
222 |
String libDir = System.getProperty("java.home") + File.separator + "lib"; |
|
223 |
File tzdbJar = new File(libDir, "tzdb.jar"); |
|
224 |
try (ZipFile zf = new ZipFile(tzdbJar); |
|
225 |
DataInputStream dis = new DataInputStream( |
|
226 |
zf.getInputStream(zf.getEntry("TZDB.dat")))) { |
|
227 |
load(dis); |
|
228 |
} |
|
229 |
} catch (Exception x) { |
|
230 |
throw new Error(x); |
|
231 |
} |
|
232 |
return null; |
|
233 |
} |
|
234 |
}); |
|
235 |
} |
|
236 |
||
237 |
// Must be invoked after loading in all data |
|
238 |
private static void addOldMapping() { |
|
239 |
String[][] oldMappings = new String[][] { |
|
240 |
{ "ACT", "Australia/Darwin" }, |
|
241 |
{ "AET", "Australia/Sydney" }, |
|
242 |
{ "AGT", "America/Argentina/Buenos_Aires" }, |
|
243 |
{ "ART", "Africa/Cairo" }, |
|
244 |
{ "AST", "America/Anchorage" }, |
|
245 |
{ "BET", "America/Sao_Paulo" }, |
|
246 |
{ "BST", "Asia/Dhaka" }, |
|
247 |
{ "CAT", "Africa/Harare" }, |
|
248 |
{ "CNT", "America/St_Johns" }, |
|
249 |
{ "CST", "America/Chicago" }, |
|
250 |
{ "CTT", "Asia/Shanghai" }, |
|
251 |
{ "EAT", "Africa/Addis_Ababa" }, |
|
252 |
{ "ECT", "Europe/Paris" }, |
|
253 |
{ "IET", "America/Indiana/Indianapolis" }, |
|
254 |
{ "IST", "Asia/Kolkata" }, |
|
255 |
{ "JST", "Asia/Tokyo" }, |
|
256 |
{ "MIT", "Pacific/Apia" }, |
|
257 |
{ "NET", "Asia/Yerevan" }, |
|
258 |
{ "NST", "Pacific/Auckland" }, |
|
259 |
{ "PLT", "Asia/Karachi" }, |
|
260 |
{ "PNT", "America/Phoenix" }, |
|
261 |
{ "PRT", "America/Puerto_Rico" }, |
|
262 |
{ "PST", "America/Los_Angeles" }, |
|
263 |
{ "SST", "Pacific/Guadalcanal" }, |
|
264 |
{ "VST", "Asia/Ho_Chi_Minh" }, |
|
265 |
}; |
|
266 |
for (String[] alias : oldMappings) { |
|
267 |
String k = alias[0]; |
|
268 |
String v = alias[1]; |
|
269 |
if (zones.containsKey(v)) { // make sure we do have the data |
|
270 |
aliases.put(k, v); |
|
271 |
zones.put(k, zones.get(v)); |
|
272 |
} |
|
273 |
} |
|
274 |
if (USE_OLDMAPPING) { |
|
275 |
if (zones.containsKey("America/New_York")) { |
|
276 |
aliases.put("EST", "America/New_York"); |
|
277 |
zones.put("EST", zones.get("America/New_York")); |
|
278 |
} |
|
279 |
if (zones.containsKey("America/Denver")) { |
|
280 |
aliases.put("MST", "America/Denver"); |
|
281 |
zones.put("MST", zones.get("America/Denver")); |
|
282 |
} |
|
283 |
if (zones.containsKey("Pacific/Honolulu")) { |
|
284 |
aliases.put("HST", "Pacific/Honolulu"); |
|
285 |
zones.put("HST", zones.get("Pacific/Honolulu")); |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Loads the rules from a DateInputStream |
|
292 |
* |
|
293 |
* @param dis the DateInputStream to load, not null |
|
294 |
* @throws Exception if an error occurs |
|
295 |
*/ |
|
296 |
private static void load(DataInputStream dis) throws ClassNotFoundException, IOException { |
|
297 |
if (dis.readByte() != 1) { |
|
298 |
throw new StreamCorruptedException("File format not recognised"); |
|
299 |
} |
|
300 |
// group |
|
301 |
String groupId = dis.readUTF(); |
|
302 |
if ("TZDB".equals(groupId) == false) { |
|
303 |
throw new StreamCorruptedException("File format not recognised"); |
|
304 |
} |
|
305 |
// versions, only keep the last one |
|
306 |
int versionCount = dis.readShort(); |
|
307 |
for (int i = 0; i < versionCount; i++) { |
|
308 |
versionId = dis.readUTF(); |
|
309 |
||
310 |
} |
|
311 |
// regions |
|
312 |
int regionCount = dis.readShort(); |
|
313 |
String[] regionArray = new String[regionCount]; |
|
314 |
for (int i = 0; i < regionCount; i++) { |
|
315 |
regionArray[i] = dis.readUTF(); |
|
316 |
} |
|
317 |
// rules |
|
318 |
int ruleCount = dis.readShort(); |
|
319 |
Object[] ruleArray = new Object[ruleCount]; |
|
320 |
for (int i = 0; i < ruleCount; i++) { |
|
321 |
byte[] bytes = new byte[dis.readShort()]; |
|
322 |
dis.readFully(bytes); |
|
323 |
ruleArray[i] = bytes; |
|
324 |
} |
|
325 |
// link version-region-rules, only keep the last version, if more than one |
|
326 |
for (int i = 0; i < versionCount; i++) { |
|
327 |
regionCount = dis.readShort(); |
|
328 |
zones.clear(); |
|
329 |
for (int j = 0; j < regionCount; j++) { |
|
330 |
String region = regionArray[dis.readShort()]; |
|
331 |
Object rule = ruleArray[dis.readShort() & 0xffff]; |
|
332 |
zones.put(region, rule); |
|
333 |
} |
|
334 |
} |
|
335 |
// remove the following ids from the map, they |
|
336 |
// are exclued from the "old" ZoneInfo |
|
337 |
zones.remove("ROC"); |
|
338 |
for (int i = 0; i < versionCount; i++) { |
|
339 |
int aliasCount = dis.readShort(); |
|
340 |
aliases.clear(); |
|
341 |
for (int j = 0; j < aliasCount; j++) { |
|
342 |
String alias = regionArray[dis.readShort()]; |
|
343 |
String region = regionArray[dis.readShort()]; |
|
344 |
aliases.put(alias, region); |
|
345 |
} |
|
346 |
} |
|
347 |
// old us time-zone names |
|
348 |
addOldMapping(); |
|
349 |
aliases = Collections.unmodifiableMap(aliases); |
|
350 |
} |
|
351 |
||
352 |
/////////////////////////Ser///////////////////////////////// |
|
353 |
public static ZoneInfo getZoneInfo(DataInput in, String zoneId) throws Exception { |
|
354 |
byte type = in.readByte(); |
|
355 |
// TBD: assert ZRULES: |
|
356 |
int stdSize = in.readInt(); |
|
357 |
long[] stdTrans = new long[stdSize]; |
|
358 |
for (int i = 0; i < stdSize; i++) { |
|
359 |
stdTrans[i] = readEpochSec(in); |
|
360 |
} |
|
361 |
int [] stdOffsets = new int[stdSize + 1]; |
|
362 |
for (int i = 0; i < stdOffsets.length; i++) { |
|
363 |
stdOffsets[i] = readOffset(in); |
|
364 |
} |
|
365 |
int savSize = in.readInt(); |
|
366 |
long[] savTrans = new long[savSize]; |
|
367 |
for (int i = 0; i < savSize; i++) { |
|
368 |
savTrans[i] = readEpochSec(in); |
|
369 |
} |
|
370 |
int[] savOffsets = new int[savSize + 1]; |
|
371 |
for (int i = 0; i < savOffsets.length; i++) { |
|
372 |
savOffsets[i] = readOffset(in); |
|
373 |
} |
|
374 |
int ruleSize = in.readByte(); |
|
375 |
ZoneOffsetTransitionRule[] rules = new ZoneOffsetTransitionRule[ruleSize]; |
|
376 |
for (int i = 0; i < ruleSize; i++) { |
|
377 |
rules[i] = readZOTRule(in); |
|
378 |
} |
|
379 |
return getZoneInfo(zoneId, stdTrans, stdOffsets, savTrans, savOffsets, rules); |
|
380 |
} |
|
381 |
||
382 |
public static int readOffset(DataInput in) throws IOException { |
|
383 |
int offsetByte = in.readByte(); |
|
384 |
return offsetByte == 127 ? in.readInt() : offsetByte * 900; |
|
385 |
} |
|
386 |
||
387 |
static long readEpochSec(DataInput in) throws IOException { |
|
388 |
int hiByte = in.readByte() & 255; |
|
389 |
if (hiByte == 255) { |
|
390 |
return in.readLong(); |
|
391 |
} else { |
|
392 |
int midByte = in.readByte() & 255; |
|
393 |
int loByte = in.readByte() & 255; |
|
394 |
long tot = ((hiByte << 16) + (midByte << 8) + loByte); |
|
395 |
return (tot * 900) - 4575744000L; |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
static ZoneOffsetTransitionRule readZOTRule(DataInput in) throws IOException { |
|
400 |
int data = in.readInt(); |
|
401 |
Month month = Month.of(data >>> 28); |
|
402 |
int dom = ((data & (63 << 22)) >>> 22) - 32; |
|
403 |
int dowByte = (data & (7 << 19)) >>> 19; |
|
404 |
DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte); |
|
405 |
int timeByte = (data & (31 << 14)) >>> 14; |
|
406 |
TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12]; |
|
407 |
int stdByte = (data & (255 << 4)) >>> 4; |
|
408 |
int beforeByte = (data & (3 << 2)) >>> 2; |
|
409 |
int afterByte = (data & 3); |
|
410 |
LocalTime time = (timeByte == 31 ? LocalTime.ofSecondOfDay(in.readInt()) : LocalTime.of(timeByte % 24, 0)); |
|
411 |
ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900)); |
|
412 |
ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800)); |
|
413 |
ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800)); |
|
414 |
return ZoneOffsetTransitionRule.of(month, dom, dow, time, timeByte == 24, defn, std, before, after); |
|
415 |
} |
|
416 |
||
417 |
/////////////////////////ZoneRules --> ZoneInfo///////////////////////////////// |
|
418 |
||
419 |
// ZoneInfo starts with UTC1900 |
|
420 |
private static final long UTC1900 = -2208988800L; |
|
421 |
// ZoneInfo ends with UTC2037 |
|
422 |
private static final long UTC2037 = |
|
423 |
LocalDateTime.of(2038, 1, 1, 0, 0, 0).toEpochSecond(ZoneOffset.UTC) - 1; |
|
424 |
||
425 |
/* Get a ZoneInfo instance. |
|
426 |
* |
|
427 |
* @param standardTransitions the standard transitions, not null |
|
428 |
* @param standardOffsets the standard offsets, not null |
|
429 |
* @param savingsInstantTransitions the standard transitions, not null |
|
430 |
* @param wallOffsets the wall offsets, not null |
|
431 |
* @param lastRules the recurring last rules, size 15 or less, not null |
|
432 |
*/ |
|
433 |
private static ZoneInfo getZoneInfo(String zoneId, |
|
434 |
long[] standardTransitions, |
|
435 |
int[] standardOffsets, |
|
436 |
long[] savingsInstantTransitions, |
|
437 |
int[] wallOffsets, |
|
438 |
ZoneOffsetTransitionRule[] lastRules) { |
|
439 |
int rawOffset = 0; |
|
440 |
int dstSavings = 0; |
|
441 |
int checksum = 0; |
|
442 |
int[] params = null; |
|
443 |
boolean willGMTOffsetChange = false; |
|
444 |
||
445 |
// rawOffset, pick the last one |
|
446 |
if (standardTransitions.length > 0) |
|
447 |
rawOffset = standardOffsets[standardOffsets.length - 1] * 1000; |
|
448 |
else |
|
449 |
rawOffset = standardOffsets[0] * 1000; |
|
450 |
||
451 |
// transitions, offsets; |
|
452 |
long[] transitions = null; |
|
453 |
int[] offsets = null; |
|
454 |
int nOffsets = 0; |
|
455 |
int nTrans = 0; |
|
456 |
||
457 |
if (savingsInstantTransitions.length != 0) { |
|
458 |
transitions = new long[250]; |
|
459 |
offsets = new int[100]; // TBD: ZoneInfo actually can't handle |
|
460 |
// offsets.length > 16 (4-bit index limit) |
|
461 |
// last year in trans table |
|
462 |
// It should not matter to use before or after offset for year |
|
463 |
int lastyear = LocalDateTime.ofEpochSecond( |
|
464 |
savingsInstantTransitions[savingsInstantTransitions.length - 1], 0, |
|
465 |
ZoneOffset.ofTotalSeconds(wallOffsets[savingsInstantTransitions.length - 1])).getYear(); |
|
466 |
// int lastyear = savingsLocalTransitions[savingsLocalTransitions.length - 1].getYear(); |
|
467 |
||
468 |
int i = 0, k = 1; |
|
469 |
while (i < savingsInstantTransitions.length && |
|
470 |
savingsInstantTransitions[i] < UTC1900) { |
|
471 |
i++; // skip any date before UTC1900 |
|
472 |
} |
|
473 |
if (i < savingsInstantTransitions.length) { |
|
474 |
// javazic writes the last GMT offset into index 0! |
|
475 |
if (i < savingsInstantTransitions.length) { |
|
476 |
offsets[0] = standardOffsets[standardOffsets.length - 1] * 1000; |
|
477 |
nOffsets = 1; |
|
478 |
} |
|
479 |
// ZoneInfo has a beginning entry for 1900. |
|
480 |
// Only add it if this is not the only one in table |
|
481 |
nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets, |
|
482 |
UTC1900, |
|
483 |
wallOffsets[i], |
|
484 |
getStandardOffset(standardTransitions, standardOffsets, UTC1900)); |
|
485 |
} |
|
486 |
for (; i < savingsInstantTransitions.length; i++) { |
|
487 |
//if (savingsLocalTransitions[i * 2].getYear() > LASTYEAR) { |
|
488 |
if (savingsInstantTransitions[i] > UTC2037) { |
|
489 |
// no trans beyond LASTYEAR |
|
490 |
lastyear = LASTYEAR; |
|
491 |
break; |
|
492 |
} |
|
493 |
long trans = savingsInstantTransitions[i]; |
|
494 |
while (k < standardTransitions.length) { |
|
495 |
// some standard offset transitions don't exist in |
|
496 |
// savingInstantTrans, if the offset "change" doesn't |
|
497 |
// really change the "effectiveWallOffset". For example |
|
498 |
// the 1999/2000 pair in Zone Arg/Buenos_Aires, in which |
|
499 |
// the daylightsaving "happened" but it actually does |
|
500 |
// not result in the timezone switch. ZoneInfo however |
|
501 |
// needs them in its transitions table |
|
502 |
long trans_s = standardTransitions[k]; |
|
503 |
if (trans_s >= UTC1900) { |
|
504 |
if (trans_s > trans) |
|
505 |
break; |
|
506 |
if (trans_s < trans) { |
|
507 |
if (nOffsets + 2 >= offsets.length) { |
|
508 |
offsets = Arrays.copyOf(offsets, offsets.length + 100); |
|
509 |
} |
|
510 |
if (nTrans + 1 >= transitions.length) { |
|
511 |
transitions = Arrays.copyOf(transitions, transitions.length + 100); |
|
512 |
} |
|
513 |
nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets, |
|
514 |
trans_s, |
|
515 |
wallOffsets[i], |
|
516 |
standardOffsets[k+1]); |
|
517 |
} |
|
518 |
} |
|
519 |
k++; |
|
520 |
} |
|
521 |
if (nOffsets + 2 >= offsets.length) { |
|
522 |
offsets = Arrays.copyOf(offsets, offsets.length + 100); |
|
523 |
} |
|
524 |
if (nTrans + 1 >= transitions.length) { |
|
525 |
transitions = Arrays.copyOf(transitions, transitions.length + 100); |
|
526 |
} |
|
527 |
nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets, |
|
528 |
trans, |
|
529 |
wallOffsets[i + 1], |
|
530 |
getStandardOffset(standardTransitions, standardOffsets, trans)); |
|
531 |
} |
|
532 |
// append any leftover standard trans |
|
533 |
while (k < standardTransitions.length) { |
|
534 |
long trans = standardTransitions[k]; |
|
535 |
if (trans >= UTC1900) { |
|
536 |
int offset = wallOffsets[i]; |
|
537 |
int offsetIndex = indexOf(offsets, 0, nOffsets, offset); |
|
538 |
if (offsetIndex == nOffsets) |
|
539 |
nOffsets++; |
|
540 |
transitions[nTrans++] = ((trans * 1000) << TRANSITION_NSHIFT) | |
|
541 |
(offsetIndex & OFFSET_MASK); |
|
542 |
} |
|
543 |
k++; |
|
544 |
} |
|
545 |
if (lastRules.length > 1) { |
|
546 |
// fill the gap between the last trans until LASTYEAR |
|
547 |
while (lastyear++ < LASTYEAR) { |
|
548 |
for (ZoneOffsetTransitionRule zotr : lastRules) { |
|
549 |
ZoneOffsetTransition zot = zotr.createTransition(lastyear); |
|
550 |
//long trans = zot.getDateTimeBefore().toEpochSecond(); |
|
551 |
long trans = zot.toEpochSecond(); |
|
552 |
if (nOffsets + 2 >= offsets.length) { |
|
553 |
offsets = Arrays.copyOf(offsets, offsets.length + 100); |
|
554 |
} |
|
555 |
if (nTrans + 1 >= transitions.length) { |
|
556 |
transitions = Arrays.copyOf(transitions, transitions.length + 100); |
|
557 |
} |
|
558 |
nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets, |
|
559 |
trans, |
|
560 |
zot.getOffsetAfter().getTotalSeconds(), |
|
561 |
getStandardOffset(standardTransitions, standardOffsets, trans)); |
|
562 |
} |
|
563 |
} |
|
564 |
ZoneOffsetTransitionRule startRule = lastRules[lastRules.length - 2]; |
|
565 |
ZoneOffsetTransitionRule endRule = lastRules[lastRules.length - 1]; |
|
566 |
params = new int[10]; |
|
567 |
if (startRule.getOffsetBefore().compareTo(startRule.getOffsetAfter()) < 0 && |
|
568 |
endRule.getOffsetBefore().compareTo(endRule.getOffsetAfter()) > 0) { |
|
569 |
ZoneOffsetTransitionRule tmp; |
|
570 |
tmp = startRule; |
|
571 |
startRule = endRule; |
|
572 |
endRule = tmp; |
|
573 |
} |
|
574 |
params[0] = startRule.getMonth().getValue() - 1; |
|
575 |
// params[1] = startRule.getDayOfMonthIndicator(); |
|
576 |
// params[2] = toCalendarDOW[startRule.getDayOfWeek().getValue()]; |
|
577 |
int dom = startRule.getDayOfMonthIndicator(); |
|
578 |
DayOfWeek dow = startRule.getDayOfWeek(); |
|
579 |
if (dow == null) { |
|
580 |
params[1] = startRule.getDayOfMonthIndicator(); |
|
581 |
params[2] = 0; |
|
582 |
} else { |
|
583 |
// ZoneRulesBuilder adjusts < 0 case (-1, for last, don't have |
|
584 |
// "<=" case yet) to positive value if not February (it appears |
|
585 |
// we don't have February cutoff in tzdata table yet) |
|
586 |
// Ideally, if JSR310 can just pass in the nagative and |
|
587 |
// we can then pass in the dom = -1, dow > 0 into ZoneInfo |
|
588 |
// |
|
589 |
// hacking, assume the >=24 is the result of ZRB optimization for |
|
590 |
// "last", it works for now. |
|
591 |
if (dom < 0 || dom >= 24) { |
|
592 |
params[1] = -1; |
|
593 |
params[2] = toCalendarDOW[dow.getValue()]; |
|
594 |
} else { |
|
595 |
params[1] = dom; |
|
596 |
// To specify a day of week on or after an exact day of month, |
|
597 |
// set the month to an exact month value, day-of-month to the |
|
598 |
// day on or after which the rule is applied, and day-of-week |
|
599 |
// to a negative Calendar.DAY_OF_WEEK DAY_OF_WEEK field value. |
|
600 |
params[2] = -toCalendarDOW[dow.getValue()]; |
|
601 |
} |
|
602 |
} |
|
603 |
params[3] = startRule.getLocalTime().toSecondOfDay() * 1000; |
|
604 |
params[4] = toSTZTime[startRule.getTimeDefinition().ordinal()]; |
|
605 |
||
606 |
params[5] = endRule.getMonth().getValue() - 1; |
|
607 |
// params[6] = endRule.getDayOfMonthIndicator(); |
|
608 |
// params[7] = toCalendarDOW[endRule.getDayOfWeek().getValue()]; |
|
609 |
dom = endRule.getDayOfMonthIndicator(); |
|
610 |
dow = endRule.getDayOfWeek(); |
|
611 |
if (dow == null) { |
|
612 |
params[6] = dom; |
|
613 |
params[7] = 0; |
|
614 |
} else { |
|
615 |
// hacking: see comment above |
|
616 |
if (dom < 0 || dom >= 24) { |
|
617 |
params[6] = -1; |
|
618 |
params[7] = toCalendarDOW[dow.getValue()]; |
|
619 |
} else { |
|
620 |
params[6] = dom; |
|
621 |
params[7] = -toCalendarDOW[dow.getValue()]; |
|
622 |
} |
|
623 |
} |
|
624 |
params[8] = endRule.getLocalTime().toSecondOfDay() * 1000; |
|
625 |
params[9] = toSTZTime[endRule.getTimeDefinition().ordinal()]; |
|
626 |
dstSavings = (startRule.getOffsetAfter().getTotalSeconds() |
|
627 |
- startRule.getOffsetBefore().getTotalSeconds()) * 1000; |
|
628 |
// Note: known mismatching -> Asia/Amman |
|
629 |
// ZoneInfo : startDayOfWeek=5 <= Thursday |
|
630 |
// startTime=86400000 <= 24 hours |
|
631 |
// This: startDayOfWeek=6 |
|
632 |
// startTime=0 |
|
633 |
// Below is the workaround, it probably slows down everyone a little |
|
634 |
if (params[2] == 6 && params[3] == 0 && zoneId.equals("Asia/Amman")) { |
|
635 |
params[2] = 5; |
|
636 |
params[3] = 86400000; |
|
637 |
} |
|
638 |
} else if (nTrans > 0) { // only do this if there is something in table already |
|
639 |
if (lastyear < LASTYEAR) { |
|
640 |
// ZoneInfo has an ending entry for 2037 |
|
641 |
long trans = OffsetDateTime.of(LASTYEAR, Month.JANUARY.getValue(), 1, 0, 0, 0, 0, |
|
642 |
ZoneOffset.ofTotalSeconds(rawOffset/1000)) |
|
643 |
.toEpochSecond(); |
|
644 |
int offsetIndex = indexOf(offsets, 0, nOffsets, rawOffset/1000); |
|
645 |
if (offsetIndex == nOffsets) |
|
646 |
nOffsets++; |
|
647 |
transitions[nTrans++] = (trans * 1000) << TRANSITION_NSHIFT | |
|
648 |
(offsetIndex & OFFSET_MASK); |
|
649 |
} else if (savingsInstantTransitions.length > 2) { |
|
650 |
// Workaround: create the params based on the last pair for |
|
651 |
// zones like Israel and Iran which have trans defined |
|
652 |
// up until 2037, but no "transition rule" defined |
|
653 |
// |
|
654 |
// Note: Known mismatching for Israel, Asia/Jerusalem/Tel Aviv |
|
655 |
// ZoneInfo: startMode=3 |
|
656 |
// startMonth=2 |
|
657 |
// startDay=26 |
|
658 |
// startDayOfWeek=6 |
|
659 |
// |
|
660 |
// This: startMode=1 |
|
661 |
// startMonth=2 |
|
662 |
// startDay=27 |
|
663 |
// startDayOfWeek=0 |
|
664 |
// these two are actually the same for 2037, the SimpleTimeZone |
|
665 |
// for the last "known" year |
|
666 |
int m = savingsInstantTransitions.length; |
|
667 |
long startTrans = savingsInstantTransitions[m - 2]; |
|
668 |
int startOffset = wallOffsets[m - 2 + 1]; |
|
669 |
int startStd = getStandardOffset(standardTransitions, standardOffsets, startTrans); |
|
670 |
long endTrans = savingsInstantTransitions[m - 1]; |
|
671 |
int endOffset = wallOffsets[m - 1 + 1]; |
|
672 |
int endStd = getStandardOffset(standardTransitions, standardOffsets, endTrans); |
|
673 |
||
674 |
if (startOffset > startStd && endOffset == endStd) { |
|
675 |
/* |
|
676 |
m = savingsLocalTransitions.length; |
|
677 |
LocalDateTime startLDT = savingsLocalTransitions[m -4]; //gap |
|
678 |
LocalDateTime endLDT = savingsLocalTransitions[m - 1]; //over |
|
679 |
*/ |
|
680 |
// last - 1 trans |
|
681 |
m = savingsInstantTransitions.length - 2; |
|
682 |
ZoneOffset before = ZoneOffset.ofTotalSeconds(wallOffsets[m]); |
|
683 |
ZoneOffset after = ZoneOffset.ofTotalSeconds(wallOffsets[m + 1]); |
|
684 |
ZoneOffsetTransition trans = ZoneOffsetTransition.of( |
|
685 |
LocalDateTime.ofEpochSecond(savingsInstantTransitions[m], 0, before), |
|
686 |
before, |
|
687 |
after); |
|
688 |
LocalDateTime startLDT; |
|
689 |
if (trans.isGap()) { |
|
690 |
startLDT = trans.getDateTimeBefore(); |
|
691 |
} else { |
|
692 |
startLDT = trans.getDateTimeAfter(); |
|
693 |
} |
|
694 |
// last trans |
|
695 |
m = savingsInstantTransitions.length - 1; |
|
696 |
before = ZoneOffset.ofTotalSeconds(wallOffsets[m]); |
|
697 |
after = ZoneOffset.ofTotalSeconds(wallOffsets[m + 1]); |
|
698 |
trans = ZoneOffsetTransition.of( |
|
699 |
LocalDateTime.ofEpochSecond(savingsInstantTransitions[m], 0, before), |
|
700 |
before, |
|
701 |
after); |
|
702 |
LocalDateTime endLDT; |
|
703 |
if (trans.isGap()) { |
|
704 |
endLDT = trans.getDateTimeAfter(); |
|
705 |
} else { |
|
706 |
endLDT = trans.getDateTimeBefore(); |
|
707 |
} |
|
708 |
params = new int[10]; |
|
709 |
params[0] = startLDT.getMonthValue() - 1; |
|
710 |
params[1] = startLDT.getDayOfMonth(); |
|
711 |
params[2] = 0; |
|
712 |
params[3] = startLDT.toLocalTime().toSecondOfDay() * 1000; |
|
713 |
params[4] = SimpleTimeZone.WALL_TIME; |
|
714 |
params[5] = endLDT.getMonthValue() - 1; |
|
715 |
params[6] = endLDT.getDayOfMonth(); |
|
716 |
params[7] = 0; |
|
717 |
params[8] = endLDT.toLocalTime().toSecondOfDay() * 1000; |
|
718 |
params[9] = SimpleTimeZone.WALL_TIME; |
|
719 |
dstSavings = (startOffset - startStd) * 1000; |
|
720 |
} |
|
721 |
} |
|
722 |
} |
|
723 |
if (transitions != null && transitions.length != nTrans) { |
|
724 |
if (nTrans == 0) { |
|
725 |
transitions = null; |
|
726 |
} else { |
|
727 |
transitions = Arrays.copyOf(transitions, nTrans); |
|
728 |
} |
|
729 |
} |
|
730 |
if (offsets != null && offsets.length != nOffsets) { |
|
731 |
if (nOffsets == 0) { |
|
732 |
offsets = null; |
|
733 |
} else { |
|
734 |
offsets = Arrays.copyOf(offsets, nOffsets); |
|
735 |
} |
|
736 |
} |
|
737 |
if (transitions != null) { |
|
738 |
Checksum sum = new Checksum(); |
|
739 |
for (i = 0; i < transitions.length; i++) { |
|
740 |
long val = transitions[i]; |
|
741 |
int dst = (int)((val >>> DST_NSHIFT) & 0xfL); |
|
742 |
int saving = (dst == 0) ? 0 : offsets[dst]; |
|
743 |
int index = (int)(val & OFFSET_MASK); |
|
744 |
int offset = offsets[index]; |
|
745 |
long second = (val >> TRANSITION_NSHIFT); |
|
746 |
// javazic uses "index of the offset in offsets", |
|
747 |
// instead of the real offset value itself to |
|
748 |
// calculate the checksum. Have to keep doing |
|
749 |
// the same thing, checksum is part of the |
|
750 |
// ZoneInfo serialization form. |
|
751 |
sum.update(second + index); |
|
752 |
sum.update(index); |
|
753 |
sum.update(dst == 0 ? -1 : dst); |
|
754 |
} |
|
755 |
checksum = (int)sum.getValue(); |
|
756 |
} |
|
757 |
} |
|
758 |
return new ZoneInfo(zoneId, rawOffset, dstSavings, checksum, transitions, |
|
759 |
offsets, params, willGMTOffsetChange); |
|
760 |
} |
|
761 |
||
762 |
private static int getStandardOffset(long[] standardTransitions, |
|
763 |
int[] standardOffsets, |
|
764 |
long epochSec) { |
|
765 |
int index = Arrays.binarySearch(standardTransitions, epochSec); |
|
766 |
if (index < 0) { |
|
767 |
// switch negative insert position to start of matched range |
|
768 |
index = -index - 2; |
|
769 |
} |
|
770 |
return standardOffsets[index + 1]; |
|
771 |
} |
|
772 |
||
773 |
private static int toCalendarDOW[] = new int[] { |
|
774 |
-1, |
|
775 |
Calendar.MONDAY, |
|
776 |
Calendar.TUESDAY, |
|
777 |
Calendar.WEDNESDAY, |
|
778 |
Calendar.THURSDAY, |
|
779 |
Calendar.FRIDAY, |
|
780 |
Calendar.SATURDAY, |
|
781 |
Calendar.SUNDAY |
|
782 |
}; |
|
783 |
||
784 |
private static int toSTZTime[] = new int[] { |
|
785 |
SimpleTimeZone.UTC_TIME, |
|
786 |
SimpleTimeZone.WALL_TIME, |
|
787 |
SimpleTimeZone.STANDARD_TIME, |
|
788 |
}; |
|
789 |
||
790 |
private static final long OFFSET_MASK = 0x0fL; |
|
791 |
private static final long DST_MASK = 0xf0L; |
|
792 |
private static final int DST_NSHIFT = 4; |
|
793 |
private static final int TRANSITION_NSHIFT = 12; |
|
794 |
private static final int LASTYEAR = 2037; |
|
795 |
||
796 |
// from: 0 for offset lookup, 1 for dstsvings lookup |
|
797 |
private static int indexOf(int[] offsets, int from, int nOffsets, int offset) { |
|
798 |
offset *= 1000; |
|
799 |
for (; from < nOffsets; from++) { |
|
800 |
if (offsets[from] == offset) |
|
801 |
return from; |
|
802 |
} |
|
803 |
offsets[from] = offset; |
|
804 |
return from; |
|
805 |
} |
|
806 |
||
807 |
// return updated nOffsets |
|
808 |
private static int addTrans(long transitions[], int nTrans, |
|
809 |
int offsets[], int nOffsets, |
|
810 |
long trans, int offset, int stdOffset) { |
|
811 |
int offsetIndex = indexOf(offsets, 0, nOffsets, offset); |
|
812 |
if (offsetIndex == nOffsets) |
|
813 |
nOffsets++; |
|
814 |
int dstIndex = 0; |
|
815 |
if (offset != stdOffset) { |
|
816 |
dstIndex = indexOf(offsets, 1, nOffsets, offset - stdOffset); |
|
817 |
if (dstIndex == nOffsets) |
|
818 |
nOffsets++; |
|
819 |
} |
|
820 |
transitions[nTrans] = ((trans * 1000) << TRANSITION_NSHIFT) | |
|
821 |
((dstIndex << DST_NSHIFT) & DST_MASK) | |
|
822 |
(offsetIndex & OFFSET_MASK); |
|
823 |
return nOffsets; |
|
824 |
} |
|
825 |
||
826 |
///////////////////////////////////////////////////////////// |
|
827 |
// ZoneInfo checksum, copy/pasted from javazic |
|
828 |
private static class Checksum extends CRC32 { |
|
829 |
public void update(int val) { |
|
830 |
byte[] b = new byte[4]; |
|
831 |
b[0] = (byte)((val >>> 24) & 0xff); |
|
832 |
b[1] = (byte)((val >>> 16) & 0xff); |
|
833 |
b[2] = (byte)((val >>> 8) & 0xff); |
|
834 |
b[3] = (byte)(val & 0xff); |
|
835 |
update(b); |
|
836 |
} |
|
837 |
void update(long val) { |
|
838 |
byte[] b = new byte[8]; |
|
839 |
b[0] = (byte)((val >>> 56) & 0xff); |
|
840 |
b[1] = (byte)((val >>> 48) & 0xff); |
|
841 |
b[2] = (byte)((val >>> 40) & 0xff); |
|
842 |
b[3] = (byte)((val >>> 32) & 0xff); |
|
843 |
b[4] = (byte)((val >>> 24) & 0xff); |
|
844 |
b[5] = (byte)((val >>> 16) & 0xff); |
|
845 |
b[6] = (byte)((val >>> 8) & 0xff); |
|
846 |
b[7] = (byte)(val & 0xff); |
|
847 |
update(b); |
|
848 |
} |
|
849 |
} |
|
2 | 850 |
} |