date-vir 5.3.0 → 5.3.2

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.
@@ -6,6 +6,7 @@ const run_time_assertions_1 = require("run-time-assertions");
6
6
  const timezones_1 = require("../timezone/timezones");
7
7
  const is_valid_full_date_1 = require("./is-valid-full-date");
8
8
  const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
9
+ const string_parsing_1 = require("./string-parsing");
9
10
  function createFullDateInUserTimezone(dateLike) {
10
11
  return createFullDate(dateLike, timezones_1.userTimezone);
11
12
  }
@@ -42,6 +43,7 @@ function toNewTimezone(fullDate, timezone) {
42
43
  }
43
44
  exports.toNewTimezone = toNewTimezone;
44
45
  function lastDitchConversion(dateLike) {
46
+ luxon_1.DateTime.fromFormat;
45
47
  /** As any cast for last ditch effort to convert the dateLike into a date. */
46
48
  const dateTime = luxon_1.DateTime.fromJSDate(new Date(dateLike));
47
49
  if (dateTime.isValid) {
@@ -62,11 +64,10 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
62
64
  return luxon_1.DateTime.fromMillis(dateLike, { zone: timezones_1.utcTimezone }).setZone(timezone);
63
65
  }
64
66
  else if ((0, run_time_assertions_1.isRunTimeType)(dateLike, 'string')) {
65
- const dateTime = luxon_1.DateTime.fromISO(dateLike, { zone: timezone });
66
- if (!dateTime.isValid) {
67
- return lastDitchConversion(dateLike);
67
+ const parsedStringDate = (0, string_parsing_1.parseDateString)(dateLike, timezone);
68
+ if (parsedStringDate) {
69
+ return parsedStringDate;
68
70
  }
69
- return dateTime;
70
71
  }
71
72
  else if (dateLike instanceof Date) {
72
73
  const dateTime = luxon_1.DateTime.fromJSDate(dateLike);
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseDateString = void 0;
4
+ const luxon_1 = require("luxon");
5
+ /**
6
+ * These formats are for luxon, documented here:
7
+ * https://moment.github.io/luxon/#/parsing?id=table-of-tokens
8
+ */
9
+ const knownStringFormats = [
10
+ // 1-2024
11
+ 'L-y',
12
+ // jan-2021
13
+ 'LLL-y',
14
+ // january-2021
15
+ 'LLLL-y',
16
+ ];
17
+ function parseDateString(dateString, timezone) {
18
+ const isoAttempt = luxon_1.DateTime.fromISO(dateString, { zone: timezone });
19
+ if (isoAttempt.isValid) {
20
+ return isoAttempt;
21
+ }
22
+ let finalizedDateTime;
23
+ knownStringFormats.some((format) => {
24
+ const dateTimeAttempt = luxon_1.DateTime.fromFormat(dateString, format, { zone: timezone });
25
+ if (!dateTimeAttempt.isValid) {
26
+ return false;
27
+ }
28
+ finalizedDateTime = dateTimeAttempt;
29
+ return true;
30
+ });
31
+ return finalizedDateTime;
32
+ }
33
+ exports.parseDateString = parseDateString;
@@ -1,5 +1,6 @@
1
1
  import { UtcIsoString } from '../formatting/string-format-types';
2
2
  import { FullDate } from '../full-date/full-date-shape';
3
+ import { UtcTimezone } from '../timezone/timezones';
3
4
  export declare function getNowInIsoString(): UtcIsoString;
4
5
  export declare function getNowInUserTimezone(): FullDate;
5
- export declare function getNowInUtcTimezone(): FullDate;
6
+ export declare function getNowInUtcTimezone(): FullDate<UtcTimezone>;
@@ -3,6 +3,7 @@ import { isRunTimeType } from 'run-time-assertions';
3
3
  import { userTimezone, utcTimezone } from '../timezone/timezones';
4
4
  import { isValidFullDate } from './is-valid-full-date';
5
5
  import { parseLuxonDateTime, toLuxonDateTime } from './luxon-date-time-conversion';
6
+ import { parseDateString } from './string-parsing';
6
7
  export function createFullDateInUserTimezone(dateLike) {
7
8
  return createFullDate(dateLike, userTimezone);
8
9
  }
@@ -35,6 +36,7 @@ export function toNewTimezone(fullDate, timezone) {
35
36
  return createFullDate(fullDate, timezone);
36
37
  }
37
38
  function lastDitchConversion(dateLike) {
39
+ DateTime.fromFormat;
38
40
  /** As any cast for last ditch effort to convert the dateLike into a date. */
39
41
  const dateTime = DateTime.fromJSDate(new Date(dateLike));
40
42
  if (dateTime.isValid) {
@@ -55,11 +57,10 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
55
57
  return DateTime.fromMillis(dateLike, { zone: utcTimezone }).setZone(timezone);
56
58
  }
57
59
  else if (isRunTimeType(dateLike, 'string')) {
58
- const dateTime = DateTime.fromISO(dateLike, { zone: timezone });
59
- if (!dateTime.isValid) {
60
- return lastDitchConversion(dateLike);
60
+ const parsedStringDate = parseDateString(dateLike, timezone);
61
+ if (parsedStringDate) {
62
+ return parsedStringDate;
61
63
  }
62
- return dateTime;
63
64
  }
64
65
  else if (dateLike instanceof Date) {
65
66
  const dateTime = DateTime.fromJSDate(dateLike);
@@ -0,0 +1,3 @@
1
+ import { DateTime } from 'luxon';
2
+ import { Timezone } from '../timezone/timezone-names';
3
+ export declare function parseDateString(dateString: string, timezone: Timezone): DateTime | undefined;
@@ -0,0 +1,29 @@
1
+ import { DateTime } from 'luxon';
2
+ /**
3
+ * These formats are for luxon, documented here:
4
+ * https://moment.github.io/luxon/#/parsing?id=table-of-tokens
5
+ */
6
+ const knownStringFormats = [
7
+ // 1-2024
8
+ 'L-y',
9
+ // jan-2021
10
+ 'LLL-y',
11
+ // january-2021
12
+ 'LLLL-y',
13
+ ];
14
+ export function parseDateString(dateString, timezone) {
15
+ const isoAttempt = DateTime.fromISO(dateString, { zone: timezone });
16
+ if (isoAttempt.isValid) {
17
+ return isoAttempt;
18
+ }
19
+ let finalizedDateTime;
20
+ knownStringFormats.some((format) => {
21
+ const dateTimeAttempt = DateTime.fromFormat(dateString, format, { zone: timezone });
22
+ if (!dateTimeAttempt.isValid) {
23
+ return false;
24
+ }
25
+ finalizedDateTime = dateTimeAttempt;
26
+ return true;
27
+ });
28
+ return finalizedDateTime;
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "date-vir",
3
- "version": "5.3.0",
3
+ "version": "5.3.2",
4
4
  "description": "Easy and explicit dates and times",
5
5
  "keywords": [
6
6
  "date",
@@ -41,15 +41,15 @@
41
41
  "test:watch": "web-test-runner --watch --color --config configs/web-test-runner.config.mjs"
42
42
  },
43
43
  "dependencies": {
44
- "@augment-vir/common": "^28.0.2",
44
+ "@augment-vir/common": "^28.1.0",
45
45
  "@types/luxon": "^3.4.2",
46
46
  "luxon": "^3.4.4",
47
- "object-shape-tester": "^2.3.2",
48
- "type-fest": "^4.18.3"
47
+ "object-shape-tester": "^2.4.0",
48
+ "type-fest": "^4.20.0"
49
49
  },
50
50
  "devDependencies": {
51
- "@augment-vir/browser": "^28.0.2",
52
- "@augment-vir/browser-testing": "^28.0.2",
51
+ "@augment-vir/browser": "^28.1.0",
52
+ "@augment-vir/browser-testing": "^28.1.0",
53
53
  "@open-wc/testing": "^4.0.0",
54
54
  "@types/chai": "^4.3.16",
55
55
  "@types/mocha": "^10.0.6",
@@ -58,17 +58,17 @@
58
58
  "@web/test-runner-commands": "^0.9.0",
59
59
  "@web/test-runner-playwright": "^0.11.0",
60
60
  "@web/test-runner-visual-regression": "^0.9.0",
61
- "cspell": "^8.8.3",
62
- "dependency-cruiser": "^16.3.2",
61
+ "cspell": "^8.8.4",
62
+ "dependency-cruiser": "^16.3.3",
63
63
  "element-vir": "^22.2.2",
64
- "esbuild": "^0.21.4",
64
+ "esbuild": "^0.21.5",
65
65
  "istanbul-smart-text-reporter": "^1.1.4",
66
66
  "markdown-code-example-inserter": "^1.0.0",
67
67
  "npm-check-updates": "~16.12.3",
68
- "prettier": "^3.2.5",
68
+ "prettier": "^3.3.2",
69
69
  "prettier-plugin-interpolated-html-tags": "^1.0.5",
70
70
  "prettier-plugin-jsdoc": "^1.3.0",
71
- "prettier-plugin-multiline-arrays": "^3.0.4",
71
+ "prettier-plugin-multiline-arrays": "^3.0.6",
72
72
  "prettier-plugin-organize-imports": "^3.2.4",
73
73
  "prettier-plugin-packagejson": "^2.5.0",
74
74
  "prettier-plugin-sort-json": "^4.0.0",