date-vir 0.6.0 → 0.8.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.
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNowInIsoString = void 0;
4
+ const create_full_date_1 = require("../full-date/create-full-date");
5
+ const primitive_conversions_1 = require("../full-date/primitive-conversions");
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, primitive_conversions_1.toIsoString)((0, create_full_date_1.getNowFullDate)(timezones_1.utcTimezone));
13
+ }
14
+ exports.getNowInIsoString = getNowInIsoString;
@@ -27,7 +27,7 @@ timezone) {
27
27
  if (!dateTime || !dateTime.isValid) {
28
28
  throw new Error(`Failed to parse date input '${dateLike}'`);
29
29
  }
30
- return (0, luxon_date_time_conversion_1.parseLuxonDateTime)(dateTime);
30
+ return (0, luxon_date_time_conversion_1.parseLuxonDateTime)(dateTime, timezone);
31
31
  }
32
32
  exports.createFullDate = createFullDate;
33
33
  /**
@@ -41,6 +41,16 @@ function toNewTimezone(fullDate, timezone) {
41
41
  return createFullDate(fullDate, timezone);
42
42
  }
43
43
  exports.toNewTimezone = toNewTimezone;
44
+ function lastDitchConversion(dateLike) {
45
+ /** As any cast for last ditch effort to convert the dateLike into a date. */
46
+ const dateTime = luxon_1.DateTime.fromJSDate(new Date(dateLike));
47
+ if (dateTime.isValid) {
48
+ return dateTime;
49
+ }
50
+ else {
51
+ return undefined;
52
+ }
53
+ }
44
54
  function convertDateLikeToLuxonDateTime(dateLike, timezone) {
45
55
  if ((0, is_valid_full_date_1.isValidFullDate)(dateLike)) {
46
56
  dateLike = (0, luxon_date_time_conversion_1.toLuxonDateTime)(dateLike).toMillis();
@@ -54,7 +64,7 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
54
64
  else if ((0, common_1.isRuntimeTypeOf)(dateLike, 'string')) {
55
65
  const dateTime = luxon_1.DateTime.fromISO(dateLike, { zone: timezone });
56
66
  if (!dateTime.isValid) {
57
- throw new Error(`Failed to parse date string '${dateLike}': unexpected string format. Please use parseStringToFullDate instead to have more control over the parsing format.`);
67
+ return lastDitchConversion(dateLike);
58
68
  }
59
69
  return dateTime;
60
70
  }
@@ -62,7 +72,7 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
62
72
  const dateTime = luxon_1.DateTime.fromJSDate(dateLike);
63
73
  return dateTime.setZone(timezone);
64
74
  }
65
- return undefined;
75
+ return lastDitchConversion(dateLike);
66
76
  }
