@fr0st/datetime 8.0.1 → 8.0.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.
- package/dist/frost-datetime.js +34 -28
- package/dist/frost-datetime.js.map +1 -1
- package/dist/frost-datetime.min.js +1 -1
- package/dist/frost-datetime.min.js.map +1 -1
- package/package.json +1 -1
- package/src/date-time.js +34 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fr0st/datetime",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Immutable date and time handling for JavaScript with locale-aware formatting, parsing, calendar math, and IANA or fixed-offset time zones.",
|
|
6
6
|
"keywords": [
|
package/src/date-time.js
CHANGED
|
@@ -38,6 +38,12 @@ import {
|
|
|
38
38
|
* An immutable date and time object with locale-aware formatting and time-zone support.
|
|
39
39
|
*/
|
|
40
40
|
export default class DateTime {
|
|
41
|
+
#date;
|
|
42
|
+
#dynamicTz;
|
|
43
|
+
#locale;
|
|
44
|
+
#offset;
|
|
45
|
+
#timeZone;
|
|
46
|
+
|
|
41
47
|
/**
|
|
42
48
|
* Clears cached formatter and locale data.
|
|
43
49
|
*/
|
|
@@ -421,8 +427,8 @@ export default class DateTime {
|
|
|
421
427
|
throw new Error('Invalid date supplied');
|
|
422
428
|
}
|
|
423
429
|
|
|
424
|
-
this
|
|
425
|
-
this
|
|
430
|
+
this.#date = new Date(timestamp);
|
|
431
|
+
this.#dynamicTz = false;
|
|
426
432
|
this.isValid = true;
|
|
427
433
|
|
|
428
434
|
let timeZone = options.timeZone;
|
|
@@ -437,37 +443,37 @@ export default class DateTime {
|
|
|
437
443
|
|
|
438
444
|
const match = timeZone.match(offsetRegExp);
|
|
439
445
|
if (match) {
|
|
440
|
-
this
|
|
446
|
+
this.#offset =
|
|
441
447
|
match[2] * 60 +
|
|
442
448
|
parseInt(match[4] || 0, 10) +
|
|
443
449
|
parseInt(match[5] || 0, 10) / 60;
|
|
444
|
-
if (this
|
|
445
|
-
this
|
|
450
|
+
if (this.#offset && match[1] === '+') {
|
|
451
|
+
this.#offset *= -1;
|
|
446
452
|
}
|
|
447
453
|
|
|
448
|
-
if (this
|
|
449
|
-
this
|
|
454
|
+
if (this.#offset) {
|
|
455
|
+
this.#timeZone = formatOffset(this.#offset);
|
|
450
456
|
} else {
|
|
451
|
-
this
|
|
452
|
-
this
|
|
457
|
+
this.#dynamicTz = true;
|
|
458
|
+
this.#timeZone = 'UTC';
|
|
453
459
|
}
|
|
454
460
|
} else {
|
|
455
|
-
this
|
|
456
|
-
this
|
|
461
|
+
this.#dynamicTz = true;
|
|
462
|
+
this.#timeZone = timeZone;
|
|
457
463
|
}
|
|
458
464
|
|
|
459
|
-
this
|
|
465
|
+
this.#locale = 'locale' in options ?
|
|
460
466
|
options.locale :
|
|
461
467
|
config.defaultLocale;
|
|
462
468
|
|
|
463
|
-
if (this
|
|
464
|
-
this
|
|
469
|
+
if (this.#dynamicTz) {
|
|
470
|
+
this.#offset = getOffset(this);
|
|
465
471
|
}
|
|
466
472
|
|
|
467
|
-
if (adjustOffset && this
|
|
473
|
+
if (adjustOffset && this.#offset) {
|
|
468
474
|
const resolvedDate = setOffsetTime(this, timestamp);
|
|
469
|
-
this.
|
|
470
|
-
this
|
|
475
|
+
this.#date.setTime(resolvedDate.getTime());
|
|
476
|
+
this.#offset = resolvedDate.getTimeZoneOffset();
|
|
471
477
|
}
|
|
472
478
|
}
|
|
473
479
|
|
|
@@ -898,7 +904,7 @@ export default class DateTime {
|
|
|
898
904
|
* @returns {string} The locale.
|
|
899
905
|
*/
|
|
900
906
|
getLocale() {
|
|
901
|
-
return this
|
|
907
|
+
return this.#locale;
|
|
902
908
|
}
|
|
903
909
|
|
|
904
910
|
/**
|
|
@@ -946,7 +952,7 @@ export default class DateTime {
|
|
|
946
952
|
* @returns {number} The number of milliseconds since the UNIX epoch.
|
|
947
953
|
*/
|
|
948
954
|
getTime() {
|
|
949
|
-
return this.
|
|
955
|
+
return this.#date.getTime();
|
|
950
956
|
}
|
|
951
957
|
|
|
952
958
|
/**
|
|
@@ -962,7 +968,7 @@ export default class DateTime {
|
|
|
962
968
|
* @returns {string} The time zone.
|
|
963
969
|
*/
|
|
964
970
|
getTimeZone() {
|
|
965
|
-
return this
|
|
971
|
+
return this.#timeZone;
|
|
966
972
|
}
|
|
967
973
|
|
|
968
974
|
/**
|
|
@@ -970,7 +976,7 @@ export default class DateTime {
|
|
|
970
976
|
* @returns {number} The UTC offset in minutes.
|
|
971
977
|
*/
|
|
972
978
|
getTimeZoneOffset() {
|
|
973
|
-
return this
|
|
979
|
+
return this.#offset;
|
|
974
980
|
}
|
|
975
981
|
|
|
976
982
|
/**
|
|
@@ -1344,7 +1350,7 @@ export default class DateTime {
|
|
|
1344
1350
|
* @returns {boolean} Whether the current time is in daylight saving time.
|
|
1345
1351
|
*/
|
|
1346
1352
|
isDst() {
|
|
1347
|
-
if (!this
|
|
1353
|
+
if (!this.#dynamicTz) {
|
|
1348
1354
|
return false;
|
|
1349
1355
|
}
|
|
1350
1356
|
|
|
@@ -1799,7 +1805,7 @@ export default class DateTime {
|
|
|
1799
1805
|
* @returns {string} The name of the time zone.
|
|
1800
1806
|
*/
|
|
1801
1807
|
timeZoneName(type = 'long') {
|
|
1802
|
-
return this
|
|
1808
|
+
return this.#dynamicTz ?
|
|
1803
1809
|
formatTimeZoneName(this.getLocale(), this.getTime(), this.getTimeZone(), type) :
|
|
1804
1810
|
'GMT' + formatOffset(this.getTimeZoneOffset(), true, type === 'short');
|
|
1805
1811
|
}
|
|
@@ -1943,7 +1949,7 @@ export default class DateTime {
|
|
|
1943
1949
|
withLocale(locale) {
|
|
1944
1950
|
return new this.constructor(this.getTime(), {
|
|
1945
1951
|
locale,
|
|
1946
|
-
timeZone: this
|
|
1952
|
+
timeZone: this.#timeZone,
|
|
1947
1953
|
});
|
|
1948
1954
|
}
|
|
1949
1955
|
|
|
@@ -2038,8 +2044,8 @@ export default class DateTime {
|
|
|
2038
2044
|
*/
|
|
2039
2045
|
withTime(time) {
|
|
2040
2046
|
return new this.constructor(time, {
|
|
2041
|
-
locale: this
|
|
2042
|
-
timeZone: this
|
|
2047
|
+
locale: this.#locale,
|
|
2048
|
+
timeZone: this.#timeZone,
|
|
2043
2049
|
});
|
|
2044
2050
|
}
|
|
2045
2051
|
|
|
@@ -2059,7 +2065,7 @@ export default class DateTime {
|
|
|
2059
2065
|
*/
|
|
2060
2066
|
withTimeZone(timeZone) {
|
|
2061
2067
|
return new this.constructor(this.getTime(), {
|
|
2062
|
-
locale: this
|
|
2068
|
+
locale: this.#locale,
|
|
2063
2069
|
timeZone,
|
|
2064
2070
|
});
|
|
2065
2071
|
}
|
|
@@ -2071,7 +2077,7 @@ export default class DateTime {
|
|
|
2071
2077
|
*/
|
|
2072
2078
|
withTimeZoneOffset(offset) {
|
|
2073
2079
|
return new this.constructor(this.getTime(), {
|
|
2074
|
-
locale: this
|
|
2080
|
+
locale: this.#locale,
|
|
2075
2081
|
timeZone: formatOffset(offset),
|
|
2076
2082
|
});
|
|
2077
2083
|
}
|