-
Notifications
You must be signed in to change notification settings - Fork 0
/
luxon.js
7044 lines (6259 loc) · 214 KB
/
luxon.js
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
// these aren't really private, but nor are they really useful to document
/**
* @private
*/
class LuxonError extends Error {}
/**
* @private
*/
class InvalidDateTimeError extends LuxonError {
constructor(reason) {
super(`Invalid DateTime: ${reason.toMessage()}`);
}
}
/**
* @private
*/
class InvalidIntervalError extends LuxonError {
constructor(reason) {
super(`Invalid Interval: ${reason.toMessage()}`);
}
}
/**
* @private
*/
class InvalidDurationError extends LuxonError {
constructor(reason) {
super(`Invalid Duration: ${reason.toMessage()}`);
}
}
/**
* @private
*/
class ConflictingSpecificationError extends LuxonError {}
/**
* @private
*/
class InvalidUnitError extends LuxonError {
constructor(unit) {
super(`Invalid unit ${unit}`);
}
}
/**
* @private
*/
class InvalidArgumentError extends LuxonError {}
/**
* @private
*/
class ZoneIsAbstractError extends LuxonError {
constructor() {
super('Zone is an abstract class');
}
}
/**
* @private
*/
const n = 'numeric';
const s = 'short';
const l = 'long';
const DATE_SHORT = {
year: n,
month: n,
day: n,
};
const DATE_MED = {
year: n,
month: s,
day: n,
};
const DATE_MED_WITH_WEEKDAY = {
year: n,
month: s,
day: n,
weekday: s,
};
const DATE_FULL = {
year: n,
month: l,
day: n,
};
const DATE_HUGE = {
year: n,
month: l,
day: n,
weekday: l,
};
const TIME_SIMPLE = {
hour: n,
minute: n,
};
const TIME_WITH_SECONDS = {
hour: n,
minute: n,
second: n,
};
const TIME_WITH_SHORT_OFFSET = {
hour: n,
minute: n,
second: n,
timeZoneName: s,
};
const TIME_WITH_LONG_OFFSET = {
hour: n,
minute: n,
second: n,
timeZoneName: l,
};
const TIME_24_SIMPLE = {
hour: n,
minute: n,
hourCycle: 'h23',
};
const TIME_24_WITH_SECONDS = {
hour: n,
minute: n,
second: n,
hourCycle: 'h23',
};
const TIME_24_WITH_SHORT_OFFSET = {
hour: n,
minute: n,
second: n,
hourCycle: 'h23',
timeZoneName: s,
};
const TIME_24_WITH_LONG_OFFSET = {
hour: n,
minute: n,
second: n,
hourCycle: 'h23',
timeZoneName: l,
};
const DATETIME_SHORT = {
year: n,
month: n,
day: n,
hour: n,
minute: n,
};
const DATETIME_SHORT_WITH_SECONDS = {
year: n,
month: n,
day: n,
hour: n,
minute: n,
second: n,
};
const DATETIME_MED = {
year: n,
month: s,
day: n,
hour: n,
minute: n,
};
const DATETIME_MED_WITH_SECONDS = {
year: n,
month: s,
day: n,
hour: n,
minute: n,
second: n,
};
const DATETIME_MED_WITH_WEEKDAY = {
year: n,
month: s,
day: n,
weekday: s,
hour: n,
minute: n,
};
const DATETIME_FULL = {
year: n,
month: l,
day: n,
hour: n,
minute: n,
timeZoneName: s,
};
const DATETIME_FULL_WITH_SECONDS = {
year: n,
month: l,
day: n,
hour: n,
minute: n,
second: n,
timeZoneName: s,
};
const DATETIME_HUGE = {
year: n,
month: l,
day: n,
weekday: l,
hour: n,
minute: n,
timeZoneName: l,
};
const DATETIME_HUGE_WITH_SECONDS = {
year: n,
month: l,
day: n,
weekday: l,
hour: n,
minute: n,
second: n,
timeZoneName: l,
};
/*
This is just a junk drawer, containing anything used across multiple classes.
Because Luxon is small(ish), this should stay small and we won't worry about splitting
it up into, say, parsingUtil.js and basicUtil.js and so on. But they are divided up by feature area.
*/
/**
* @private
*/
// TYPES
function isUndefined(o) {
return typeof o === 'undefined';
}
function isNumber(o) {
return typeof o === 'number';
}
function isInteger(o) {
return typeof o === 'number' && o % 1 === 0;
}
function isString(o) {
return typeof o === 'string';
}
function isDate(o) {
return Object.prototype.toString.call(o) === '[object Date]';
}
// CAPABILITIES
function hasRelative() {
try {
return typeof Intl !== 'undefined' && !!Intl.RelativeTimeFormat;
} catch (e) {
return false;
}
}
// OBJECTS AND ARRAYS
function maybeArray(thing) {
return Array.isArray(thing) ? thing : [thing];
}
function bestBy(arr, by, compare) {
if (arr.length === 0) {
return undefined;
}
return arr.reduce((best, next) => {
const pair = [by(next), next];
if (!best) {
return pair;
} if (compare(best[0], pair[0]) === best[0]) {
return best;
}
return pair;
}, null)[1];
}
function pick(obj, keys) {
return keys.reduce((a, k) => {
a[k] = obj[k];
return a;
}, {});
}
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
// NUMBERS AND STRINGS
function integerBetween(thing, bottom, top) {
return isInteger(thing) && thing >= bottom && thing <= top;
}
// x % n but takes the sign of n instead of x
function floorMod(x, n) {
return x - n * Math.floor(x / n);
}
function padStart(input, n = 2) {
const isNeg = input < 0;
let padded;
if (isNeg) {
padded = `-${(`${-input}`).padStart(n, '0')}`;
} else {
padded = (`${input}`).padStart(n, '0');
}
return padded;
}
function parseInteger(string) {
if (isUndefined(string) || string === null || string === '') {
return undefined;
}
return parseInt(string, 10);
}
function parseFloating(string) {
if (isUndefined(string) || string === null || string === '') {
return undefined;
}
return parseFloat(string);
}
function parseMillis(fraction) {
// Return undefined (instead of 0) in these cases, where fraction is not set
if (isUndefined(fraction) || fraction === null || fraction === '') {
return undefined;
}
const f = parseFloat(`0.${fraction}`) * 1000;
return Math.floor(f);
}
function roundTo(number, digits, towardZero = false) {
const factor = 10 ** digits;
const rounder = towardZero ? Math.trunc : Math.round;
return rounder(number * factor) / factor;
}
// DATE BASICS
function isLeapYear(year) {
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
}
function daysInYear(year) {
return isLeapYear(year) ? 366 : 365;
}
function daysInMonth(year, month) {
const modMonth = floorMod(month - 1, 12) + 1;
const modYear = year + (month - modMonth) / 12;
if (modMonth === 2) {
return isLeapYear(modYear) ? 29 : 28;
}
return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][modMonth - 1];
}
// covert a calendar object to a local timestamp (epoch, but with the offset baked in)
function objToLocalTS(obj) {
let d = Date.UTC(
obj.year,
obj.month - 1,
obj.day,
obj.hour,
obj.minute,
obj.second,
obj.millisecond,
);
// for legacy reasons, years between 0 and 99 are interpreted as 19XX; revert that
if (obj.year < 100 && obj.year >= 0) {
d = new Date(d);
d.setUTCFullYear(d.getUTCFullYear() - 1900);
}
return +d;
}
function weeksInWeekYear(weekYear) {
const p1 = (weekYear
+ Math.floor(weekYear / 4)
- Math.floor(weekYear / 100)
+ Math.floor(weekYear / 400))
% 7;
const last = weekYear - 1;
const p2 = (last + Math.floor(last / 4) - Math.floor(last / 100) + Math.floor(last / 400)) % 7;
return p1 === 4 || p2 === 3 ? 53 : 52;
}
function untruncateYear(year) {
if (year > 99) {
return year;
} return year > 60 ? 1900 + year : 2000 + year;
}
// PARSING
function parseZoneInfo(ts, offsetFormat, locale, timeZone = null) {
const date = new Date(ts);
const intlOpts = {
hourCycle: 'h23',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
};
if (timeZone) {
intlOpts.timeZone = timeZone;
}
const modified = { timeZoneName: offsetFormat, ...intlOpts };
const parsed = new Intl.DateTimeFormat(locale, modified)
.formatToParts(date)
.find((m) => m.type.toLowerCase() === 'timezonename');
return parsed ? parsed.value : null;
}
// signedOffset('-5', '30') -> -330
function signedOffset(offHourStr, offMinuteStr) {
let offHour = parseInt(offHourStr, 10);
// don't || this because we want to preserve -0
if (Number.isNaN(offHour)) {
offHour = 0;
}
const offMin = parseInt(offMinuteStr, 10) || 0;
const offMinSigned = offHour < 0 || Object.is(offHour, -0) ? -offMin : offMin;
return offHour * 60 + offMinSigned;
}
// COERCION
function asNumber(value) {
const numericValue = Number(value);
if (typeof value === 'boolean' || value === '' || Number.isNaN(numericValue)) throw new InvalidArgumentError(`Invalid unit value ${value}`);
return numericValue;
}
function normalizeObject(obj, normalizer) {
const normalized = {};
for (const u in obj) {
if (hasOwnProperty(obj, u)) {
const v = obj[u];
if (v === undefined || v === null) continue;
normalized[normalizer(u)] = asNumber(v);
}
}
return normalized;
}
function formatOffset(offset, format) {
const hours = Math.trunc(Math.abs(offset / 60));
const minutes = Math.trunc(Math.abs(offset % 60));
const sign = offset >= 0 ? '+' : '-';
switch (format) {
case 'short':
return `${sign}${padStart(hours, 2)}:${padStart(minutes, 2)}`;
case 'narrow':
return `${sign}${hours}${minutes > 0 ? `:${minutes}` : ''}`;
case 'techie':
return `${sign}${padStart(hours, 2)}${padStart(minutes, 2)}`;
default:
throw new RangeError(`Value format ${format} is out of range for property format`);
}
}
function timeObject(obj) {
return pick(obj, ['hour', 'minute', 'second', 'millisecond']);
}
const ianaRegex = /[A-Za-z_+-]{1,256}(?::?\/[A-Za-z0-9_+-]{1,256}(?:\/[A-Za-z0-9_+-]{1,256})?)?/;
/**
* @private
*/
const monthsLong = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
const monthsShort = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
const monthsNarrow = ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D'];
function months(length) {
switch (length) {
case 'narrow':
return [...monthsNarrow];
case 'short':
return [...monthsShort];
case 'long':
return [...monthsLong];
case 'numeric':
return ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
case '2-digit':
return ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
default:
return null;
}
}
const weekdaysLong = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
];
const weekdaysShort = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const weekdaysNarrow = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];
function weekdays(length) {
switch (length) {
case 'narrow':
return [...weekdaysNarrow];
case 'short':
return [...weekdaysShort];
case 'long':
return [...weekdaysLong];
case 'numeric':
return ['1', '2', '3', '4', '5', '6', '7'];
default:
return null;
}
}
const meridiems = ['AM', 'PM'];
const erasLong = ['Before Christ', 'Anno Domini'];
const erasShort = ['BC', 'AD'];
const erasNarrow = ['B', 'A'];
function eras(length) {
switch (length) {
case 'narrow':
return [...erasNarrow];
case 'short':
return [...erasShort];
case 'long':
return [...erasLong];
default:
return null;
}
}
function meridiemForDateTime(dt) {
return meridiems[dt.hour < 12 ? 0 : 1];
}
function weekdayForDateTime(dt, length) {
return weekdays(length)[dt.weekday - 1];
}
function monthForDateTime(dt, length) {
return months(length)[dt.month - 1];
}
function eraForDateTime(dt, length) {
return eras(length)[dt.year < 0 ? 0 : 1];
}
function formatRelativeTime(unit, count, numeric = 'always', narrow = false) {
const units = {
years: ['year', 'yr.'],
quarters: ['quarter', 'qtr.'],
months: ['month', 'mo.'],
weeks: ['week', 'wk.'],
days: ['day', 'day', 'days'],
hours: ['hour', 'hr.'],
minutes: ['minute', 'min.'],
seconds: ['second', 'sec.'],
};
const lastable = ['hours', 'minutes', 'seconds'].indexOf(unit) === -1;
if (numeric === 'auto' && lastable) {
const isDay = unit === 'days';
switch (count) {
case 1:
return isDay ? 'tomorrow' : `next ${units[unit][0]}`;
case -1:
return isDay ? 'yesterday' : `last ${units[unit][0]}`;
case 0:
return isDay ? 'today' : `this ${units[unit][0]}`;
}
}
const isInPast = Object.is(count, -0) || count < 0;
const fmtValue = Math.abs(count);
const singular = fmtValue === 1;
const lilUnits = units[unit];
const fmtUnit = narrow
? singular
? lilUnits[1]
: lilUnits[2] || lilUnits[1]
: singular
? units[unit][0]
: unit;
return isInPast ? `${fmtValue} ${fmtUnit} ago` : `in ${fmtValue} ${fmtUnit}`;
}
function stringifyTokens(splits, tokenToString) {
let s = '';
for (const token of splits) {
if (token.literal) {
s += token.val;
} else {
s += tokenToString(token.val);
}
}
return s;
}
const macroTokenToFormatOpts = {
D: DATE_SHORT,
DD: DATE_MED,
DDD: DATE_FULL,
DDDD: DATE_HUGE,
t: TIME_SIMPLE,
tt: TIME_WITH_SECONDS,
ttt: TIME_WITH_SHORT_OFFSET,
tttt: TIME_WITH_LONG_OFFSET,
T: TIME_24_SIMPLE,
TT: TIME_24_WITH_SECONDS,
TTT: TIME_24_WITH_SHORT_OFFSET,
TTTT: TIME_24_WITH_LONG_OFFSET,
f: DATETIME_SHORT,
ff: DATETIME_MED,
fff: DATETIME_FULL,
ffff: DATETIME_HUGE,
F: DATETIME_SHORT_WITH_SECONDS,
FF: DATETIME_MED_WITH_SECONDS,
FFF: DATETIME_FULL_WITH_SECONDS,
FFFF: DATETIME_HUGE_WITH_SECONDS,
};
/**
* @private
*/
class Formatter {
static create(locale, opts = {}) {
return new Formatter(locale, opts);
}
static parseFormat(fmt) {
let current = null;
let currentFull = '';
let bracketed = false;
const splits = [];
for (let i = 0; i < fmt.length; i++) {
const c = fmt.charAt(i);
if (c === "'") {
if (currentFull.length > 0) {
splits.push({ literal: bracketed, val: currentFull });
}
current = null;
currentFull = '';
bracketed = !bracketed;
} else if (bracketed) {
currentFull += c;
} else if (c === current) {
currentFull += c;
} else {
if (currentFull.length > 0) {
splits.push({ literal: false, val: currentFull });
}
currentFull = c;
current = c;
}
}
if (currentFull.length > 0) {
splits.push({ literal: bracketed, val: currentFull });
}
return splits;
}
static macroTokenToFormatOpts(token) {
return macroTokenToFormatOpts[token];
}
constructor(locale, formatOpts) {
this.opts = formatOpts;
this.loc = locale;
this.systemLoc = null;
}
formatWithSystemDefault(dt, opts) {
if (this.systemLoc === null) {
this.systemLoc = this.loc.redefaultToSystem();
}
const df = this.systemLoc.dtFormatter(dt, { ...this.opts, ...opts });
return df.format();
}
formatDateTime(dt, opts = {}) {
const df = this.loc.dtFormatter(dt, { ...this.opts, ...opts });
return df.format();
}
formatDateTimeParts(dt, opts = {}) {
const df = this.loc.dtFormatter(dt, { ...this.opts, ...opts });
return df.formatToParts();
}
resolvedOptions(dt, opts = {}) {
const df = this.loc.dtFormatter(dt, { ...this.opts, ...opts });
return df.resolvedOptions();
}
num(n, p = 0) {
// we get some perf out of doing this here, annoyingly
if (this.opts.forceSimple) {
return padStart(n, p);
}
const opts = { ...this.opts };
if (p > 0) {
opts.padTo = p;
}
return this.loc.numberFormatter(opts).format(n);
}
formatDateTimeFromString(dt, fmt) {
const knownEnglish = this.loc.listingMode() === 'en';
const useDateTimeFormatter = this.loc.outputCalendar && this.loc.outputCalendar !== 'gregory';
const string = (opts, extract) => this.loc.extract(dt, opts, extract);
const formatOffset = (opts) => {
if (dt.isOffsetFixed && dt.offset === 0 && opts.allowZ) {
return 'Z';
}
return dt.isValid ? dt.zone.formatOffset(dt.ts, opts.format) : '';
};
const meridiem = () => (knownEnglish
? meridiemForDateTime(dt)
: string({ hour: 'numeric', hourCycle: 'h12' }, 'dayperiod'));
const month = (length, standalone) => (knownEnglish
? monthForDateTime(dt, length)
: string(standalone ? { month: length } : { month: length, day: 'numeric' }, 'month'));
const weekday = (length, standalone) => (knownEnglish
? weekdayForDateTime(dt, length)
: string(
standalone ? { weekday: length } : { weekday: length, month: 'long', day: 'numeric' },
'weekday',
));
const maybeMacro = (token) => {
const formatOpts = Formatter.macroTokenToFormatOpts(token);
if (formatOpts) {
return this.formatWithSystemDefault(dt, formatOpts);
}
return token;
};
const era = (length) => (knownEnglish ? eraForDateTime(dt, length) : string({ era: length }, 'era'));
const tokenToString = (token) => {
// Where possible: http://cldr.unicode.org/translation/date-time-1/date-time#TOC-Standalone-vs.-Format-Styles
switch (token) {
// ms
case 'S':
return this.num(dt.millisecond);
case 'u':
// falls through
case 'SSS':
return this.num(dt.millisecond, 3);
// seconds
case 's':
return this.num(dt.second);
case 'ss':
return this.num(dt.second, 2);
// fractional seconds
case 'uu':
return this.num(Math.floor(dt.millisecond / 10), 2);
case 'uuu':
return this.num(Math.floor(dt.millisecond / 100));
// minutes
case 'm':
return this.num(dt.minute);
case 'mm':
return this.num(dt.minute, 2);
// hours
case 'h':
return this.num(dt.hour % 12 === 0 ? 12 : dt.hour % 12);
case 'hh':
return this.num(dt.hour % 12 === 0 ? 12 : dt.hour % 12, 2);
case 'H':
return this.num(dt.hour);
case 'HH':
return this.num(dt.hour, 2);
// offset
case 'Z':
// like +6
return formatOffset({ format: 'narrow', allowZ: this.opts.allowZ });
case 'ZZ':
// like +06:00
return formatOffset({ format: 'short', allowZ: this.opts.allowZ });
case 'ZZZ':
// like +0600
return formatOffset({ format: 'techie', allowZ: this.opts.allowZ });
case 'ZZZZ':
// like EST
return dt.zone.offsetName(dt.ts, { format: 'short', locale: this.loc.locale });
case 'ZZZZZ':
// like Eastern Standard Time
return dt.zone.offsetName(dt.ts, { format: 'long', locale: this.loc.locale });
// zone
case 'z':
// like America/New_York
return dt.zoneName;
// meridiems
case 'a':
return meridiem();
// dates
case 'd':
return useDateTimeFormatter ? string({ day: 'numeric' }, 'day') : this.num(dt.day);
case 'dd':
return useDateTimeFormatter ? string({ day: '2-digit' }, 'day') : this.num(dt.day, 2);
// weekdays - standalone
case 'c':
// like 1
return this.num(dt.weekday);
case 'ccc':
// like 'Tues'
return weekday('short', true);
case 'cccc':
// like 'Tuesday'
return weekday('long', true);
case 'ccccc':
// like 'T'
return weekday('narrow', true);
// weekdays - format
case 'E':
// like 1
return this.num(dt.weekday);
case 'EEE':
// like 'Tues'
return weekday('short', false);
case 'EEEE':
// like 'Tuesday'
return weekday('long', false);
case 'EEEEE':
// like 'T'
return weekday('narrow', false);
// months - standalone
case 'L':
// like 1
return useDateTimeFormatter
? string({ month: 'numeric', day: 'numeric' }, 'month')
: this.num(dt.month);
case 'LL':
// like 01, doesn't seem to work
return useDateTimeFormatter
? string({ month: '2-digit', day: 'numeric' }, 'month')
: this.num(dt.month, 2);
case 'LLL':
// like Jan
return month('short', true);
case 'LLLL':
// like January
return month('long', true);
case 'LLLLL':
// like J
return month('narrow', true);
// months - format
case 'M':
// like 1
return useDateTimeFormatter
? string({ month: 'numeric' }, 'month')
: this.num(dt.month);
case 'MM':
// like 01
return useDateTimeFormatter
? string({ month: '2-digit' }, 'month')
: this.num(dt.month, 2);
case 'MMM':
// like Jan
return month('short', false);
case 'MMMM':
// like January
return month('long', false);
case 'MMMMM':
// like J
return month('narrow', false);
// years
case 'y':
// like 2014
return useDateTimeFormatter ? string({ year: 'numeric' }, 'year') : this.num(dt.year);
case 'yy':
// like 14
return useDateTimeFormatter
? string({ year: '2-digit' }, 'year')
: this.num(dt.year.toString().slice(-2), 2);
case 'yyyy':
// like 0012
return useDateTimeFormatter
? string({ year: 'numeric' }, 'year')
: this.num(dt.year, 4);
case 'yyyyyy':
// like 000012
return useDateTimeFormatter
? string({ year: 'numeric' }, 'year')
: this.num(dt.year, 6);
// eras
case 'G':
// like AD
return era('short');
case 'GG':
// like Anno Domini
return era('long');
case 'GGGGG':
return era('narrow');
case 'kk':
return this.num(dt.weekYear.toString().slice(-2), 2);
case 'kkkk':
return this.num(dt.weekYear, 4);
case 'W':
return this.num(dt.weekNumber);
case 'WW':
return this.num(dt.weekNumber, 2);
case 'o':
return this.num(dt.ordinal);
case 'ooo':
return this.num(dt.ordinal, 3);
case 'q':
// like 1
return this.num(dt.quarter);
case 'qq':
// like 01
return this.num(dt.quarter, 2);
case 'X':
return this.num(Math.floor(dt.ts / 1000));
case 'x':
return this.num(dt.ts);
default:
return maybeMacro(token);
}