-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
832 lines (751 loc) · 25.4 KB
/
index.ts
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
/** @file Data types and functions for working with dates in the Gregorian calendar. */
import type {Comparator} from "@softwareventures/ordered";
import {Comparison} from "@softwareventures/ordered";
import isInteger = require("is-integer");
import isIntegerInRange from "is-integer-in-range";
import * as format from "@softwareventures/format-date";
import {hasProperty} from "unknown";
import {JsDate} from "./js-date";
/** A date in the Gregorian calendar, with no associated time zone. */
export interface Date {
/** Type discriminator identifying the object as a `Date`. */
readonly type: "Date";
/** The day of the month. Should be an integer in the range `1`-`31`. */
day: number;
/** The month of the year. Should be an integer in the range `1`-`12`. */
month: number;
/**
* The year.
*
* Should be an integer.
*
* Positive values represent years in the Common Era (CE/AD). For example
* `2020` represents 2020 CE, the year this module was first published to
* npm.
*
* Negative values or zero represent years before the Common Era (BCE/BC).
* Zero represents 1 BCE, `-1` represents 2 BCE, `-2` represents 3 BCE,
* etc.
*
* Note that there is no year zero in the Gregorian calendar. The year
* 1 BCE was immediately followed by 1 CE.
*/
year: number;
}
/**
* Options required to construct a `Date`.
*
* An instance of {@link Date} may always be used in place of `DateOptions`.
*/
export interface DateOptions {
/**
* Type discriminator identifying the object as a `Date`.
*
* If specified, must be the string `"Date"`. This is to prevent errors
* caused by a `DateTime` being accidentally passed to `Date` functions.
*/
readonly type?: "Date";
/** The day of the month. Should be in the range `1`-`31`. */
readonly day: number;
/** The month of the year. Should be in the range `1`-`12`. */
readonly month: number;
/**
* The year.
*
* Positive values represent years in the Common Era (CE/AD). For example
* `2020` represents 2020 CE, the year this module was first published to
* npm.
*
* Negative values or zero represent years before the Common Era (BCE/BC).
* Zero represents 1 BCE, `-1` represents 2 BCE, `-2` represents 3 BCE,
* etc.
*
* Note that there is no year zero in the Gregorian calendar. The year
* 1 BCE was immediately followed by 1 CE.
*/
readonly year: number;
}
/** The numeric representation of the month of January. */
export const JANUARY = 1; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of February. */
export const FEBRUARY = 2; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of March. */
export const MARCH = 3; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of April. */
export const APRIL = 4; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of May. */
export const MAY = 5; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of June. */
export const JUNE = 6; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of July. */
export const JULY = 7; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of August. */
export const AUGUST = 8; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of September. */
export const SEPTEMBER = 9; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of October. */
export const OCTOBER = 10; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of November. */
export const NOVEMBER = 11; // eslint-disable-line @typescript-eslint/naming-convention
/** The numeric representation of the month of December. */
export const DECEMBER = 12; // eslint-disable-line @typescript-eslint/naming-convention
/**
* Tests if the specified year is a leap year. Returns `true` if it is,
* otherwise `false`.
*
* Positive values represent years in the Common Era (CE/AD). For example
* `2020` represents 2020 CE, the year this module was first published to npm.
*
* Negative values or zero represent years before the Common Era (BCE/BC).
* Zero represents 1 BCE, `-1` represents 2 BCE, `-2` represents 3 BCE,
* etc.
*
* Note that there is no year zero in the Gregorian calendar. The year
* 1 BCE was immediately followed by 1 CE.
*/
export function isLeapYear(year: number): boolean {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
/**
* Returns the number of days in the specified month in the specified year.
*
* @param month - An integer representing the month, in the range `1` (January)
* to `12` (December).
*
* @param year - An integer representing the year. Positive values represent
* years in the Common Era (CE/AD). For example `2020` represents 2020 CE,
* the year this module was first published to npm. Negative values or zero
* represent years before the Common Era (BCE/BC). Zero represents 1 BCE,
* `-1` represents 2 BCE, `-2` represents 3 BCE, etc. There is no year zero
* in the Gregorian calendar. The year 1 BCE was immediately followed by 1
* CE.
*/
export function daysInMonth(month: number, year: number): number {
if (month < JANUARY || month > DECEMBER) {
throw new Error("Invalid month");
} else if (month === FEBRUARY && isLeapYear(year)) {
return 29;
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]!;
}
}
/**
* Returns `true` if the specified value has the shape of a {@link Date}
* object.
*
* The `year`, `month` and `day` fields may be non-integers or outside the
* valid range, meaning that the object may not represent a valid date.
*
* To test if the object represents a valid date, call {@link isValid} or
* {@link isValidDate}.
*/
export function isDate(value: unknown): value is Date {
return (
typeof value === "object" &&
value != null &&
hasProperty(value, "type") &&
value.type === "Date" &&
hasProperty(value, "year") &&
typeof value.year === "number" &&
hasProperty(value, "month") &&
typeof value.month === "number" &&
hasProperty(value, "day") &&
typeof (value as {day: unknown}).day === "number"
);
}
/**
* Tests if the specified value is a {@link Date} object representing a valid
* date.
*
* Returns `true` if the value has the shape of a `Date` object and the `year`,
* `month`, and `day` fields are all integers inside the valid range.
*
* Dates returned by functions in this library are always valid.
*/
export function isValidDate(value: unknown): value is Date {
return isDate(value) && isValid(value);
}
/**
* Tests if the specified {@link Date} object represents a valid date.
*
* Returns `true` if the `year`, `month` and `day` fields are all integers inside the
* valid range.
*
* Dates returned by functions in this library are always valid.
*/
export function isValid(date: DateOptions): boolean {
return (
(!hasProperty(date, "type") || date.type === "Date") &&
isInteger(date.year) &&
isIntegerInRange(date.month, JANUARY, DECEMBER) &&
isIntegerInRange(date.day, 1, daysInMonth(date.month, date.year))
);
}
/**
* Tests if the specified {@link Date} object represents a valid date.
*
* Returns `true` if the `year`, `month` and `day` fields are all integers
* inside the valid range.
*
* Dates returned by functions in this library are always valid.
*
* Alias of {@link isValid}, useful for disambiguation from similar functions
* that operate on other types.
*/
export const isDateValid = isValid;
/**
* Tests if the specified {@link Date} object represents a valid date.
*
* Returns `true` if the `year`, `month` and `day` fields are all integers
* inside the valid range.
*
* Dates returned by functions in this library are always valid.
*
* Alias of {@link isValid}, useful for disambiguation from similar functions
* that operate on other types.
*
* @deprecated Use {@link isDateValid} instead.
*/
export const dateIsValid = isValid;
/**
* Asserts that the specified {@link Date} object represents a valid date.
*
* Dates returned by functions in this library are always valid.
*
* @throws {Error} if any of the `year`, `month` or `day` fields are
* non-integers or outside the valid range.
*/
export function validate(date: DateOptions): void {
if (!isValid(date)) {
throw new Error("Invalid date");
}
}
/**
* Asserts that the specified {@link Date} object represents a valid date.
*
* Dates returned by functions in this library are always valid.
*
* Alias of {@link validate}.
*
* @throws {Error} if any of the `year`, `month` or `day` fields are
* non-integers or outside the valid range.
*/
export const validateDate = validate;
/**
* Constructs a normalized {@link Date} object from the specified `year`,
* `month` and `day`.
*
* If the `month` or `day` fields are outside the valid range, then they will
* roll over into the next month or year.
*
* @throws {Error} if the `year`, `month` or `day` fields are not finite
* numbers
*/
export function date(date: DateOptions): Date {
return fromReferenceDays(toReferenceDays(date));
}
/**
* Normalizes the specified {@link Date} object so that it represents a valid
* date.
*
* If the `month` or `day` fields are outside the valid range, then they will
* roll over into the next month or year.
*
* Alias of {@link date}. Calling the function by this name instead might make
* code clearer in cases where the purpose is to normalize an existing `Date`
* object.
*
* @throws {Error} if the `year`, `month` or `day` fields are not finite
* numbers */
export const normalize = date;
/**
* Normalizes the specified {@link Date} object so that it represents a valid date.
*
* If the `month` or `day` fields are outside the valid range, then they will
* roll over into the next month or year.
*
* Alias of {@link date}. Calling the function by this name instead might make
* code clearer in cases where the purpose is to normalize an existing `Date`
* object.
*
* @throws {Error} if the `year`, `month` or `day` fields are not finite
* numbers */
export const normalizeDate = normalize;
/**
* Converts the specified {@link Date} to a count of the number of days since the
* reference date of 1st January, 1 CE.
*/
export function toReferenceDays(date: Partial<DateOptions>): number {
if (hasProperty(date, "type") && date.type !== "Date") {
throw new TypeError();
}
const day = date.day ?? 1;
const month = date.month ?? 1;
const year = date.year ?? 1;
const referenceMonths = (year - 1) * 12 + month - 1;
return (
Math.floor((referenceMonths * 365) / 12) +
Math.floor((referenceMonths + 10) / 48) -
Math.floor((referenceMonths + 10) / 1200) +
Math.floor((referenceMonths + 10) / 4800) +
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
[0, 1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0][(12 + (Math.floor(referenceMonths) % 12)) % 12]! +
day -
1
);
}
/**
* Creates a {@link Date} corresponding to the specified count of the number
* of days since the reference date of 1st January, 1 CE.
*
* @throws {Error} if `referenceDays` is non-finite.
*/
export function fromReferenceDays(referenceDays: number): Date {
if (!isFinite(referenceDays)) {
throw new Error("Invalid date");
}
const rd = Math.floor(referenceDays);
const quadricentennium = Math.floor((rd + 366) / 146097);
const dayInQuadricentennium = rd + 366 - quadricentennium * 146097;
const centuryInQuadricentennium =
dayInQuadricentennium === 0 ? 0 : Math.floor((dayInQuadricentennium - 1) / 36524);
const longCentury = centuryInQuadricentennium === 0;
const dayInCentury =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
dayInQuadricentennium - [0, 36525, 73049, 109573][centuryInQuadricentennium]!;
const quadrenniumInCentury = Math.floor(
(dayInCentury + Number(centuryInQuadricentennium !== 0)) / 1461
);
const longQuadrennium = quadrenniumInCentury !== 0 || longCentury;
const dayInQuadrennium =
dayInCentury -
quadrenniumInCentury * 1461 +
Number(quadrenniumInCentury !== 0 && !longCentury);
const yearInQuadrennium =
dayInQuadrennium === 0 ? 0 : Math.floor((dayInQuadrennium - Number(longQuadrennium)) / 365);
const dayInYear =
dayInQuadrennium -
yearInQuadrennium * 365 -
Number(yearInQuadrennium !== 0 && longQuadrennium);
const leapDay = Number(longQuadrennium && yearInQuadrennium === 0);
const year =
quadricentennium * 400 +
centuryInQuadricentennium * 100 +
quadrenniumInCentury * 4 +
yearInQuadrennium;
let month: number;
let day: number;
if (dayInYear < 181 + leapDay) {
if (dayInYear < 90 + leapDay) {
if (dayInYear < 31) {
month = 1;
day = dayInYear + 1;
} else if (dayInYear < 59 + leapDay) {
month = 2;
day = dayInYear - 30;
} else {
month = 3;
day = dayInYear - 58 - leapDay;
}
} else if (dayInYear < 120 + leapDay) {
month = 4;
day = dayInYear - 89 - leapDay;
} else if (dayInYear < 151 + leapDay) {
month = 5;
day = dayInYear - 119 - leapDay;
} else {
month = 6;
day = dayInYear - 150 - leapDay;
}
} else if (dayInYear < 273 + leapDay) {
if (dayInYear < 212 + leapDay) {
month = 7;
day = dayInYear - 180 - leapDay;
} else if (dayInYear < 243 + leapDay) {
month = 8;
day = dayInYear - 211 - leapDay;
} else {
month = 9;
day = dayInYear - 242 - leapDay;
}
} else if (dayInYear < 304 + leapDay) {
month = 10;
day = dayInYear - 272 - leapDay;
} else if (dayInYear < 334 + leapDay) {
month = 11;
day = dayInYear - 303 - leapDay;
} else {
month = 12;
day = dayInYear - 333 - leapDay;
}
return {type: "Date", day, month, year};
}
/**
* Returns `true` if `a` and `b` refer to the same date.
*/
export function equal(a: DateOptions, b: DateOptions): boolean {
return toReferenceDays(a) === toReferenceDays(b);
}
/**
* Returns `true` if `a` and `b` refer to the same date.
*
* Alias of {@link equal}, for disambiguation from other equality functions.
*/
export const datesEqual = equal;
/**
* Returns `true` if `a` and `b` refer to the same date.
*
* Curried variant of {@link equal}.
*/
export function equalFn(b: DateOptions): (a: DateOptions) => boolean {
return a => equal(a, b);
}
/**
* Returns `true` if `a` and `b` refer to the same date.
*
* Curried variant of {@link datesEqual}.
*/
export const datesEqualFn = equalFn;
/**
* Returns `true` if `a` and `b` refer to different dates.
*/
export function notEqual(a: DateOptions, b: DateOptions): boolean {
return toReferenceDays(a) !== toReferenceDays(b);
}
/**
* Returns `true` if `a` and `b` refer to different dates.
*
* Alias of {@link notEqual}, for disambiguation from other inequality functions.
*/
export const datesNotEqual = notEqual;
/**
* Returns `true` if `a` and `b` refer to different dates.
*
* Curried variant of {@link notEqual}.
*/
export function notEqualFn(b: DateOptions): (a: DateOptions) => boolean {
return a => notEqual(a, b);
}
/**
* Returns `true` if `a` and `b` refer to different dates.
*
* Curried variant of {@link datesNotEqual}.
*/
export const datesNotEqualFn = notEqualFn;
/**
* Compares two dates and returns a {@link Comparison} specifying if `a` is
* before, equal to, or after `b`.
*/
export const compare: Comparator<DateOptions> = (a, b) => {
const ad = toReferenceDays(a);
const bd = toReferenceDays(b);
if (ad < bd) {
return Comparison.before;
} else if (ad > bd) {
return Comparison.after;
} else if (ad === bd) {
return Comparison.equal;
} else {
return Comparison.undefined;
}
};
/**
* Compares two dates and returns a {@link Comparison} specifying if `a` is
* before, equal to, or after `b`.
*
* Alias of {@link compare}, useful for disambiguation from other comparison
* functions.
*/
export const compareDates = compare;
/**
* Compares two dates and returns a {@link Comparison} specifying if `a` is
* before, equal to, or after `b`.
*
* Curried variant of {@link compare}.
*/
export function compareFn(b: DateOptions): (a: DateOptions) => Comparison {
return a => compare(a, b);
}
/**
* Compares two dates and returns a {@link Comparison} specifying if `a` is before,
* equal to, or after `b`.
*
* Curried variant of {@link compareDates}.
*/
export const compareDatesFn = compareFn;
/**
* Returns `true` if `a` refers to a date before `b`.
*/
export function before(a: DateOptions, b: DateOptions): boolean {
return toReferenceDays(a) < toReferenceDays(b);
}
/**
* Returns `true` if `a` refers to a date before `b`.
*
* Alias of {@link before}, useful for disambiguation from similar functions
* that operate on other date/time types.
*/
export const dateBefore = before;
/**
* Returns `true` if `a` refers to a date before `b`.
*
* Curried variant of {@link before}.
*/
export function beforeFn(b: DateOptions): (a: DateOptions) => boolean {
return a => before(a, b);
}
/**
* Returns `true` if `a` refers to a date before `b`.
*
* Curried variant of {@link dateBefore}.
*/
export const dateBeforeFn = beforeFn;
/**
* Returns `true` if `a` refers to a date before or the same as `b`.
*/
export function beforeOrEqual(a: DateOptions, b: DateOptions): boolean {
return toReferenceDays(a) <= toReferenceDays(b);
}
/**
* Returns `true` if `a` refers to a date before or the same as `b`.
*
* Alias of {@link beforeOrEqual}, useful for disambiguation from similar
* functions that operate on other date/time types.
*/
export const dateBeforeOrEqual = beforeOrEqual;
/**
* Returns `true` if `a` refers to a date before or the same as `b`.
*
* Curried variant of {@link beforeOrEqual}.
*/
export function beforeOrEqualFn(b: DateOptions): (a: DateOptions) => boolean {
return a => beforeOrEqual(a, b);
}
/**
* Returns `true` if `a` refers to a date before or the same as `b`.
*
* Curried variant of {@link dateBeforeOrEqual}.
*/
export const dateBeforeOrEqualFn = beforeOrEqualFn;
/**
* Returns `true` if `a` refers to a date after `b`.
*/
export function after(a: DateOptions, b: DateOptions): boolean {
return toReferenceDays(a) > toReferenceDays(b);
}
/**
* Returns `true` if `a` refers to a date after `b`.
*
* Alias of {@link after}, useful for disambiguation from similar functions
* that operate on other date/time types.
*/
export const dateAfter = after;
/**
* Returns `true` if `a` refers to a date after `b`.
*
* Curried variant of {@link after}.
*/
export function afterFn(b: DateOptions): (a: DateOptions) => boolean {
return a => after(a, b);
}
/**
* Returns `true` if `a` refers to a date after `b`.
*
* Curried variant of {@link dateAfter}.
*/
export const dateAfterFn = afterFn;
/**
* Returns `true` if `a` refers to a date after or the same as `b`.
*/
export function afterOrEqual(a: DateOptions, b: DateOptions): boolean {
return toReferenceDays(a) >= toReferenceDays(b);
}
/**
* Returns `true` if `a` refers to a date after or the same as `b`.
*
* Alias of {@link afterOrEqual}, useful for disambiguation from similar
* functions that operate on other date/time types.
*/
export const dateAfterOrEqual = afterOrEqual;
/**
* Returns `true` if `a` refers to a date after or the same as `b`.
*
* Curried variant of {@link afterOrEqual}.
*/
export function afterOrEqualFn(b: DateOptions): (a: DateOptions) => boolean {
return a => afterOrEqual(a, b);
}
/**
* Returns `true` if `a` refers to a date after or the same as `b`.
*
* Curried variant of {@link dateAfterOrEqual}.
*/
export const dateAfterOrEqualFn = afterOrEqualFn;
/**
* Compares two dates and returns the earlier of the two.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export function earliest(a: DateOptions, b: DateOptions): Date {
const ad = toReferenceDays(a);
const bd = toReferenceDays(b);
return fromReferenceDays(ad < bd ? ad : bd);
}
/**
* Compares two dates and returns the earlier of the two.
*
* Alias of {@link earliest}, useful for disambiguation from similar functions
* that operate on other date/time types.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export const earliestDate = earliest;
/**
* Compares two dates and returns the earlier of the two.
*
* Curried variant of {@link earliest}.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export function earliestFn(b: DateOptions): (a: DateOptions) => Date {
return a => earliest(a, b);
}
/**
* Compares two dates and returns the earlier of the two.
*
* Curried variant of {@link earliestDate}.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export const earliestDateFn = earliestFn;
/**
* Compares two dates and returns the later of the two.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export function latest(a: DateOptions, b: DateOptions): Date {
const ad = toReferenceDays(a);
const bd = toReferenceDays(b);
return fromReferenceDays(ad > bd ? ad : bd);
}
/**
* Compares two dates and returns the later of the two.
*
* Alias of {@link latest}, useful for disambiguation from similar functions
* that operate on other date/time types.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export const latestDate = latest;
/**
* Compares two dates and returns the later of the two.
*
* Curried variant of {@link latest}.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export function latestFn(b: DateOptions): (a: DateOptions) => Date {
return a => latest(a, b);
}
/**
* Compares two dates and returns the later of the two.
*
* Curried variant of {@link latestDate}.
*
* @throws {Error} if both specified `Date`s contain numeric fields that
* are non-finite.
*/
export const latestDateFn = latestFn;
/**
* Returns today's date according to UTC.
*/
export function todayUtc(): Date {
const today = new JsDate();
return {
type: "Date",
day: today.getUTCDate(),
month: today.getUTCMonth() + 1,
year: today.getUTCFullYear()
};
}
/**
* Returns today's date, according to the device's local timezone.
*/
export function todayDeviceLocal(): Date {
const today = new JsDate();
return {
type: "Date",
day: today.getDate(),
month: today.getMonth() + 1,
year: today.getFullYear()
};
}
/**
* Returns today's date, according to the device's local timezone.
*
* Alias of {@link todayDeviceLocal}, retained for backwards compatibility.
*
* @deprecated Use {@link todayDeviceLocal} instead. This function has been
* renamed to make it clear that the timezone used is the local timezone as
* reported by the device.
*/
export const todayLocal = todayDeviceLocal;
/**
* Parses a {@link Date} from text in ISO 8601 format.
*
* The ISO 8601 text must not specify a time zone offset.
*
* If the specified text is not a valid ISO 8601 date then this function
* returns `null`.
*
* Both extended `YYYY-MM-DD` and basic `YYYYMMDD` ISO 8601 formats are
* accepted.
*/
export function parseIso8601(text: string): Date | null {
const match = /^([+-]?\d{4,})-?(\d{2})-?(\d{2})$/u.exec(text);
if (match?.[1] == null || match[2] == null || match[3] == null) {
return null;
}
const year = parseInt(match[1], 10);
const month = parseInt(match[2], 10);
const day = parseInt(match[3], 10);
return {type: "Date", day, month, year};
}
/**
* Parses a {@link Date} from text in ISO 8601 format.
*
* The ISO 8601 text must not specify a time zone offset.
*
* If the specified text is not a valid ISO 8601 date then this function
* returns `null`.
*
* Both extended `YYYY-MM-DD` and basic `YYYYMMDD` ISO 8601 formats are
* accepted.
*
* Alias of {@link parseIso8601}, useful for disambiguation from similar
* functions that operate on other date/time types.
*/
export const parseDateIso8601 = parseIso8601;
/**
* Formats the specified Date as IS0 8601 extended, e.g. `2021-05-01`.
*
* For other formats, see `@softwareventures/format-date`.
*/
export const formatIso8601 = format.iso8601;
/**
* Formats the specified Date as IS0 8601 extended, e.g. `2021-05-01`.
*
* For other formats, see `@softwareventures/format-date`.
*
* Alias of {@link formatIso8601}, useful for disambiguation from similar
* functions that operate on other date/time types.
*/
export const formatDateIso8601 = format.iso8601;