@timestamp-js/core 0.1.0-alpha.1 → 0.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/index.d.ts +325 -307
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +396 -292
- package/dist/index.js.map +1 -1
- package/package.json +6 -4
package/dist/index.js
CHANGED
|
@@ -112,7 +112,7 @@ export const MILLISECONDS_IN_WEEK = TIME_CONSTANTS.MILLISECONDS_IN.WEEK;
|
|
|
112
112
|
/**
|
|
113
113
|
* Frozen empty timestamp template.
|
|
114
114
|
*
|
|
115
|
-
* Use
|
|
115
|
+
* Use copyTimestamp or parser helpers to create new timestamp objects
|
|
116
116
|
* instead of mutating this shared default.
|
|
117
117
|
*/
|
|
118
118
|
export const Timestamp = freezeTimestamp({
|
|
@@ -163,11 +163,11 @@ export function validateTimestamp(input) {
|
|
|
163
163
|
* Fast low-level parser for date and date-time strings.
|
|
164
164
|
*
|
|
165
165
|
* This parser fills numeric fields, but does not update formatted date,
|
|
166
|
-
* weekday, day-of-year, workweek, or relative flags. Use
|
|
167
|
-
*
|
|
166
|
+
* weekday, day-of-year, workweek, or relative flags. Use parseTimestamp()
|
|
167
|
+
* when those derived fields are needed.
|
|
168
168
|
*
|
|
169
169
|
* @param {string} input In the form `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like date time with optional milliseconds and timezone suffix.
|
|
170
|
-
* @returns {Timestamp}
|
|
170
|
+
* @returns {Timestamp} Minimal Timestamp object, or `null` when the input cannot be parsed.
|
|
171
171
|
*/
|
|
172
172
|
export function parsed(input) {
|
|
173
173
|
if (typeof input !== "string")
|
|
@@ -211,13 +211,7 @@ export function parsed(input) {
|
|
|
211
211
|
timestamp.time = getTime(timestamp);
|
|
212
212
|
return freezeTimestamp(timestamp);
|
|
213
213
|
}
|
|
214
|
-
|
|
215
|
-
* Takes a JavaScript Date and returns a {@link Timestamp}. The {@link Timestamp} is not updated with relative information.
|
|
216
|
-
* @param {Date} date JavaScript Date
|
|
217
|
-
* @param {boolean} utc If set the {@link Timestamp} will parse the Date as UTC
|
|
218
|
-
* @returns {Timestamp} A minimal {@link Timestamp} without updated or relative updates.
|
|
219
|
-
*/
|
|
220
|
-
export function parseDate(date, utc = false) {
|
|
214
|
+
function parseDateByMode(date, utc) {
|
|
221
215
|
if (!(date instanceof Date))
|
|
222
216
|
return null;
|
|
223
217
|
const UTC = utc ? "UTC" : "";
|
|
@@ -255,6 +249,30 @@ export function parseDate(date, utc = false) {
|
|
|
255
249
|
}
|
|
256
250
|
return updateFormatted(timestamp);
|
|
257
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* Converts a JavaScript Date into a formatted Timestamp using host-local fields.
|
|
254
|
+
*
|
|
255
|
+
* Use parseDateUTC() when the Date represents an instant that should be read
|
|
256
|
+
* with UTC getters instead of host-local getters.
|
|
257
|
+
*
|
|
258
|
+
* @param {Date} date JavaScript Date to convert.
|
|
259
|
+
* @returns {Timestamp} Formatted Timestamp object, or `null` for invalid input.
|
|
260
|
+
*/
|
|
261
|
+
export function parseDate(date) {
|
|
262
|
+
return parseDateByMode(date, false);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Converts a JavaScript Date into a formatted Timestamp using UTC fields.
|
|
266
|
+
*
|
|
267
|
+
* Use this when server and client output should agree on the same UTC calendar
|
|
268
|
+
* and time fields for a native Date instant.
|
|
269
|
+
*
|
|
270
|
+
* @param {Date} date JavaScript Date to convert.
|
|
271
|
+
* @returns {Timestamp} Formatted Timestamp object, or `null` for invalid input.
|
|
272
|
+
*/
|
|
273
|
+
export function parseDateUTC(date) {
|
|
274
|
+
return parseDateByMode(date, true);
|
|
275
|
+
}
|
|
258
276
|
/**
|
|
259
277
|
* Pads a number to a requested string length.
|
|
260
278
|
*
|
|
@@ -290,9 +308,10 @@ export function daysInMonth(year, month) {
|
|
|
290
308
|
return (isLeapYear(year) ? DAYS_IN_MONTH_LEAP[month] : DAYS_IN_MONTH[month]);
|
|
291
309
|
}
|
|
292
310
|
/**
|
|
293
|
-
* Returns a
|
|
294
|
-
*
|
|
295
|
-
* @
|
|
311
|
+
* Returns a new Timestamp for the next calendar day.
|
|
312
|
+
*
|
|
313
|
+
* @param {Timestamp} timestamp Base Timestamp object.
|
|
314
|
+
* @returns {Timestamp} New Timestamp representing the next day.
|
|
296
315
|
*/
|
|
297
316
|
export function nextDay(timestamp) {
|
|
298
317
|
const date = new Date(timestamp.year, timestamp.month - 1, timestamp.day + 1);
|
|
@@ -304,9 +323,10 @@ export function nextDay(timestamp) {
|
|
|
304
323
|
}));
|
|
305
324
|
}
|
|
306
325
|
/**
|
|
307
|
-
* Returns a
|
|
308
|
-
*
|
|
309
|
-
* @
|
|
326
|
+
* Returns a new Timestamp for the previous calendar day.
|
|
327
|
+
*
|
|
328
|
+
* @param {Timestamp} timestamp Base Timestamp object.
|
|
329
|
+
* @returns {Timestamp} New Timestamp representing the previous day.
|
|
310
330
|
*/
|
|
311
331
|
export function prevDay(timestamp) {
|
|
312
332
|
const date = new Date(timestamp.year, timestamp.month - 1, timestamp.day - 1);
|
|
@@ -318,13 +338,48 @@ export function prevDay(timestamp) {
|
|
|
318
338
|
}));
|
|
319
339
|
}
|
|
320
340
|
/**
|
|
321
|
-
* Returns today's date
|
|
341
|
+
* Returns today's date using the host runtime timezone.
|
|
342
|
+
*
|
|
343
|
+
* For SSR or static rendering, server and client runtimes can produce different
|
|
344
|
+
* values when they run in different timezones. Use todayUTC() when the app
|
|
345
|
+
* wants a stable UTC calendar date instead.
|
|
346
|
+
*
|
|
322
347
|
* @returns {string} Date string in the form `YYYY-MM-DD`
|
|
323
348
|
*/
|
|
324
349
|
export function today() {
|
|
325
350
|
const d = new Date(), month = d.getMonth() + 1, day = d.getDate(), year = d.getFullYear();
|
|
326
351
|
return [year, padNumber(month, 2), padNumber(day, 2)].join("-");
|
|
327
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* Returns today's date using UTC calendar fields.
|
|
355
|
+
*
|
|
356
|
+
* Pass a Date fixture to make SSR, tests, and hydration-sensitive render paths
|
|
357
|
+
* deterministic. This helper reads UTC fields only; it does not convert an
|
|
358
|
+
* existing Timestamp or timezone-suffixed string.
|
|
359
|
+
*
|
|
360
|
+
* @param {Date} date Date source to read. Defaults to the current Date.
|
|
361
|
+
* @returns {string} UTC date string in the form `YYYY-MM-DD`
|
|
362
|
+
*/
|
|
363
|
+
export function todayUTC(date = new Date()) {
|
|
364
|
+
return [
|
|
365
|
+
padNumber(date.getUTCFullYear(), 4),
|
|
366
|
+
padNumber(date.getUTCMonth() + 1, 2),
|
|
367
|
+
padNumber(date.getUTCDate(), 2),
|
|
368
|
+
].join("-");
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Returns the current date-time as an immutable Timestamp using UTC fields.
|
|
372
|
+
*
|
|
373
|
+
* Use this when server and client output should agree on UTC calendar and time
|
|
374
|
+
* values. For fully deterministic SSR output, pass a Date captured by the
|
|
375
|
+
* caller instead of allowing each runtime to create its own current Date.
|
|
376
|
+
*
|
|
377
|
+
* @param {Date} date Date source to read. Defaults to the current Date.
|
|
378
|
+
* @returns {Timestamp} Immutable Timestamp built from UTC fields.
|
|
379
|
+
*/
|
|
380
|
+
export function nowUTC(date = new Date()) {
|
|
381
|
+
return parseDateUTC(date);
|
|
382
|
+
}
|
|
328
383
|
/**
|
|
329
384
|
* Takes a date string ('YYYY-MM-DD') and validates if it is today's date
|
|
330
385
|
* @param {string} date Date string in the form 'YYYY-MM-DD'
|
|
@@ -334,12 +389,25 @@ export function isToday(date) {
|
|
|
334
389
|
return date === today();
|
|
335
390
|
}
|
|
336
391
|
/**
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
392
|
+
* Checks whether a date string matches today's UTC date.
|
|
393
|
+
*
|
|
394
|
+
* Pass a Date fixture when SSR, tests, or hydration-sensitive render paths need
|
|
395
|
+
* deterministic behavior.
|
|
396
|
+
*
|
|
397
|
+
* @param {string} date Date string in the form `YYYY-MM-DD`.
|
|
398
|
+
* @param {Date} now Date source to read. Defaults to the current Date.
|
|
399
|
+
* @returns {boolean} True when the date matches the UTC date.
|
|
400
|
+
*/
|
|
401
|
+
export function isTodayUTC(date, now = new Date()) {
|
|
402
|
+
return date === todayUTC(now);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Returns the start of the week for a Timestamp and weekday set.
|
|
406
|
+
* If a current Timestamp is provided, the returned Timestamp includes updated relative information.
|
|
407
|
+
* @param {Timestamp} timestamp The Timestamp to use to find the start of the week
|
|
340
408
|
* @param {number[]} weekdays The array is [0,1,2,3,4,5,6] where 0=Sunday and 6=Saturday
|
|
341
|
-
* @param {Timestamp=} today
|
|
342
|
-
* @returns {Timestamp} A new
|
|
409
|
+
* @param {Timestamp=} today Current timestamp used to update relative information
|
|
410
|
+
* @returns {Timestamp} A new Timestamp representing the start of the week
|
|
343
411
|
*/
|
|
344
412
|
export function getStartOfWeek(timestamp, weekdays, today) {
|
|
345
413
|
let start = cloneTimestamp(timestamp);
|
|
@@ -359,12 +427,12 @@ export function getStartOfWeek(timestamp, weekdays, today) {
|
|
|
359
427
|
return start;
|
|
360
428
|
}
|
|
361
429
|
/**
|
|
362
|
-
* Returns the end of the week
|
|
363
|
-
* If
|
|
364
|
-
* @param {Timestamp} timestamp The
|
|
430
|
+
* Returns the end of the week for a Timestamp and weekday set.
|
|
431
|
+
* If a current Timestamp is provided, the returned Timestamp includes updated relative information.
|
|
432
|
+
* @param {Timestamp} timestamp The Timestamp to use to find the end of the week
|
|
365
433
|
* @param {number[]} weekdays The array is [0,1,2,3,4,5,6] where 0=Sunday and 6=Saturday
|
|
366
|
-
* @param {Timestamp=} today
|
|
367
|
-
* @returns {Timestamp} A new
|
|
434
|
+
* @param {Timestamp=} today Current timestamp used to update relative information
|
|
435
|
+
* @returns {Timestamp} A new Timestamp representing the end of the week
|
|
368
436
|
*/
|
|
369
437
|
export function getEndOfWeek(timestamp, weekdays, today) {
|
|
370
438
|
let end = cloneTimestamp(timestamp);
|
|
@@ -386,9 +454,9 @@ export function getEndOfWeek(timestamp, weekdays, today) {
|
|
|
386
454
|
return end;
|
|
387
455
|
}
|
|
388
456
|
/**
|
|
389
|
-
* Finds the start of the month based on the passed in
|
|
390
|
-
* @param {Timestamp} timestamp The
|
|
391
|
-
* @returns {Timestamp} A
|
|
457
|
+
* Finds the start of the month based on the passed in Timestamp
|
|
458
|
+
* @param {Timestamp} timestamp The Timestamp to use to find the start of the month
|
|
459
|
+
* @returns {Timestamp} A Timestamp of the start of the month
|
|
392
460
|
*/
|
|
393
461
|
export function getStartOfMonth(timestamp) {
|
|
394
462
|
let start = cloneTimestamp(timestamp);
|
|
@@ -397,9 +465,9 @@ export function getStartOfMonth(timestamp) {
|
|
|
397
465
|
return start;
|
|
398
466
|
}
|
|
399
467
|
/**
|
|
400
|
-
* Finds the end of the month based on the passed in
|
|
401
|
-
* @param {Timestamp} timestamp The
|
|
402
|
-
* @returns {Timestamp} A
|
|
468
|
+
* Finds the end of the month based on the passed in Timestamp
|
|
469
|
+
* @param {Timestamp} timestamp The Timestamp to use to find the end of the month
|
|
470
|
+
* @returns {Timestamp} A Timestamp of the end of the month
|
|
403
471
|
*/
|
|
404
472
|
export function getEndOfMonth(timestamp) {
|
|
405
473
|
let end = cloneTimestamp(timestamp);
|
|
@@ -445,10 +513,11 @@ export function parseTime(input) {
|
|
|
445
513
|
return false;
|
|
446
514
|
}
|
|
447
515
|
/**
|
|
448
|
-
* Compares two
|
|
449
|
-
*
|
|
450
|
-
* @param {Timestamp}
|
|
451
|
-
* @
|
|
516
|
+
* Compares two Timestamp objects for exact date, time, and timezone equality.
|
|
517
|
+
*
|
|
518
|
+
* @param {Timestamp} ts1 First Timestamp object.
|
|
519
|
+
* @param {Timestamp} ts2 Second Timestamp object.
|
|
520
|
+
* @returns {boolean} True when both timestamps match exactly.
|
|
452
521
|
*/
|
|
453
522
|
export function compareTimestamps(ts1, ts2) {
|
|
454
523
|
if (!ts1 || !ts2)
|
|
@@ -463,41 +532,44 @@ export function compareTimestamps(ts1, ts2) {
|
|
|
463
532
|
ts1.timezone === ts2.timezone);
|
|
464
533
|
}
|
|
465
534
|
/**
|
|
466
|
-
* Compares the date of two
|
|
467
|
-
*
|
|
468
|
-
* @param {Timestamp}
|
|
469
|
-
* @
|
|
535
|
+
* Compares the calendar date portion of two Timestamp objects.
|
|
536
|
+
*
|
|
537
|
+
* @param {Timestamp} ts1 First Timestamp object.
|
|
538
|
+
* @param {Timestamp} ts2 Second Timestamp object.
|
|
539
|
+
* @returns {boolean} True when both dates are the same.
|
|
470
540
|
*/
|
|
471
541
|
export function compareDate(ts1, ts2) {
|
|
472
542
|
return getDate(ts1) === getDate(ts2);
|
|
473
543
|
}
|
|
474
544
|
/**
|
|
475
|
-
* Compares the time of two
|
|
476
|
-
*
|
|
477
|
-
* @param {Timestamp}
|
|
478
|
-
* @
|
|
545
|
+
* Compares the formatted time portion of two Timestamp objects.
|
|
546
|
+
*
|
|
547
|
+
* @param {Timestamp} ts1 First Timestamp object.
|
|
548
|
+
* @param {Timestamp} ts2 Second Timestamp object.
|
|
549
|
+
* @returns {boolean} True when both times are the same.
|
|
479
550
|
*/
|
|
480
551
|
export function compareTime(ts1, ts2) {
|
|
481
552
|
return getTime(ts1) === getTime(ts2);
|
|
482
553
|
}
|
|
483
554
|
/**
|
|
484
|
-
* Compares the date and time of two
|
|
485
|
-
*
|
|
486
|
-
* @param {Timestamp}
|
|
487
|
-
* @
|
|
555
|
+
* Compares the formatted date and time portions of two Timestamp objects.
|
|
556
|
+
*
|
|
557
|
+
* @param {Timestamp} ts1 First Timestamp object.
|
|
558
|
+
* @param {Timestamp} ts2 Second Timestamp object.
|
|
559
|
+
* @returns {boolean} True when both date-time values are the same.
|
|
488
560
|
*/
|
|
489
561
|
export function compareDateTime(ts1, ts2) {
|
|
490
562
|
return getDateTime(ts1) === getDateTime(ts2);
|
|
491
563
|
}
|
|
492
564
|
/**
|
|
493
|
-
*
|
|
565
|
+
* Converts a supported date or date-time string into a formatted Timestamp object.
|
|
494
566
|
*
|
|
495
567
|
* If `now` is supplied, the returned timestamp also includes relative flags
|
|
496
568
|
* such as `past`, `current`, `future`, and `currentWeekday`.
|
|
497
569
|
*
|
|
498
|
-
* @param {string} input
|
|
499
|
-
* @param {Timestamp} now
|
|
500
|
-
* @returns {Timestamp}
|
|
570
|
+
* @param {string} input Date or date-time string, such as `YYYY-MM-DD`, `YYYY-MM-DD HH:mm:ss`, or an ISO-like value with optional milliseconds and timezone suffix.
|
|
571
|
+
* @param {Timestamp} now Optional Timestamp used to calculate relative flags.
|
|
572
|
+
* @returns {Timestamp} Formatted Timestamp object, or `null` when the input cannot be parsed.
|
|
501
573
|
*/
|
|
502
574
|
export function parseTimestamp(input, now = null) {
|
|
503
575
|
let timestamp = parsed(input);
|
|
@@ -510,9 +582,10 @@ export function parseTimestamp(input, now = null) {
|
|
|
510
582
|
return timestamp;
|
|
511
583
|
}
|
|
512
584
|
/**
|
|
513
|
-
* Converts a
|
|
514
|
-
*
|
|
515
|
-
* @
|
|
585
|
+
* Converts a Timestamp date into a sortable numeric identifier.
|
|
586
|
+
*
|
|
587
|
+
* @param {Timestamp} timestamp Timestamp object to read.
|
|
588
|
+
* @returns {number} Numeric date identifier.
|
|
516
589
|
*/
|
|
517
590
|
export function getDayIdentifier(timestamp) {
|
|
518
591
|
return ((timestamp.year ?? 0) * 100000000 +
|
|
@@ -520,9 +593,10 @@ export function getDayIdentifier(timestamp) {
|
|
|
520
593
|
(timestamp.day ?? 0) * 10000);
|
|
521
594
|
}
|
|
522
595
|
/**
|
|
523
|
-
* Converts a
|
|
524
|
-
*
|
|
525
|
-
* @
|
|
596
|
+
* Converts a Timestamp time into a sortable numeric identifier.
|
|
597
|
+
*
|
|
598
|
+
* @param {Timestamp} timestamp Timestamp object to read.
|
|
599
|
+
* @returns {number} Numeric time identifier.
|
|
526
600
|
*/
|
|
527
601
|
export function getTimeIdentifier(timestamp) {
|
|
528
602
|
return (timestamp.hour ?? 0) * 100 + (timestamp.minute ?? 0);
|
|
@@ -535,17 +609,18 @@ function getTimeComparisonValue(timestamp) {
|
|
|
535
609
|
(timestamp.millisecond ?? 0));
|
|
536
610
|
}
|
|
537
611
|
/**
|
|
538
|
-
* Converts a
|
|
539
|
-
*
|
|
540
|
-
* @
|
|
612
|
+
* Converts a Timestamp date and time into a sortable numeric identifier.
|
|
613
|
+
*
|
|
614
|
+
* @param {Timestamp} timestamp Timestamp object to read.
|
|
615
|
+
* @returns {number} Numeric date-time identifier.
|
|
541
616
|
*/
|
|
542
617
|
export function getDayTimeIdentifier(timestamp) {
|
|
543
618
|
return getDayIdentifier(timestamp) + getTimeIdentifier(timestamp);
|
|
544
619
|
}
|
|
545
620
|
/**
|
|
546
|
-
* Returns the difference between two
|
|
547
|
-
* @param {Timestamp} ts1 The first
|
|
548
|
-
* @param {Timestamp} ts2 The second
|
|
621
|
+
* Returns the difference between two Timestamps
|
|
622
|
+
* @param {Timestamp} ts1 The first Timestamp
|
|
623
|
+
* @param {Timestamp} ts2 The second Timestamp
|
|
549
624
|
* @param {boolean=} strict Optional flag to not to return negative numbers
|
|
550
625
|
* @returns {number} The difference
|
|
551
626
|
*/
|
|
@@ -560,11 +635,16 @@ export function diffTimestamp(ts1, ts2, strict = false) {
|
|
|
560
635
|
return utc2 - utc1;
|
|
561
636
|
}
|
|
562
637
|
/**
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
638
|
+
* Returns a Timestamp with relative flags compared to a supplied `now` value.
|
|
639
|
+
*
|
|
640
|
+
* The returned object includes `past`, `current`, `future`, and
|
|
641
|
+
* `currentWeekday` flags. Pass `true` for `time` when both values should be
|
|
642
|
+
* compared at time-of-day precision.
|
|
643
|
+
*
|
|
644
|
+
* @param {Timestamp} timestamp Timestamp object to update.
|
|
645
|
+
* @param {Timestamp} now Timestamp representing the comparison point.
|
|
646
|
+
* @param {boolean=} time Include time-of-day in the comparison when true.
|
|
647
|
+
* @returns {Timestamp} New Timestamp object with relative flags.
|
|
568
648
|
*/
|
|
569
649
|
export function updateRelative(timestamp, now, time = false) {
|
|
570
650
|
const ts = cloneTimestamp(timestamp);
|
|
@@ -588,10 +668,10 @@ export function updateRelative(timestamp, now, time = false) {
|
|
|
588
668
|
* The returned timestamp has updated hour/minute fields and clears second and
|
|
589
669
|
* millisecond precision because this helper is minute-oriented.
|
|
590
670
|
*
|
|
591
|
-
* @param {Timestamp} timestamp The
|
|
671
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
592
672
|
* @param {number} minutes The number of minutes to set from midnight
|
|
593
|
-
* @param {Timestamp=} now Optional
|
|
594
|
-
* @returns {Timestamp} A new
|
|
673
|
+
* @param {Timestamp=} now Optional Timestamp representing current date and time
|
|
674
|
+
* @returns {Timestamp} A new Timestamp
|
|
595
675
|
*/
|
|
596
676
|
export function updateMinutes(timestamp, minutes, now = null) {
|
|
597
677
|
let ts = cloneTimestamp(timestamp);
|
|
@@ -607,8 +687,8 @@ export function updateMinutes(timestamp, minutes, now = null) {
|
|
|
607
687
|
return freezeTimestamp(ts);
|
|
608
688
|
}
|
|
609
689
|
/**
|
|
610
|
-
* Updates the
|
|
611
|
-
* @param {Timestamp} timestamp The
|
|
690
|
+
* Updates the Timestamp with the weekday
|
|
691
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
612
692
|
* @returns A new Timestamp
|
|
613
693
|
*/
|
|
614
694
|
export function updateWeekday(timestamp) {
|
|
@@ -617,8 +697,8 @@ export function updateWeekday(timestamp) {
|
|
|
617
697
|
return freezeTimestamp(ts);
|
|
618
698
|
}
|
|
619
699
|
/**
|
|
620
|
-
* Updates the
|
|
621
|
-
* @param {Timestamp} timestamp The
|
|
700
|
+
* Updates the Timestamp with the day of the year (doy)
|
|
701
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
622
702
|
* @returns A new Timestamp
|
|
623
703
|
*/
|
|
624
704
|
export function updateDayOfYear(timestamp) {
|
|
@@ -627,9 +707,9 @@ export function updateDayOfYear(timestamp) {
|
|
|
627
707
|
return freezeTimestamp(ts);
|
|
628
708
|
}
|
|
629
709
|
/**
|
|
630
|
-
* Updates the
|
|
631
|
-
* @param {Timestamp} timestamp The
|
|
632
|
-
* @returns A new
|
|
710
|
+
* Updates the Timestamp with the workweek
|
|
711
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
712
|
+
* @returns A new Timestamp
|
|
633
713
|
*/
|
|
634
714
|
export function updateWorkWeek(timestamp) {
|
|
635
715
|
const ts = cloneTimestamp(timestamp);
|
|
@@ -682,13 +762,13 @@ function isTimestampInDisabledDay(timestamp, day) {
|
|
|
682
762
|
return disabledDay !== null && getDayIdentifier(disabledDay) === target;
|
|
683
763
|
}
|
|
684
764
|
/**
|
|
685
|
-
* Updates the passed
|
|
686
|
-
* @param {Timestamp} timestamp The
|
|
765
|
+
* Updates the passed Timestamp with disabled, if needed
|
|
766
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
687
767
|
* @param {string} [disabledBefore] In `YYYY-MM-DD` format
|
|
688
768
|
* @param {string} [disabledAfter] In `YYYY-MM-DD` format
|
|
689
769
|
* @param {number[]} [disabledWeekdays] An array of numbers representing weekdays [0 = Sun, ..., 6 = Sat]
|
|
690
770
|
* @param {DisabledDays} [disabledDays] An array of days in 'YYYY-MM-DD' format. If an array with a pair of dates is in first array, then this is treated as a range. Object entries can include date/from/to plus color metadata.
|
|
691
|
-
* @returns A new
|
|
771
|
+
* @returns A new Timestamp
|
|
692
772
|
*/
|
|
693
773
|
export function updateDisabled(timestamp, disabledBefore, disabledAfter, disabledWeekdays, disabledDays) {
|
|
694
774
|
let ts = cloneTimestamp(timestamp);
|
|
@@ -730,9 +810,9 @@ export function updateDisabled(timestamp, disabledBefore, disabledAfter, disable
|
|
|
730
810
|
return freezeTimestamp(ts);
|
|
731
811
|
}
|
|
732
812
|
/**
|
|
733
|
-
* Updates the passed
|
|
734
|
-
* @param {Timestamp} timestamp The
|
|
735
|
-
* @returns A new
|
|
813
|
+
* Updates the passed Timestamp with formatted data (time string, date string, weekday, day of year and workweek)
|
|
814
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
815
|
+
* @returns A new Timestamp
|
|
736
816
|
*/
|
|
737
817
|
export function updateFormatted(timestamp) {
|
|
738
818
|
const ts = cloneTimestamp(timestamp);
|
|
@@ -745,8 +825,8 @@ export function updateFormatted(timestamp) {
|
|
|
745
825
|
return freezeTimestamp(ts);
|
|
746
826
|
}
|
|
747
827
|
/**
|
|
748
|
-
* Returns day of the year (doy) for the passed in
|
|
749
|
-
* @param {Timestamp} timestamp The
|
|
828
|
+
* Returns day of the year (doy) for the passed in Timestamp
|
|
829
|
+
* @param {Timestamp} timestamp The Timestamp to use
|
|
750
830
|
* @returns {number} The day of the year
|
|
751
831
|
*/
|
|
752
832
|
export function getDayOfYear(timestamp) {
|
|
@@ -760,8 +840,8 @@ export function getDayOfYear(timestamp) {
|
|
|
760
840
|
1000);
|
|
761
841
|
}
|
|
762
842
|
/**
|
|
763
|
-
* Returns workweek for the passed in
|
|
764
|
-
* @param {Timestamp} timestamp The
|
|
843
|
+
* Returns workweek for the passed in Timestamp
|
|
844
|
+
* @param {Timestamp} timestamp The Timestamp to use
|
|
765
845
|
* @returns {number} The work week
|
|
766
846
|
*/
|
|
767
847
|
export function getWorkWeek(timestamp) {
|
|
@@ -787,8 +867,8 @@ export function getWorkWeek(timestamp) {
|
|
|
787
867
|
return weekNumber;
|
|
788
868
|
}
|
|
789
869
|
/**
|
|
790
|
-
* Returns weekday for the passed in
|
|
791
|
-
* @param {Timestamp} timestamp The
|
|
870
|
+
* Returns weekday for the passed in Timestamp
|
|
871
|
+
* @param {Timestamp} timestamp The Timestamp to use
|
|
792
872
|
* @returns {number} The weekday
|
|
793
873
|
*/
|
|
794
874
|
export function getWeekday(timestamp) {
|
|
@@ -813,17 +893,19 @@ export function getWeekday(timestamp) {
|
|
|
813
893
|
return weekday ?? 0;
|
|
814
894
|
}
|
|
815
895
|
/**
|
|
816
|
-
*
|
|
817
|
-
*
|
|
818
|
-
* @
|
|
896
|
+
* Returns an immutable copy of a Timestamp object.
|
|
897
|
+
*
|
|
898
|
+
* @param {Timestamp} timestamp Timestamp object to copy.
|
|
899
|
+
* @returns {Timestamp} Frozen Timestamp copy.
|
|
819
900
|
*/
|
|
820
901
|
export function copyTimestamp(timestamp) {
|
|
821
902
|
return freezeTimestamp(timestamp);
|
|
822
903
|
}
|
|
823
904
|
/**
|
|
824
|
-
*
|
|
825
|
-
*
|
|
826
|
-
* @
|
|
905
|
+
* Formats the date portion of a Timestamp object.
|
|
906
|
+
*
|
|
907
|
+
* @param {Timestamp} timestamp Timestamp object to format.
|
|
908
|
+
* @returns {string} Date string such as `YYYY-MM-DD`.
|
|
827
909
|
*/
|
|
828
910
|
export function getDate(timestamp) {
|
|
829
911
|
let str = `${padNumber(timestamp.year, 4)}-${padNumber(timestamp.month, 2)}`;
|
|
@@ -832,9 +914,13 @@ export function getDate(timestamp) {
|
|
|
832
914
|
return str;
|
|
833
915
|
}
|
|
834
916
|
/**
|
|
835
|
-
*
|
|
836
|
-
*
|
|
837
|
-
*
|
|
917
|
+
* Formats the time portion of a Timestamp object.
|
|
918
|
+
*
|
|
919
|
+
* Minute precision is formatted as `HH:mm`; second precision as `HH:mm:ss`;
|
|
920
|
+
* millisecond precision as `HH:mm:ss.SSS`.
|
|
921
|
+
*
|
|
922
|
+
* @param {Timestamp} timestamp Timestamp object to format.
|
|
923
|
+
* @returns {string} Time string, or an empty string when the timestamp has no time.
|
|
838
924
|
*/
|
|
839
925
|
export function getTime(timestamp) {
|
|
840
926
|
if (!timestamp.hasTime) {
|
|
@@ -850,31 +936,32 @@ export function getTime(timestamp) {
|
|
|
850
936
|
return time;
|
|
851
937
|
}
|
|
852
938
|
/**
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
* @
|
|
939
|
+
* Formats a Timestamp as date plus time.
|
|
940
|
+
*
|
|
941
|
+
* @param {Timestamp} timestamp Timestamp object to format.
|
|
942
|
+
* @returns {string} Date-time string such as `YYYY-MM-DD HH:mm`.
|
|
856
943
|
*/
|
|
857
944
|
export function getDateTime(timestamp) {
|
|
858
945
|
return getDate(timestamp) + " " + (timestamp.hasTime ? getTime(timestamp) : "00:00");
|
|
859
946
|
}
|
|
860
947
|
/**
|
|
861
|
-
*
|
|
862
|
-
* @param {Timestamp} timestamp The
|
|
948
|
+
* Alias for relativeDays.
|
|
949
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
863
950
|
* @param {function} [mover=nextDay] The mover function to use (ie: {nextDay} or {prevDay}).
|
|
864
951
|
* @param {number} [days=1] The number of days to move.
|
|
865
952
|
* @param {number[]} [allowedWeekdays=[ 0, 1, 2, 3, 4, 5, 6 ]] An array of numbers representing the weekdays. ie: [0 = Sun, ..., 6 = Sat].
|
|
866
|
-
* @returns A new
|
|
953
|
+
* @returns A new Timestamp
|
|
867
954
|
*/
|
|
868
955
|
export function moveRelativeDays(timestamp, mover = nextDay, days = 1, allowedWeekdays = [0, 1, 2, 3, 4, 5, 6]) {
|
|
869
956
|
return relativeDays(timestamp, mover, days, allowedWeekdays);
|
|
870
957
|
}
|
|
871
958
|
/**
|
|
872
|
-
* Moves the
|
|
873
|
-
* @param {Timestamp} timestamp The
|
|
959
|
+
* Moves the Timestamp the number of relative days
|
|
960
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
874
961
|
* @param {function} [mover=nextDay] The mover function to use (ie: {nextDay} or {prevDay}).
|
|
875
962
|
* @param {number} [days=1] The number of days to move.
|
|
876
963
|
* @param {number[]} [allowedWeekdays=[ 0, 1, 2, 3, 4, 5, 6 ]] An array of numbers representing the weekdays. ie: [0 = Sun, ..., 6 = Sat].
|
|
877
|
-
* @returns A new
|
|
964
|
+
* @returns A new Timestamp
|
|
878
965
|
*/
|
|
879
966
|
export function relativeDays(timestamp, mover = nextDay, days = 1, allowedWeekdays = [0, 1, 2, 3, 4, 5, 6]) {
|
|
880
967
|
let ts = copyTimestamp(timestamp);
|
|
@@ -890,12 +977,12 @@ export function relativeDays(timestamp, mover = nextDay, days = 1, allowedWeekda
|
|
|
890
977
|
return ts;
|
|
891
978
|
}
|
|
892
979
|
/**
|
|
893
|
-
* Finds the specified weekday (forward or back) based on the
|
|
894
|
-
* @param {Timestamp} timestamp The
|
|
980
|
+
* Finds the specified weekday (forward or back) based on the Timestamp
|
|
981
|
+
* @param {Timestamp} timestamp The Timestamp to transform
|
|
895
982
|
* @param {number} weekday The weekday number (Sun = 0, ..., Sat = 6)
|
|
896
983
|
* @param {function} [mover=nextDay] The function to use ({prevDay} or {nextDay}).
|
|
897
984
|
* @param {number} [maxDays=6] The number of days to look forward or back.
|
|
898
|
-
* @returns A new
|
|
985
|
+
* @returns A new Timestamp
|
|
899
986
|
*/
|
|
900
987
|
export function findWeekday(timestamp, weekday, mover = nextDay, maxDays = 6) {
|
|
901
988
|
let ts = copyTimestamp(timestamp);
|
|
@@ -904,18 +991,22 @@ export function findWeekday(timestamp, weekday, mover = nextDay, maxDays = 6) {
|
|
|
904
991
|
return ts;
|
|
905
992
|
}
|
|
906
993
|
/**
|
|
907
|
-
* Creates an
|
|
908
|
-
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
* @param {
|
|
913
|
-
* @param {
|
|
914
|
-
* @param {
|
|
915
|
-
* @param {
|
|
916
|
-
* @param {
|
|
917
|
-
* @param {
|
|
918
|
-
* @
|
|
994
|
+
* Creates an inclusive list of Timestamp days between start and end.
|
|
995
|
+
*
|
|
996
|
+
* The returned days are formatted, marked with relative flags against `now`,
|
|
997
|
+
* and can include disabled metadata when disabled options are supplied.
|
|
998
|
+
*
|
|
999
|
+
* @param {Timestamp} start First day in the list.
|
|
1000
|
+
* @param {Timestamp} end Last day boundary for the list.
|
|
1001
|
+
* @param {Timestamp} now Timestamp used to calculate relative flags.
|
|
1002
|
+
* @param {number[]} weekdays Weekday numbers to include, from `0` Sunday to `6` Saturday.
|
|
1003
|
+
* @param {string} [disabledBefore] Disable days before this `YYYY-MM-DD` date.
|
|
1004
|
+
* @param {string} [disabledAfter] Disable days after this `YYYY-MM-DD` date.
|
|
1005
|
+
* @param {number[]} [disabledWeekdays] Weekday numbers to mark disabled.
|
|
1006
|
+
* @param {DisabledDays} [disabledDays] Specific dates or date ranges to mark disabled.
|
|
1007
|
+
* @param {number} [max=42] Maximum number of days to return.
|
|
1008
|
+
* @param {number} [min=0] Minimum number of days to return.
|
|
1009
|
+
* @returns {Timestamp[]} Timestamp days.
|
|
919
1010
|
*/
|
|
920
1011
|
export function createDayList(start, end, now, weekdays = [0, 1, 2, 3, 4, 5, 6], disabledBefore = undefined, disabledAfter = undefined, disabledWeekdays = [], disabledDays = [], max = 42, min = 0) {
|
|
921
1012
|
const begin = getDayIdentifier(start);
|
|
@@ -947,13 +1038,14 @@ export function createDayList(start, end, now, weekdays = [0, 1, 2, 3, 4, 5, 6],
|
|
|
947
1038
|
return days;
|
|
948
1039
|
}
|
|
949
1040
|
/**
|
|
950
|
-
* Creates an array of interval
|
|
951
|
-
*
|
|
952
|
-
* @param {
|
|
953
|
-
* @param {number}
|
|
954
|
-
* @param {number}
|
|
955
|
-
* @param {
|
|
956
|
-
* @
|
|
1041
|
+
* Creates an array of interval Timestamp objects for one day.
|
|
1042
|
+
*
|
|
1043
|
+
* @param {Timestamp} timestamp Base date for the intervals.
|
|
1044
|
+
* @param {number} first Starting interval index.
|
|
1045
|
+
* @param {number} minutes Minutes between intervals, such as 60, 30, or 15.
|
|
1046
|
+
* @param {number} count Number of intervals to create.
|
|
1047
|
+
* @param {Timestamp} now Timestamp used to calculate relative flags.
|
|
1048
|
+
* @returns {Timestamp[]} Interval Timestamp objects.
|
|
957
1049
|
*/
|
|
958
1050
|
export function createIntervalList(timestamp, first, minutes, count, now) {
|
|
959
1051
|
const intervals = [];
|
|
@@ -965,76 +1057,109 @@ export function createIntervalList(timestamp, first, minutes, count, now) {
|
|
|
965
1057
|
}
|
|
966
1058
|
/**
|
|
967
1059
|
* @callback getOptions
|
|
968
|
-
* @param {Timestamp} timestamp A
|
|
1060
|
+
* @param {Timestamp} timestamp A Timestamp object
|
|
969
1061
|
* @param {boolean} short True if using short options
|
|
970
1062
|
* @returns {Object} An Intl object representing options to be used
|
|
971
1063
|
*/
|
|
972
1064
|
/**
|
|
973
1065
|
* @callback formatter
|
|
974
|
-
* @param {Timestamp} timestamp The
|
|
1066
|
+
* @param {Timestamp} timestamp The Timestamp being used
|
|
975
1067
|
* @param {boolean} short If short format is being requested
|
|
976
|
-
* @returns {string} The localized string of the formatted
|
|
1068
|
+
* @returns {string} The localized string of the formatted Timestamp
|
|
977
1069
|
*/
|
|
978
|
-
|
|
979
|
-
* Returns a locale formatter backed by `Intl.DateTimeFormat`.
|
|
980
|
-
*
|
|
981
|
-
* The helper is SSR-safe: if `Intl.DateTimeFormat` is unavailable in a target
|
|
982
|
-
* runtime, it returns a formatter that produces an empty string instead of
|
|
983
|
-
* throwing during module load.
|
|
984
|
-
*
|
|
985
|
-
* @param {string} locale The locale to use (ie: en-US)
|
|
986
|
-
* @param {getOptions} cb The function to call for options. This function should return an Intl formatted object. The function is passed (timestamp, short).
|
|
987
|
-
* @returns {formatter} The function has params (timestamp, short). The short is to use the short options.
|
|
988
|
-
*/
|
|
989
|
-
export function createNativeLocaleFormatter(locale, cb) {
|
|
1070
|
+
function createNativeLocaleFormatterByMode(locale, cb, toDate) {
|
|
990
1071
|
const emptyFormatter = () => "";
|
|
991
|
-
/* istanbul ignore next */
|
|
992
1072
|
if (typeof Intl === "undefined" || typeof Intl.DateTimeFormat === "undefined") {
|
|
993
1073
|
return emptyFormatter;
|
|
994
1074
|
}
|
|
995
1075
|
return (timestamp, short) => {
|
|
996
1076
|
try {
|
|
997
1077
|
const intlFormatter = new Intl.DateTimeFormat(locale || undefined, cb(timestamp, short));
|
|
998
|
-
return intlFormatter.format(
|
|
1078
|
+
return intlFormatter.format(toDate(timestamp));
|
|
999
1079
|
}
|
|
1000
|
-
catch (e)
|
|
1080
|
+
catch (e) {
|
|
1001
1081
|
console.error(`Intl.DateTimeFormat: ${e.message} -> ${getDateTime(timestamp)}`);
|
|
1002
1082
|
return "";
|
|
1003
1083
|
}
|
|
1004
1084
|
};
|
|
1005
1085
|
}
|
|
1006
1086
|
/**
|
|
1007
|
-
*
|
|
1008
|
-
*
|
|
1009
|
-
*
|
|
1010
|
-
*
|
|
1087
|
+
* Returns a host-local locale formatter backed by `Intl.DateTimeFormat`.
|
|
1088
|
+
*
|
|
1089
|
+
* The helper is SSR-safe: if `Intl.DateTimeFormat` is unavailable in a target
|
|
1090
|
+
* runtime, it returns a formatter that produces an empty string instead of
|
|
1091
|
+
* throwing during module load.
|
|
1092
|
+
*
|
|
1093
|
+
* Use `createNativeLocaleFormatterUTC()` when Timestamp values should be read
|
|
1094
|
+
* as UTC fields before formatting.
|
|
1095
|
+
*
|
|
1096
|
+
* @param {string} locale The locale to use (ie: en-US)
|
|
1097
|
+
* @param {getOptions} cb The function to call for options. This function should return an Intl formatted object. The function is passed (timestamp, short).
|
|
1098
|
+
* @returns {formatter} The function has params (timestamp, short). The short is to use the short options.
|
|
1099
|
+
*/
|
|
1100
|
+
export function createNativeLocaleFormatter(locale, cb) {
|
|
1101
|
+
return createNativeLocaleFormatterByMode(locale, cb, makeDateTime);
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Returns a UTC locale formatter backed by `Intl.DateTimeFormat`.
|
|
1105
|
+
*
|
|
1106
|
+
* This helper constructs the native `Date` with UTC fields before formatting.
|
|
1107
|
+
* Pair it with `timeZone: "UTC"` when calendar labels must remain pinned to
|
|
1108
|
+
* the Timestamp's UTC date instead of the viewer's local timezone.
|
|
1109
|
+
*
|
|
1110
|
+
* @param {string} locale The locale to use (ie: en-US)
|
|
1111
|
+
* @param {getOptions} cb The function to call for options. This function should return an Intl formatted object. The function is passed (timestamp, short).
|
|
1112
|
+
* @returns {formatter} The function has params (timestamp, short). The short is to use the short options.
|
|
1113
|
+
*/
|
|
1114
|
+
export function createNativeLocaleFormatterUTC(locale, cb) {
|
|
1115
|
+
return createNativeLocaleFormatterByMode(locale, cb, makeDateTimeUTC);
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Converts a Timestamp date into a host-local JavaScript Date.
|
|
1119
|
+
*
|
|
1120
|
+
* @param {Timestamp} timestamp Timestamp object to convert.
|
|
1121
|
+
* @returns {Date} Host-local JavaScript Date object.
|
|
1011
1122
|
*/
|
|
1012
|
-
export function makeDate(timestamp
|
|
1013
|
-
if (utc)
|
|
1014
|
-
return new Date(Date.UTC(timestamp.year, timestamp.month - 1, timestamp.day, 0, 0));
|
|
1123
|
+
export function makeDate(timestamp) {
|
|
1015
1124
|
return new Date(timestamp.year, timestamp.month - 1, timestamp.day, 0, 0);
|
|
1016
1125
|
}
|
|
1017
1126
|
/**
|
|
1018
|
-
*
|
|
1019
|
-
*
|
|
1020
|
-
* @param {
|
|
1021
|
-
* @returns {Date}
|
|
1127
|
+
* Converts a Timestamp date into a UTC JavaScript Date.
|
|
1128
|
+
*
|
|
1129
|
+
* @param {Timestamp} timestamp Timestamp object to convert.
|
|
1130
|
+
* @returns {Date} JavaScript Date object built with `Date.UTC()`.
|
|
1022
1131
|
*/
|
|
1023
|
-
export function
|
|
1024
|
-
|
|
1025
|
-
|
|
1132
|
+
export function makeDateUTC(timestamp) {
|
|
1133
|
+
return new Date(Date.UTC(timestamp.year, timestamp.month - 1, timestamp.day, 0, 0));
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Converts a Timestamp date and time into a host-local JavaScript Date.
|
|
1137
|
+
*
|
|
1138
|
+
* @param {Timestamp} timestamp Timestamp object to convert.
|
|
1139
|
+
* @returns {Date} Host-local JavaScript Date object.
|
|
1140
|
+
*/
|
|
1141
|
+
export function makeDateTime(timestamp) {
|
|
1026
1142
|
return new Date(timestamp.year, timestamp.month - 1, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second ?? 0, timestamp.millisecond ?? 0);
|
|
1027
1143
|
}
|
|
1028
1144
|
/**
|
|
1029
|
-
* Converts a
|
|
1145
|
+
* Converts a Timestamp date and time into a UTC JavaScript Date.
|
|
1030
1146
|
*
|
|
1031
|
-
*
|
|
1147
|
+
* @param {Timestamp} timestamp Timestamp object to convert.
|
|
1148
|
+
* @returns {Date} JavaScript Date object built with `Date.UTC()`.
|
|
1149
|
+
*/
|
|
1150
|
+
export function makeDateTimeUTC(timestamp) {
|
|
1151
|
+
return new Date(Date.UTC(timestamp.year, timestamp.month - 1, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second ?? 0, timestamp.millisecond ?? 0));
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* Converts a Timestamp to a local JavaScript Date.
|
|
1032
1155
|
*
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1156
|
+
* This is equivalent to `makeDateTime(timestamp)`.
|
|
1157
|
+
*
|
|
1158
|
+
* @param {Timestamp} timestamp Timestamp object to convert.
|
|
1159
|
+
* @returns {Date} Local JavaScript Date object.
|
|
1035
1160
|
*/
|
|
1036
1161
|
export function getDateObject(timestamp) {
|
|
1037
|
-
return makeDateTime(timestamp
|
|
1162
|
+
return makeDateTime(timestamp);
|
|
1038
1163
|
}
|
|
1039
1164
|
/**
|
|
1040
1165
|
* Validates if the input is a finite number.
|
|
@@ -1047,10 +1172,11 @@ export function validateNumber(input) {
|
|
|
1047
1172
|
return isFinite(Number(input));
|
|
1048
1173
|
}
|
|
1049
1174
|
/**
|
|
1050
|
-
*
|
|
1051
|
-
*
|
|
1052
|
-
* @param {
|
|
1053
|
-
* @
|
|
1175
|
+
* Finds the latest Timestamp in an array.
|
|
1176
|
+
*
|
|
1177
|
+
* @param {Timestamp[]} timestamps Timestamp objects to compare.
|
|
1178
|
+
* @param {boolean=} useTime Include time-of-day in the comparison when true.
|
|
1179
|
+
* @returns Latest Timestamp object.
|
|
1054
1180
|
*/
|
|
1055
1181
|
export function maxTimestamp(timestamps, useTime = false) {
|
|
1056
1182
|
const func = useTime === true ? getDayTimeIdentifier : getDayIdentifier;
|
|
@@ -1059,10 +1185,11 @@ export function maxTimestamp(timestamps, useTime = false) {
|
|
|
1059
1185
|
});
|
|
1060
1186
|
}
|
|
1061
1187
|
/**
|
|
1062
|
-
*
|
|
1063
|
-
*
|
|
1064
|
-
* @param {
|
|
1065
|
-
* @
|
|
1188
|
+
* Finds the earliest Timestamp in an array.
|
|
1189
|
+
*
|
|
1190
|
+
* @param {Timestamp[]} timestamps Timestamp objects to compare.
|
|
1191
|
+
* @param {boolean=} useTime Include time-of-day in the comparison when true.
|
|
1192
|
+
* @returns Earliest Timestamp object.
|
|
1066
1193
|
*/
|
|
1067
1194
|
export function minTimestamp(timestamps, useTime = false) {
|
|
1068
1195
|
const func = useTime === true ? getDayTimeIdentifier : getDayIdentifier;
|
|
@@ -1071,12 +1198,13 @@ export function minTimestamp(timestamps, useTime = false) {
|
|
|
1071
1198
|
});
|
|
1072
1199
|
}
|
|
1073
1200
|
/**
|
|
1074
|
-
*
|
|
1075
|
-
*
|
|
1076
|
-
* @param {Timestamp}
|
|
1077
|
-
* @param {Timestamp}
|
|
1078
|
-
* @param {
|
|
1079
|
-
* @
|
|
1201
|
+
* Checks whether a Timestamp falls inside an inclusive range.
|
|
1202
|
+
*
|
|
1203
|
+
* @param {Timestamp} timestamp Timestamp object to test.
|
|
1204
|
+
* @param {Timestamp} startTimestamp Inclusive start boundary.
|
|
1205
|
+
* @param {Timestamp} endTimestamp Inclusive end boundary.
|
|
1206
|
+
* @param {boolean=} useTime Include time-of-day in the comparison when true.
|
|
1207
|
+
* @returns {boolean} True when the timestamp is inside the range.
|
|
1080
1208
|
*/
|
|
1081
1209
|
export function isBetweenDates(timestamp, startTimestamp, endTimestamp, useTime = false) {
|
|
1082
1210
|
const cd = getDayIdentifier(timestamp) + (useTime === true ? getTimeIdentifier(timestamp) : 0);
|
|
@@ -1085,12 +1213,13 @@ export function isBetweenDates(timestamp, startTimestamp, endTimestamp, useTime
|
|
|
1085
1213
|
return cd >= sd && cd <= ed;
|
|
1086
1214
|
}
|
|
1087
1215
|
/**
|
|
1088
|
-
*
|
|
1089
|
-
*
|
|
1090
|
-
* @param {Timestamp}
|
|
1091
|
-
* @param {Timestamp}
|
|
1092
|
-
* @param {Timestamp}
|
|
1093
|
-
* @
|
|
1216
|
+
* Checks whether two inclusive Timestamp ranges overlap.
|
|
1217
|
+
*
|
|
1218
|
+
* @param {Timestamp} startTimestamp Start of the first range.
|
|
1219
|
+
* @param {Timestamp} endTimestamp End of the first range.
|
|
1220
|
+
* @param {Timestamp} firstTimestamp Start of the second range.
|
|
1221
|
+
* @param {Timestamp} lastTimestamp End of the second range.
|
|
1222
|
+
* @returns {boolean} True when the ranges overlap.
|
|
1094
1223
|
*/
|
|
1095
1224
|
export function isOverlappingDates(startTimestamp, endTimestamp, firstTimestamp, lastTimestamp) {
|
|
1096
1225
|
const start = getDayIdentifier(startTimestamp);
|
|
@@ -1105,11 +1234,12 @@ export function isOverlappingDates(startTimestamp, endTimestamp, firstTimestamp,
|
|
|
1105
1234
|
/**
|
|
1106
1235
|
* Adds or subtracts date/time units from a timestamp.
|
|
1107
1236
|
*
|
|
1108
|
-
* This function returns a new frozen
|
|
1109
|
-
* timestamp passed in.
|
|
1237
|
+
* This function returns a new frozen Timestamp; it does not mutate the
|
|
1238
|
+
* timestamp passed in. Invalid target dates are normalized through JavaScript
|
|
1239
|
+
* Date rules, so month overflow can roll into the following month.
|
|
1110
1240
|
*
|
|
1111
|
-
* @param {Timestamp} timestamp
|
|
1112
|
-
* @param {Object} options
|
|
1241
|
+
* @param {Timestamp} timestamp Timestamp object to offset.
|
|
1242
|
+
* @param {Object} options Date/time units to add or subtract.
|
|
1113
1243
|
* @param {number=} options.year If positive, adds years. If negative, removes years.
|
|
1114
1244
|
* @param {number=} options.month If positive, adds months. If negative, removes month.
|
|
1115
1245
|
* @param {number=} options.day If positive, adds days. If negative, removes days.
|
|
@@ -1117,7 +1247,7 @@ export function isOverlappingDates(startTimestamp, endTimestamp, firstTimestamp,
|
|
|
1117
1247
|
* @param {number=} options.minute If positive, adds minutes. If negative, removes minutes.
|
|
1118
1248
|
* @param {number=} options.second If positive, adds seconds. If negative, removes seconds.
|
|
1119
1249
|
* @param {number=} options.millisecond If positive, adds milliseconds. If negative, removes milliseconds.
|
|
1120
|
-
* @returns {Timestamp}
|
|
1250
|
+
* @returns {Timestamp} New normalized Timestamp object.
|
|
1121
1251
|
*/
|
|
1122
1252
|
export function addToDate(timestamp, options) {
|
|
1123
1253
|
const ts = cloneTimestamp(timestamp);
|
|
@@ -1137,6 +1267,61 @@ export function addToDate(timestamp, options) {
|
|
|
1137
1267
|
ts.millisecond = (ts.millisecond ?? 0) + options.millisecond;
|
|
1138
1268
|
return updateFormatted(normalizeTimestamp(ts));
|
|
1139
1269
|
}
|
|
1270
|
+
/**
|
|
1271
|
+
* Adds or subtracts date/time units from a timestamp while clamping invalid
|
|
1272
|
+
* target month days to the last valid day in the target month.
|
|
1273
|
+
*
|
|
1274
|
+
* This helper is useful for calendar and billing workflows where adding one
|
|
1275
|
+
* month to January 31 should produce February 28/29 instead of rolling into
|
|
1276
|
+
* March. Day and time offsets still use normal JavaScript Date normalization
|
|
1277
|
+
* after the year/month clamp is applied.
|
|
1278
|
+
*
|
|
1279
|
+
* This function returns a new frozen Timestamp; it does not mutate the
|
|
1280
|
+
* timestamp passed in.
|
|
1281
|
+
*
|
|
1282
|
+
* @param {Timestamp} timestamp Timestamp object to offset.
|
|
1283
|
+
* @param {Object} options Date/time units to add or subtract.
|
|
1284
|
+
* @param {number=} options.year If positive, adds years. If negative, removes years.
|
|
1285
|
+
* @param {number=} options.month If positive, adds months. If negative, removes month.
|
|
1286
|
+
* @param {number=} options.day If positive, adds days. If negative, removes days.
|
|
1287
|
+
* @param {number=} options.hour If positive, adds hours. If negative, removes hours.
|
|
1288
|
+
* @param {number=} options.minute If positive, adds minutes. If negative, removes minutes.
|
|
1289
|
+
* @param {number=} options.second If positive, adds seconds. If negative, removes seconds.
|
|
1290
|
+
* @param {number=} options.millisecond If positive, adds milliseconds. If negative, removes milliseconds.
|
|
1291
|
+
* @returns {Timestamp} New normalized Timestamp object.
|
|
1292
|
+
*/
|
|
1293
|
+
export function addToDateClamped(timestamp, options) {
|
|
1294
|
+
const ts = cloneTimestamp(timestamp);
|
|
1295
|
+
if (options.year || options.month) {
|
|
1296
|
+
const target = normalizeYearMonth(ts.year + (options.year ?? 0), ts.month + (options.month ?? 0));
|
|
1297
|
+
ts.year = target.year;
|
|
1298
|
+
ts.month = target.month;
|
|
1299
|
+
ts.day = Math.min(ts.day, daysInMonth(ts.year, ts.month));
|
|
1300
|
+
}
|
|
1301
|
+
if (options.day)
|
|
1302
|
+
ts.day += options.day;
|
|
1303
|
+
if (options.hour)
|
|
1304
|
+
ts.hour += options.hour;
|
|
1305
|
+
if (options.minute)
|
|
1306
|
+
ts.minute += options.minute;
|
|
1307
|
+
if (options.second)
|
|
1308
|
+
ts.second = (ts.second ?? 0) + options.second;
|
|
1309
|
+
if (options.millisecond)
|
|
1310
|
+
ts.millisecond = (ts.millisecond ?? 0) + options.millisecond;
|
|
1311
|
+
return updateFormatted(normalizeTimestamp(ts));
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* Normalizes a year/month pair while keeping the day out of the calculation.
|
|
1315
|
+
* This lets clamped date math choose the final day explicitly instead of
|
|
1316
|
+
* letting JavaScript Date roll an overflowing day into the next month.
|
|
1317
|
+
*/
|
|
1318
|
+
function normalizeYearMonth(year, month) {
|
|
1319
|
+
const date = new Date(year, month - 1, 1);
|
|
1320
|
+
return {
|
|
1321
|
+
year: date.getFullYear(),
|
|
1322
|
+
month: date.getMonth() + 1,
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1140
1325
|
/**
|
|
1141
1326
|
* Normalizes a timestamp object by creating a JavaScript Date object and extracting standardized values.
|
|
1142
1327
|
* This function ensures that the timestamp values are consistent and correctly represent a valid date and time.
|
|
@@ -1170,9 +1355,9 @@ function normalizeTimestamp(ts) {
|
|
|
1170
1355
|
return freezeTimestamp(timestamp);
|
|
1171
1356
|
}
|
|
1172
1357
|
/**
|
|
1173
|
-
* Returns number of days between two
|
|
1174
|
-
* @param {Timestamp} ts1 The first
|
|
1175
|
-
* @param {Timestamp} ts2 The second
|
|
1358
|
+
* Returns number of days between two Timestamps
|
|
1359
|
+
* @param {Timestamp} ts1 The first Timestamp
|
|
1360
|
+
* @param {Timestamp} ts2 The second Timestamp
|
|
1176
1361
|
* @returns Number of days
|
|
1177
1362
|
*/
|
|
1178
1363
|
export function daysBetween(ts1, ts2) {
|
|
@@ -1180,9 +1365,9 @@ export function daysBetween(ts1, ts2) {
|
|
|
1180
1365
|
return Math.floor(diff / TIME_CONSTANTS.MILLISECONDS_IN.DAY);
|
|
1181
1366
|
}
|
|
1182
1367
|
/**
|
|
1183
|
-
* Returns number of weeks between two
|
|
1184
|
-
* @param {Timestamp} ts1 The first
|
|
1185
|
-
* @param {Timestamp} ts2 The second
|
|
1368
|
+
* Returns number of weeks between two Timestamps
|
|
1369
|
+
* @param {Timestamp} ts1 The first Timestamp
|
|
1370
|
+
* @param {Timestamp} ts2 The second Timestamp
|
|
1186
1371
|
*/
|
|
1187
1372
|
export function weeksBetween(ts1, ts2) {
|
|
1188
1373
|
let t1 = copyTimestamp(ts1);
|
|
@@ -1231,7 +1416,6 @@ export function getWeekdayFormatter() {
|
|
|
1231
1416
|
short: { timeZone: "UTC", weekday: "short" },
|
|
1232
1417
|
narrow: { timeZone: "UTC", weekday: "narrow" },
|
|
1233
1418
|
};
|
|
1234
|
-
/* istanbul ignore next */
|
|
1235
1419
|
if (typeof Intl === "undefined" || typeof Intl.DateTimeFormat === "undefined") {
|
|
1236
1420
|
return emptyFormatter;
|
|
1237
1421
|
}
|
|
@@ -1248,7 +1432,7 @@ export function getWeekdayFormatter() {
|
|
|
1248
1432
|
const intlFormatter = new Intl.DateTimeFormat(locale || undefined, resolveIntlNameFormat(options, type));
|
|
1249
1433
|
return intlFormatter.format(weekdayDateMap[weekday]);
|
|
1250
1434
|
}
|
|
1251
|
-
catch (e)
|
|
1435
|
+
catch (e) {
|
|
1252
1436
|
if (e instanceof Error) {
|
|
1253
1437
|
console.error(`Intl.DateTimeFormat: ${e.message} -> day of week: ${weekday}`);
|
|
1254
1438
|
}
|
|
@@ -1258,11 +1442,11 @@ export function getWeekdayFormatter() {
|
|
|
1258
1442
|
return weekdayFormatter;
|
|
1259
1443
|
}
|
|
1260
1444
|
/**
|
|
1261
|
-
* Retrieves
|
|
1445
|
+
* Retrieves localized weekday names.
|
|
1262
1446
|
*
|
|
1263
|
-
* @param {string} type
|
|
1264
|
-
* @param {string}
|
|
1265
|
-
* @returns {string[]}
|
|
1447
|
+
* @param {string} type Format type: `narrow`, `short`, or `long`.
|
|
1448
|
+
* @param {string} locale Locale to use for formatting, such as `en-US`.
|
|
1449
|
+
* @returns {string[]} Localized weekday names in Sunday-first order.
|
|
1266
1450
|
*/
|
|
1267
1451
|
export function getWeekdayNames(type, locale) {
|
|
1268
1452
|
const shortWeekdays = Object.keys(weekdayDateMap);
|
|
@@ -1288,7 +1472,6 @@ export function getMonthFormatter() {
|
|
|
1288
1472
|
short: { timeZone: "UTC", month: "short" },
|
|
1289
1473
|
narrow: { timeZone: "UTC", month: "narrow" },
|
|
1290
1474
|
};
|
|
1291
|
-
/* istanbul ignore next */
|
|
1292
1475
|
if (typeof Intl === "undefined" || typeof Intl.DateTimeFormat === "undefined") {
|
|
1293
1476
|
return emptyFormatter;
|
|
1294
1477
|
}
|
|
@@ -1308,7 +1491,7 @@ export function getMonthFormatter() {
|
|
|
1308
1491
|
date.setMonth(month);
|
|
1309
1492
|
return intlFormatter.format(date);
|
|
1310
1493
|
}
|
|
1311
|
-
catch (e)
|
|
1494
|
+
catch (e) {
|
|
1312
1495
|
if (e instanceof Error) {
|
|
1313
1496
|
console.error(`Intl.DateTimeFormat: ${e.message} -> month: ${month}`);
|
|
1314
1497
|
}
|
|
@@ -1318,93 +1501,14 @@ export function getMonthFormatter() {
|
|
|
1318
1501
|
return monthFormatter;
|
|
1319
1502
|
}
|
|
1320
1503
|
/**
|
|
1321
|
-
* Retrieves
|
|
1504
|
+
* Retrieves localized month names.
|
|
1322
1505
|
*
|
|
1323
|
-
* @param {string} type
|
|
1324
|
-
* @param {string}
|
|
1325
|
-
* @returns {string[]}
|
|
1506
|
+
* @param {string} type Format type: `narrow`, `short`, or `long`.
|
|
1507
|
+
* @param {string} locale Locale to use for formatting, such as `en-US`.
|
|
1508
|
+
* @returns {string[]} Localized month names in January-first order.
|
|
1326
1509
|
*/
|
|
1327
1510
|
export function getMonthNames(type, locale) {
|
|
1328
1511
|
const monthFormatter = getMonthFormatter();
|
|
1329
1512
|
return [...Array(12).keys()].map((month) => monthFormatter(month, type, locale));
|
|
1330
1513
|
}
|
|
1331
|
-
// the exports...
|
|
1332
|
-
export default {
|
|
1333
|
-
PARSE_DATETIME,
|
|
1334
|
-
PARSE_DATE,
|
|
1335
|
-
PARSE_TIME,
|
|
1336
|
-
DAYS_IN_MONTH,
|
|
1337
|
-
DAYS_IN_MONTH_LEAP,
|
|
1338
|
-
DAYS_IN_MONTH_MIN,
|
|
1339
|
-
DAYS_IN_MONTH_MAX,
|
|
1340
|
-
MONTH_MAX,
|
|
1341
|
-
MONTH_MIN,
|
|
1342
|
-
DAY_MIN,
|
|
1343
|
-
TIME_CONSTANTS,
|
|
1344
|
-
DAYS_IN_WEEK,
|
|
1345
|
-
MINUTES_IN_HOUR,
|
|
1346
|
-
HOURS_IN_DAY,
|
|
1347
|
-
FIRST_HOUR,
|
|
1348
|
-
MILLISECONDS_IN_MINUTE,
|
|
1349
|
-
MILLISECONDS_IN_HOUR,
|
|
1350
|
-
MILLISECONDS_IN_DAY,
|
|
1351
|
-
MILLISECONDS_IN_WEEK,
|
|
1352
|
-
Timestamp,
|
|
1353
|
-
TimeObject,
|
|
1354
|
-
today,
|
|
1355
|
-
getStartOfWeek,
|
|
1356
|
-
getEndOfWeek,
|
|
1357
|
-
getStartOfMonth,
|
|
1358
|
-
getEndOfMonth,
|
|
1359
|
-
parseTime,
|
|
1360
|
-
validateTimestamp,
|
|
1361
|
-
parsed,
|
|
1362
|
-
parseTimestamp,
|
|
1363
|
-
parseDate,
|
|
1364
|
-
getDayIdentifier,
|
|
1365
|
-
getTimeIdentifier,
|
|
1366
|
-
getDayTimeIdentifier,
|
|
1367
|
-
diffTimestamp,
|
|
1368
|
-
updateRelative,
|
|
1369
|
-
updateMinutes,
|
|
1370
|
-
updateWeekday,
|
|
1371
|
-
updateDayOfYear,
|
|
1372
|
-
updateWorkWeek,
|
|
1373
|
-
updateDisabled,
|
|
1374
|
-
updateFormatted,
|
|
1375
|
-
getDayOfYear,
|
|
1376
|
-
getWorkWeek,
|
|
1377
|
-
getWeekday,
|
|
1378
|
-
isLeapYear,
|
|
1379
|
-
daysInMonth,
|
|
1380
|
-
copyTimestamp,
|
|
1381
|
-
padNumber,
|
|
1382
|
-
getDate,
|
|
1383
|
-
getTime,
|
|
1384
|
-
getDateTime,
|
|
1385
|
-
nextDay,
|
|
1386
|
-
prevDay,
|
|
1387
|
-
relativeDays,
|
|
1388
|
-
findWeekday,
|
|
1389
|
-
createDayList,
|
|
1390
|
-
createIntervalList,
|
|
1391
|
-
createNativeLocaleFormatter,
|
|
1392
|
-
makeDate,
|
|
1393
|
-
makeDateTime,
|
|
1394
|
-
getDateObject,
|
|
1395
|
-
validateNumber,
|
|
1396
|
-
isBetweenDates,
|
|
1397
|
-
isOverlappingDates,
|
|
1398
|
-
daysBetween,
|
|
1399
|
-
weeksBetween,
|
|
1400
|
-
addToDate,
|
|
1401
|
-
compareTimestamps,
|
|
1402
|
-
compareDate,
|
|
1403
|
-
compareTime,
|
|
1404
|
-
compareDateTime,
|
|
1405
|
-
getWeekdayFormatter,
|
|
1406
|
-
getWeekdayNames,
|
|
1407
|
-
getMonthFormatter,
|
|
1408
|
-
getMonthNames,
|
|
1409
|
-
};
|
|
1410
1514
|
//# sourceMappingURL=index.js.map
|