date-vir 5.3.2 → 7.0.0
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/LICENSE-MIT +1 -1
- package/README.md +28 -28
- package/dist/date-operations/calculate-relative-date.d.ts +17 -0
- package/dist/{esm/date-operations → date-operations}/calculate-relative-date.js +10 -1
- package/dist/date-operations/combine-dates.d.ts +43 -0
- package/dist/{esm/date-operations → date-operations}/combine-dates.js +6 -4
- package/dist/date-operations/date-position.d.ts +149 -0
- package/dist/date-operations/date-position.js +175 -0
- package/dist/date-operations/diff-dates.d.ts +95 -0
- package/dist/date-operations/diff-dates.js +124 -0
- package/dist/extra-utils/end.d.ts +11 -0
- package/dist/extra-utils/end.js +11 -0
- package/dist/extra-utils/has-date-props.d.ts +35 -0
- package/dist/extra-utils/has-date-props.js +46 -0
- package/dist/extra-utils/js-date-conversion.d.ts +26 -0
- package/dist/extra-utils/js-date-conversion.js +28 -0
- package/dist/extra-utils/now.d.ts +56 -0
- package/dist/extra-utils/now.js +68 -0
- package/dist/extra-utils/override-date.d.ts +27 -0
- package/dist/extra-utils/override-date.js +27 -0
- package/dist/extra-utils/zero.d.ts +109 -0
- package/dist/extra-utils/zero.js +94 -0
- package/dist/formatting/formatted-string.d.ts +58 -0
- package/dist/formatting/formatted-string.js +73 -0
- package/dist/formatting/html-input.d.ts +30 -0
- package/dist/formatting/html-input.js +47 -0
- package/dist/formatting/relative-string.d.ts +53 -0
- package/dist/formatting/relative-string.js +93 -0
- package/dist/formatting/simple-strings.d.ts +187 -0
- package/dist/formatting/simple-strings.js +120 -0
- package/dist/formatting/string-format-types.d.ts +59 -0
- package/dist/formatting/string-format-types.js +19 -0
- package/dist/formatting/timestamp.d.ts +51 -0
- package/dist/formatting/timestamp.js +61 -0
- package/dist/full-date/create-full-date.d.ts +91 -0
- package/dist/full-date/create-full-date.js +142 -0
- package/dist/full-date/date-like.d.ts +9 -0
- package/dist/{esm/full-date → full-date}/format-presets.d.ts +4 -2
- package/dist/{esm/full-date → full-date}/format-presets.js +4 -2
- package/dist/full-date/full-date-parts.d.ts +44 -0
- package/dist/full-date/full-date-parts.js +47 -0
- package/dist/full-date/full-date-shape.d.ts +108 -0
- package/dist/full-date/full-date-shape.js +45 -0
- package/dist/full-date/is-valid-full-date.d.ts +21 -0
- package/dist/full-date/is-valid-full-date.js +38 -0
- package/dist/full-date/luxon-date-time-conversion.d.ts +19 -0
- package/dist/full-date/luxon-date-time-conversion.js +43 -0
- package/dist/full-date/parsing.d.ts +33 -0
- package/dist/full-date/parsing.js +47 -0
- package/dist/full-date/string-parsing.d.ts +9 -0
- package/dist/{esm/full-date → full-date}/string-parsing.js +6 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +29 -0
- package/dist/locales.d.ts +6 -0
- package/dist/locales.js +7 -0
- package/dist/timezone/timezone-checks.d.ts +21 -0
- package/dist/timezone/timezone-checks.js +34 -0
- package/dist/{esm/timezone → timezone}/timezone-names.d.ts +11 -5
- package/dist/{esm/timezone → timezone}/timezone-names.js +11 -3
- package/dist/timezone/timezones.d.ts +42 -0
- package/dist/timezone/timezones.js +31 -0
- package/package.json +33 -51
- package/cspell.config.cjs +0 -13
- package/dist/cjs/date-operations/calculate-relative-date.js +0 -12
- package/dist/cjs/date-operations/combine-dates.js +0 -50
- package/dist/cjs/date-operations/diff-dates.js +0 -105
- package/dist/cjs/duration/duration-unit.js +0 -41
- package/dist/cjs/duration/duration-utils.js +0 -42
- package/dist/cjs/duration/duration.js +0 -2
- package/dist/cjs/extra-utils/date-transforms.js +0 -8
- package/dist/cjs/extra-utils/has-date-props.js +0 -25
- package/dist/cjs/extra-utils/js-date-conversion.js +0 -8
- package/dist/cjs/extra-utils/now.js +0 -22
- package/dist/cjs/extra-utils/zero.js +0 -31
- package/dist/cjs/formatting/formatted-string.js +0 -34
- package/dist/cjs/formatting/html-input.js +0 -37
- package/dist/cjs/formatting/relative-string.js +0 -77
- package/dist/cjs/formatting/simple-strings.js +0 -58
- package/dist/cjs/formatting/string-format-types.js +0 -9
- package/dist/cjs/formatting/timestamp.js +0 -21
- package/dist/cjs/full-date/create-full-date.js +0 -81
- package/dist/cjs/full-date/date-like.js +0 -2
- package/dist/cjs/full-date/format-presets.js +0 -32
- package/dist/cjs/full-date/full-date-shape.js +0 -54
- package/dist/cjs/full-date/is-valid-full-date.js +0 -28
- package/dist/cjs/full-date/luxon-date-time-conversion.js +0 -43
- package/dist/cjs/full-date/parsing.js +0 -48
- package/dist/cjs/full-date/string-parsing.js +0 -33
- package/dist/cjs/index.js +0 -44
- package/dist/cjs/locales.js +0 -5
- package/dist/cjs/timezone/timezone-checks.js +0 -34
- package/dist/cjs/timezone/timezone-names.js +0 -479
- package/dist/cjs/timezone/timezones.js +0 -14
- package/dist/esm/date-operations/calculate-relative-date.d.ts +0 -8
- package/dist/esm/date-operations/combine-dates.d.ts +0 -35
- package/dist/esm/date-operations/diff-dates.d.ts +0 -66
- package/dist/esm/date-operations/diff-dates.js +0 -100
- package/dist/esm/duration/duration-unit.d.ts +0 -27
- package/dist/esm/duration/duration-unit.js +0 -38
- package/dist/esm/duration/duration-utils.d.ts +0 -11
- package/dist/esm/duration/duration-utils.js +0 -37
- package/dist/esm/duration/duration.d.ts +0 -32
- package/dist/esm/duration/duration.js +0 -1
- package/dist/esm/extra-utils/date-transforms.d.ts +0 -4
- package/dist/esm/extra-utils/date-transforms.js +0 -4
- package/dist/esm/extra-utils/has-date-props.d.ts +0 -9
- package/dist/esm/extra-utils/has-date-props.js +0 -20
- package/dist/esm/extra-utils/js-date-conversion.d.ts +0 -2
- package/dist/esm/extra-utils/js-date-conversion.js +0 -4
- package/dist/esm/extra-utils/now.d.ts +0 -6
- package/dist/esm/extra-utils/now.js +0 -16
- package/dist/esm/extra-utils/zero.d.ts +0 -31
- package/dist/esm/extra-utils/zero.js +0 -26
- package/dist/esm/formatting/formatted-string.d.ts +0 -14
- package/dist/esm/formatting/formatted-string.js +0 -29
- package/dist/esm/formatting/html-input.d.ts +0 -19
- package/dist/esm/formatting/html-input.js +0 -33
- package/dist/esm/formatting/relative-string.d.ts +0 -31
- package/dist/esm/formatting/relative-string.js +0 -73
- package/dist/esm/formatting/simple-strings.d.ts +0 -71
- package/dist/esm/formatting/simple-strings.js +0 -51
- package/dist/esm/formatting/string-format-types.d.ts +0 -7
- package/dist/esm/formatting/string-format-types.js +0 -5
- package/dist/esm/formatting/timestamp.d.ts +0 -6
- package/dist/esm/formatting/timestamp.js +0 -16
- package/dist/esm/full-date/create-full-date.d.ts +0 -21
- package/dist/esm/full-date/create-full-date.js +0 -73
- package/dist/esm/full-date/date-like.d.ts +0 -5
- package/dist/esm/full-date/full-date-shape.d.ts +0 -80
- package/dist/esm/full-date/full-date-shape.js +0 -51
- package/dist/esm/full-date/is-valid-full-date.d.ts +0 -5
- package/dist/esm/full-date/is-valid-full-date.js +0 -22
- package/dist/esm/full-date/luxon-date-time-conversion.d.ts +0 -15
- package/dist/esm/full-date/luxon-date-time-conversion.js +0 -38
- package/dist/esm/full-date/parsing.d.ts +0 -27
- package/dist/esm/full-date/parsing.js +0 -43
- package/dist/esm/full-date/string-parsing.d.ts +0 -3
- package/dist/esm/index.d.ts +0 -28
- package/dist/esm/index.js +0 -28
- package/dist/esm/locales.d.ts +0 -1
- package/dist/esm/locales.js +0 -2
- package/dist/esm/timezone/timezone-checks.d.ts +0 -17
- package/dist/esm/timezone/timezone-checks.js +0 -29
- package/dist/esm/timezone/timezones.d.ts +0 -480
- package/dist/esm/timezone/timezones.js +0 -11
- package/public/index.css +0 -7
- /package/dist/{esm/full-date → full-date}/date-like.js +0 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
/**
|
|
3
|
+
* Create a string based on just the date parts of a FullDate.
|
|
4
|
+
*
|
|
5
|
+
* **CAUTION**: this does not user locale setting. Prefer {@link toLocaleString} whenever possible.
|
|
6
|
+
*
|
|
7
|
+
* Example output: `'2023-12-05'`
|
|
8
|
+
*
|
|
9
|
+
* @category Internal
|
|
10
|
+
*/
|
|
11
|
+
export function toSimpleDatePartString(fullDate) {
|
|
12
|
+
const dateParts = [
|
|
13
|
+
String(Math.abs(fullDate.year)).padStart(4, '0'),
|
|
14
|
+
String(Math.abs(fullDate.month)).padStart(2, '0'),
|
|
15
|
+
String(Math.abs(fullDate.day)).padStart(2, '0'),
|
|
16
|
+
];
|
|
17
|
+
const dateString = dateParts.join('-');
|
|
18
|
+
return dateString;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a string based on just the time parts of a FullDate.
|
|
22
|
+
*
|
|
23
|
+
* **CAUTION**: this does not user locale setting. Prefer {@link toLocaleString} whenever possible.
|
|
24
|
+
*
|
|
25
|
+
* Example output: `'05:32:21'`
|
|
26
|
+
*
|
|
27
|
+
* @category Internal
|
|
28
|
+
*/
|
|
29
|
+
export function toSimpleTimePartString(fullDate, includeSeconds) {
|
|
30
|
+
const seconds = check.hasKey(fullDate, 'second') && includeSeconds
|
|
31
|
+
? String(Math.abs(fullDate.second)).padStart(2, '0')
|
|
32
|
+
: undefined;
|
|
33
|
+
const timeParts = [
|
|
34
|
+
String(Math.abs(fullDate.hour)).padStart(2, '0'),
|
|
35
|
+
String(Math.abs(fullDate.minute)).padStart(2, '0'),
|
|
36
|
+
seconds,
|
|
37
|
+
].filter(check.isTruthy);
|
|
38
|
+
return timeParts.join(':');
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Splits a FullDate up into multiple formatted string representations.
|
|
42
|
+
*
|
|
43
|
+
* **CAUTION**: this does not user locale setting. Prefer {@link toLocaleString} whenever possible.
|
|
44
|
+
*
|
|
45
|
+
* This uses numbers for everything.
|
|
46
|
+
*
|
|
47
|
+
* @category Formatting
|
|
48
|
+
* @example
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* import {toDatePartStrings, utcTimezone} from 'date-vir';
|
|
52
|
+
*
|
|
53
|
+
* let result = toDatePartStrings(
|
|
54
|
+
* {
|
|
55
|
+
* year: 2024,
|
|
56
|
+
* month: 4,
|
|
57
|
+
* day: 24,
|
|
58
|
+
* hour: 6,
|
|
59
|
+
* minute: 4,
|
|
60
|
+
* second: 9,
|
|
61
|
+
* millisecond: 123,
|
|
62
|
+
* timezone: utcTimezone,
|
|
63
|
+
* },
|
|
64
|
+
* {includeSeconds: true},
|
|
65
|
+
* );
|
|
66
|
+
* // outputs:
|
|
67
|
+
* result = {
|
|
68
|
+
* date: '2024-04-24',
|
|
69
|
+
* time: '06:04:09',
|
|
70
|
+
* timezone: 'UTC',
|
|
71
|
+
* };
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function toDatePartStrings(fullDate, options) {
|
|
75
|
+
return {
|
|
76
|
+
date: toSimpleDatePartString(fullDate),
|
|
77
|
+
time: toSimpleTimePartString(fullDate, options.includeSeconds),
|
|
78
|
+
timezone: fullDate.timezone,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Converts a date into a simple, unambiguous date string.
|
|
83
|
+
*
|
|
84
|
+
* **CAUTION**: this does not user locale setting. Prefer {@link toLocaleString} whenever possible.
|
|
85
|
+
*
|
|
86
|
+
* @category Formatting
|
|
87
|
+
* @example
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* import {toSimpleString} from 'date-vir';
|
|
91
|
+
*
|
|
92
|
+
* let result = toSimpleString(
|
|
93
|
+
* {
|
|
94
|
+
* year: 2024,
|
|
95
|
+
* month: 4,
|
|
96
|
+
* day: 24,
|
|
97
|
+
* hour: 6,
|
|
98
|
+
* minute: 4,
|
|
99
|
+
* second: 9,
|
|
100
|
+
* millisecond: 123,
|
|
101
|
+
* timezone: utcTimezone,
|
|
102
|
+
* },
|
|
103
|
+
* {
|
|
104
|
+
* includeSeconds: false,
|
|
105
|
+
* includeTimezone: false,
|
|
106
|
+
* },
|
|
107
|
+
* );
|
|
108
|
+
* // outputs:
|
|
109
|
+
* result = '2024-04-24 06:04';
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export function toSimpleString(fullDate, options) {
|
|
113
|
+
const parts = toDatePartStrings(fullDate, options);
|
|
114
|
+
const stringParts = [
|
|
115
|
+
parts.date,
|
|
116
|
+
parts.time,
|
|
117
|
+
options.includeTimezone && `(${parts.timezone})`,
|
|
118
|
+
].filter(check.isTruthy);
|
|
119
|
+
return stringParts.join(' ');
|
|
120
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A string that represents only date parts of a date. It's intended to be used like this:
|
|
3
|
+
* year-month-day.
|
|
4
|
+
*
|
|
5
|
+
* @category Internal
|
|
6
|
+
*/
|
|
7
|
+
export type JustDateString = `${number}-${number}-${number}`;
|
|
8
|
+
/**
|
|
9
|
+
* A string that represents only time parts of a date, with seconds. It's intended to be used like
|
|
10
|
+
* this: hour:minute:seconds.
|
|
11
|
+
*
|
|
12
|
+
* @category Internal
|
|
13
|
+
*/
|
|
14
|
+
export type JustTimeWithSecondsString = `${number}:${number}:${number}`;
|
|
15
|
+
/**
|
|
16
|
+
* A string that represents only time parts of a date, without seconds. It's intended to be used
|
|
17
|
+
* like this: hour:minute.
|
|
18
|
+
*
|
|
19
|
+
* @category Internal
|
|
20
|
+
*/
|
|
21
|
+
export type JustTimeString = `${number}:${number}`;
|
|
22
|
+
/**
|
|
23
|
+
* A string that represents date and time parts of a date, with seconds. It's intended to be used
|
|
24
|
+
* like this: year-month-dayThour:minute:seconds.
|
|
25
|
+
*
|
|
26
|
+
* @category Internal
|
|
27
|
+
*/
|
|
28
|
+
export type DateTimeWithSeconds = `${JustDateString}T${JustTimeWithSecondsString}`;
|
|
29
|
+
/**
|
|
30
|
+
* A string that represents date and time parts of a date, without seconds. It's intended to be used
|
|
31
|
+
* like this: year-month-dayThour:minute.
|
|
32
|
+
*
|
|
33
|
+
* @category Internal
|
|
34
|
+
*/
|
|
35
|
+
export type DateTimeString = `${JustDateString}T${JustTimeString}`;
|
|
36
|
+
/**
|
|
37
|
+
* A full UTC ISO date time string: YYYY-MM-DDTHH:mm:ss.sssZ.
|
|
38
|
+
*
|
|
39
|
+
* @category ISO
|
|
40
|
+
* @see
|
|
41
|
+
* - https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date-time-string-format
|
|
42
|
+
* - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format
|
|
43
|
+
*/
|
|
44
|
+
export type UtcIsoString = `${JustDateString}T${JustTimeWithSecondsString}.${number}Z`;
|
|
45
|
+
/**
|
|
46
|
+
* Checks if the input is a valid UTC ISO string and type guards the input.
|
|
47
|
+
*
|
|
48
|
+
* @category ISO
|
|
49
|
+
* @category Assertion
|
|
50
|
+
* @example
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* import {isValidIsoString} from 'date-vir';
|
|
54
|
+
*
|
|
55
|
+
* isValidIsoString('no'); // `false`
|
|
56
|
+
* isValidIsoString('2024-05-01T20:18:17.123Z'); // `true`
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function isValidIsoString(input: unknown): input is UtcIsoString;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the input is a valid UTC ISO string and type guards the input.
|
|
4
|
+
*
|
|
5
|
+
* @category ISO
|
|
6
|
+
* @category Assertion
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {isValidIsoString} from 'date-vir';
|
|
11
|
+
*
|
|
12
|
+
* isValidIsoString('no'); // `false`
|
|
13
|
+
* isValidIsoString('2024-05-01T20:18:17.123Z'); // `true`
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function isValidIsoString(input) {
|
|
17
|
+
const datetime = DateTime.fromISO(input);
|
|
18
|
+
return datetime.toUTC().toISO() === input;
|
|
19
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { FullDate } from '../full-date/full-date-shape.js';
|
|
2
|
+
import { UtcIsoString } from './string-format-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Convert a FullDate into a unix timestamp with milliseconds.
|
|
5
|
+
*
|
|
6
|
+
* @category Formatting
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {toTimestamp} from 'date-vir';
|
|
11
|
+
*
|
|
12
|
+
* const exampleDate: FullDate = {
|
|
13
|
+
* year: 2024,
|
|
14
|
+
* month: 1,
|
|
15
|
+
* day: 5,
|
|
16
|
+
* hour: 1,
|
|
17
|
+
* minute: 1,
|
|
18
|
+
* second: 1,
|
|
19
|
+
* millisecond: 1,
|
|
20
|
+
* timezone: 'UTC',
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* toTimestamp(exampleDate); // `1704416461001`
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function toTimestamp(fullDate: FullDate): number;
|
|
27
|
+
/**
|
|
28
|
+
* Convert a FullDate into a UTC ISO string, always ending in Z.
|
|
29
|
+
*
|
|
30
|
+
* @category ISO
|
|
31
|
+
* @category Formatting
|
|
32
|
+
* @example
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* import {toUtcIsoString} from 'date-vir';
|
|
36
|
+
*
|
|
37
|
+
* const exampleDate: FullDate = {
|
|
38
|
+
* year: 2024,
|
|
39
|
+
* month: 1,
|
|
40
|
+
* day: 5,
|
|
41
|
+
* hour: 1,
|
|
42
|
+
* minute: 1,
|
|
43
|
+
* second: 1,
|
|
44
|
+
* millisecond: 1,
|
|
45
|
+
* timezone: 'UTC',
|
|
46
|
+
* };
|
|
47
|
+
*
|
|
48
|
+
* toUtcIsoString(exampleDate); // `'2024-01-05T01:01:01.001Z'`
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function toUtcIsoString(fullDate: FullDate): UtcIsoString;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { toLuxonDateTime } from '../full-date/luxon-date-time-conversion.js';
|
|
2
|
+
import { utcTimezone } from '../timezone/timezones.js';
|
|
3
|
+
/**
|
|
4
|
+
* Convert a FullDate into a unix timestamp with milliseconds.
|
|
5
|
+
*
|
|
6
|
+
* @category Formatting
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {toTimestamp} from 'date-vir';
|
|
11
|
+
*
|
|
12
|
+
* const exampleDate: FullDate = {
|
|
13
|
+
* year: 2024,
|
|
14
|
+
* month: 1,
|
|
15
|
+
* day: 5,
|
|
16
|
+
* hour: 1,
|
|
17
|
+
* minute: 1,
|
|
18
|
+
* second: 1,
|
|
19
|
+
* millisecond: 1,
|
|
20
|
+
* timezone: 'UTC',
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* toTimestamp(exampleDate); // `1704416461001`
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function toTimestamp(fullDate) {
|
|
27
|
+
return toLuxonDateTime(fullDate).toMillis();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Convert a FullDate into a UTC ISO string, always ending in Z.
|
|
31
|
+
*
|
|
32
|
+
* @category ISO
|
|
33
|
+
* @category Formatting
|
|
34
|
+
* @example
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* import {toUtcIsoString} from 'date-vir';
|
|
38
|
+
*
|
|
39
|
+
* const exampleDate: FullDate = {
|
|
40
|
+
* year: 2024,
|
|
41
|
+
* month: 1,
|
|
42
|
+
* day: 5,
|
|
43
|
+
* hour: 1,
|
|
44
|
+
* minute: 1,
|
|
45
|
+
* second: 1,
|
|
46
|
+
* millisecond: 1,
|
|
47
|
+
* timezone: 'UTC',
|
|
48
|
+
* };
|
|
49
|
+
*
|
|
50
|
+
* toUtcIsoString(exampleDate); // `'2024-01-05T01:01:01.001Z'`
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function toUtcIsoString(fullDate) {
|
|
54
|
+
const isoString = toLuxonDateTime(fullDate).setZone(utcTimezone).toISO();
|
|
55
|
+
/** Ignore this edge case in the types and idk how to trigger it. */
|
|
56
|
+
/* c8 ignore next 3 */
|
|
57
|
+
if (!isoString) {
|
|
58
|
+
throw new Error(`Unable to get ISO string for '${JSON.stringify(fullDate)}'`);
|
|
59
|
+
}
|
|
60
|
+
return isoString;
|
|
61
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Timezone, UtcTimezone } from '../timezone/timezones.js';
|
|
2
|
+
import { DateLike } from './date-like.js';
|
|
3
|
+
import { FullDate } from './full-date-shape.js';
|
|
4
|
+
/**
|
|
5
|
+
* Parses the given {@link DateLike} and converts it into a {@link FullDate} instance with the user's
|
|
6
|
+
* current timezone.
|
|
7
|
+
*
|
|
8
|
+
* @category FullDate
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {createFullDateInUserTimezone} from 'date-vir';
|
|
13
|
+
*
|
|
14
|
+
* createFullDateInUserTimezone('June 1, 2024');
|
|
15
|
+
* createFullDateInUserTimezone(new Date());
|
|
16
|
+
* createFullDateInUserTimezone(112300120);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function createFullDateInUserTimezone(dateLike: Readonly<DateLike>): FullDate;
|
|
20
|
+
/**
|
|
21
|
+
* Parses the given {@link DateLike} and converts it into a {@link FullDate} instance with the UTC
|
|
22
|
+
* timezone.
|
|
23
|
+
*
|
|
24
|
+
* @category FullDate
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import {createUtcFullDate} from 'date-vir';
|
|
29
|
+
*
|
|
30
|
+
* createUtcFullDate('June 1, 2024');
|
|
31
|
+
* createUtcFullDate(new Date());
|
|
32
|
+
* createUtcFullDate(112300120);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function createUtcFullDate(dateLike: Readonly<DateLike>): FullDate<UtcTimezone>;
|
|
36
|
+
/**
|
|
37
|
+
* Parses the given {@link DateLike} and converts it into a {@link FullDate} instance with the given
|
|
38
|
+
* timezone.
|
|
39
|
+
*
|
|
40
|
+
* @category FullDate
|
|
41
|
+
* @example
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import {createFullDate, timezones, utcTimezone} from 'date-vir';
|
|
45
|
+
*
|
|
46
|
+
* createFullDate('June 1, 2024', utcTimezone);
|
|
47
|
+
* createFullDate(new Date(), timezones['Australia/Brisbane']);
|
|
48
|
+
* createFullDate(112300120, timezones['Etc/GMT-11']);
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function createFullDate<const SpecificTimezone extends Timezone>(
|
|
52
|
+
/** The original date representation to be converted into a FullDate. */
|
|
53
|
+
dateLike: Readonly<DateLike>,
|
|
54
|
+
/** The timezone that this date/time is meant for / originated from. */
|
|
55
|
+
timezone: SpecificTimezone): FullDate<SpecificTimezone>;
|
|
56
|
+
/**
|
|
57
|
+
* Converts a {@link FullDate} instance into a new instance with the original's date and time
|
|
58
|
+
* represented in the new timezone.
|
|
59
|
+
*
|
|
60
|
+
* @category Conversion
|
|
61
|
+
* @example
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* import {toNewTimezone} from 'date-vir';
|
|
65
|
+
*
|
|
66
|
+
* const exampleDate: FullDate = {
|
|
67
|
+
* year: 2024,
|
|
68
|
+
* month: 1,
|
|
69
|
+
* day: 5,
|
|
70
|
+
* hour: 1,
|
|
71
|
+
* minute: 1,
|
|
72
|
+
* second: 1,
|
|
73
|
+
* millisecond: 1,
|
|
74
|
+
* timezone: 'UTC',
|
|
75
|
+
* };
|
|
76
|
+
*
|
|
77
|
+
* let result = toNewTimezone(exampleDate, timezones['Australia/Brisbane']);
|
|
78
|
+
* // output:
|
|
79
|
+
* result = {
|
|
80
|
+
* year: 2024,
|
|
81
|
+
* month: 1,
|
|
82
|
+
* day: 5,
|
|
83
|
+
* hour: 11,
|
|
84
|
+
* minute: 1,
|
|
85
|
+
* second: 1,
|
|
86
|
+
* millisecond: 1,
|
|
87
|
+
* timezone: timezones['Australia/Brisbane'],
|
|
88
|
+
* };
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function toNewTimezone<const SpecificTimezone extends Timezone>(fullDate: Readonly<FullDate>, timezone: SpecificTimezone): FullDate<SpecificTimezone>;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
import { check } from '@augment-vir/assert';
|
|
3
|
+
import { stringify } from '@augment-vir/common';
|
|
4
|
+
import { userTimezone, utcTimezone } from '../timezone/timezones.js';
|
|
5
|
+
import { isValidFullDate } from './is-valid-full-date.js';
|
|
6
|
+
import { parseLuxonDateTime, toLuxonDateTime } from './luxon-date-time-conversion.js';
|
|
7
|
+
import { parseDateString } from './string-parsing.js';
|
|
8
|
+
/**
|
|
9
|
+
* Parses the given {@link DateLike} and converts it into a {@link FullDate} instance with the user's
|
|
10
|
+
* current timezone.
|
|
11
|
+
*
|
|
12
|
+
* @category FullDate
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import {createFullDateInUserTimezone} from 'date-vir';
|
|
17
|
+
*
|
|
18
|
+
* createFullDateInUserTimezone('June 1, 2024');
|
|
19
|
+
* createFullDateInUserTimezone(new Date());
|
|
20
|
+
* createFullDateInUserTimezone(112300120);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function createFullDateInUserTimezone(dateLike) {
|
|
24
|
+
return createFullDate(dateLike, userTimezone);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parses the given {@link DateLike} and converts it into a {@link FullDate} instance with the UTC
|
|
28
|
+
* timezone.
|
|
29
|
+
*
|
|
30
|
+
* @category FullDate
|
|
31
|
+
* @example
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import {createUtcFullDate} from 'date-vir';
|
|
35
|
+
*
|
|
36
|
+
* createUtcFullDate('June 1, 2024');
|
|
37
|
+
* createUtcFullDate(new Date());
|
|
38
|
+
* createUtcFullDate(112300120);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function createUtcFullDate(dateLike) {
|
|
42
|
+
return createFullDate(dateLike, utcTimezone);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Parses the given {@link DateLike} and converts it into a {@link FullDate} instance with the given
|
|
46
|
+
* timezone.
|
|
47
|
+
*
|
|
48
|
+
* @category FullDate
|
|
49
|
+
* @example
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import {createFullDate, timezones, utcTimezone} from 'date-vir';
|
|
53
|
+
*
|
|
54
|
+
* createFullDate('June 1, 2024', utcTimezone);
|
|
55
|
+
* createFullDate(new Date(), timezones['Australia/Brisbane']);
|
|
56
|
+
* createFullDate(112300120, timezones['Etc/GMT-11']);
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function createFullDate(
|
|
60
|
+
/** The original date representation to be converted into a FullDate. */
|
|
61
|
+
dateLike,
|
|
62
|
+
/** The timezone that this date/time is meant for / originated from. */
|
|
63
|
+
timezone) {
|
|
64
|
+
const dateTime = convertDateLikeToLuxonDateTime(dateLike, timezone);
|
|
65
|
+
if (!dateTime?.isValid) {
|
|
66
|
+
throw new Error(`Failed to parse date input ${stringify(dateLike)}`);
|
|
67
|
+
}
|
|
68
|
+
return parseLuxonDateTime(dateTime, timezone);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Converts a {@link FullDate} instance into a new instance with the original's date and time
|
|
72
|
+
* represented in the new timezone.
|
|
73
|
+
*
|
|
74
|
+
* @category Conversion
|
|
75
|
+
* @example
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import {toNewTimezone} from 'date-vir';
|
|
79
|
+
*
|
|
80
|
+
* const exampleDate: FullDate = {
|
|
81
|
+
* year: 2024,
|
|
82
|
+
* month: 1,
|
|
83
|
+
* day: 5,
|
|
84
|
+
* hour: 1,
|
|
85
|
+
* minute: 1,
|
|
86
|
+
* second: 1,
|
|
87
|
+
* millisecond: 1,
|
|
88
|
+
* timezone: 'UTC',
|
|
89
|
+
* };
|
|
90
|
+
*
|
|
91
|
+
* let result = toNewTimezone(exampleDate, timezones['Australia/Brisbane']);
|
|
92
|
+
* // output:
|
|
93
|
+
* result = {
|
|
94
|
+
* year: 2024,
|
|
95
|
+
* month: 1,
|
|
96
|
+
* day: 5,
|
|
97
|
+
* hour: 11,
|
|
98
|
+
* minute: 1,
|
|
99
|
+
* second: 1,
|
|
100
|
+
* millisecond: 1,
|
|
101
|
+
* timezone: timezones['Australia/Brisbane'],
|
|
102
|
+
* };
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export function toNewTimezone(fullDate, timezone) {
|
|
106
|
+
if (fullDate.timezone === timezone) {
|
|
107
|
+
return fullDate;
|
|
108
|
+
}
|
|
109
|
+
return createFullDate(fullDate, timezone);
|
|
110
|
+
}
|
|
111
|
+
function lastDitchConversion(dateLike) {
|
|
112
|
+
/** As any cast for last ditch effort to convert the dateLike into a date. */
|
|
113
|
+
const dateTime = DateTime.fromJSDate(new Date(dateLike));
|
|
114
|
+
if (dateTime.isValid) {
|
|
115
|
+
return dateTime;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function convertDateLikeToLuxonDateTime(dateLike, timezone) {
|
|
122
|
+
if (isValidFullDate(dateLike)) {
|
|
123
|
+
dateLike = toLuxonDateTime(dateLike).toMillis();
|
|
124
|
+
}
|
|
125
|
+
if (DateTime.isDateTime(dateLike)) {
|
|
126
|
+
return dateLike.setZone(timezone);
|
|
127
|
+
}
|
|
128
|
+
else if (check.isNumber(dateLike)) {
|
|
129
|
+
return DateTime.fromMillis(dateLike, { zone: utcTimezone }).setZone(timezone);
|
|
130
|
+
}
|
|
131
|
+
else if (check.isString(dateLike)) {
|
|
132
|
+
const parsedStringDate = parseDateString(dateLike, timezone);
|
|
133
|
+
if (parsedStringDate) {
|
|
134
|
+
return parsedStringDate;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else if (dateLike instanceof Date) {
|
|
138
|
+
const dateTime = DateTime.fromJSDate(dateLike);
|
|
139
|
+
return dateTime.setZone(timezone);
|
|
140
|
+
}
|
|
141
|
+
return lastDitchConversion(dateLike);
|
|
142
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
import { Timezone } from '../timezone/timezones.js';
|
|
3
|
+
import { FullDate } from './full-date-shape.js';
|
|
4
|
+
/**
|
|
5
|
+
* Types that can be converted into {@link FullDate} instances.
|
|
6
|
+
*
|
|
7
|
+
* @category Internal
|
|
8
|
+
*/
|
|
9
|
+
export type DateLike<SpecificTimezone extends Timezone = Timezone> = string | Date | number | DateTime | FullDate<SpecificTimezone>;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Format
|
|
3
|
-
*
|
|
2
|
+
* Format presets to be used with {@link toLocaleString}. To see examples of all presets, go here:
|
|
3
|
+
* https://moment.github.io/luxon/#/formatting?id=presets
|
|
4
|
+
*
|
|
5
|
+
* @category Formatting
|
|
4
6
|
*/
|
|
5
7
|
export declare const formatPresets: {
|
|
6
8
|
readonly DateShort: Intl.DateTimeFormatOptions;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { DateTime } from 'luxon';
|
|
2
2
|
/**
|
|
3
|
-
* Format
|
|
4
|
-
*
|
|
3
|
+
* Format presets to be used with {@link toLocaleString}. To see examples of all presets, go here:
|
|
4
|
+
* https://moment.github.io/luxon/#/formatting?id=presets
|
|
5
|
+
*
|
|
6
|
+
* @category Formatting
|
|
5
7
|
*/
|
|
6
8
|
export const formatPresets = {
|
|
7
9
|
DateShort: DateTime.DATE_SHORT,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents various parts of a {@link FullDate}. Each value is also a valid "type" attribute for an
|
|
3
|
+
* `<input>` HTML element.
|
|
4
|
+
*
|
|
5
|
+
* @category Internal
|
|
6
|
+
*/
|
|
7
|
+
export declare enum FullDatePart {
|
|
8
|
+
/**
|
|
9
|
+
* Used for `type="date"` HTML `<input>` elements:
|
|
10
|
+
* https://developer.mozilla.org/docs/Web/HTML/Element/input/date
|
|
11
|
+
*/
|
|
12
|
+
Date = "date",
|
|
13
|
+
/**
|
|
14
|
+
* Used for `type="time"` HTML `<input>` elements:
|
|
15
|
+
* https://developer.mozilla.org/docs/Web/HTML/Element/input/time
|
|
16
|
+
*/
|
|
17
|
+
Time = "time",
|
|
18
|
+
/**
|
|
19
|
+
* Used for `type="datetime-local"` HTML `<input>` elements:
|
|
20
|
+
* https://developer.mozilla.org/docs/Web/HTML/Element/input/datetime-local
|
|
21
|
+
*/
|
|
22
|
+
DateTime = "datetime-local"
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* {@link FullDate} keys used for telling time only.
|
|
26
|
+
*
|
|
27
|
+
* @category Internal
|
|
28
|
+
*/
|
|
29
|
+
export declare enum TimeKey {
|
|
30
|
+
Hour = "hour",
|
|
31
|
+
Minute = "minute",
|
|
32
|
+
Second = "second",
|
|
33
|
+
Millisecond = "millisecond"
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* {@link FullDate} keys used for telling the date only.
|
|
37
|
+
*
|
|
38
|
+
* @category Internal
|
|
39
|
+
*/
|
|
40
|
+
export declare enum DateKey {
|
|
41
|
+
Year = "year",
|
|
42
|
+
Month = "month",
|
|
43
|
+
Day = "day"
|
|
44
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents various parts of a {@link FullDate}. Each value is also a valid "type" attribute for an
|
|
3
|
+
* `<input>` HTML element.
|
|
4
|
+
*
|
|
5
|
+
* @category Internal
|
|
6
|
+
*/
|
|
7
|
+
export var FullDatePart;
|
|
8
|
+
(function (FullDatePart) {
|
|
9
|
+
/**
|
|
10
|
+
* Used for `type="date"` HTML `<input>` elements:
|
|
11
|
+
* https://developer.mozilla.org/docs/Web/HTML/Element/input/date
|
|
12
|
+
*/
|
|
13
|
+
FullDatePart["Date"] = "date";
|
|
14
|
+
/**
|
|
15
|
+
* Used for `type="time"` HTML `<input>` elements:
|
|
16
|
+
* https://developer.mozilla.org/docs/Web/HTML/Element/input/time
|
|
17
|
+
*/
|
|
18
|
+
FullDatePart["Time"] = "time";
|
|
19
|
+
/**
|
|
20
|
+
* Used for `type="datetime-local"` HTML `<input>` elements:
|
|
21
|
+
* https://developer.mozilla.org/docs/Web/HTML/Element/input/datetime-local
|
|
22
|
+
*/
|
|
23
|
+
FullDatePart["DateTime"] = "datetime-local";
|
|
24
|
+
})(FullDatePart || (FullDatePart = {}));
|
|
25
|
+
/**
|
|
26
|
+
* {@link FullDate} keys used for telling time only.
|
|
27
|
+
*
|
|
28
|
+
* @category Internal
|
|
29
|
+
*/
|
|
30
|
+
export var TimeKey;
|
|
31
|
+
(function (TimeKey) {
|
|
32
|
+
TimeKey["Hour"] = "hour";
|
|
33
|
+
TimeKey["Minute"] = "minute";
|
|
34
|
+
TimeKey["Second"] = "second";
|
|
35
|
+
TimeKey["Millisecond"] = "millisecond";
|
|
36
|
+
})(TimeKey || (TimeKey = {}));
|
|
37
|
+
/**
|
|
38
|
+
* {@link FullDate} keys used for telling the date only.
|
|
39
|
+
*
|
|
40
|
+
* @category Internal
|
|
41
|
+
*/
|
|
42
|
+
export var DateKey;
|
|
43
|
+
(function (DateKey) {
|
|
44
|
+
DateKey["Year"] = "year";
|
|
45
|
+
DateKey["Month"] = "month";
|
|
46
|
+
DateKey["Day"] = "day";
|
|
47
|
+
})(DateKey || (DateKey = {}));
|