-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDateTools.java
More file actions
485 lines (433 loc) · 16.5 KB
/
Copy pathDateTools.java
File metadata and controls
485 lines (433 loc) · 16.5 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
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
/*
* #%L
* Common package for I/O and related utilities
* %%
* Copyright (C) 2005 - 2016 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
*
* 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 HOLDERS 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.
* #L%
*/
package loci.common;
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.IllegalInstantException;
import org.joda.time.Instant;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A utility class with convenience methods for working with dates.
*
* @author Curtis Rueden ctrueden at wisc.edu
* @author Chris Allan callan at blackcat.ca
* @author Melissa Linkert melissa at glencoesoftware.com
*/
public final class DateTools {
// -- Constants --
/** Timestamp formats. */
public static final int UNIX = 0; // January 1, 1970
public static final int COBOL = 1; // January 1, 1601
public static final int MICROSOFT = 2; // December 30, 1899
public static final int ZVI = 3;
public static final int ALT_ZVI = 4;
/** Milliseconds until UNIX epoch. */
public static final long UNIX_EPOCH = 0;
public static final long COBOL_EPOCH = 11644473600000L;
public static final long MICROSOFT_EPOCH = 2209143600000L;
public static final long ZVI_EPOCH = 2921084975759000L;
public static final long ALT_ZVI_EPOCH = 2921084284761000L;
/** ISO 8601 date output formatter with milliseconds. */
public static final String ISO8601_FORMAT_MS = "yyyy-MM-dd'T'HH:mm:ss.SSS";
/** ISO 8601 date output formatter without milliseconds. */
public static final String ISO8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
/** Human readable timestamp string */
public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss";
/** Human readable timestamp filename string */
public static final String FILENAME_FORMAT = "yyyy-MM-dd_HH-mm-ss";
/** ISO 8601 date formatter with milliseconds. */
private static final DateTimeFormatter ISO8601_FORMATTER_MS =
DateTimeFormat.forPattern(ISO8601_FORMAT_MS);
/** ISO 8601 date formatter without milliseconds. */
private static final DateTimeFormatter ISO8601_FORMATTER =
DateTimeFormat.forPattern(ISO8601_FORMAT);
/** Human readable timestamp formatter. */
private static final DateTimeFormatter TIMESTAMP_FORMATTER =
DateTimeFormat.forPattern(TIMESTAMP_FORMAT);
/** Human readable timestamp filename formatter. */
private static final DateTimeFormatter FILENAME_FORMATTER =
DateTimeFormat.forPattern(FILENAME_FORMAT);
/** Logger for this class. */
private static final Logger LOGGER = LoggerFactory.getLogger(DateTools.class);
// -- Constructor --
private DateTools() { }
// -- Date handling --
/**
* Converts from two-word tick representation to milliseconds.
* Mainly useful in conjunction with COBOL date conversion.
*
* @param hi the upper 32 bits of the tick count
* @param lo the lower 32 bits of the tick count
* @return the number of milliseconds corresponding to the tick count,
* where 1 tick = 100 ns
*/
public static long getMillisFromTicks(long hi, long lo) {
long ticks = ((hi << 32) | lo);
return ticks / 10000; // 100 ns = 0.0001 ms
}
/**
* Converts the given timestamp into an ISO8601 date.
*
* @param stamp the format-dependent timestamp in milliseconds
* @param format the format in which <code>stamp</code> is stored.
* This is used to select the epoch value used for normalizing
* the timestamp to milliseconds since the UNIX epoch. Valid
* values are #UNIX, #COBOL, #MICROSOFT, #ZVI, and #ALT_ZVI.
* @return an ISO 8601 formatted timestamp
* @see #UNIX_EPOCH
* @see #COBOL_EPOCH
* @see #MICROSOFT_EPOCH
* @see #ZVI_EPOCH
* @see #ALT_ZVI_EPOCH
*/
public static String convertDate(long stamp, int format) {
return convertDate(stamp, format, ISO8601_FORMAT);
}
/**
* Converts the given timestamp into a date string with the given format.
*
* @param stamp the format-dependent timestamp in milliseconds
* @param format the format in which <code>stamp</code> is stored.
* This is used to select the epoch value used for normalizing
* the timestamp to milliseconds since the UNIX epoch. Valid
* values are #UNIX, #COBOL, #MICROSOFT, #ZVI, and #ALT_ZVI.
* @param outputFormat the pattern used for formatting the timestamp
* @return a timestamp in the specified output format
* @see #UNIX_EPOCH
* @see #COBOL_EPOCH
* @see #MICROSOFT_EPOCH
* @see #ZVI_EPOCH
* @see #ALT_ZVI_EPOCH
* @see DateTimeFormat
*/
public static String convertDate(long stamp, int format, String outputFormat)
{
return convertDate(stamp, format, outputFormat, false);
}
/**
* Converts the given timestamp into a date string with the given format.
*
* If correctTimeZoneForGMT is set, then the timestamp will be interpreted
* as being relative to GMT and not the local time zone.
*
* @param stamp the format-dependent timestamp in milliseconds
* @param format the format in which <code>stamp</code> is stored.
* This is used to select the epoch value used for normalizing
* the timestamp to milliseconds since the UNIX epoch. Valid
* values are #UNIX, #COBOL, #MICROSOFT, #ZVI, and #ALT_ZVI.
* @param outputFormat the pattern used for formatting the timestamp
* @param correctTimeZoneForGMT true if the timestamp is relative to GMT
* @return a timestamp in the specified output format
* @see #UNIX_EPOCH
* @see #COBOL_EPOCH
* @see #MICROSOFT_EPOCH
* @see #ZVI_EPOCH
* @see #ALT_ZVI_EPOCH
* @see DateTimeFormat
*/
public static String convertDate(long stamp, int format, String outputFormat,
boolean correctTimeZoneForGMT)
{
// see http://www.merlyn.demon.co.uk/critdate.htm for more information on
// dates than you will ever need (or want)
long ms = stamp;
switch (format) {
case UNIX:
ms -= UNIX_EPOCH;
break;
case COBOL:
ms -= COBOL_EPOCH;
break;
case MICROSOFT:
ms -= MICROSOFT_EPOCH;
break;
case ZVI:
ms -= ZVI_EPOCH;
break;
case ALT_ZVI:
ms -= ALT_ZVI_EPOCH;
break;
}
final DateTimeFormatter fmt = DateTimeFormat.forPattern(outputFormat);
try {
if (correctTimeZoneForGMT) {
DateTimeZone tz = DateTimeZone.getDefault();
ms = tz.convertLocalToUTC(ms, false);
}
}
catch (ArithmeticException e) {}
catch (IllegalInstantException e) {}
DateTime d = new DateTime(ms, DateTimeZone.UTC);
return fmt.print(d);
}
/**
* Parse the given date as a Joda instant
*
* @param date The date to parse as a Joda timestamp
* @param format The date format to parse the string date
* @param separator The separator for milliseconds
* @return the Joda Instant object representing the timestamp
* @see Instant
*/
protected static Instant parseDate(String date, String format, String separator) {
long ms = 0L;
int msSeparator = 0;
String newDate = date.trim();
if (separator != null) {
msSeparator = date.lastIndexOf(separator);
}
if (msSeparator > 0) {
newDate = date.substring(0, msSeparator);
String msString = date.substring(msSeparator + 1);
// Handle formats ending with another pattern, e.g. "SSS aa" or "SSSZ"
int postmsSeparator = 0;
for (int pos=0; pos<msString.length(); pos++) {
if (!Character.isDigit(msString.charAt(pos))) {
postmsSeparator = pos;
break;
}
}
if (postmsSeparator > 0) {
try {
ms = Long.parseLong(msString.substring(0, postmsSeparator));
}
catch (NumberFormatException e) {
LOGGER.debug("Failed to parse milliseconds from '{}'", ms);
}
newDate = newDate + msString.substring(postmsSeparator);
}
else {
try {
ms = Long.parseLong(msString);
}
catch (NumberFormatException e) {
LOGGER.debug("Failed to parse milliseconds from '{}'", ms);
}
}
}
final DateTimeFormatter parser =
DateTimeFormat.forPattern(format).withZone(DateTimeZone.UTC);
Instant timestamp = null;
try {
timestamp = Instant.parse(newDate, parser);
timestamp = timestamp.plus(ms);
}
catch (IllegalArgumentException e) {
LOGGER.debug("Invalid timestamp '{}' for current locale", date);
// if the date couldn't be parsed with the current locale,
// try to parse with an English-language locale
// this is helpful for dates that use a three-letter month
DateTimeFormatter usParser = parser.withLocale(Locale.US);
try {
timestamp = Instant.parse(newDate, usParser);
timestamp = timestamp.plus(ms);
}
catch (IllegalArgumentException|UnsupportedOperationException exc) {
LOGGER.debug("Could not parse timestamp '{}' with EN_US locale",
date, exc);
}
}
catch (UnsupportedOperationException e) {
LOGGER.debug("Error parsing timestamp '{}'", date, e);
}
return timestamp;
}
/**
* Formats the given date as an ISO 8601 date.
* Delegates to {@link #formatDate(String, String, boolean)}, with the
* {@code lenient} flag set to false.
*
* @param date The date to format as ISO 8601
* @param format The date format to parse the string date
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String format) {
return formatDate(date, format, false);
}
/**
* Formats the given date as an ISO 8601 date.
* Delegates to {@link #formatDate(String, String, boolean, String)} with the
* {@code lenient} flag set to false.
*
* @param date The date to format as ISO 8601
* @param format The date format to parse the string date
* @param separator The separator for milliseconds
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String format, String separator) {
return formatDate(date, format, false, separator);
}
/**
* Formats the given date as an ISO 8601 date.
*
* @param date The date to format as ISO 8601.
* @param format The date format to parse the string date
* @param lenient Whether or not to leniently parse the date.
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String format, boolean lenient) {
return formatDate(date, format, false, null);
}
/**
* Formats the given date as an ISO 8601 date.
*
* @param date The date to format as ISO 8601
* @param format The date format to parse the string date
* @param lenient Whether or not to leniently parse the date.
* @param separator The separator for milliseconds
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String format, boolean lenient, String separator) {
if (date == null) return null;
Instant timestamp = parseDate(date, format, separator);
if (timestamp == null) {
return null;
}
final DateTimeFormatter isoformat;
if ((timestamp.getMillis() % 1000) != 0) {
isoformat = ISO8601_FORMATTER_MS;
}
else {
isoformat = ISO8601_FORMATTER;
}
return isoformat.print(timestamp);
}
/**
* Formats the given date as an ISO 8601 date.
*
* Delegates to {@link #formatDate(String, String[], boolean, String)} with
* {@code lenient} set to false and {@code separator} set to null.
*
* @param date The date to format as ISO 8601
* @param formats The date possible formats to parse the string date
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String[] formats) {
return formatDate(date, formats, false, null);
}
/**
* Formats the given date as an ISO 8601 date.
*
* Delegates to {@link #formatDate(String, String[], boolean, String)} with
* {@code separator} set to null.
*
* @param date The date to format as ISO 8601.
* @param formats The date possible formats to parse the string date
* @param lenient Whether or not to leniently parse the date.
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String[] formats,
boolean lenient)
{
return formatDate(date, formats, lenient, null);
}
/**
* Formats the given date as an ISO 8601 date.
* Delegates to {@link #formatDate(String, String[], boolean, String)} with
* {@code lenient} set to false.
*
* @param date The date to format as ISO 8601
* @param formats The date possible formats to parse the string date
* @param separator The separator for milliseconds
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String[] formats, String separator) {
return formatDate(date, formats, false, separator);
}
/**
* Formats the given date as an ISO 8601 date.
*
* @param date The date to format as ISO 8601.
* @param formats The date possible formats to parse the string date
* @param lenient Whether or not to leniently parse the date.
* @param separator The separator for milliseconds
* @return an ISO 8601 formatted timestamp
*/
public static String formatDate(String date, String[] formats,
boolean lenient, String separator)
{
for (int i=0; i<formats.length; i++) {
String result = formatDate(date, formats[i], lenient, separator);
if (result != null) return result;
}
return null;
}
/**
* Converts a string date in the given format to a long timestamp
* (in Unix format: milliseconds since January 1, 1970).
*
* @param date The date to convert
* @param format The date format to parse the string date
* @return The date in milliseconds
*/
public static long getTime(String date, String format) {
return getTime(date, format, null);
}
/**
* Converts a string date in the given format to a long timestamp
* (in Unix format: milliseconds since January 1, 1970) with special
* milliseconds handling.
*
* @param date The date to convert
* @param format The date format to parse the string date
* @param separator The separator for milliseconds
* @return The date in milliseconds
*/
public static long getTime(String date, String format, String separator) {
Instant timestamp = parseDate(date, format, separator);
if (timestamp == null) {
return -1;
}
else {
return timestamp.getMillis();
}
}
/**
* @return a timestamp for the current timezone in a
* human-readable locale-independent format ("YYYY-MM-DD HH:MM:SS")
*/
public static String getTimestamp() {
return TIMESTAMP_FORMATTER.print(new DateTime());
}
/**
* @return a timestamp for the current timezone in a format suitable
* for a filename in a locale-independent format
* ("YYYY-MM-DD_HH-MM-SS")
*/
public static String getFileTimestamp() {
return FILENAME_FORMATTER.print(new DateTime());
}
}