-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimeStamp.java
More file actions
430 lines (387 loc) · 14.8 KB
/
Copy pathTimeStamp.java
File metadata and controls
430 lines (387 loc) · 14.8 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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nts;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* TimeStamp class represents the Network Time Protocol (NTP) timestamp as defined in RFC-1305 and
* SNTP (RFC-2030). It is represented as a 64-bit unsigned fixed-point number in seconds relative to
* 0-hour on 1-January-1900. The 32-bit low-order bits are the fractional seconds whose precision is
* about 200 picoseconds. Assumes overflow date when date passes MAX_LONG and reverts back to 0 is
* 2036 and not 1900. Test for most significant bit: if MSB=0 then 2036 basis is used otherwise 1900
* if MSB=1.
*
* <p>Methods exist to convert NTP timestamps to and from the equivalent Java date representation,
* which is the number of milliseconds since the standard base time known as "the epoch", namely
* January 1, 1970, 00:00:00 GMT.
*
* @see java.util.Date
*/
public class TimeStamp implements Serializable, Comparable<TimeStamp> {
private static final long serialVersionUID = 8139806907588338737L;
/** Baseline NTP time if bit-0=0 is 7-Feb-2036 @ 06:28:16 UTC */
protected static final long msb0baseTime = 2085978496000L;
/** Baseline NTP time if bit-0=1 is 1-Jan-1900 @ 01:00:00 UTC */
protected static final long msb1baseTime = -2208988800000L;
/**
* Default NTP date string format. E.g. Fri, Sep 12 2003 21:06:23.860. See <code>
* java.text.SimpleDateFormat</code> for code descriptions.
*/
public static final String NTP_DATE_FORMAT = "EEE, MMM dd yyyy HH:mm:ss.SSS";
/**
* Left-pad 8-character hexadecimal string with 0's
*
* @param buf - StringBuilder which is appended with leading 0's.
* @param l - a long.
*/
private static void appendHexString(final StringBuilder buf, final long l) {
final String s = Long.toHexString(l);
for (int i = s.length(); i < 8; i++) {
buf.append('0');
}
buf.append(s);
}
/**
* Convert NTP timestamp hexstring (e.g. "c1a089bd.fc904f6d") to the NTP 64-bit unsigned
* fixed-point number.
*
* @param hexString the string to convert
* @return NTP 64-bit timestamp value.
* @throws NumberFormatException - if the string does not contain a parsable timestamp.
*/
protected static long decodeNtpHexString(final String hexString) throws NumberFormatException {
if (hexString == null) {
throw new NumberFormatException("null");
}
final int ind = hexString.indexOf('.');
if (ind == -1) {
if (hexString.isEmpty()) {
return 0;
}
return Long.parseLong(hexString, 16) << 32; // no decimal
}
return Long.parseLong(hexString.substring(0, ind), 16) << 32
| Long.parseLong(hexString.substring(ind + 1), 16);
}
/**
* Constructs a NTP timestamp object and initializes it so that it represents the time at which it
* was allocated, measured to the nearest millisecond.
*
* @return NTP timestamp object set to the current time.
* @see System#currentTimeMillis()
*/
public static TimeStamp getCurrentTime() {
return getNtpTime(System.currentTimeMillis());
}
// initialization of static time bases
/*
* static { TimeZone utcZone = TimeZone.getTimeZone("UTC"); Calendar calendar = Calendar.getInstance(utcZone); calendar.set(1900, Calendar.JANUARY, 1, 0, 0,
* 0); calendar.set(Calendar.MILLISECOND, 0); msb1baseTime = calendar.getTime().getTime(); calendar.set(2036, Calendar.FEBRUARY, 7, 6, 28, 16);
* calendar.set(Calendar.MILLISECOND, 0); msb0baseTime = calendar.getTime().getTime(); }
*/
/**
* Helper method to convert Java time to NTP timestamp object. Note that Java time (milliseconds)
* by definition has less precision than NTP time (picoseconds) so converting Ntptime to Javatime
* and back to Ntptime loses precision. For example, Tue, Dec 17 2002 09:07:24.810 is represented
* by a single Java-based time value of f22cd1fc8a, but its NTP equivalent are all values from
* c1a9ae1c.cf5c28f5 to c1a9ae1c.cf9db22c.
*
* @param dateMillis the milliseconds since January 1, 1970, 00:00:00 GMT.
* @return NTP timestamp object at the specified date.
*/
public static TimeStamp getNtpTime(final long dateMillis) {
return new TimeStamp(toNtpTime(dateMillis));
}
/**
* Converts 64-bit NTP timestamp to Java standard time.
*
* <p>Note that java time (milliseconds) by definition has less precision than NTP time
* (picoseconds) so converting NTP timestamp to Java time and back to NTP timestamp loses
* precision. For example, Tue, Dec 17 2002 09:07:24.810 EST is represented by a single Java-based
* time value of f22cd1fc8a, but its NTP equivalent are all values ranging from c1a9ae1c.cf5c28f5
* to c1a9ae1c.cf9db22c.
*
* @param ntpTimeValue the input time
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this NTP
* timestamp value.
*/
public static long getTime(final long ntpTimeValue) {
final long seconds = ntpTimeValue >>> 32 & 0xffffffffL; // high-order 32-bits
long fraction = ntpTimeValue & 0xffffffffL; // low-order 32-bits
// Use round-off on fractional part to preserve going to lower precision
fraction = Math.round(1000D * fraction / 0x100000000L);
/*
* If the most significant bit (MSB) on the seconds field is set we use a different time base. The following text is a quote from RFC-2030 (SNTP v4):
*
* If bit 0 is set, the UTC time is in the range 1968-2036 and UTC time is reckoned from 0h 0m 0s UTC on 1 January 1900. If bit 0 is not set, the time
* is in the range 2036-2104 and UTC time is reckoned from 6h 28m 16s UTC on 7 February 2036.
*/
final long msb = seconds & 0x80000000L;
if (msb == 0) {
// use base: 7-Feb-2036 @ 06:28:16 UTC
return msb0baseTime + seconds * 1000 + fraction;
}
// use base: 1-Jan-1900 @ 01:00:00 UTC
return msb1baseTime + seconds * 1000 + fraction;
}
/**
* Parses the string argument as a NTP hexidecimal timestamp representation string (e.g.
* "c1a089bd.fc904f6d").
*
* @param s - hexstring.
* @return the Timestamp represented by the argument in hexidecimal.
* @throws NumberFormatException - if the string does not contain a parsable timestamp.
*/
public static TimeStamp parseNtpString(final String s) throws NumberFormatException {
return new TimeStamp(decodeNtpHexString(s));
}
/**
* Converts Java time to 64-bit NTP time representation.
*
* @param millis Java time
* @return NTP timestamp representation of Java time value.
*/
protected static long toNtpTime(final long millis) {
final boolean useBase1 = millis < msb0baseTime; // time < Feb-2036
final long baseTimeMillis;
if (useBase1) {
baseTimeMillis = millis - msb1baseTime; // dates <= Feb-2036
} else {
// if base0 needed for dates >= Feb-2036
baseTimeMillis = millis - msb0baseTime;
}
long seconds = baseTimeMillis / 1000;
final long fraction = baseTimeMillis % 1000 * 0x100000000L / 1000;
if (useBase1) {
seconds |= 0x80000000L; // set high-order bit if msb1baseTime 1900 used
}
return seconds << 32 | fraction;
}
/**
* Converts 64-bit NTP timestamp value to a <code>String</code>. The NTP timestamp value is
* represented as hexadecimal string with seconds separated by fractional seconds by a decimal
* point; e.g. c1a089bd.fc904f6d == Tue, Dec 10 2002 10:41:49.986
*
* @param ntpTime the 64 bit timestamp
* @return NTP timestamp 64-bit long value as hexadecimal string with seconds separated by
* fractional seconds.
*/
public static String toString(final long ntpTime) {
final StringBuilder buf = new StringBuilder();
// high-order second bits (32..63) as hexstring
appendHexString(buf, ntpTime >>> 32 & 0xffffffffL);
// low-order fractional seconds bits (0..31) as hexstring
buf.append('.');
appendHexString(buf, ntpTime & 0xffffffffL);
return buf.toString();
}
/**
* NTP timestamp value: 64-bit unsigned fixed-point number as defined in RFC-1305 with high-order
* 32 bits the seconds field and the low-order 32-bits the fractional field.
*/
private final long ntpTime;
private DateFormat simpleFormatter;
private DateFormat utcFormatter;
/**
* Constructs a newly allocated NTP timestamp object that represents the Java Date argument.
*
* @param d - the Date to be represented by the Timestamp object.
*/
public TimeStamp(final Date d) {
ntpTime = d == null ? 0 : toNtpTime(d.getTime());
}
/**
* Constructs a newly allocated NTP timestamp object that represents the native 64-bit long
* argument.
*
* @param ntpTime the timestamp
*/
public TimeStamp(final long ntpTime) {
this.ntpTime = ntpTime;
}
/**
* Constructs a newly allocated NTP timestamp object that represents the value represented by the
* string in hexdecimal form (e.g. "c1a089bd.fc904f6d").
*
* @param hexStamp the hexadecimal timestamp
* @throws NumberFormatException - if the string does not contain a parsable timestamp.
*/
public TimeStamp(final String hexStamp) throws NumberFormatException {
ntpTime = decodeNtpHexString(hexStamp);
}
/**
* Compares two Timestamps numerically.
*
* @param anotherTimeStamp - the <code>TimeStamp</code> to be compared.
* @return the value <code>0</code> if the argument TimeStamp is equal to this TimeStamp; a value
* less than <code>0</code> if this TimeStamp is numerically less than the TimeStamp argument;
* and a value greater than <code>0</code> if this TimeStamp is numerically greater than the
* TimeStamp argument (signed comparison).
*/
@Override
public int compareTo(final TimeStamp anotherTimeStamp) {
final long thisVal = this.ntpTime;
final long anotherVal = anotherTimeStamp.ntpTime;
return Long.compare(thisVal, anotherVal);
}
/**
* Compares this object against the specified object. The result is {@code true} if and only if
* the argument is not {@code null} and is a <code>Long</code> object that contains the same
* <code>long</code> value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same; {@code false} otherwise.
*/
@Override
public boolean equals(final Object obj) {
if (obj instanceof TimeStamp) {
return ntpTime == ((TimeStamp) obj).ntpValue();
}
return false;
}
/**
* Converts NTP timestamp to Java Date object.
*
* @return NTP Timestamp in Java Date
*/
public Date getDate() {
return new Date(getTime(ntpTime));
}
/**
* Returns low-order 32-bits representing the fractional seconds.
*
* @return fractional seconds represented by this NTP timestamp.
*/
public long getFraction() {
return ntpTime & 0xffffffffL;
}
/**
* Returns high-order 32-bits representing the seconds of this NTP timestamp.
*
* @return seconds represented by this NTP timestamp.
*/
public long getSeconds() {
return ntpTime >>> 32 & 0xffffffffL;
}
/**
* Converts NTP timestamp to Java standard time.
*
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this NTP
* timestamp value.
*/
public long getTime() {
return getTime(ntpTime);
}
/**
* Computes a hash code for this Timestamp. The result is the exclusive OR of the two halves of
* the primitive <code>long</code> value represented by this <code>TimeStamp</code> object. That
* is, the hash code is the value of the expression:
*
* <blockquote>
*
* <pre>{@code
* (int) (this.ntpValue() ^ (this.ntpValue() >>> 32))
* }</pre>
*
* </blockquote>
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
return (int) (ntpTime ^ ntpTime >>> 32);
}
/**
* Returns the value of this Timestamp as a long value.
*
* @return the 64-bit long value represented by this object.
*/
public long ntpValue() {
return ntpTime;
}
private void readObject(final java.io.ObjectInputStream in) {
throw new UnsupportedOperationException("Serialization is not supported");
}
/**
* Converts this <code>TimeStamp</code> object to a <code>String</code> of the form:
*
* <blockquote>
*
* <pre>
* EEE, MMM dd yyyy HH:mm:ss.SSS
* </pre>
*
* </blockquote>
*
* See java.text.SimpleDataFormat for code descriptions.
*
* @return a string representation of this date.
*/
public String toDateString() {
if (simpleFormatter == null) {
simpleFormatter = new SimpleDateFormat(NTP_DATE_FORMAT, Locale.US);
simpleFormatter.setTimeZone(TimeZone.getDefault());
}
final Date ntpDate = getDate();
return simpleFormatter.format(ntpDate);
}
/**
* Converts this <code>TimeStamp</code> object to a <code>String</code>. The NTP timestamp 64-bit
* long value is represented as hexadecimal string with seconds separated by fractional seconds by
* a decimal point; e.g. c1a089bd.fc904f6d == Tue, Dec 10 2002 10:41:49.986
*
* @return NTP timestamp 64-bit long value as hexadecimal string with seconds separated by
* fractional seconds.
*/
@Override
public String toString() {
return toString(ntpTime);
}
/*
* Serialization is unnecessary for this class. Reject attempts to do so until such time as the Serializable attribute can be dropped.
*/
/**
* Converts this <code>TimeStamp</code> object to a <code>String</code> of the form:
*
* <blockquote>
*
* <pre>
* EEE, MMM dd yyyy HH:mm:ss.SSS UTC
* </pre>
*
* </blockquote>
*
* See java.text.SimpleDataFormat for code descriptions.
*
* @return a string representation of this date in UTC.
*/
public String toUTCString() {
if (utcFormatter == null) {
utcFormatter = new SimpleDateFormat(NTP_DATE_FORMAT + " 'UTC'", Locale.US);
utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
}
final Date ntpDate = getDate();
return utcFormatter.format(ntpDate);
}
private void writeObject(final java.io.ObjectOutputStream out) {
throw new UnsupportedOperationException("Serialization is not supported");
}
}