67
77
  function getNowFullDate(timezone) {
68
78
  return createFullDate(Date.now(), timezone);
@@ -25,7 +25,7 @@ exports.toLuxonDateTime = toLuxonDateTime;
25
25
  * could be helpful if you need it. Usually you should prefer using the createFullDate function
26
26
  * instead.
27
27
  */
28
- function parseLuxonDateTime(dateTimeInput) {
28
+ function parseLuxonDateTime(dateTimeInput, forcedTimezone) {
29
29
  if (!dateTimeInput.isValid) {
30
30
  throw new Error(`Invalid input: '${dateTimeInput.toISO()}'`);
31
31
  }
@@ -37,7 +37,7 @@ function parseLuxonDateTime(dateTimeInput) {
37
37
  minute: dateTimeInput.minute,
38
38
  second: dateTimeInput.second,
39
39
  millisecond: dateTimeInput.millisecond,
40
- timezone: dateTimeInput.zoneName,
40
+ timezone: forcedTimezone ?? dateTimeInput.zoneName,
41
41
  };
42
42
  }
43
43
  exports.parseLuxonDateTime = parseLuxonDateTime;
@@ -1,2 +1,9 @@
1
1
  "use strict";
2
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;
package/dist/cjs/index.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./date-operations/calculate-relative-date"), exports);
18
18
  __exportStar(require("./date-operations/combine-dates"), exports);
19
19
  __exportStar(require("./date-operations/diff-dates"), exports);
20
+ __exportStar(require("./extra-utils/now-in-iso-string"), exports);
20
21
  __exportStar(require("./full-date/create-full-date"), exports);
21
22
  __exportStar(require("./full-date/date-like"), exports);
22
23
  __exportStar(require("./full-date/date-transforms"), exports);
@@ -0,0 +1,2 @@
1
+ import { UtcIsoString } from '../full-date/string-format-types';
2
+ export declare function getNowInIsoString(): UtcIsoString;
@@ -0,0 +1,10 @@
1
+ import { getNowFullDate } from '../full-date/create-full-date';
2
+ import { toIsoString } from '../full-date/primitive-conversions';
3
+ import { utcTimezone } from '../timezone/timezones';
4
+ export function getNowInIsoString() {
5
+ /**
6
+ * The time zone doesn't matter here because the ISO string will be identical for a given date
7
+ * no matter what its timezone is.
8
+ */
9
+ return toIsoString(getNowFullDate(utcTimezone));
10
+ }
@@ -1,20 +1,20 @@
1
1
  import { Timezone } from '../timezone/timezone-names';
2
2
  import { DateLike } from './date-like';
3
3
  import { FullDate } from './full-date-shape';
4
- export declare function createFullDateInUserTimezone(dateLike: DateLike): FullDate;
5
- export declare function createUtcFullDate(dateLike: DateLike): FullDate;
4
+ export declare function createFullDateInUserTimezone(dateLike: Readonly<DateLike>): FullDate;
5
+ export declare function createUtcFullDate(dateLike: Readonly<DateLike>): FullDate;
6
6
  /**
7
7
  * Parses the given dateLike and then reads the fullDate properties from that parsed value as if it
8
8
  * were in the given timezone.
9
9
  */
10
10
  export declare function createFullDate(
11
11
  /** The original date representation to be converted into a FullDate. */
12
- dateLike: DateLike,
12
+ dateLike: Readonly<DateLike>,
13
13
  /** The timezone that this date/time is meant for / originated from. */
14
14
  timezone: Timezone): FullDate;
15
15
  /**
16
16
  * Creates and returns a new FullDate object with its values shifted to be the same original time
17
17
  * but represented in a new timezone.
18
18
  */
19
- export declare function toNewTimezone(fullDate: FullDate, timezone: Timezone): FullDate;
19
+ export declare function toNewTimezone(fullDate: Readonly<FullDate>, timezone: Timezone): Readonly<FullDate>;
20
20
  export declare function getNowFullDate(timezone: Timezone): FullDate;
@@ -22,7 +22,7 @@ timezone) {
22
22
  if (!dateTime || !dateTime.isValid) {
23
23
  throw new Error(`Failed to parse date input '${dateLike}'`);
24
24
  }
25
- return parseLuxonDateTime(dateTime);
25
+ return parseLuxonDateTime(dateTime, timezone);
26
26
  }
27
27
  /**
28
28
  * Creates and returns a new FullDate object with its values shifted to be the same original time
@@ -34,6 +34,16 @@ export function toNewTimezone(fullDate, timezone) {
34
34
  }
35
35
  return createFullDate(fullDate, timezone);
36
36
  }
37
+ function lastDitchConversion(dateLike) {
38
+ /** As any cast for last ditch effort to convert the dateLike into a date. */
39
+ const dateTime = DateTime.fromJSDate(new Date(dateLike));
40
+ if (dateTime.isValid) {
41
+ return dateTime;
42
+ }
43
+ else {
44
+ return undefined;
45
+ }
46
+ }
37
47
  function convertDateLikeToLuxonDateTime(dateLike, timezone) {
38
48
  if (isValidFullDate(dateLike)) {
39
49
  dateLike = toLuxonDateTime(dateLike).toMillis();
@@ -47,7 +57,7 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
47
57
  else if (isRuntimeTypeOf(dateLike, 'string')) {
48
58
  const dateTime = DateTime.fromISO(dateLike, { zone: timezone });
49
59
  if (!dateTime.isValid) {
50
- throw new Error(`Failed to parse date string '${dateLike}': unexpected string format. Please use parseStringToFullDate instead to have more control over the parsing format.`);
60
+ return lastDitchConversion(dateLike);
51
61
  }
52
62
  return dateTime;
53
63
  }
@@ -55,7 +65,7 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
55
65
  const dateTime = DateTime.fromJSDate(dateLike);
56
66
  return dateTime.setZone(timezone);
57
67
  }
58
- return undefined;
68
+ return lastDitchConversion(dateLike);
59
69
  }
