date-vir 5.3.2 → 6.0.1

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.
Files changed (141) hide show
  1. package/LICENSE-MIT +1 -1
  2. package/README.md +28 -28
  3. package/dist/date-operations/calculate-relative-date.d.ts +17 -0
  4. package/dist/{esm/date-operations → date-operations}/calculate-relative-date.js +10 -1
  5. package/dist/date-operations/combine-dates.d.ts +43 -0
  6. package/dist/{esm/date-operations → date-operations}/combine-dates.js +6 -4
  7. package/dist/date-operations/diff-dates.d.ts +95 -0
  8. package/dist/date-operations/diff-dates.js +124 -0
  9. package/dist/extra-utils/has-date-props.d.ts +35 -0
  10. package/dist/extra-utils/has-date-props.js +46 -0
  11. package/dist/extra-utils/js-date-conversion.d.ts +26 -0
  12. package/dist/extra-utils/js-date-conversion.js +28 -0
  13. package/dist/extra-utils/now.d.ts +56 -0
  14. package/dist/extra-utils/now.js +68 -0
  15. package/dist/extra-utils/override-date.d.ts +27 -0
  16. package/dist/extra-utils/override-date.js +27 -0
  17. package/dist/extra-utils/zero.d.ts +99 -0
  18. package/dist/extra-utils/zero.js +94 -0
  19. package/dist/formatting/formatted-string.d.ts +58 -0
  20. package/dist/formatting/formatted-string.js +73 -0
  21. package/dist/formatting/html-input.d.ts +30 -0
  22. package/dist/formatting/html-input.js +47 -0
  23. package/dist/formatting/relative-string.d.ts +53 -0
  24. package/dist/formatting/relative-string.js +93 -0
  25. package/dist/formatting/simple-strings.d.ts +187 -0
  26. package/dist/formatting/simple-strings.js +120 -0
  27. package/dist/formatting/string-format-types.d.ts +59 -0
  28. package/dist/formatting/string-format-types.js +19 -0
  29. package/dist/formatting/timestamp.d.ts +51 -0
  30. package/dist/formatting/timestamp.js +61 -0
  31. package/dist/full-date/create-full-date.d.ts +91 -0
  32. package/dist/full-date/create-full-date.js +142 -0
  33. package/dist/full-date/date-like.d.ts +9 -0
  34. package/dist/{esm/full-date → full-date}/format-presets.d.ts +4 -2
  35. package/dist/{esm/full-date → full-date}/format-presets.js +4 -2
  36. package/dist/full-date/full-date-parts.d.ts +48 -0
  37. package/dist/full-date/full-date-parts.js +45 -0
  38. package/dist/full-date/full-date-shape.d.ts +106 -0
  39. package/dist/{esm/full-date → full-date}/full-date-shape.js +15 -22
  40. package/dist/full-date/is-valid-full-date.d.ts +21 -0
  41. package/dist/full-date/is-valid-full-date.js +38 -0
  42. package/dist/full-date/luxon-date-time-conversion.d.ts +19 -0
  43. package/dist/{esm/full-date → full-date}/luxon-date-time-conversion.js +10 -6
  44. package/dist/full-date/parsing.d.ts +33 -0
  45. package/dist/full-date/parsing.js +47 -0
  46. package/dist/full-date/string-parsing.d.ts +9 -0
  47. package/dist/{esm/full-date → full-date}/string-parsing.js +6 -0
  48. package/dist/index.d.ts +27 -0
  49. package/dist/index.js +27 -0
  50. package/dist/locales.d.ts +6 -0
  51. package/dist/locales.js +7 -0
  52. package/dist/timezone/timezone-checks.d.ts +21 -0
  53. package/dist/timezone/timezone-checks.js +34 -0
  54. package/dist/{esm/timezone → timezone}/timezone-names.d.ts +11 -5
  55. package/dist/{esm/timezone → timezone}/timezone-names.js +11 -3
  56. package/dist/timezone/timezones.d.ts +42 -0
  57. package/dist/timezone/timezones.js +31 -0
  58. package/package.json +30 -48
  59. package/cspell.config.cjs +0 -13
  60. package/dist/cjs/date-operations/calculate-relative-date.js +0 -12
  61. package/dist/cjs/date-operations/combine-dates.js +0 -50
  62. package/dist/cjs/date-operations/diff-dates.js +0 -105
  63. package/dist/cjs/duration/duration-unit.js +0 -41
  64. package/dist/cjs/duration/duration-utils.js +0 -42
  65. package/dist/cjs/duration/duration.js +0 -2
  66. package/dist/cjs/extra-utils/date-transforms.js +0 -8
  67. package/dist/cjs/extra-utils/has-date-props.js +0 -25
  68. package/dist/cjs/extra-utils/js-date-conversion.js +0 -8
  69. package/dist/cjs/extra-utils/now.js +0 -22
  70. package/dist/cjs/extra-utils/zero.js +0 -31
  71. package/dist/cjs/formatting/formatted-string.js +0 -34
  72. package/dist/cjs/formatting/html-input.js +0 -37
  73. package/dist/cjs/formatting/relative-string.js +0 -77
  74. package/dist/cjs/formatting/simple-strings.js +0 -58
  75. package/dist/cjs/formatting/string-format-types.js +0 -9
  76. package/dist/cjs/formatting/timestamp.js +0 -21
  77. package/dist/cjs/full-date/create-full-date.js +0 -81
  78. package/dist/cjs/full-date/date-like.js +0 -2
  79. package/dist/cjs/full-date/format-presets.js +0 -32
  80. package/dist/cjs/full-date/full-date-shape.js +0 -54
  81. package/dist/cjs/full-date/is-valid-full-date.js +0 -28
  82. package/dist/cjs/full-date/luxon-date-time-conversion.js +0 -43
  83. package/dist/cjs/full-date/parsing.js +0 -48
  84. package/dist/cjs/full-date/string-parsing.js +0 -33
  85. package/dist/cjs/index.js +0 -44
  86. package/dist/cjs/locales.js +0 -5
  87. package/dist/cjs/timezone/timezone-checks.js +0 -34
  88. package/dist/cjs/timezone/timezone-names.js +0 -479
  89. package/dist/cjs/timezone/timezones.js +0 -14
  90. package/dist/esm/date-operations/calculate-relative-date.d.ts +0 -8
  91. package/dist/esm/date-operations/combine-dates.d.ts +0 -35
  92. package/dist/esm/date-operations/diff-dates.d.ts +0 -66
  93. package/dist/esm/date-operations/diff-dates.js +0 -100
  94. package/dist/esm/duration/duration-unit.d.ts +0 -27
  95. package/dist/esm/duration/duration-unit.js +0 -38
  96. package/dist/esm/duration/duration-utils.d.ts +0 -11
  97. package/dist/esm/duration/duration-utils.js +0 -37
  98. package/dist/esm/duration/duration.d.ts +0 -32
  99. package/dist/esm/duration/duration.js +0 -1
  100. package/dist/esm/extra-utils/date-transforms.d.ts +0 -4
  101. package/dist/esm/extra-utils/date-transforms.js +0 -4
  102. package/dist/esm/extra-utils/has-date-props.d.ts +0 -9
  103. package/dist/esm/extra-utils/has-date-props.js +0 -20
  104. package/dist/esm/extra-utils/js-date-conversion.d.ts +0 -2
  105. package/dist/esm/extra-utils/js-date-conversion.js +0 -4
  106. package/dist/esm/extra-utils/now.d.ts +0 -6
  107. package/dist/esm/extra-utils/now.js +0 -16
  108. package/dist/esm/extra-utils/zero.d.ts +0 -31
  109. package/dist/esm/extra-utils/zero.js +0 -26
  110. package/dist/esm/formatting/formatted-string.d.ts +0 -14
  111. package/dist/esm/formatting/formatted-string.js +0 -29
  112. package/dist/esm/formatting/html-input.d.ts +0 -19
  113. package/dist/esm/formatting/html-input.js +0 -33
  114. package/dist/esm/formatting/relative-string.d.ts +0 -31
  115. package/dist/esm/formatting/relative-string.js +0 -73
  116. package/dist/esm/formatting/simple-strings.d.ts +0 -71
  117. package/dist/esm/formatting/simple-strings.js +0 -51
  118. package/dist/esm/formatting/string-format-types.d.ts +0 -7
  119. package/dist/esm/formatting/string-format-types.js +0 -5
  120. package/dist/esm/formatting/timestamp.d.ts +0 -6
  121. package/dist/esm/formatting/timestamp.js +0 -16
  122. package/dist/esm/full-date/create-full-date.d.ts +0 -21
  123. package/dist/esm/full-date/create-full-date.js +0 -73
  124. package/dist/esm/full-date/date-like.d.ts +0 -5
  125. package/dist/esm/full-date/full-date-shape.d.ts +0 -80
  126. package/dist/esm/full-date/is-valid-full-date.d.ts +0 -5
  127. package/dist/esm/full-date/is-valid-full-date.js +0 -22
  128. package/dist/esm/full-date/luxon-date-time-conversion.d.ts +0 -15
  129. package/dist/esm/full-date/parsing.d.ts +0 -27
  130. package/dist/esm/full-date/parsing.js +0 -43
  131. package/dist/esm/full-date/string-parsing.d.ts +0 -3
  132. package/dist/esm/index.d.ts +0 -28
  133. package/dist/esm/index.js +0 -28
  134. package/dist/esm/locales.d.ts +0 -1
  135. package/dist/esm/locales.js +0 -2
  136. package/dist/esm/timezone/timezone-checks.d.ts +0 -17
  137. package/dist/esm/timezone/timezone-checks.js +0 -29
  138. package/dist/esm/timezone/timezones.d.ts +0 -480
  139. package/dist/esm/timezone/timezones.js +0 -11
  140. package/public/index.css +0 -7
  141. /package/dist/{esm/full-date → full-date}/date-like.js +0 -0
