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
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertDuration = exports.roundDuration = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const luxon_1 = require("luxon");
6
- const run_time_assertions_1 = require("run-time-assertions");
7
- /** Round every unit in a given duration to the given number of decimal points. */
8
- function roundDuration(duration,
9
- /**
10
- * How many decimals to round to. Leave undefined to skip rounding. (Though if you skip
11
- * rounding, why are you using this function?)
12
- */
13
- decimalCount) {
14
- return (0, common_1.mapObjectValues)(duration, (key, value) => {
15
- if (decimalCount == undefined || !(0, run_time_assertions_1.isRunTimeType)(value, 'number')) {
16
- return value;
17
- }
18
- else {
19
- return (0, common_1.round)({ number: value, digits: decimalCount });
20
- }
21
- });
22
- }
23
- exports.roundDuration = roundDuration;
24
- /** Convert a duration from any combination of units into a single unit. */
25
- function convertDuration(inputDuration, requiredUnit) {
26
- if (hasInfinity(inputDuration)) {
27
- return { [requiredUnit]: Infinity };
28
- }
29
- else if (hasNegativeInfinity(inputDuration)) {
30
- return { [requiredUnit]: -Infinity };
31
- }
32
- const luxonDuration = luxon_1.Duration.fromObject(inputDuration);
33
- const newUnitValue = luxonDuration.as(requiredUnit);
34
- return { [requiredUnit]: newUnitValue };
35
- }
36
- exports.convertDuration = convertDuration;
37
- function hasInfinity(duration) {
38
- return Object.values(duration).some((value) => value === Infinity);
39
- }
40
- function hasNegativeInfinity(duration) {
41
- return Object.values(duration).some((value) => value === -Infinity);
42
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.overrideDateParts = void 0;
4
- /** Override the initial provided FullDate with all subsequent FullDate parts. */
5
- function overrideDateParts(date, ...overrides) {
6
- return Object.assign({}, date, ...overrides);
7
- }
8
- exports.overrideDateParts = overrideDateParts;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assertHasDateProps = exports.hasDateProps = void 0;
4
- function hasDateProps(dateInput, keys) {
5
- try {
6
- assertHasDateProps(dateInput, keys);
7
- return true;
8
- }
9
- catch (error) {
10
- return false;
11
- }
12
- }
13
- exports.hasDateProps = hasDateProps;
14
- function assertHasDateProps(dateInput, keys) {
15
- const missingKeys = [];
16
- keys.forEach((key) => {
17
- if (dateInput[key] == undefined) {
18
- missingKeys.push(key);
19
- }
20
- });
21
- if (missingKeys.length) {
22
- throw new Error(`Missing required FullDate key(s): ${missingKeys.join(', ')}`);
23
- }
24
- }
25
- exports.assertHasDateProps = assertHasDateProps;
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toJsDate = void 0;
4
- const timestamp_1 = require("../formatting/timestamp");
5
- function toJsDate(fullDate) {
6
- return new Date((0, timestamp_1.toTimestamp)(fullDate));
7
- }
8
- exports.toJsDate = toJsDate;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNowInUtcTimezone = exports.getNowInUserTimezone = exports.getNowInIsoString = void 0;
4
- const timestamp_1 = require("../formatting/timestamp");
5
- const create_full_date_1 = require("../full-date/create-full-date");
6
- const timezones_1 = require("../timezone/timezones");
7
- function getNowInIsoString() {
8
- /**
9
- * The time zone doesn't matter here because the ISO string will be identical for a given date
10
- * no matter what its timezone is.
11
- */
12
- return (0, timestamp_1.toIsoString)((0, create_full_date_1.getNowFullDate)(timezones_1.utcTimezone));
13
- }
14
- exports.getNowInIsoString = getNowInIsoString;
15
- function getNowInUserTimezone() {
16
- return (0, create_full_date_1.getNowFullDate)(timezones_1.userTimezone);
17
- }
18
- exports.getNowInUserTimezone = getNowInUserTimezone;
19
- function getNowInUtcTimezone() {
20
- return (0, create_full_date_1.getNowFullDate)(timezones_1.utcTimezone);
21
- }
22
- exports.getNowInUtcTimezone = getNowInUtcTimezone;
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.clearParts = exports.clearTime = exports.zeroTime = exports.emptyDate = exports.zeroDate = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const full_date_shape_1 = require("../full-date/full-date-shape");
6
- const date_transforms_1 = require("./date-transforms");
7
- exports.zeroDate = {
8
- year: 0,
9
- month: 1,
10
- day: 1,
11
- hour: 0,
12
- minute: 0,
13
- second: 0,
14
- millisecond: 0,
15
- };
16
- exports.emptyDate = exports.zeroDate;
17
- exports.zeroTime = (0, common_1.pickObjectKeys)(exports.zeroDate, full_date_shape_1.timeFullDateKeys);
18
- function clearTime(inputFullDate) {
19
- return clearParts(inputFullDate, full_date_shape_1.timeFullDateKeys);
20
- }
21
- exports.clearTime = clearTime;
22
- function clearParts(inputFullDate, parts) {
23
- const clearParts = (0, common_1.typedObjectFromEntries)(parts.map((keyName) => {
24
- return [
25
- keyName,
26
- exports.zeroDate[keyName],
27
- ];
28
- }));
29
- return (0, date_transforms_1.overrideDateParts)(inputFullDate, clearParts);
30
- }
31
- exports.clearParts = clearParts;
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toFormattedString = exports.toLocaleString = void 0;
4
- const luxon_date_time_conversion_1 = require("../full-date/luxon-date-time-conversion");
5
- const locales_1 = require("../locales");
6
- /**
7
- * Chromium has a weird behavior where the "NARROW NO-BREAK SPACE" character (code 8239) is used
8
- * instead of a space (code 32) before AM/PM.
9
- */
10
- function fixChromiumSpace(original) {
11
- return original.replace(new RegExp(String.fromCharCode(8239), 'g'), ' ');
12
- }
13
- /** Converts the given FullDate into a string in the user's locale, or the given localeOverride. */
14
- function toLocaleString(fullDate, formatOptions) {
15
- const localeString = (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toLocaleString({
16
- ...formatOptions,
17
- }, {
18
- locale: formatOptions?.locale ?? locales_1.userLocale,
19
- });
20
- return fixChromiumSpace(localeString);
21
- }
22
- exports.toLocaleString = toLocaleString;
23
- /**
24
- * Provides arbitrary date formatting using Luxon. For full details on the options for the format
25
- * string input, see https://moment.github.io/luxon/#/formatting?id=table-of-tokens
26
- *
27
- * You probably should **prefer toLocaleString** so your users see the format they expect. It
28
- * already has many formatting options.
29
- */
30
- function toFormattedString(fullDate, format, localeOverride = locales_1.userLocale) {
31
- const formattedString = (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toFormat(format, { locale: localeOverride });
32
- return fixChromiumSpace(formattedString);
33
- }
34
- exports.toFormattedString = toFormattedString;
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toHtmlInputString = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const has_date_props_1 = require("../extra-utils/has-date-props");
6
- const full_date_shape_1 = require("../full-date/full-date-shape");
7
- const simple_strings_1 = require("./simple-strings");
8
- function toHtmlInputString(fullDate, inputType, includeSeconds) {
9
- const internalFullDate = fullDate;
10
- if (inputType === full_date_shape_1.FullDatePartEnum.Date) {
11
- (0, has_date_props_1.assertHasDateProps)(internalFullDate, [
12
- 'year',
13
- 'month',
14
- 'day',
15
- ]);
16
- return (0, simple_strings_1.toSimpleDatePartString)(internalFullDate);
17
- }
18
- else if (inputType === full_date_shape_1.FullDatePartEnum.Time) {
19
- if (includeSeconds && internalFullDate.second == undefined) {
20
- throw new Error('Tried to include seconds in the time string but no seconds were provided.');
21
- }
22
- (0, has_date_props_1.assertHasDateProps)(internalFullDate, [
23
- 'hour',
24
- 'minute',
25
- ]);
26
- return (0, simple_strings_1.toSimpleTimePartString)(internalFullDate, !!includeSeconds);
27
- }
28
- else if (inputType === full_date_shape_1.FullDatePartEnum.DateTime) {
29
- const datePart = toHtmlInputString(fullDate, full_date_shape_1.FullDatePartEnum.Date);
30
- const timePart = toHtmlInputString(fullDate, full_date_shape_1.FullDatePartEnum.Time, includeSeconds);
31
- return `${datePart}T${timePart}`;
32
- }
33
- else {
34
- throw new Error(`Unexpected inputTyped: '${inputType}'. Expected usage of FullDatePartEnum or one of ${(0, common_1.getEnumTypedValues)(full_date_shape_1.FullDatePartEnum).join(', ')}.`);
35
- }
36
- }
37
- exports.toHtmlInputString = toHtmlInputString;
@@ -1,77 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toRelativeString = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const run_time_assertions_1 = require("run-time-assertions");
6
- const diff_dates_1 = require("../date-operations/diff-dates");
7
- const duration_unit_1 = require("../duration/duration-unit");
8
- const maxDurations = {
9
- [duration_unit_1.DurationUnit.Milliseconds]: 1000,
10
- [duration_unit_1.DurationUnit.Seconds]: 60,
11
- [duration_unit_1.DurationUnit.Minutes]: 60,
12
- [duration_unit_1.DurationUnit.Hours]: 24,
13
- [duration_unit_1.DurationUnit.Days]: 30,
14
- [duration_unit_1.DurationUnit.Weeks]: 4,
15
- [duration_unit_1.DurationUnit.Months]: 12,
16
- [duration_unit_1.DurationUnit.Quarters]: 4,
17
- [duration_unit_1.DurationUnit.Years]: Infinity,
18
- };
19
- function toRelativeString({ fullDate, relativeTo, options = {}, }) {
20
- const roundingDigits = options.decimalDigitCount || 0;
21
- const allUnitDiffs = (0, diff_dates_1.diffDates)({
22
- start: fullDate,
23
- end: relativeTo,
24
- units: duration_unit_1.orderedDurationUnits,
25
- diffType: diff_dates_1.DiffType.EquivalentUnits,
26
- });
27
- const unitsWithinBounds = (0, common_1.mapObjectValues)(allUnitDiffs, (durationUnit, duration) => {
28
- const roundedDuration = Math.floor(Math.abs((0, common_1.round)({ digits: 1, number: duration })));
29
- const isAboveZero = roundedDuration > 0;
30
- const isBelowMax = options.limitUnitMax
31
- ? roundedDuration < maxDurations[durationUnit]
32
- : true;
33
- return isAboveZero && isBelowMax;
34
- });
35
- const unitToUse = duration_unit_1.orderedDurationUnits.reduce((chosenUnit, currentUnit) => {
36
- const isUnitAllowed = !options.blockedRelativeUnits?.includes(currentUnit);
37
- if (isUnitAllowed && unitsWithinBounds[currentUnit]) {
38
- return currentUnit;
39
- }
40
- else {
41
- return chosenUnit;
42
- }
43
- }, undefined);
44
- const shouldUseJustNow = options.blockJustNow
45
- ? false
46
- : (unitToUse == undefined && allUnitDiffs.minutes < 2) ||
47
- (0, run_time_assertions_1.isJsonEqual)(fullDate, relativeTo) ||
48
- (unitToUse === duration_unit_1.DurationUnit.Minutes &&
49
- options.blockedRelativeUnits?.includes(duration_unit_1.DurationUnit.Seconds) &&
50
- allUnitDiffs.minutes < 2) ||
51
- (unitToUse === duration_unit_1.DurationUnit.Seconds &&
52
- options.blockedRelativeUnits?.includes(duration_unit_1.DurationUnit.Milliseconds) &&
53
- allUnitDiffs.seconds < 11) ||
54
- (unitToUse === duration_unit_1.DurationUnit.Milliseconds && allUnitDiffs.milliseconds < 710);
55
- /** Some short circuits. */
56
- if (shouldUseJustNow) {
57
- return 'just now';
58
- }
59
- else if (unitToUse == undefined) {
60
- return undefined;
61
- }
62
- const duration = allUnitDiffs[unitToUse];
63
- const roundedAbsoluteDuration = Math.abs((0, common_1.round)({ digits: roundingDigits, number: duration }));
64
- const isDurationSingular = roundedAbsoluteDuration === 1;
65
- const unitName = [
66
- duration_unit_1.singularDurationUnitNames[unitToUse],
67
- isDurationSingular ? '' : 's',
68
- ].join('');
69
- const value = isDurationSingular ? 'a' : roundedAbsoluteDuration;
70
- if (duration < 0) {
71
- return `in ${value} ${unitName}`;
72
- }
73
- else {
74
- return `${value} ${unitName} ago`;
75
- }
76
- }
77
- exports.toRelativeString = toRelativeString;
@@ -1,58 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toSimpleString = exports.toSimplePartStrings = exports.toSimpleTimePartString = exports.toSimpleDatePartString = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- /**
6
- * Create a string based on just the date parts of a FullDate.
7
- *
8
- * CAUTION: this does not user locale setting and should therefore not be used for user-facing date
9
- * formatting. Prefer using `toLocaleString` instead for user-facing strings.
10
- *
11
- * Example output: `'2023-12-05'`
12
- */
13
- function toSimpleDatePartString(fullDate) {
14
- const dateParts = [
15
- String(Math.abs(fullDate.year)).padStart(4, '0'),
16
- String(Math.abs(fullDate.month)).padStart(2, '0'),
17
- String(Math.abs(fullDate.day)).padStart(2, '0'),
18
- ];
19
- const dateString = dateParts.join('-');
20
- return dateString;
21
- }
22
- exports.toSimpleDatePartString = toSimpleDatePartString;
23
- function toSimpleTimePartString(fullDate, includeSeconds) {
24
- const seconds = (0, common_1.typedHasProperty)(fullDate, 'second') && includeSeconds
25
- ? String(Math.abs(fullDate.second)).padStart(2, '0')
26
- : undefined;
27
- const timeParts = [
28
- String(Math.abs(fullDate.hour)).padStart(2, '0'),
29
- String(Math.abs(fullDate.minute)).padStart(2, '0'),
30
- seconds,
31
- ].filter(common_1.isTruthy);
32
- return timeParts.join(':');
33
- }
34
- exports.toSimpleTimePartString = toSimpleTimePartString;
35
- function toSimplePartStrings(fullDate, options) {
36
- return {
37
- date: toSimpleDatePartString(fullDate),
38
- time: toSimpleTimePartString(fullDate, options.includeSeconds),
39
- timezone: fullDate.timezone,
40
- };
41
- }
42
- exports.toSimplePartStrings = toSimplePartStrings;
43
- /**
44
- * Converts a date into a simple, unambiguous date string.
45
- *
46
- * CAUTION: this does not user locale setting and should therefore not be used for user-facing date
47
- * formatting. Prefer using `toLocaleString` instead for user-facing strings.
48
- */
49
- function toSimpleString(fullDate, options) {
50
- const parts = toSimplePartStrings(fullDate, options);
51
- const stringParts = [
52
- parts.date,
53
- parts.time,
54
- options.includeTimezone && `(${parts.timezone})`,
55
- ].filter(common_1.isTruthy);
56
- return stringParts.join(' ');
57
- }
58
- exports.toSimpleString = toSimpleString;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidIsoString = void 0;
4
- const luxon_1 = require("luxon");
5
- function isValidIsoString(input) {
6
- const datetime = luxon_1.DateTime.fromISO(input);
7
- return datetime.toISO() === input;
8
- }
9
- exports.isValidIsoString = isValidIsoString;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toIsoString = exports.toTimestamp = void 0;
4
- const luxon_date_time_conversion_1 = require("../full-date/luxon-date-time-conversion");
5
- const timezones_1 = require("../timezone/timezones");
6
- /** Convert a FullDate into a unix timestamp with milliseconds. */
7
- function toTimestamp(fullDate) {
8
- return (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).toMillis();
9
- }
10
- exports.toTimestamp = toTimestamp;
11
- /** Convert a FullDate into an ISO string in UTC; always ends in Z. */
12
- function toIsoString(fullDate) {
13
- const isoString = (0, luxon_date_time_conversion_1.toLuxonDateTime)(fullDate).setZone(timezones_1.utcTimezone).toISO();
14
- /** Ignore this edge case in the types and idk how to trigger it. */
15
- /* c8 ignore next 3 */
16
- if (!isoString) {
17
- throw new Error(`Unable to get ISO string for '${JSON.stringify(fullDate)}'`);
18
- }
19
- return isoString;
20
- }
21
- exports.toIsoString = toIsoString;
@@ -1,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNowFullDate = exports.toNewTimezone = exports.createFullDate = exports.createUtcFullDate = exports.createFullDateInUserTimezone = void 0;
4
- const luxon_1 = require("luxon");
5
- const run_time_assertions_1 = require("run-time-assertions");
6
- const timezones_1 = require("../timezone/timezones");
7
- const is_valid_full_date_1 = require("./is-valid-full-date");
8
- const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
9
- const string_parsing_1 = require("./string-parsing");
10
- function createFullDateInUserTimezone(dateLike) {
11
- return createFullDate(dateLike, timezones_1.userTimezone);
12
- }
13
- exports.createFullDateInUserTimezone = createFullDateInUserTimezone;
14
- function createUtcFullDate(dateLike) {
15
- return createFullDate(dateLike, timezones_1.utcTimezone);
16
- }
17
- exports.createUtcFullDate = createUtcFullDate;
18
- /**
19
- * Parses the given dateLike and then reads the fullDate properties from that parsed value as if it
20
- * were in the given timezone.
21
- */
22
- function createFullDate(
23
- /** The original date representation to be converted into a FullDate. */
24
- dateLike,
25
- /** The timezone that this date/time is meant for / originated from. */
26
- timezone) {
27
- const dateTime = convertDateLikeToLuxonDateTime(dateLike, timezone);
28
- if (!dateTime || !dateTime.isValid) {
29
- throw new Error(`Failed to parse date input '${dateLike}'`);
30
- }
31
- return (0, luxon_date_time_conversion_1.parseLuxonDateTime)(dateTime, timezone);
32
- }
33
- exports.createFullDate = createFullDate;
34
- /**
35
- * Creates and returns a new FullDate object with its values shifted to be the same original time
36
- * but represented in a new timezone.
37
- */
38
- function toNewTimezone(fullDate, timezone) {
39
- if (fullDate.timezone === timezone) {
40
- return fullDate;
41
- }
42
- return createFullDate(fullDate, timezone);
43
- }
44
- exports.toNewTimezone = toNewTimezone;
45
- function lastDitchConversion(dateLike) {
46
- luxon_1.DateTime.fromFormat;
47
- /** As any cast for last ditch effort to convert the dateLike into a date. */
48
- const dateTime = luxon_1.DateTime.fromJSDate(new Date(dateLike));
49
- if (dateTime.isValid) {
50
- return dateTime;
51
- }
52
- else {
53
- return undefined;
54
- }
55
- }
56
- function convertDateLikeToLuxonDateTime(dateLike, timezone) {
57
- if ((0, is_valid_full_date_1.isValidFullDate)(dateLike)) {
58
- dateLike = (0, luxon_date_time_conversion_1.toLuxonDateTime)(dateLike).toMillis();
59
- }
60
- if (luxon_1.DateTime.isDateTime(dateLike)) {
61
- return dateLike.setZone(timezone);
62
- }
63
- else if ((0, run_time_assertions_1.isRunTimeType)(dateLike, 'number')) {
64
- return luxon_1.DateTime.fromMillis(dateLike, { zone: timezones_1.utcTimezone }).setZone(timezone);
65
- }
66
- else if ((0, run_time_assertions_1.isRunTimeType)(dateLike, 'string')) {
67
- const parsedStringDate = (0, string_parsing_1.parseDateString)(dateLike, timezone);
68
- if (parsedStringDate) {
69
- return parsedStringDate;
70
- }
71
- }
72
- else if (dateLike instanceof Date) {
73
- const dateTime = luxon_1.DateTime.fromJSDate(dateLike);
74
- return dateTime.setZone(timezone);
75
- }
76
- return lastDitchConversion(dateLike);
77
- }
78
- function getNowFullDate(timezone) {
79
- return createFullDate(Date.now(), timezone);
80
- }
81
- exports.getNowFullDate = getNowFullDate;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatPresets = void 0;
4
- const luxon_1 = require("luxon");
5
- /**
6
- * Format preset re-exported from luxon to be used with toLocaleString. To see examples of all
7
- * presets, go here: https://moment.github.io/luxon/#/formatting?id=presets
8
- */
9
- exports.formatPresets = {
10
- DateShort: luxon_1.DateTime.DATE_SHORT,
11
- DateMed: luxon_1.DateTime.DATE_MED,
12
- DateMedWithWeekday: luxon_1.DateTime.DATE_MED_WITH_WEEKDAY,
13
- DateFull: luxon_1.DateTime.DATE_FULL,
14
- DateHuge: luxon_1.DateTime.DATE_HUGE,
15
- TimeSimple: luxon_1.DateTime.TIME_SIMPLE,
16
- TimeWithSeconds: luxon_1.DateTime.TIME_WITH_SECONDS,
17
- TimeWithShortOffset: luxon_1.DateTime.TIME_WITH_SHORT_OFFSET,
18
- TimeWithLongOffset: luxon_1.DateTime.TIME_WITH_LONG_OFFSET,
19
- Time24Simple: luxon_1.DateTime.TIME_24_SIMPLE,
20
- Time24WithSeconds: luxon_1.DateTime.TIME_24_WITH_SECONDS,
21
- Time24WithShortOffset: luxon_1.DateTime.TIME_24_WITH_SHORT_OFFSET,
22
- Time24WithLongOffset: luxon_1.DateTime.TIME_24_WITH_LONG_OFFSET,
23
- DatetimeShort: luxon_1.DateTime.DATETIME_SHORT,
24
- DatetimeShortWithSeconds: luxon_1.DateTime.DATETIME_SHORT_WITH_SECONDS,
25
- DatetimeMed: luxon_1.DateTime.DATETIME_MED,
26
- DatetimeMedWithSeconds: luxon_1.DateTime.DATETIME_MED_WITH_SECONDS,
27
- DatetimeMedWithWeekday: luxon_1.DateTime.DATETIME_MED_WITH_WEEKDAY,
28
- DatetimeFull: luxon_1.DateTime.DATETIME_FULL,
29
- DatetimeFullWithSeconds: luxon_1.DateTime.DATETIME_FULL_WITH_SECONDS,
30
- DatetimeHuge: luxon_1.DateTime.DATETIME_HUGE,
31
- DatetimeHugeWithSeconds: luxon_1.DateTime.DATETIME_HUGE_WITH_SECONDS,
32
- };
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fullDateShape = exports.datePartShape = exports.timePartShape = exports.dateFullDateKeys = exports.timeFullDateKeys = exports.FullDatePartEnum = void 0;
4
- const object_shape_tester_1 = require("object-shape-tester");
5
- const timezones_1 = require("../timezone/timezones");
6
- /**
7
- * Represents various parts of a FullDate. Each value is also a valid "type" attribute for an
8
- * <input> HTML element.
9
- */
10
- var FullDatePartEnum;
11
- (function (FullDatePartEnum) {
12
- FullDatePartEnum["Date"] = "date";
13
- FullDatePartEnum["Time"] = "time";
14
- FullDatePartEnum["DateTime"] = "datetime-local";
15
- })(FullDatePartEnum || (exports.FullDatePartEnum = FullDatePartEnum = {}));
16
- exports.timeFullDateKeys = [
17
- 'hour',
18
- 'minute',
19
- 'second',
20
- 'millisecond',
21
- ];
22
- exports.dateFullDateKeys = [
23
- 'year',
24
- 'month',
25
- 'day',
26
- ];
27
- exports.timePartShape = (0, object_shape_tester_1.defineShape)({
28
- /** Hour of the day in 24 time: 0-23 */
29
- hour: 14,
30
- /** Minute of the hour: 0-59 */
31
- minute: 19,
32
- /** Second of the minute: 0-59 */
33
- second: 7,
34
- /** Millisecond of the second: 0-999 */
35
- millisecond: 877,
36
- /** The timezone that this date/time is meant for / originated from. */
37
- timezone: timezones_1.utcTimezone,
38
- });
39
- exports.datePartShape = (0, object_shape_tester_1.defineShape)({
40
- /**
41
- * The full, four digit year.
42
- *
43
- * @example
44
- * 2023;
45
- */
46
- year: 2023,
47
- /** A month of the year: 1-12 */
48
- month: 6,
49
- /** A day of the month: 1-31 depending on the month */
50
- day: 5,
51
- /** The timezone that this date/time is meant for / originated from. */
52
- timezone: timezones_1.utcTimezone,
53
- });
54
- exports.fullDateShape = (0, object_shape_tester_1.defineShape)((0, object_shape_tester_1.and)(exports.datePartShape, exports.timePartShape));
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasTimezone = exports.isValidFullDate = exports.assertIsValidFullDate = void 0;
4
- const object_shape_tester_1 = require("object-shape-tester");
5
- const timezone_checks_1 = require("../timezone/timezone-checks");
6
- const full_date_shape_1 = require("./full-date-shape");
7
- const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
8
- function assertIsValidFullDate(input) {
9
- (0, object_shape_tester_1.assertValidShape)(input, full_date_shape_1.fullDateShape);
10
- (0, timezone_checks_1.assertValidTimezone)(input.timezone);
11
- /** Check that the input can be converted to a luxon date correctly. */
12
- (0, luxon_date_time_conversion_1.toLuxonDateTime)(input);
13
- }
14
- exports.assertIsValidFullDate = assertIsValidFullDate;
15
- function isValidFullDate(input) {
16
- try {
17
- assertIsValidFullDate(input);
18
- return true;
19
- }
20
- catch (error) {
21
- return false;
22
- }
23
- }
24
- exports.isValidFullDate = isValidFullDate;
25
- function hasTimezone(input, timezone) {
26
- return input.timezone === timezone;
27
- }
28
- exports.hasTimezone = hasTimezone;
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseLuxonDateTime = exports.toLuxonDateTime = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const luxon_1 = require("luxon");
6
- /**
7
- * Converts a FullDate object into a Luxon DateTime library. This is only needed if you need complex
8
- * operations on dates. It does, however, internally power a lot of the date-vir functionality as it
9
- * uses the Luxon library for conversions between timezones.
10
- */
11
- function toLuxonDateTime(fullDateInput) {
12
- const dateTime = luxon_1.DateTime.fromObject((0, common_1.omitObjectKeys)(fullDateInput, ['timezone']), {
13
- zone: fullDateInput.timezone,
14
- });
15
- /** Ignore this edge case in coverage cause idk how to trigger it. */
16
- /* c8 ignore next 3 */
17
- if (!dateTime.isValid) {
18
- throw new Error(dateTime.invalidExplanation ?? undefined);
19
- }
20
- return dateTime;
21
- }
22
- exports.toLuxonDateTime = toLuxonDateTime;
23
- /**
24
- * Convert a Luxon DateTime object into a DateTime object. Used internally inside of date-vir, but
25
- * could be helpful if you need it. Usually you should prefer using the createFullDate function
26
- * instead.
27
- */
28
- function parseLuxonDateTime(dateTimeInput, forcedTimezone) {
29
- if (!dateTimeInput.isValid) {
30
- throw new Error(`Invalid input: '${dateTimeInput.toISO()}'`);
31
- }
32
- return {
33
- day: dateTimeInput.day,
34
- month: dateTimeInput.month,
35
- year: dateTimeInput.year,
36
- hour: dateTimeInput.hour,
37
- minute: dateTimeInput.minute,
38
- second: dateTimeInput.second,
39
- millisecond: dateTimeInput.millisecond,
40
- timezone: forcedTimezone ?? dateTimeInput.zoneName,
41
- };
42
- }
43
- exports.parseLuxonDateTime = parseLuxonDateTime;