-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIso8601DateFormat.java
More file actions
649 lines (570 loc) · 18.1 KB
/
Iso8601DateFormat.java
File metadata and controls
649 lines (570 loc) · 18.1 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
* Copyright (c) 2016. SocialEmergence.org
* This code is released under the MIT License
* https://opensource.org/licenses/MIT
*/
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.*;
/**
* Implementation of ISO-8601 date and time format. This version
* is more complete/correct than either Java's java.text.SimpleDateFormat
* and Jackson's ISO8601Utils ond ISO8601DateFormat. It is about 5x faster
* than SimpleDateFormat and 2x faster than Jackson.
* <p>
* Full implementation of dates and times. Intervals are not implemented.
*
* @author Philip DeCamp
*/
public class Iso8601DateFormat extends DateFormat {
public static final TimeZone UTC = TimeZone.getTimeZone( "UTC" );
private static final Iso8601DateFormat INSTANCE = new Iso8601DateFormat();
/**
* Parses a ISO8601 string and converts to a
* unix epoch millisecond using a static singleton
* instance of this class.
*
* @param str ISO8601 formatted timestamp
* @return equivalent timestamp in unix time milliseconds
*/
public static Long stringToMillis( String str ) {
return INSTANCE.parseToMillis( str );
}
/**
* Converts a unix epoch millisecond timestamp into ISO8601
* format using a static singleton instance of this class.
* @see Iso8601DateFormat#format(long)
* @param t Unix timestamp in milliseconds
* @return equivalent timestamp in ISO8601 format
*/
public static String millisToString( long t ) {
return INSTANCE.format( t );
}
public static void format( Calendar cal, StringBuilder out ) {
int zone = cal.get( Calendar.ZONE_OFFSET );
out.ensureCapacity( out.length() + zone == 0 ? 24 : 29 );
formatNum( cal.get( Calendar.YEAR ), 4, out );
out.append( '-' );
formatNum( cal.get( Calendar.MONTH ) + 1, 2, out );
out.append( '-' );
formatNum( cal.get( Calendar.DAY_OF_MONTH ), 2, out );
out.append( 'T' );
formatNum( cal.get( Calendar.HOUR_OF_DAY ), 2, out );
out.append( ':' );
formatNum( cal.get( Calendar.MINUTE ), 2, out );
out.append( ':' );
formatNum( cal.get( Calendar.SECOND ), 2, out );
out.append( '.' );
formatNum( cal.get( Calendar.MILLISECOND ), 3, out );
if( zone == 0 ) {
out.append( 'Z' );
return;
}
boolean positive = true;
if( zone < 0 ) {
positive = false;
zone = -zone;
}
if( zone == 0 ) {
out.append( 'Z' );
return;
}
int hour = zone / ( 60 * 60 * 1000 );
int minute = ( zone % ( 60 * 60 * 1000 ) ) / ( 60 * 1000 );
if( hour == 0 && minute == 0 ) {
out.append( 'Z' );
return;
}
out.append( positive ? '+' : '-' );
formatNum( hour, 2, out );
out.append( ':' );
formatNum( minute, 2, out );
}
/**
* Parses string containing ISO8601 formatted value. Valid formats include: <ul>
* <li>[date]
* <li>[date]T[time]
* <li>[date]T[time][zone]
* </ul>
* Where: <ul>
* <li>[date] = Date component (see {@link #parseDate})
* <li>T = Time component indicator. May be 'T', 't', or ' ', although only 'T' is 8601 compliant.
* <li>[time] = Time component (see {@link #parseTime})
* <li>[zone] = Zone Offset component( see {@link #parseZoneOffset}).
* </ul>
*
* @param s Input string.
* @param off Offset into string.
* @param out Calendar to use for parsing. WILL be cleared.
* @return On success, position where parsing finished (always last character of string).
* On error, (-errPos-1), where errPos is the string position where parsing failed.
*
* @see #parseDate
* @see #parseTime
* @see #parseZoneOffset
*/
public static int parse( CharSequence s, final int off, Calendar out ) {
int pos = off;
int end = s.length();
out.clear();
pos = parseDate( s, pos, out );
if( pos < 0 || pos == end ) {
return pos;
}
// Check for date-time separator.
// Formally, we should only accept a 'T' char, but
// I'm also allowing a lower-case 't' or a space.
switch( s.charAt( pos++ ) ) {
case ' ':
case 'T':
case 't':
pos = parseTime( s, pos, out );
// Return if error or no more data.
if( pos < 0 || pos == end ) {
return pos;
}
pos = parseZoneOffset( s, pos, out );
// Return if error or no more data.
if( pos < 0 || pos == end ) {
return pos;
}
return -pos - 1;
default:
return -pos;
}
}
/**
* Parses date component. The following formats are valid: <ul>
* <li>yyyy
* <li>yyyy-MM
* <li>yyyy-MM-dd
* <li>yyyy-'W'ww
* <li>yyyy-'W'ww-d
* <li>yyyy-ddd
* <li>yyyyMMdd
* <li>yyyy'W'ww
* <li>yyyy'W'wwd
* <li>yyyyDDD
* </ul>
* Where: <ul>
* <li>y = year digit
* <li>M = month digit
* <li>w = week digit ('W' means the letter W)
* <li>D = day-of-year digit
* <li>d = day-of-month digit
* </ul>
* Note that yyyyMM is not a valid format!
*
* @param s Input string
* @param pos Position into string.
* @param out Calendar to receive [YEAR,MONTH,WEEK,DAY_OF_YEAR,DAY_OF_MONTH] values.
* These fields are assumed to be clear, otherwise results are undefined.
* @return On success, position where parsing ended.
* On error, (-errPos - 1), where errPos is the string position where parsing failed.
*/
public static int parseDate( CharSequence s, int pos, Calendar out ) {
int end = s.length();
int val = parseNum( s, pos, 4 );
if( val < 0 ) {
// Error. No year.
return val;
}
out.set( Calendar.YEAR, val );
pos += 4;
if( pos == end ) {
// Done at year.
return pos;
}
// Check if dashes are being used between date components.
boolean useDash = s.charAt( pos++ ) == '-';
if( !useDash ) {
pos--;
}
char c = s.charAt( pos );
if( c == 'w' || c == 'W' ) {
// Week format: yyyy-Www OR yyyyWww
val = parseNum( s, ++pos, 2 );
if( val < 0 ) {
// Invalid week. Error.
return val;
}
pos += 2;
out.set( Calendar.WEEK_OF_YEAR, val );
if( pos == end ) {
// Done at week.
return pos;
}
// Day of week format:
// yyyy-Www-d OR yyyyWwwd
if( useDash ) {
if( s.charAt( pos++ ) != '-' ) {
// Stop at week.
return pos - 1;
}
val = parseNum( s, pos, 1 );
if( val < 0 ) {
// Invalid day. Error.
return val;
}
} else {
val = parseNum( s, pos, 1 );
if( val < 0) {
// No error.
return pos;
}
}
// Done at day.
pos++;
val = ( val % 7 ) + 1;
out.set( Calendar.DAY_OF_WEEK, val );
return pos;
}
// Parse month (MM) OR day-of-year (ddd).
// Possible formats:
// yyyy-MM, yyyyMMdd, yyyy-MM-dd
// yyyyDDD, yyyy-DDD
// NOT yyyyMM (NB, the earlier check of length >= 7 will catch this case)
switch( countDigits( s, pos, 4 ) ) {
case 0:
// Done at year.
return pos;
case 1:
// Invalid month or day-of-year value. Error.
return -pos-2;
case 2:
case 4:
// Two-digit month
val = parseNum( s, pos, 2 );
pos += 2;
out.set( Calendar.MONTH, val - 1 );
break;
case 3:
// Three-digit day-of-year
val = parseNum( s, pos, 3 );
pos += 3;
out.set( Calendar.DAY_OF_MONTH, val );
// Done at day-of-year.
return pos;
}
if( pos == end ) {
// Done at month.
return pos;
}
// Parse day of month.
if( useDash ) {
if( s.charAt( pos++ ) != '-' ) {
// Done at month.
return pos -1;
}
val = parseNum( s, pos, 2 );
if( val < 0 ) {
// Invalid day. Error.
return val;
}
} else {
val = parseNum( s, pos, 2 );
if( val < 0 ) {
// Done at month.
return pos;
}
}
// Done at day-of-month.
pos += 2;
out.set( Calendar.DAY_OF_MONTH, val );
return pos;
}
/**
* Parses time component. The following formats are valid: <ul>
* <li>HH
* <li>HH:mm
* <li>HH:mm:ss
* <li>HH:mm:ss.SSS
* <li>HHmm
* <li>HHmmss
* <li>HHmmss.SSS
* </ul>
* Where: <ul>
* <li>H = hour-of-day digit
* <li>m = minute digit
* <li>s = second digit
* <li>S = millisecond digit
* </ul>
*
* @param s Input string
* @param pos Position into string.
* @param out Calendar to receive [HOUR_OF_DAY,MINUTE,SECOND,MILLISECOND] values.
* These fields are assumed to be clear, otherwise results are undefined.
* @return On success, position where parsing ended.
* On error, (-errPos - 1), where errPos is the string position where parsing failed.
*/
public static int parseTime( CharSequence s, int pos, Calendar out ) {
final int end = s.length();
// Parse hour.
int val = parseNum( s, pos, 2 );
if( val < 0 ) {
// Invalid hour. Error.
return val;
}
pos += 2;
out.set( Calendar.HOUR_OF_DAY, val );
// Check if done.
if( pos == end ) {
return pos;
}
// Check if using colon separators.
boolean useColons = s.charAt( pos++ ) == ':';
if( useColons ) {
val = parseNum( s, pos, 2 );
if( val < 0 ) {
// Invalid minute. Error.
return val;
}
} else {
val = parseNum( s, --pos, 2 );
if( val < 0 ) {
// Done at hour. No error.
return pos;
}
}
pos += 2;
out.set( Calendar.MINUTE, val );
if( pos == end ) {
// Done at minute.
return pos;
}
if( useColons ) {
if( s.charAt( pos++ ) != ':' ) {
// Done at minute.
return pos - 1;
}
val = parseNum( s, pos, 2 );
if( val < 0 ) {
// Invalid minute. Error.
return val;
}
} else {
val = parseNum( s, pos, 2 );
if( val < 0 ) {
// Done at minute.
return pos;
}
}
pos += 2;
out.set( Calendar.SECOND, val );
if( pos == end || s.charAt( pos ) != '.' ) {
// Done at seconds.
return pos;
}
val = parseNum( s, ++pos, 3 );
if( val < 0 ) {
// Invalid milliseconds. Error.
return val;
}
pos += 3;
out.set( Calendar.MILLISECOND, val );
return pos;
}
/**
* Parses zone offset component. The following formats are valid: <ul>
* <li>Z
* <li>z (not 8601 compliant, but accepted here)
* <li>±HH:mm
* <li>±HHmm
* </ul>
* Where: <ul>
* <li>Z = the letter 'z', indicating no offset.
* <li>± = a plus or minus, indicating sign of offset.
* <li>H = hour digit
* <li>m = minute digit
* </ul>
*
* @param s Input string
* @param pos Position into string.
* @param out Calendar to receive [ZONE_OFFSET] value.
* @return On success, position where parsing ended.
* On error, (-errPos - 1), where errPos is the string position where parsing failed.
*/
public static int parseZoneOffset( CharSequence s, int pos, Calendar out ) {
final int end = s.length();
if( pos == end ) {
// Error. No data.
return -pos - 1;
}
int sign = -1;
switch( s.charAt( pos++ ) ) {
case 'z':
case 'Z':
// Zulu / UTC time.
return pos;
case '+':
sign = 1;
case '-':
break;
default:
// Invalid character.
return -pos;
}
int val = parseNum( s, pos, 2 );
if( val < 0 ) {
// No hour. Error.
return val;
}
pos += 2;
int offset = val * ( 60 * 60 * 1000 );
if( pos == end ) {
// Done at hour.
out.set( Calendar.ZONE_OFFSET, sign * offset );
return pos;
}
if( s.charAt( pos++ ) == ':' ) {
val = parseNum( s, pos, 2 );
if( val < 0 ) {
// Invalid minute. Error.
return -pos;
}
} else {
val = parseNum( s, --pos, 2 );
if( val < 0 ) {
// Done at hour.
out.set( Calendar.ZONE_OFFSET, sign * offset );
return pos;
}
}
pos += 2;
offset += val * ( 60 * 1000 );
out.set( Calendar.ZONE_OFFSET, sign * offset );
// Done at minute.
return pos;
}
private static int parseNum( CharSequence s, int off, int len ) {
if( off + len > s.length() ) {
return -s.length() - 1;
}
int ret = 0;
for( int i = 0; i < len; i++ ) {
int d = s.charAt( off + i ) - '0';
if( d < 0 || 9 < d ) {
return -(off + i + 1);
}
ret = 10 * ret + d;
}
return ret;
}
private static int countDigits( CharSequence s, int off, int max ) {
final int end = Math.min( s.length(), off + max );
for( int i = 0; i < end - off; i++ ) {
char c = s.charAt( i + off );
if( c < '0' || '9' < c ) {
return i;
}
}
return end - off;
}
private static void formatNum( int num, int len, StringBuilder out ) {
String s;
if( num < 0 ) {
s = "";
} else {
s = String.valueOf( num );
}
int lead = len - s.length();
if( lead >= 0 ) {
while( lead-- > 0 ) {
out.append( '0' );
}
out.append( s );
} else {
for( int i = 0; i < len; i++ ) {
out.append( '9' );
}
}
}
private TimeZone mZone = UTC;
private final Calendar mCalendar = new GregorianCalendar( UTC, Locale.US );
public synchronized Long parseToMillis( CharSequence s ) {
int err = parse( s, 0, mCalendar );
return err >= 0 ? mCalendar.getTimeInMillis() : null;
}
public String format( long millis ) {
StringBuilder ret = new StringBuilder( 29 );
format( millis, ret );
return ret.toString();
}
public synchronized void format( long millis, StringBuilder out ) {
mCalendar.setTimeZone( mZone );
mCalendar.setTimeInMillis( millis );
format( mCalendar, out );
}
@Override
public StringBuffer format( Date date, StringBuffer toAppendTo, FieldPosition field ) {
StringBuilder s = new StringBuilder();
format( date.getTime(), s );
final int off = s.length();
toAppendTo.append( s );
switch( field.getField() ) {
case YEAR_FIELD:
field.setBeginIndex( off + 0 );
field.setEndIndex( off + 4 );
break;
case MONTH_FIELD:
field.setBeginIndex( off + 5 );
field.setEndIndex( off + 7 );
break;
case DATE_FIELD:
field.setBeginIndex( off + 8 );
field.setEndIndex( off + 10 );
break;
case HOUR_OF_DAY0_FIELD:
field.setBeginIndex( off + 11 );
field.setEndIndex( off + 13 );
break;
case MINUTE_FIELD:
field.setBeginIndex( off + 14 );
field.setEndIndex( off + 16 );
break;
case SECOND_FIELD:
field.setBeginIndex( off + 17 );
field.setEndIndex( off + 19 );
break;
case MILLISECOND_FIELD:
field.setBeginIndex( off + 20 );
field.setEndIndex( off + 23 );
break;
case TIMEZONE_FIELD:
field.setBeginIndex( off + 23 );
field.setEndIndex( s.length() );
default:
field.setBeginIndex( off + 0 );
field.setEndIndex( off + 0 );
}
return toAppendTo;
}
@Override
public synchronized Date parse( String source, ParsePosition pos ) {
int err = parse( source, pos.getIndex(), mCalendar );
if( err >= 0 ) {
pos.setIndex( err );
return new Date( mCalendar.getTimeInMillis() );
}
pos.setErrorIndex( -err - 1 );
return null;
}
@Override
public synchronized Date parseObject( String source ) throws ParseException {
int err = parse( source, 0, mCalendar );
if( err >= 0 ) {
return new Date( mCalendar.getTimeInMillis() );
}
throw new ParseException( "Iso8601DateFormat.parseObject(String) failed", -err - 1 );
}
@Override
public synchronized void setTimeZone( TimeZone zone ) {
mZone = zone;
}
@Override
public TimeZone getTimeZone() {
return mZone;
}
}