@@ -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 preset re-exported from luxon to be used with toLocaleString. To see examples of all
3
- * presets, go here: https://moment.github.io/luxon/#/formatting?id=presets
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 preset re-exported from luxon to be used with toLocaleString. To see examples of all
4
- * presets, go here: https://moment.github.io/luxon/#/formatting?id=presets
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,48 @@
1
+ import type { ArrayElement } from '@augment-vir/common';
2
+ /**
3
+ * Represents various parts of a {@link FullDate}. Each value is also a valid "type" attribute for an
4
+ * `<input>` HTML element.
5
+ *
6
+ * @category Internal
7
+ */
8
+ export declare enum FullDatePart {
9
+ /**
10
+ * Used for `type="date"` HTML `<input>` elements:
11
+ * https://developer.mozilla.org/docs/Web/HTML/Element/input/date
12
+ */
13
+ Date = "date",
14
+ /**
15
+ * Used for `type="time"` HTML `<input>` elements:
16
+ * https://developer.mozilla.org/docs/Web/HTML/Element/input/time
17
+ */
18
+ 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
+ DateTime = "datetime-local"
24
+ }
25
+ /**
26
+ * {@link FullDate} keys used for telling time only.
27
+ *
28
+ * @category Internal
29
+ */
30
+ export declare const timeFullDateKeys: readonly ["hour", "minute", "second", "millisecond"];
31
+ /**
32
+ * {@link FullDate} keys used for telling time only.
33
+ *
34
+ * @category Internal
35
+ */
36
+ export type TimeFullDateKeys = ArrayElement<typeof timeFullDateKeys>;
37
+ /**
38
+ * {@link FullDate} keys used for telling the date only.
39
+ *
40
+ * @category Internal
41
+ */
42
+ export declare const dateFullDateKeys: readonly ["year", "month", "day"];
43
+ /**
44
+ * {@link FullDate} keys used for telling the date only.
45
+ *
46
+ * @category Internal
47
+ */
48
+ export type DateFullDateKeys = ArrayElement<typeof dateFullDateKeys>;
@@ -0,0 +1,45 @@
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 const timeFullDateKeys = [
31
+ 'hour',
32
+ 'minute',
33
+ 'second',
34
+ 'millisecond',
35
+ ];
36
+ /**
37
+ * {@link FullDate} keys used for telling the date only.
38
+ *
39
+ * @category Internal
40
+ */
41
+ export const dateFullDateKeys = [
42
+ 'year',
43
+ 'month',
44
+ 'day',
45
+ ];
@@ -0,0 +1,106 @@
1
+ import { Overwrite, type SetRequired } from '@augment-vir/common';
2
+ import { type Timezone } from '../timezone/timezones.js';
3
+ /**
4
+ * Time part of {@link FullDate} represented in a shape definition.
5
+ *
6
+ * @category Shape
7
+ */
8
+ export declare const timePartShape: import("object-shape-tester").ShapeDefinition<{
9
+ /** Hour of the day in 24 time: 0-23 */
10
+ hour: number;
11
+ /** Minute of the hour: 0-59 */
12
+ minute: number;
13
+ /** Second of the minute: 0-59 */
14
+ second: number;
15
+ /** Millisecond of the second: 0-999 */
16
+ millisecond: number;
17
+ /** The timezone that this date/time is meant for / originated from. */
18
+ timezone: Timezone;
19
+ }, false>;
20
+ /**
21
+ * Time part of a {@link FullDate}.
22
+ *
23
+ * @category Internal
24
+ */
25
+ export type TimePart<SpecificTimezone extends Timezone = Timezone> = SetRequired<Partial<FullDate<SpecificTimezone>>, keyof (typeof timePartShape)['runtimeType']>;
26
+ /**
27
+ * Date part of {@link FullDate} represented in a shape definition.
28
+ *
29
+ * @category Shape
30
+ */
31
+ export declare const datePartShape: import("object-shape-tester").ShapeDefinition<{
32
+ /**
33
+ * The full, four digit year.
34
+ *
35
+ * @example 2023;
36
+ */
37
+ year: number;
38
+ /** A month of the year: 1-12 */
39
+ month: number;
40
+ /** A day of the month: 1-31 depending on the month */
41
+ day: number;
42
+ /** The timezone that this date/time is meant for / originated from. */
43
+ timezone: Timezone;
44
+ }, false>;
45
+ /**
46
+ * Date part of {@link FullDate}.
47
+ *
48
+ * @category Internal
49
+ */
50
+ export type DatePart<SpecificTimezone extends Timezone = Timezone> = SetRequired<Partial<FullDate<SpecificTimezone>>, keyof (typeof datePartShape)['runtimeType']>;
51
+ /**
52
+ * The complete {@link FullDate} represented in a shape definition.
53
+ *
54
+ * @category Shape
55
+ */
56
+ export declare const fullDateShape: import("object-shape-tester").ShapeDefinition<import("object-shape-tester").ShapeAnd<[import("object-shape-tester").ShapeDefinition<{
57
+ /**
58
+ * The full, four digit year.
59
+ *
60
+ * @example 2023;
61
+ */
62
+ year: number;
63
+ /** A month of the year: 1-12 */
64
+ month: number;
65
+ /** A day of the month: 1-31 depending on the month */
66
+ day: number;
67
+ /** The timezone that this date/time is meant for / originated from. */
68
+ timezone: Timezone;
69
+ }, false>, import("object-shape-tester").ShapeDefinition<{
70
+ /** Hour of the day in 24 time: 0-23 */
71
+ hour: number;
72
+ /** Minute of the hour: 0-59 */
73
+ minute: number;
74
+ /** Second of the minute: 0-59 */
75
+ second: number;
76
+ /** Millisecond of the second: 0-999 */
77
+ millisecond: number;
78
+ /** The timezone that this date/time is meant for / originated from. */
79
+ timezone: Timezone;
80
+ }, false>]>, false>;
81
+ /**
82
+ * A serializable object that completely specifies how any given date should be represented,
83
+ * including its timezone, to millisecond accuracy. This is the core type of the `date-vir` package,
84
+ * with most operations either requiring or returning this type.
85
+ *
86
+ * @category FullDate
87
+ * @example
88
+ *
89
+ * ```ts
90
+ * import {FullDate, timezones} from 'date-vir';
91
+ *
92
+ * const myDate: FullDate = {
93
+ * year: 2024,
94
+ * month: 6,
95
+ * day: 2,
96
+ * hour: 13,
97
+ * minute: 32,
98
+ * second: 12,
99
+ * milliseconds: 94,
100
+ * timezone: timezones['Australia/Brisbane'],
101
+ * };
102
+ * ```
103
+ */
104
+ export type FullDate<SpecificTimezone extends Timezone = Timezone> = Overwrite<(typeof fullDateShape)['runtimeType'], {
105
+ timezone: SpecificTimezone;
106
+ }>;
@@ -1,26 +1,10 @@
1
1
  import { and, defineShape } from 'object-shape-tester';