60
70
  export function getNowFullDate(timezone) {
61
71
  return createFullDate(Date.now(), timezone);
@@ -1,4 +1,5 @@
1
1
  import { DateTime } from 'luxon';
2
+ import { Timezone } from '../timezone/timezone-names';
2
3
  import { FullDate } from './full-date-shape';
3
4
  /**
4
5
  * Converts a FullDate object into a Luxon DateTime library. This is only needed if you need complex
@@ -11,4 +12,4 @@ export declare function toLuxonDateTime(fullDateInput: FullDate): DateTime;
11
12
  * could be helpful if you need it. Usually you should prefer using the createFullDate function
12
13
  * instead.
13
14
  */
14
- export declare function parseLuxonDateTime(dateTimeInput: DateTime): FullDate;
15
+ export declare function parseLuxonDateTime(dateTimeInput: DateTime, forcedTimezone?: Timezone): FullDate;
@@ -21,7 +21,7 @@ export function toLuxonDateTime(fullDateInput) {
21
21
  * could be helpful if you need it. Usually you should prefer using the createFullDate function
22
22
  * instead.
23
23
  */
24
- export function parseLuxonDateTime(dateTimeInput) {
24
+ export function parseLuxonDateTime(dateTimeInput, forcedTimezone) {
25
25
  if (!dateTimeInput.isValid) {
26
26
  throw new Error(`Invalid input: '${dateTimeInput.toISO()}'`);
27
27
  }
@@ -33,6 +33,6 @@ export function parseLuxonDateTime(dateTimeInput) {
33
33
  minute: dateTimeInput.minute,
34
34
  second: dateTimeInput.second,
35
35
  millisecond: dateTimeInput.millisecond,
36
- timezone: dateTimeInput.zoneName,
36
+ timezone: forcedTimezone ?? dateTimeInput.zoneName,
37
37
  };
38
38
  }
@@ -4,3 +4,4 @@ export type JustTimeString = `${number}:${number}`;
4
4
  export type DateTimeWithSeconds = `${JustDateString}T${JustTimeWithSecondsString}`;
5
5
  export type DateTimeString = `${JustDateString}T${JustTimeString}`;
6
6
  export type UtcIsoString = `${JustDateString}T${JustTimeWithSecondsString}.${number}Z`;
7
+ export declare function isValidIsoString(input: unknown): input is UtcIsoString;
@@ -1 +1,5 @@
1
- export {};
1
+ import { DateTime } from 'luxon';
2
+ export function isValidIsoString(input) {
3
+ const datetime = DateTime.fromISO(input);
4
+ return datetime.toISO() === input;
5
+ }
@@ -1,6 +1,7 @@
1
1
  export * from './date-operations/calculate-relative-date';
2
2
  export * from './date-operations/combine-dates';
3
3
  export * from './date-operations/diff-dates';
4
+ export * from './extra-utils/now-in-iso-string';
4
5
  export * from './full-date/create-full-date';
5
6
  export * from './full-date/date-like';
6
7
  export * from './full-date/date-transforms';
package/dist/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './date-operations/calculate-relative-date';
2
2
  export * from './date-operations/combine-dates';
3
3
  export * from './date-operations/diff-dates';
4
+ export * from './extra-utils/now-in-iso-string';
4
5
  export * from './full-date/create-full-date';
5
6
  export * from './full-date/date-like';
6
7
  export * from './full-date/date-transforms';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "date-vir",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Easy and explicit dates and times",
5
5
  "keywords": [
6
6
  "date",
@@ -40,15 +40,15 @@
40
40
  "test:watch": "web-test-runner --watch --color --config configs/web-test-runner.config.mjs"
41
41
  },
42
42
  "dependencies": {
43
- "@augment-vir/common": "^16.2.0",
43
+ "@augment-vir/common": "^16.3.0",
44
44
  "@types/luxon": "^3.3.1",
45
- "luxon": "^3.3.0",
45
+ "luxon": "^3.4.0",
46
46
  "object-shape-tester": "^0.4.2",
47
47
  "type-fest": "^4.2.0"
48
48
  },
49
49
  "devDependencies": {
50
- "@augment-vir/browser": "^16.2.0",
51
- "@augment-vir/browser-testing": "^16.2.0",
50
+ "@augment-vir/browser": "^16.3.0",
51
+ "@augment-vir/browser-testing": "^16.3.0",
52
52
  "@open-wc/testing": "^3.2.0",
53
53
  "@types/chai": "^4.3.5",
54
54
  "@types/mocha": "^10.0.1",
@@ -57,9 +57,9 @@
57
57
  "@web/test-runner-commands": "^0.8.0",
58
58
  "@web/test-runner-playwright": "^0.10.1",
59
59
  "@web/test-runner-visual-regression": "^0.8.2",
60
- "cspell": "^6.31.3",
60
+ "cspell": "^7.0.0",
61
61
  "element-vir": "^16.3.1",
62
- "esbuild": "^0.19.0",
62
+ "esbuild": "^0.19.1",
63
63
  "istanbul-smart-text-reporter": "^1.1.2",
64
64
  "markdown-code-example-inserter": "^0.3.1",
65
65
  "npm-check-updates": "^16.10.18",