forked from ThreeTen/threeten-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemUtcRules.java
More file actions
304 lines (286 loc) · 12.3 KB
/
SystemUtcRules.java
File metadata and controls
304 lines (286 loc) · 12.3 KB
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
/*
* Copyright (c) 2007-present, 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 org.threeten.extra.scale;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.temporal.JulianFields;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* System default UTC rules.
*
* <h3>Implementation Requirements:</h3>
* This class is immutable and thread-safe.
*/
final class SystemUtcRules extends UtcRules implements Serializable {
/**
* The leap seconds config file.
*/
private static final String LEAP_SECONDS_TXT = "org/threeten/extra/scale/LeapSeconds.txt";
/**
* Leap second file format.
*/
private static final Pattern LEAP_FILE_FORMAT = Pattern.compile("([0-9-]{10})[ ]+([0-9]+)");
/**
* Serialization version.
*/
private static final long serialVersionUID = 7594178360693417218L;
/**
* Singleton.
*/
static final SystemUtcRules INSTANCE = new SystemUtcRules();
/**
* The table of leap second dates.
*/
private AtomicReference<Data> dataRef = new AtomicReference<Data>(loadLeapSeconds());
/** Data holder. */
private static final class Data implements Serializable {
/** Serialization version. */
private static final long serialVersionUID = -3655687912882817265L;
/** Constructor. */
private Data(long[] dates, int[] offsets, long[] taiSeconds) {
super();
this.dates = dates;
this.offsets = offsets;
this.taiSeconds = taiSeconds;
}
/** The table of leap second date when the leap second occurs. */
private final long[] dates;
/** The table of TAI offset after the leap second. */
private final int[] offsets;
/** The table of TAI second when the new offset starts. */
private final long[] taiSeconds;
/**
* @return The modified Julian Date of the newest leap second
*/
public long getNewestDate() {
return dates[dates.length - 1];
}
}
//-----------------------------------------------------------------------
/**
* Restricted constructor.
*/
private SystemUtcRules() {
}
/**
* Resolves singleton.
*
* @return the resolved instance, not null
*/
private Object readResolve() {
return INSTANCE;
}
//-----------------------------------------------------------------------
/**
* Adds a new leap second to these rules.
*
* @param mjDay the Modified Julian Day that the leap second occurs at the end of
* @param leapAdjustment the leap seconds to add/remove at the end of the day, either -1 or 1
* @throws IllegalArgumentException if the leap adjustment is invalid
* @throws IllegalArgumentException if the day is before or equal the last known leap second day
* and the definition does not match a previously registered leap
* @throws ConcurrentModificationException if another thread updates the rules at the same time
*/
void register(long mjDay, int leapAdjustment) {
if (leapAdjustment != -1 && leapAdjustment != 1) {
throw new IllegalArgumentException("Leap adjustment must be -1 or 1");
}
Data data = dataRef.get();
int pos = Arrays.binarySearch(data.dates, mjDay);
int currentAdj = pos > 0 ? data.offsets[pos] - data.offsets[pos - 1] : 0;
if (currentAdj == leapAdjustment) {
return; // matches previous definition
}
if (mjDay <= data.dates[data.dates.length - 1]) {
throw new IllegalArgumentException("Date must be after the last configured leap second date");
}
long[] dates = Arrays.copyOf(data.dates, data.dates.length + 1);
int[] offsets = Arrays.copyOf(data.offsets, data.offsets.length + 1);
long[] taiSeconds = Arrays.copyOf(data.taiSeconds, data.taiSeconds.length + 1);
int offset = offsets[offsets.length - 2] + leapAdjustment;
dates[dates.length - 1] = mjDay;
offsets[offsets.length - 1] = offset;
taiSeconds[taiSeconds.length - 1] = tai(mjDay, offset);
Data newData = new Data(dates, offsets, taiSeconds);
if (dataRef.compareAndSet(data, newData) == false) {
throw new ConcurrentModificationException("Unable to update leap second rules as they have already been updated");
}
}
//-----------------------------------------------------------------------
@Override
public String getName() {
return "System";
}
@Override
public int getLeapSecondAdjustment(long mjDay) {
Data data = dataRef.get();
int pos = Arrays.binarySearch(data.dates, mjDay);
return pos > 0 ? data.offsets[pos] - data.offsets[pos - 1] : 0;
}
@Override
public int getTaiOffset(long mjDay) {
Data data = dataRef.get();
int pos = Arrays.binarySearch(data.dates, mjDay);
pos = (pos < 0 ? ~pos : pos);
return pos > 0 ? data.offsets[pos - 1] : 10;
}
@Override
public long[] getLeapSecondDates() {
Data data = dataRef.get();
return data.dates.clone();
}
//-----------------------------------------------------------------------
@Override
public UtcInstant convertToUtc(TaiInstant taiInstant) {
Data data = dataRef.get();
long[] mjds = data.dates;
long[] tais = data.taiSeconds;
int pos = Arrays.binarySearch(tais, taiInstant.getTaiSeconds());
pos = (pos >= 0 ? pos : ~pos - 1);
int taiOffset = (pos >= 0 ? data.offsets[pos] : 10);
long adjustedTaiSecs = taiInstant.getTaiSeconds() - taiOffset;
long mjd = Math.floorDiv(adjustedTaiSecs, SECS_PER_DAY) + OFFSET_MJD_TAI;
long nod = Math.floorMod(adjustedTaiSecs, SECS_PER_DAY) * NANOS_PER_SECOND + taiInstant.getNano();
long mjdNextRegionStart = (pos + 1 < mjds.length ? mjds[pos + 1] + 1 : Long.MAX_VALUE);
if (mjd == mjdNextRegionStart) { // in leap second
mjd--;
nod = SECS_PER_DAY * NANOS_PER_SECOND + (nod / NANOS_PER_SECOND) * NANOS_PER_SECOND + nod % NANOS_PER_SECOND;
}
return UtcInstant.ofModifiedJulianDay(mjd, nod);
}
//-----------------------------------------------------------------------
/**
* Loads the rules from files in the class loader, often jar files.
*
* @return the list of loaded rules, not null
* @throws Exception if an error occurs
*/
private static Data loadLeapSeconds() {
Data bestData = null;
URL url = null;
try {
// this is the new location of the file, working on Java 8, Java 9 class path and Java 9 module path
Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources("META-INF/" + LEAP_SECONDS_TXT);
while (en.hasMoreElements()) {
url = en.nextElement();
Data candidate = loadLeapSeconds(url);
if (bestData == null || candidate.getNewestDate() > bestData.getNewestDate()) {
bestData = candidate;
}
}
// this location does not work on Java 9 module path because the resource is encapsulated
en = Thread.currentThread().getContextClassLoader().getResources(LEAP_SECONDS_TXT);
while (en.hasMoreElements()) {
url = en.nextElement();
Data candidate = loadLeapSeconds(url);
if (bestData == null || candidate.getNewestDate() > bestData.getNewestDate()) {
bestData = candidate;
}
}
// this location is the canonical one, and class-based loading works on Java 9 module path
url = SystemUtcRules.class.getResource("/" + LEAP_SECONDS_TXT);
if (url != null) {
Data candidate = loadLeapSeconds(url);
if (bestData == null || candidate.getNewestDate() > bestData.getNewestDate()) {
bestData = candidate;
}
}
} catch (Exception ex) {
throw new RuntimeException("Unable to load time-zone rule data: " + url, ex);
}
if (bestData == null) {
// no data on classpath, but we allow manual registration of leap seconds
// setup basic known data - MJD 1972-01-01 is 41317L, where offset was 10
bestData = new Data(new long[] {41317L}, new int[] {10}, new long[] {tai(41317L, 10)});
}
return bestData;
}
/**
* Loads the leap second rules from a URL, often in a jar file.
*
* @param url the jar file to load, not null
* @throws Exception if an error occurs
*/
private static Data loadLeapSeconds(URL url) throws ClassNotFoundException, IOException {
List<String> lines;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
lines = reader.lines().parallel().collect(Collectors.toList());
}
List<Long> dates = new ArrayList<>();
List<Integer> offsets = new ArrayList<>();
for (String line : lines) {
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue;
}
Matcher matcher = LEAP_FILE_FORMAT.matcher(line);
if (matcher.matches() == false) {
throw new StreamCorruptedException("Invalid leap second file");
}
dates.add(LocalDate.parse(matcher.group(1)).getLong(JulianFields.MODIFIED_JULIAN_DAY));
offsets.add(Integer.valueOf(matcher.group(2)));
}
long[] datesData = new long[dates.size()];
int[] offsetsData = new int[dates.size()];
long[] taiData = new long[dates.size()];
for (int i = 0; i < datesData.length; i++) {
datesData[i] = dates.get(i);
offsetsData[i] = offsets.get(i);
taiData[i] = tai(datesData[i], offsetsData[i]);
}
return new Data(datesData, offsetsData, taiData);
}
/**
* Gets the TAI seconds for the start of the day following the day passed in.
*
* @param changeMjd the MJD that the leap second is added to
* @param offset the new offset after the leap
* @return the TAI seconds
*/
private static long tai(long changeMjd, int offset) {
return (changeMjd + 1 - OFFSET_MJD_TAI) * SECS_PER_DAY + offset;
}
}