2
- import { utcTimezone } from '../timezone/timezones';
2
+ import { utcTimezone } from '../timezone/timezones.js';
3
3
  /**
4
- * Represents various parts of a FullDate. Each value is also a valid "type" attribute for an
5
- * <input> HTML element.
4
+ * Time part of {@link FullDate} represented in a shape definition.
5
+ *
6
+ * @category Shape
6
7
  */
7
- export var FullDatePartEnum;
8
- (function (FullDatePartEnum) {
9
- FullDatePartEnum["Date"] = "date";
10
- FullDatePartEnum["Time"] = "time";
11
- FullDatePartEnum["DateTime"] = "datetime-local";
12
- })(FullDatePartEnum || (FullDatePartEnum = {}));
13
- export const timeFullDateKeys = [
14
- 'hour',
15
- 'minute',
16
- 'second',
17
- 'millisecond',
18
- ];
19
- export const dateFullDateKeys = [
20
- 'year',
21
- 'month',
22
- 'day',
23
- ];
24
8
  export const timePartShape = defineShape({
25
9
  /** Hour of the day in 24 time: 0-23 */
26
10
  hour: 14,
@@ -33,12 +17,16 @@ export const timePartShape = defineShape({
33
17
  /** The timezone that this date/time is meant for / originated from. */
34
18
  timezone: utcTimezone,
35
19
  });
20
+ /**
21
+ * Date part of {@link FullDate} represented in a shape definition.
22
+ *
23
+ * @category Shape
24
+ */
36
25
  export const datePartShape = defineShape({
37
26
  /**
38
27
  * The full, four digit year.
39
28
  *
40
- * @example
41
- * 2023;
29
+ * @example 2023;
42
30
  */
43
31
  year: 2023,
44
32
  /** A month of the year: 1-12 */
@@ -48,4 +36,9 @@ export const datePartShape = defineShape({
48
36
  /** The timezone that this date/time is meant for / originated from. */
49
37
  timezone: utcTimezone,
50
38
  });
39
+ /**
40
+ * The complete {@link FullDate} represented in a shape definition.
41
+ *
42
+ * @category Shape
43
+ */
51
44
  export const fullDateShape = defineShape(and(datePartShape, timePartShape));
@@ -0,0 +1,21 @@
1
+ import { Timezone } from '../timezone/timezones.js';
2
+ import { FullDate } from './full-date-shape.js';
3
+ /**
4
+ * Asserts that the input is a valid {@link FullDate}.
5
+ *
6
+ * @category Assertion
7
+ * @throws An Error if the input is not a valid {@link FullDate}.
8
+ */
9
+ export declare function assertValidFullDate(input: unknown): asserts input is FullDate;
10
+ /**
11
+ * Checks and type guards that the input is a valid {@link FullDate}.
12
+ *
13
+ * @category Assertion
14
+ */
15
+ export declare function isValidFullDate(input: unknown): input is FullDate;
16
+ /**
17
+ * Checks and type guards that the given {@link FullDate} instance has the given timezone.
18
+ *
19
+ * @category Assertion
20
+ */
21
+ export declare function hasTimezone<const SpecificTimezone extends Timezone>(input: FullDate, timezone: SpecificTimezone): input is FullDate<SpecificTimezone>;
@@ -0,0 +1,38 @@
1
+ import { assertValidShape } from 'object-shape-tester';
2
+ import { assertValidTimezone } from '../timezone/timezone-checks.js';
3
+ import { fullDateShape } from './full-date-shape.js';
4
+ import { toLuxonDateTime } from './luxon-date-time-conversion.js';
5
+ /**
6
+ * Asserts that the input is a valid {@link FullDate}.
7
+ *
8
+ * @category Assertion
9
+ * @throws An Error if the input is not a valid {@link FullDate}.
10
+ */
11
+ export function assertValidFullDate(input) {
12
+ assertValidShape(input, fullDateShape);
13
+ assertValidTimezone(input.timezone);
14
+ /** Check that the input can be converted to a luxon date correctly. */
15
+ toLuxonDateTime(input);
16
+ }
17
+ /**
18
+ * Checks and type guards that the input is a valid {@link FullDate}.
19
+ *
20
+ * @category Assertion
21
+ */
22
+ export function isValidFullDate(input) {
23
+ try {
24
+ assertValidFullDate(input);
25
+ return true;
26
+ }
27
+ catch {
28
+ return false;
29
+ }
30
+ }
31
+ /**
32
+ * Checks and type guards that the given {@link FullDate} instance has the given timezone.
33
+ *
34
+ * @category Assertion
35
+ */
36
+ export function hasTimezone(input, timezone) {
37
+ return input.timezone === timezone;
38
+ }
@@ -0,0 +1,19 @@
1
+ import { DateTime } from 'luxon';
2
+ import { Timezone } from '../timezone/timezones.js';
3
+ import { FullDate } from './full-date-shape.js';
4
+ /**
5
+ * Converts a {@link FullDate} object into a Luxon DateTime library. This is only needed if you need
6
+ * complex operations on dates. It does, however, internally power a lot of date-vir functionality
7
+ * as it uses the Luxon library for conversions between timezones.
8
+ *
9
+ * @category Internal
10
+ */
11
+ export declare function toLuxonDateTime(fullDateInput: Readonly<FullDate>): DateTime;
12
+ /**
13
+ * Convert a Luxon DateTime object into a {@link FullDate} instance. Used internally inside of
14
+ * date-vir, but could be helpful if you need it. Usually you should prefer using the
15
+ * `createFullDate` function instead.
16
+ *
17
+ * @category Internal
18
+ */
19
+ export declare function parseLuxonDateTime<const SpecificTimezone extends Timezone>(dateTimeInput: Readonly<DateTime>, forcedTimezone?: SpecificTimezone): FullDate<SpecificTimezone>;
@@ -1,9 +1,11 @@
1
1
  import { omitObjectKeys } from '@augment-vir/common';
2
2
  import { DateTime } from 'luxon';
3
3
  /**
4
- * Converts a FullDate object into a Luxon DateTime library. This is only needed if you need complex
5
- * operations on dates. It does, however, internally power a lot of the date-vir functionality as it
6
- * uses the Luxon library for conversions between timezones.
4
+ * Converts a {@link FullDate} object into a Luxon DateTime library. This is only needed if you need
5
+ * complex operations on dates. It does, however, internally power a lot of date-vir functionality
6
+ * as it uses the Luxon library for conversions between timezones.
7
+ *
8
+ * @category Internal
7
9
  */
8
10
  export function toLuxonDateTime(fullDateInput) {
9
11
  const dateTime = DateTime.fromObject(omitObjectKeys(fullDateInput, ['timezone']), {
@@ -17,9 +19,11 @@ export function toLuxonDateTime(fullDateInput) {
17
19
  return dateTime;
18
20
  }
19
21
  /**
20
- * Convert a Luxon DateTime object into a DateTime object. Used internally inside of date-vir, but
21
- * could be helpful if you need it. Usually you should prefer using the createFullDate function
22
- * instead.
22
+ * Convert a Luxon DateTime object into a {@link FullDate} instance. Used internally inside of
23
+ * date-vir, but could be helpful if you need it. Usually you should prefer using the
24
+ * `createFullDate` function instead.
25
+ *
26
+ * @category Internal
23
27
  */
24
28
  export function parseLuxonDateTime(dateTimeInput, forcedTimezone) {
25
29
  if (!dateTimeInput.isValid) {
@@ -0,0 +1,33 @@
1
+ import { Timezone } from '../timezone/timezones.js';
2
+ import { FullDate } from './full-date-shape.js';
3
+ /**
4
+ * Parses any date string into a {@link FullDate} using the specified pattern and timezone. Prefer
5
+ * using {@link createFullDate} if possible.
6
+ *
7
+ * Since this supports any arbitrary date string pattern, it is more flexible than
8
+ * {@link createFullDate} but you must know the date pattern ahead of time.
9
+ *
10
+ * The `formatString` input is passed to Luxon. All format pattern options are listed here:
11
+ * https://moment.github.io/luxon/#/parsing?id=table-of-tokens
12
+ *
13
+ * @category FullDate
14
+ */
15
+ export declare function parseDateStringWithPattern<const SpecificTimezone extends Timezone>({ dateString, formatString, timezone, }: {
16
+ /** The date string to be parsed. */
17
+ dateString: string;
18
+ /**
19
+ * The format string that should match Luxon's expectations here:
20
+ * https://moment.github.io/luxon/#/parsing?id=table-of-tokens
21
+ */
22
+ formatString: string;
23
+ /** The timezone that this date/time is meant for / originated from. */
24
+ timezone: SpecificTimezone;
25
+ }): FullDate<SpecificTimezone>;
26
+ /**
27
+ * Read either an `HTMLInputElement`'s value directly, or the value string retrieved from an
28
+ * `HTMLInputElement`. Handles both `type="date"` and `type="time"` <input> elements.
29
+ *
30
+ * @category FullDate
31
+ * @category HTML
32
+ */
33
+ export declare function parseInputElementValue<const SpecificTimezone extends Timezone>(elementOrValue: Readonly<Pick<HTMLInputElement, 'value'>> | string | null | undefined, timezone: SpecificTimezone): FullDate<SpecificTimezone> | undefined;
@@ -0,0 +1,47 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { DateTime } from 'luxon';
3
+ import { createFullDate } from './create-full-date.js';
4
+ import { parseLuxonDateTime } from './luxon-date-time-conversion.js';
5
+ /**
6
+ * Parses any date string into a {@link FullDate} using the specified pattern and timezone. Prefer
7
+ * using {@link createFullDate} if possible.
8
+ *
9
+ * Since this supports any arbitrary date string pattern, it is more flexible than
10
+ * {@link createFullDate} but you must know the date pattern ahead of time.
11
+ *
12
+ * The `formatString` input is passed to Luxon. All format pattern options are listed here:
13
+ * https://moment.github.io/luxon/#/parsing?id=table-of-tokens
14
+ *
15
+ * @category FullDate
16
+ */
17
+ export function parseDateStringWithPattern({ dateString, formatString, timezone, }) {
18
+ try {
19
+ const dateTime = DateTime.fromFormat(dateString, formatString, {
20
+ zone: timezone,
21
+ setZone: true,
22
+ });
23
+ return parseLuxonDateTime(dateTime);
24
+ }
25
+ catch {
26
+ throw new Error(`Failed to parse string '${dateString}' with format '${formatString}'`);
27
+ }
28
+ }
29
+ /**
30
+ * Read either an `HTMLInputElement`'s value directly, or the value string retrieved from an
31
+ * `HTMLInputElement`. Handles both `type="date"` and `type="time"` <input> elements.
32
+ *
33
+ * @category FullDate
34
+ * @category HTML
35
+ */
36
+ export function parseInputElementValue(elementOrValue, timezone) {
37
+ if (!elementOrValue) {
38
+ return undefined;
39
+ }
40
+ const value = check.isString(elementOrValue) ? elementOrValue : elementOrValue.value;
41
+ try {
42
+ return createFullDate(value, timezone);
43
+ }
44
+ catch {
45
+ return undefined;
46
+ }
47
+ }
@@ -0,0 +1,9 @@
1
+ import { DateTime } from 'luxon';
2
+ import { Timezone } from '../timezone/timezones.js';
3
+ /**
4
+ * Tries extra known formats for parsing date strings, even those that browsers do not natively
5
+ * support.
6
+ *
7
+ * @category Internal
8
+ */
9
+ export declare function parseDateString(dateString: string, timezone: Timezone): DateTime | undefined;
@@ -11,6 +11,12 @@ const knownStringFormats = [
11
11
  // january-2021
12
12
  'LLLL-y',
13
13
  ];
14
+ /**
15
+ * Tries extra known formats for parsing date strings, even those that browsers do not natively
16
+ * support.
17
+ *
18
+ * @category Internal
19
+ */
14
20
  export function parseDateString(dateString, timezone) {
15
21
  const isoAttempt = DateTime.fromISO(dateString, { zone: timezone });
16
22
  if (isoAttempt.isValid) {