-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
DateTimeFunctions.java
1031 lines (927 loc) · 46.2 KB
/
DateTimeFunctions.java
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed 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 com.facebook.presto.operator.scalar;
import com.facebook.presto.operator.Description;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.type.StandardTypes;
import com.facebook.presto.type.SqlType;
import com.facebook.presto.util.ThreadLocalCache;
import com.google.common.primitives.Ints;
import io.airlift.slice.Slice;
import org.joda.time.DateTime;
import org.joda.time.DateTimeField;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.ISODateTimeFormat;
import java.util.Locale;
import static com.facebook.presto.operator.scalar.QuarterOfYearDateTimeField.QUARTER_OF_YEAR;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.spi.type.DateTimeEncoding.packDateTimeWithZone;
import static com.facebook.presto.spi.type.DateTimeEncoding.unpackMillisUtc;
import static com.facebook.presto.spi.type.DateTimeEncoding.unpackZoneKey;
import static com.facebook.presto.spi.type.DateTimeEncoding.updateMillisUtc;
import static com.facebook.presto.spi.type.TimeZoneKey.getTimeZoneKeyForOffset;
import static com.facebook.presto.type.DateTimeOperators.modulo24Hour;
import static com.facebook.presto.util.DateTimeZoneIndex.extractZoneOffsetMinutes;
import static com.facebook.presto.util.DateTimeZoneIndex.getChronology;
import static com.facebook.presto.util.DateTimeZoneIndex.packDateTimeWithZone;
import static com.facebook.presto.util.DateTimeZoneIndex.unpackChronology;
import static com.facebook.presto.util.Failures.checkCondition;
import static io.airlift.slice.Slices.utf8Slice;
import static java.util.Locale.ENGLISH;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.joda.time.DateTimeZone.UTC;
public final class DateTimeFunctions
{
private static final ThreadLocalCache<Slice, DateTimeFormatter> DATETIME_FORMATTER_CACHE = new ThreadLocalCache<Slice, DateTimeFormatter>(100)
{
@Override
protected DateTimeFormatter load(Slice format)
{
return createDateTimeFormatter(format);
}
};
private static final ISOChronology UTC_CHRONOLOGY = ISOChronology.getInstance(UTC);
private static final DateTimeField SECOND_OF_MINUTE = UTC_CHRONOLOGY.secondOfMinute();
private static final DateTimeField DAY_OF_WEEK = UTC_CHRONOLOGY.dayOfWeek();
private static final DateTimeField DAY_OF_MONTH = UTC_CHRONOLOGY.dayOfMonth();
private static final DateTimeField DAY_OF_YEAR = UTC_CHRONOLOGY.dayOfYear();
private static final DateTimeField WEEK_OF_YEAR = UTC_CHRONOLOGY.weekOfWeekyear();
private static final DateTimeField YEAR_OF_WEEK = UTC_CHRONOLOGY.weekyear();
private static final DateTimeField MONTH_OF_YEAR = UTC_CHRONOLOGY.monthOfYear();
private static final DateTimeField QUARTER = QUARTER_OF_YEAR.getField(UTC_CHRONOLOGY);
private static final DateTimeField YEAR = UTC_CHRONOLOGY.year();
private static final int MILLISECONDS_IN_SECOND = 1000;
private static final int MILLISECONDS_IN_MINUTE = 60 * MILLISECONDS_IN_SECOND;
private static final int MILLISECONDS_IN_HOUR = 60 * MILLISECONDS_IN_MINUTE;
private static final int MILLISECONDS_IN_DAY = 24 * MILLISECONDS_IN_HOUR;
private DateTimeFunctions() {}
@Description("current date")
@ScalarFunction
@SqlType(StandardTypes.DATE)
public static long currentDate(ConnectorSession session)
{
long millis = getChronology(session.getTimeZoneKey()).dayOfMonth().roundFloor(session.getStartTime());
return MILLISECONDS.toDays(millis);
}
@Description("current time with time zone")
@ScalarFunction
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long currentTime(ConnectorSession session)
{
// Stack value is number of milliseconds from start of the current day,
// but the start of the day is relative to the current time zone.
long millis = getChronology(session.getTimeZoneKey()).millisOfDay().get(session.getStartTime());
return packDateTimeWithZone(millis, session.getTimeZoneKey());
}
@Description("current time without time zone")
@ScalarFunction("localtime")
@SqlType(StandardTypes.TIME)
public static long localTime(ConnectorSession session)
{
// Stack value is number of milliseconds from start of the current day,
// but the start of the day is relative to the current time zone.
return getChronology(session.getTimeZoneKey()).millisOfDay().get(session.getStartTime());
}
@Description("current time zone")
@ScalarFunction("current_timezone")
@SqlType(StandardTypes.VARCHAR)
public static Slice currentTimeZone(ConnectorSession session)
{
return utf8Slice(session.getTimeZoneKey().getId());
}
@Description("current timestamp with time zone")
@ScalarFunction(value = "current_timestamp", alias = "now")
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long currentTimestamp(ConnectorSession session)
{
return packDateTimeWithZone(session.getStartTime(), session.getTimeZoneKey());
}
@Description("current timestamp without time zone")
@ScalarFunction("localtimestamp")
@SqlType(StandardTypes.TIMESTAMP)
public static long localTimestamp(ConnectorSession session)
{
return session.getStartTime();
}
@ScalarFunction("from_unixtime")
@SqlType(StandardTypes.TIMESTAMP)
public static long fromUnixTime(@SqlType(StandardTypes.DOUBLE) double unixTime)
{
return Math.round(unixTime * 1000);
}
@ScalarFunction("from_unixtime")
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long fromUnixTime(@SqlType(StandardTypes.DOUBLE) double unixTime, @SqlType(StandardTypes.BIGINT) long hoursOffset, @SqlType(StandardTypes.BIGINT) long minutesOffset)
{
return packDateTimeWithZone(Math.round(unixTime * 1000), (int) (hoursOffset * 60 + minutesOffset));
}
@ScalarFunction("to_unixtime")
@SqlType(StandardTypes.DOUBLE)
public static double toUnixTime(@SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return timestamp / 1000.0;
}
@ScalarFunction("to_unixtime")
@SqlType(StandardTypes.DOUBLE)
public static double toUnixTimeFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackMillisUtc(timestampWithTimeZone) / 1000.0;
}
@ScalarFunction("to_iso8601")
@SqlType(StandardTypes.VARCHAR)
public static Slice toISO8601FromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
DateTimeFormatter formatter = ISODateTimeFormat.dateTime()
.withChronology(getChronology(session.getTimeZoneKey()));
return utf8Slice(formatter.print(timestamp));
}
@ScalarFunction("to_iso8601")
@SqlType(StandardTypes.VARCHAR)
public static Slice toISO8601FromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
long millisUtc = unpackMillisUtc(timestampWithTimeZone);
DateTimeFormatter formatter = ISODateTimeFormat.dateTime()
.withChronology(getChronology(unpackZoneKey(timestampWithTimeZone)));
return utf8Slice(formatter.print(millisUtc));
}
@ScalarFunction("to_iso8601")
@SqlType(StandardTypes.VARCHAR)
public static Slice toISO8601FromDate(ConnectorSession session, @SqlType(StandardTypes.DATE) long date)
{
DateTimeFormatter formatter = ISODateTimeFormat.date()
.withChronology(UTC_CHRONOLOGY);
return utf8Slice(formatter.print(DAYS.toMillis(date)));
}
@ScalarFunction("from_iso8601_timestamp")
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long fromISO8601Timestamp(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice iso8601DateTime)
{
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser()
.withChronology(getChronology(session.getTimeZoneKey()))
.withOffsetParsed();
return packDateTimeWithZone(parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8()));
}
@ScalarFunction("from_iso8601_date")
@SqlType(StandardTypes.DATE)
public static long fromISO8601Date(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice iso8601DateTime)
{
DateTimeFormatter formatter = ISODateTimeFormat.dateElementParser()
.withChronology(UTC_CHRONOLOGY);
DateTime dateTime = parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8());
return MILLISECONDS.toDays(dateTime.getMillis());
}
@ScalarFunction(value = "at_timezone", hidden = true)
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long timeAtTimeZone(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone, @SqlType(StandardTypes.VARCHAR) Slice zoneId)
{
return packDateTimeWithZone(unpackMillisUtc(timeWithTimeZone), zoneId.toStringUtf8());
}
@ScalarFunction(value = "at_timezone", hidden = true)
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long timeAtTimeZone(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long zoneOffset)
{
checkCondition((zoneOffset % 60_000) == 0, INVALID_FUNCTION_ARGUMENT, "Invalid time zone offset interval: interval contains seconds");
int zoneOffsetMinutes = (int) (zoneOffset / 60_000);
return packDateTimeWithZone(unpackMillisUtc(timeWithTimeZone), getTimeZoneKeyForOffset(zoneOffsetMinutes));
}
@ScalarFunction(value = "at_timezone", hidden = true)
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long timestampAtTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone, @SqlType(StandardTypes.VARCHAR) Slice zoneId)
{
return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), zoneId.toStringUtf8());
}
@ScalarFunction(value = "at_timezone", hidden = true)
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long timestampAtTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long zoneOffset)
{
checkCondition((zoneOffset % 60_000) == 0, INVALID_FUNCTION_ARGUMENT, "Invalid time zone offset interval: interval contains seconds");
int zoneOffsetMinutes = (int) (zoneOffset / 60_000);
return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), getTimeZoneKeyForOffset(zoneOffsetMinutes));
}
@Description("truncate to the specified precision in the session timezone")
@ScalarFunction("date_trunc")
@SqlType(StandardTypes.DATE)
public static long truncateDate(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.DATE) long date)
{
long millis = getDateField(UTC_CHRONOLOGY, unit).roundFloor(DAYS.toMillis(date));
return MILLISECONDS.toDays(millis);
}
@Description("truncate to the specified precision in the session timezone")
@ScalarFunction("date_trunc")
@SqlType(StandardTypes.TIME)
public static long truncateTime(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIME) long time)
{
return getTimeField(getChronology(session.getTimeZoneKey()), unit).roundFloor(time);
}
@Description("truncate to the specified precision")
@ScalarFunction("date_trunc")
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long truncateTimeWithTimeZone(@SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone)
{
long millis = getTimeField(unpackChronology(timeWithTimeZone), unit).roundFloor(unpackMillisUtc(timeWithTimeZone));
return updateMillisUtc(millis, timeWithTimeZone);
}
@Description("truncate to the specified precision in the session timezone")
@ScalarFunction("date_trunc")
@SqlType(StandardTypes.TIMESTAMP)
public static long truncateTimestamp(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getTimestampField(getChronology(session.getTimeZoneKey()), unit).roundFloor(timestamp);
}
@Description("truncate to the specified precision")
@ScalarFunction("date_trunc")
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long truncateTimestampWithTimezone(@SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
long millis = getTimestampField(unpackChronology(timestampWithTimeZone), unit).roundFloor(unpackMillisUtc(timestampWithTimeZone));
return updateMillisUtc(millis, timestampWithTimeZone);
}
@Description("add the specified amount of date to the given date")
@ScalarFunction("date_add")
@SqlType(StandardTypes.DATE)
public static long addFieldValueDate(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.DATE) long date)
{
long millis = getDateField(UTC_CHRONOLOGY, unit).add(DAYS.toMillis(date), Ints.checkedCast(value));
return MILLISECONDS.toDays(millis);
}
@Description("add the specified amount of time to the given time")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIME)
public static long addFieldValueTime(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME) long time)
{
ISOChronology chronology = getChronology(session.getTimeZoneKey());
return modulo24Hour(chronology, getTimeField(chronology, unit).add(time, Ints.checkedCast(value)));
}
@Description("add the specified amount of time to the given time")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long addFieldValueTimeWithTimeZone(
@SqlType(StandardTypes.VARCHAR) Slice unit,
@SqlType(StandardTypes.BIGINT) long value,
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone)
{
ISOChronology chronology = unpackChronology(timeWithTimeZone);
long millis = modulo24Hour(chronology, getTimeField(chronology, unit).add(unpackMillisUtc(timeWithTimeZone), Ints.checkedCast(value)));
return updateMillisUtc(millis, timeWithTimeZone);
}
@Description("add the specified amount of time to the given timestamp")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIMESTAMP)
public static long addFieldValueTimestamp(
ConnectorSession session,
@SqlType(StandardTypes.VARCHAR) Slice unit,
@SqlType(StandardTypes.BIGINT) long value,
@SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getTimestampField(getChronology(session.getTimeZoneKey()), unit).add(timestamp, Ints.checkedCast(value));
}
@Description("add the specified amount of time to the given timestamp")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long addFieldValueTimestampWithTimeZone(
@SqlType(StandardTypes.VARCHAR) Slice unit,
@SqlType(StandardTypes.BIGINT) long value,
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
long millis = getTimestampField(unpackChronology(timestampWithTimeZone), unit).add(unpackMillisUtc(timestampWithTimeZone), Ints.checkedCast(value));
return updateMillisUtc(millis, timestampWithTimeZone);
}
@Description("difference of the given dates in the given unit")
@ScalarFunction("date_diff")
@SqlType(StandardTypes.BIGINT)
public static long diffDate(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.DATE) long date1, @SqlType(StandardTypes.DATE) long date2)
{
return getDateField(UTC_CHRONOLOGY, unit).getDifferenceAsLong(DAYS.toMillis(date2), DAYS.toMillis(date1));
}
@Description("difference of the given times in the given unit")
@ScalarFunction("date_diff")
@SqlType(StandardTypes.BIGINT)
public static long diffTime(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIME) long time1, @SqlType(StandardTypes.TIME) long time2)
{
ISOChronology chronology = getChronology(session.getTimeZoneKey());
return getTimeField(chronology, unit).getDifferenceAsLong(time2, time1);
}
@Description("difference of the given times in the given unit")
@ScalarFunction("date_diff")
@SqlType(StandardTypes.BIGINT)
public static long diffTimeWithTimeZone(
@SqlType(StandardTypes.VARCHAR) Slice unit,
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone1,
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone2)
{
return getTimeField(unpackChronology(timeWithTimeZone1), unit).getDifferenceAsLong(unpackMillisUtc(timeWithTimeZone2), unpackMillisUtc(timeWithTimeZone1));
}
@Description("difference of the given times in the given unit")
@ScalarFunction("date_diff")
@SqlType(StandardTypes.BIGINT)
public static long diffTimestamp(
ConnectorSession session,
@SqlType(StandardTypes.VARCHAR) Slice unit,
@SqlType(StandardTypes.TIMESTAMP) long timestamp1,
@SqlType(StandardTypes.TIMESTAMP) long timestamp2)
{
return getTimestampField(getChronology(session.getTimeZoneKey()), unit).getDifferenceAsLong(timestamp2, timestamp1);
}
@Description("difference of the given times in the given unit")
@ScalarFunction("date_diff")
@SqlType(StandardTypes.BIGINT)
public static long diffTimestampWithTimeZone(
@SqlType(StandardTypes.VARCHAR) Slice unit,
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone1,
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone2)
{
return getTimestampField(unpackChronology(timestampWithTimeZone1), unit).getDifferenceAsLong(unpackMillisUtc(timestampWithTimeZone2), unpackMillisUtc(timestampWithTimeZone1));
}
private static DateTimeField getDateField(ISOChronology chronology, Slice unit)
{
String unitString = unit.toStringUtf8().toLowerCase(ENGLISH);
switch (unitString) {
case "day":
return chronology.dayOfMonth();
case "week":
return chronology.weekOfWeekyear();
case "month":
return chronology.monthOfYear();
case "quarter":
return QUARTER_OF_YEAR.getField(chronology);
case "year":
return chronology.year();
}
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "'" + unitString + "' is not a valid DATE field");
}
private static DateTimeField getTimeField(ISOChronology chronology, Slice unit)
{
String unitString = unit.toStringUtf8().toLowerCase(ENGLISH);
switch (unitString) {
case "millisecond":
return chronology.millisOfSecond();
case "second":
return chronology.secondOfMinute();
case "minute":
return chronology.minuteOfHour();
case "hour":
return chronology.hourOfDay();
}
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "'" + unitString + "' is not a valid Time field");
}
private static DateTimeField getTimestampField(ISOChronology chronology, Slice unit)
{
String unitString = unit.toStringUtf8().toLowerCase(ENGLISH);
switch (unitString) {
case "millisecond":
return chronology.millisOfSecond();
case "second":
return chronology.secondOfMinute();
case "minute":
return chronology.minuteOfHour();
case "hour":
return chronology.hourOfDay();
case "day":
return chronology.dayOfMonth();
case "week":
return chronology.weekOfWeekyear();
case "month":
return chronology.monthOfYear();
case "quarter":
return QUARTER_OF_YEAR.getField(chronology);
case "year":
return chronology.year();
}
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "'" + unitString + "' is not a valid Timestamp field");
}
@Description("parses the specified date/time by the given format")
@ScalarFunction
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long parseDatetime(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice datetime, @SqlType(StandardTypes.VARCHAR) Slice formatString)
{
String pattern = formatString.toStringUtf8();
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern)
.withChronology(getChronology(session.getTimeZoneKey()))
.withOffsetParsed()
.withLocale(session.getLocale());
String datetimeString = datetime.toStringUtf8();
return packDateTimeWithZone(parseDateTimeHelper(formatter, datetimeString));
}
private static DateTime parseDateTimeHelper(DateTimeFormatter formatter, String datetimeString)
{
try {
return formatter.parseDateTime(datetimeString);
}
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
}
}
@Description("formats the given time by the given format")
@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice formatDatetime(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp, @SqlType(StandardTypes.VARCHAR) Slice formatString)
{
return formatDatetime(getChronology(session.getTimeZoneKey()), session.getLocale(), timestamp, formatString);
}
@Description("formats the given time by the given format")
@ScalarFunction("format_datetime")
@SqlType(StandardTypes.VARCHAR)
public static Slice formatDatetimeWithTimeZone(
ConnectorSession session,
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone,
@SqlType(StandardTypes.VARCHAR) Slice formatString)
{
return formatDatetime(unpackChronology(timestampWithTimeZone), session.getLocale(), unpackMillisUtc(timestampWithTimeZone), formatString);
}
private static Slice formatDatetime(ISOChronology chronology, Locale locale, long timestamp, Slice formatString)
{
String pattern = formatString.toStringUtf8();
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern)
.withChronology(chronology)
.withLocale(locale);
String datetimeString = formatter.print(timestamp);
return utf8Slice(datetimeString);
}
@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice dateFormat(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp, @SqlType(StandardTypes.VARCHAR) Slice formatString)
{
return dateFormat(getChronology(session.getTimeZoneKey()), session.getLocale(), timestamp, formatString);
}
@ScalarFunction("date_format")
@SqlType(StandardTypes.VARCHAR)
public static Slice dateFormatWithTimeZone(
ConnectorSession session,
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone,
@SqlType(StandardTypes.VARCHAR) Slice formatString)
{
return dateFormat(unpackChronology(timestampWithTimeZone), session.getLocale(), unpackMillisUtc(timestampWithTimeZone), formatString);
}
private static Slice dateFormat(ISOChronology chronology, Locale locale, long timestamp, Slice formatString)
{
DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
.withChronology(chronology)
.withLocale(locale);
return utf8Slice(formatter.print(timestamp));
}
@ScalarFunction
@SqlType(StandardTypes.TIMESTAMP)
public static long dateParse(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice dateTime, @SqlType(StandardTypes.VARCHAR) Slice formatString)
{
DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
.withChronology(getChronology(session.getTimeZoneKey()))
.withLocale(session.getLocale());
try {
return formatter.parseMillis(dateTime.toStringUtf8());
}
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
}
}
@Description("second of the minute of the given timestamp")
@ScalarFunction("second")
@SqlType(StandardTypes.BIGINT)
public static long secondFromTimestamp(@SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
// Time is effectively UTC so no need for a custom chronology
return SECOND_OF_MINUTE.get(timestamp);
}
@Description("second of the minute of the given timestamp")
@ScalarFunction("second")
@SqlType(StandardTypes.BIGINT)
public static long secondFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
// Time is effectively UTC so no need for a custom chronology
return SECOND_OF_MINUTE.get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("second of the minute of the given time")
@ScalarFunction("second")
@SqlType(StandardTypes.BIGINT)
public static long secondFromTime(@SqlType(StandardTypes.TIME) long time)
{
// Time is effectively UTC so no need for a custom chronology
return SECOND_OF_MINUTE.get(time);
}
@Description("second of the minute of the given time")
@ScalarFunction("second")
@SqlType(StandardTypes.BIGINT)
public static long secondFromTimeWithTimeZone(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long time)
{
// Time is effectively UTC so no need for a custom chronology
return SECOND_OF_MINUTE.get(unpackMillisUtc(time));
}
@Description("second of the minute of the given interval")
@ScalarFunction("second")
@SqlType(StandardTypes.BIGINT)
public static long secondFromInterval(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long milliseconds)
{
return (milliseconds % MILLISECONDS_IN_MINUTE) / MILLISECONDS_IN_SECOND;
}
@Description("minute of the hour of the given timestamp")
@ScalarFunction("minute")
@SqlType(StandardTypes.BIGINT)
public static long minuteFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).minuteOfHour().get(timestamp);
}
@Description("minute of the hour of the given timestamp")
@ScalarFunction("minute")
@SqlType(StandardTypes.BIGINT)
public static long minuteFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).minuteOfHour().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("minute of the hour of the given time")
@ScalarFunction("minute")
@SqlType(StandardTypes.BIGINT)
public static long minuteFromTime(ConnectorSession session, @SqlType(StandardTypes.TIME) long time)
{
return getChronology(session.getTimeZoneKey()).minuteOfHour().get(time);
}
@Description("minute of the hour of the given time")
@ScalarFunction("minute")
@SqlType(StandardTypes.BIGINT)
public static long minuteFromTimeWithTimeZone(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone)
{
return unpackChronology(timeWithTimeZone).minuteOfHour().get(unpackMillisUtc(timeWithTimeZone));
}
@Description("minute of the hour of the given interval")
@ScalarFunction("minute")
@SqlType(StandardTypes.BIGINT)
public static long minuteFromInterval(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long milliseconds)
{
return (milliseconds % MILLISECONDS_IN_HOUR) / MILLISECONDS_IN_MINUTE;
}
@Description("hour of the day of the given timestamp")
@ScalarFunction("hour")
@SqlType(StandardTypes.BIGINT)
public static long hourFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).hourOfDay().get(timestamp);
}
@Description("hour of the day of the given timestamp")
@ScalarFunction("hour")
@SqlType(StandardTypes.BIGINT)
public static long hourFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).hourOfDay().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("hour of the day of the given time")
@ScalarFunction("hour")
@SqlType(StandardTypes.BIGINT)
public static long hourFromTime(ConnectorSession session, @SqlType(StandardTypes.TIME) long time)
{
return getChronology(session.getTimeZoneKey()).hourOfDay().get(time);
}
@Description("hour of the day of the given time")
@ScalarFunction("hour")
@SqlType(StandardTypes.BIGINT)
public static long hourFromTimeWithTimeZone(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone)
{
return unpackChronology(timeWithTimeZone).hourOfDay().get(unpackMillisUtc(timeWithTimeZone));
}
@Description("hour of the day of the given interval")
@ScalarFunction("hour")
@SqlType(StandardTypes.BIGINT)
public static long hourFromInterval(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long milliseconds)
{
return (milliseconds % MILLISECONDS_IN_DAY) / MILLISECONDS_IN_HOUR;
}
@Description("day of the week of the given timestamp")
@ScalarFunction(value = "day_of_week", alias = "dow")
@SqlType(StandardTypes.BIGINT)
public static long dayOfWeekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).dayOfWeek().get(timestamp);
}
@Description("day of the week of the given timestamp")
@ScalarFunction(value = "day_of_week", alias = "dow")
@SqlType(StandardTypes.BIGINT)
public static long dayOfWeekFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).dayOfWeek().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("day of the week of the given date")
@ScalarFunction(value = "day_of_week", alias = "dow")
@SqlType(StandardTypes.BIGINT)
public static long dayOfWeekFromDate(@SqlType(StandardTypes.DATE) long date)
{
return DAY_OF_WEEK.get(DAYS.toMillis(date));
}
@Description("day of the month of the given timestamp")
@ScalarFunction(value = "day", alias = "day_of_month")
@SqlType(StandardTypes.BIGINT)
public static long dayFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).dayOfMonth().get(timestamp);
}
@Description("day of the month of the given timestamp")
@ScalarFunction(value = "day", alias = "day_of_month")
@SqlType(StandardTypes.BIGINT)
public static long dayFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).dayOfMonth().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("day of the month of the given date")
@ScalarFunction(value = "day", alias = "day_of_month")
@SqlType(StandardTypes.BIGINT)
public static long dayFromDate(@SqlType(StandardTypes.DATE) long date)
{
return DAY_OF_MONTH.get(DAYS.toMillis(date));
}
@Description("day of the month of the given interval")
@ScalarFunction(value = "day", alias = "day_of_month")
@SqlType(StandardTypes.BIGINT)
public static long dayFromInterval(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long milliseconds)
{
return milliseconds / MILLISECONDS_IN_DAY;
}
@Description("day of the year of the given timestamp")
@ScalarFunction(value = "day_of_year", alias = "doy")
@SqlType(StandardTypes.BIGINT)
public static long dayOfYearFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).dayOfYear().get(timestamp);
}
@Description("day of the year of the given timestamp")
@ScalarFunction(value = "day_of_year", alias = "doy")
@SqlType(StandardTypes.BIGINT)
public static long dayOfYearFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).dayOfYear().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("day of the year of the given date")
@ScalarFunction(value = "day_of_year", alias = "doy")
@SqlType(StandardTypes.BIGINT)
public static long dayOfYearFromDate(@SqlType(StandardTypes.DATE) long date)
{
return DAY_OF_YEAR.get(DAYS.toMillis(date));
}
@Description("week of the year of the given timestamp")
@ScalarFunction(value = "week", alias = "week_of_year")
@SqlType(StandardTypes.BIGINT)
public static long weekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).weekOfWeekyear().get(timestamp);
}
@Description("week of the year of the given timestamp")
@ScalarFunction(value = "week", alias = "week_of_year")
@SqlType(StandardTypes.BIGINT)
public static long weekFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).weekOfWeekyear().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("week of the year of the given date")
@ScalarFunction(value = "week", alias = "week_of_year")
@SqlType(StandardTypes.BIGINT)
public static long weekFromDate(@SqlType(StandardTypes.DATE) long date)
{
return WEEK_OF_YEAR.get(DAYS.toMillis(date));
}
@Description("year of the ISO week of the given timestamp")
@ScalarFunction(value = "year_of_week", alias = "yow")
@SqlType(StandardTypes.BIGINT)
public static long yearOfWeekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).weekyear().get(timestamp);
}
@Description("year of the ISO week of the given timestamp")
@ScalarFunction(value = "year_of_week", alias = "yow")
@SqlType(StandardTypes.BIGINT)
public static long yearOfWeekFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).weekyear().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("year of the ISO week of the given date")
@ScalarFunction(value = "year_of_week", alias = "yow")
@SqlType(StandardTypes.BIGINT)
public static long yearOfWeekFromDate(@SqlType(StandardTypes.DATE) long date)
{
return YEAR_OF_WEEK.get(DAYS.toMillis(date));
}
@Description("month of the year of the given timestamp")
@ScalarFunction("month")
@SqlType(StandardTypes.BIGINT)
public static long monthFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).monthOfYear().get(timestamp);
}
@Description("month of the year of the given timestamp")
@ScalarFunction("month")
@SqlType(StandardTypes.BIGINT)
public static long monthFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).monthOfYear().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("month of the year of the given date")
@ScalarFunction("month")
@SqlType(StandardTypes.BIGINT)
public static long monthFromDate(@SqlType(StandardTypes.DATE) long date)
{
return MONTH_OF_YEAR.get(DAYS.toMillis(date));
}
@Description("month of the year of the given interval")
@ScalarFunction("month")
@SqlType(StandardTypes.BIGINT)
public static long monthFromInterval(@SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long months)
{
return months % 12;
}
@Description("quarter of the year of the given timestamp")
@ScalarFunction("quarter")
@SqlType(StandardTypes.BIGINT)
public static long quarterFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return QUARTER_OF_YEAR.getField(getChronology(session.getTimeZoneKey())).get(timestamp);
}
@Description("quarter of the year of the given timestamp")
@ScalarFunction("quarter")
@SqlType(StandardTypes.BIGINT)
public static long quarterFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return QUARTER_OF_YEAR.getField(unpackChronology(timestampWithTimeZone)).get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("quarter of the year of the given date")
@ScalarFunction("quarter")
@SqlType(StandardTypes.BIGINT)
public static long quarterFromDate(@SqlType(StandardTypes.DATE) long date)
{
return QUARTER.get(DAYS.toMillis(date));
}
@Description("year of the given timestamp")
@ScalarFunction("year")
@SqlType(StandardTypes.BIGINT)
public static long yearFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp)
{
return getChronology(session.getTimeZoneKey()).year().get(timestamp);
}
@Description("year of the given timestamp")
@ScalarFunction("year")
@SqlType(StandardTypes.BIGINT)
public static long yearFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return unpackChronology(timestampWithTimeZone).year().get(unpackMillisUtc(timestampWithTimeZone));
}
@Description("year of the given date")
@ScalarFunction("year")
@SqlType(StandardTypes.BIGINT)
public static long yearFromDate(@SqlType(StandardTypes.DATE) long date)
{
return YEAR.get(DAYS.toMillis(date));
}
@Description("year of the given interval")
@ScalarFunction("year")
@SqlType(StandardTypes.BIGINT)
public static long yearFromInterval(@SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long months)
{
return months / 12;
}
@Description("time zone minute of the given timestamp")
@ScalarFunction("timezone_minute")
@SqlType(StandardTypes.BIGINT)
public static long timeZoneMinuteFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return extractZoneOffsetMinutes(timestampWithTimeZone) % 60;
}
@Description("time zone hour of the given timestamp")
@ScalarFunction("timezone_hour")
@SqlType(StandardTypes.BIGINT)
public static long timeZoneHourFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
return extractZoneOffsetMinutes(timestampWithTimeZone) / 60;
}
@SuppressWarnings("fallthrough")
public static DateTimeFormatter createDateTimeFormatter(Slice format)
{
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
String formatString = format.toStringUtf8();
boolean escaped = false;
for (int i = 0; i < format.length(); i++) {
char character = formatString.charAt(i);
if (escaped) {
switch (character) {
case 'a': // %a Abbreviated weekday name (Sun..Sat)
builder.appendDayOfWeekShortText();
break;
case 'b': // %b Abbreviated month name (Jan..Dec)
builder.appendMonthOfYearShortText();
break;
case 'c': // %c Month, numeric (0..12)
builder.appendMonthOfYear(1);
break;
case 'd': // %d Day of the month, numeric (00..31)
builder.appendDayOfMonth(2);
break;
case 'e': // %e Day of the month, numeric (0..31)
builder.appendDayOfMonth(1);
break;
case 'f': // %f Microseconds (000000..999999)
builder.appendFractionOfSecond(6, 6);
break;
case 'H': // %H Hour (00..23)
builder.appendHourOfDay(2);
break;
case 'h': // %h Hour (01..12)
case 'I': // %I Hour (01..12)
builder.appendClockhourOfHalfday(2);
break;
case 'i': // %i Minutes, numeric (00..59)
builder.appendMinuteOfHour(2);
break;
case 'j': // %j Day of year (001..366)
builder.appendDayOfYear(3);
break;
case 'k': // %k Hour (0..23)
builder.appendHourOfDay(1);
break;
case 'l': // %l Hour (1..12)
builder.appendClockhourOfHalfday(1);
break;
case 'M': // %M Month name (January..December)
builder.appendMonthOfYearText();
break;
case 'm': // %m Month, numeric (00..12)
builder.appendMonthOfYear(2);
break;
case 'p': // %p AM or PM
builder.appendHalfdayOfDayText();
break;
case 'r': // %r Time, 12-hour (hh:mm:ss followed by AM or PM)
builder.appendClockhourOfHalfday(2)
.appendLiteral(':')
.appendMinuteOfHour(2)
.appendLiteral(':')
.appendSecondOfMinute(2)
.appendLiteral(' ')
.appendHalfdayOfDayText();
break;
case 'S': // %S Seconds (00..59)
case 's': // %s Seconds (00..59)
builder.appendSecondOfMinute(2);
break;
case 'T': // %T Time, 24-hour (hh:mm:ss)
builder.appendHourOfDay(2)
.appendLiteral(':')
.appendMinuteOfHour(2)
.appendLiteral(':')
.appendSecondOfMinute(2);
break;
case 'v': // %v Week (01..53), where Monday is the first day of the week; used with %x
builder.appendWeekOfWeekyear(2);
break;
case 'x': // %x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
builder.appendWeekyear(4, 4);
break;
case 'W': // %W Weekday name (Sunday..Saturday)
builder.appendDayOfWeekText();
break;
case 'w': // %w Day of the week (0=Sunday..6=Saturday)
builder.appendDayOfWeek(1);
break;
case 'Y': // %Y Year, numeric, four digits
builder.appendYear(4, 4);
break;
case 'y': // %y Year, numeric (two digits)
builder.appendYearOfCentury(2, 2);
break;