@fr0st/datetime 8.0.1 → 8.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fr0st/datetime",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
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": [
@@ -41,15 +41,15 @@
41
41
  "src"
42
42
  ],
43
43
  "scripts": {
44
- "build": "npm run js-compile && npm run js-minify",
45
- "js-compile": "rollup --config",
46
- "js-lint": "eslint",
47
- "js-minify": "terser --compress passes=2 --mangle --source-map \"content=dist/frost-datetime.js.map,url=frost-datetime.min.js.map\" --output dist/frost-datetime.min.js dist/frost-datetime.js",
44
+ "build": "npm run bundle && npm run minify",
45
+ "bundle": "rollup --config",
46
+ "lint": "eslint",
47
+ "minify": "terser --compress passes=2 --mangle --source-map \"content=dist/frost-datetime.js.map,url=frost-datetime.min.js.map\" --output dist/frost-datetime.min.js dist/frost-datetime.js",
48
48
  "locales": "php generate-locales.php > src/formatter/locales.js",
49
49
  "test": "mocha --forbid-only --file test/setup.js \"test/unit/**/*.test.js\""
50
50
  },
51
51
  "devDependencies": {
52
- "@fr0st/eslint-config": "^4.0.0",
52
+ "@fr0st/eslint-config": "^5.0.0",
53
53
  "eslint": "^10.8.1",
54
54
  "mocha": "^11.8.0",
55
55
  "rollup": "^4.62.4",
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._date = new Date(timestamp);
425
- this._dynamicTz = false;
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._offset =
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._offset && match[1] === '+') {
445
- this._offset *= -1;
450
+ if (this.#offset && match[1] === '+') {
451
+ this.#offset *= -1;
446
452
  }
447
453
 
448
- if (this._offset) {
449
- this._timeZone = formatOffset(this._offset);
454
+ if (this.#offset) {
455
+ this.#timeZone = formatOffset(this.#offset);
450
456
  } else {
451
- this._dynamicTz = true;
452
- this._timeZone = 'UTC';
457
+ this.#dynamicTz = true;
458
+ this.#timeZone = 'UTC';
453
459
  }
454
460
  } else {
455
- this._dynamicTz = true;
456
- this._timeZone = timeZone;
461
+ this.#dynamicTz = true;
462
+ this.#timeZone = timeZone;
457
463
  }
458
464
 
459
- this._locale = 'locale' in options ?
465
+ this.#locale = 'locale' in options ?
460
466
  options.locale :
461
467
  config.defaultLocale;
462
468
 
463
- if (this._dynamicTz) {
464
- this._offset = getOffset(this);
469
+ if (this.#dynamicTz) {
470
+ this.#offset = getOffset(this);
465
471
  }
466
472
 
467
- if (adjustOffset && this._offset) {
473
+ if (adjustOffset && this.#offset) {
468
474
  const resolvedDate = setOffsetTime(this, timestamp);
469
- this._date.setTime(resolvedDate.getTime());
470
- this._offset = resolvedDate.getTimeZoneOffset();
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._locale;
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._date.getTime();
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._timeZone;
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._offset;
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._dynamicTz) {
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._dynamicTz ?
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
  }
@@ -1922,10 +1928,8 @@ export default class DateTime {
1922
1928
 
1923
1929
  /**
1924
1930
  * Returns a copy with the hours changed in the current time zone.
1925
- * @param {number} hours The hours. (0-23)
1926
- * @param {number} [minutes] The minutes. (0-59)
1927
- * @param {number} [seconds] The seconds. (0-59)
1928
- * @param {number} [milliseconds] The milliseconds.
1931
+ * @param {...number} args The hours (0-23), optionally followed by minutes
1932
+ * (0-59), seconds (0-59), and milliseconds.
1929
1933
  * @returns {DateTime} A new DateTime instance.
1930
1934
  */
1931
1935
  withHours(...args) {
@@ -1943,7 +1947,7 @@ export default class DateTime {
1943
1947
  withLocale(locale) {
1944
1948
  return new this.constructor(this.getTime(), {
1945
1949
  locale,
1946
- timeZone: this._timeZone,
1950
+ timeZone: this.#timeZone,
1947
1951
  });
1948
1952
  }
1949
1953
 
@@ -1961,9 +1965,8 @@ export default class DateTime {
1961
1965
 
1962
1966
  /**
1963
1967
  * Returns a copy with the minutes changed in the current time zone.
1964
- * @param {number} minutes The minutes. (0-59)
1965
- * @param {number} [seconds] The seconds. (0-59)
1966
- * @param {number} [milliseconds] The milliseconds.
1968
+ * @param {...number} args The minutes (0-59), optionally followed by seconds
1969
+ * (0-59) and milliseconds.
1967
1970
  * @returns {DateTime} A new DateTime instance.
1968
1971
  */
1969
1972
  withMinutes(...args) {
@@ -2020,8 +2023,8 @@ export default class DateTime {
2020
2023
 
2021
2024
  /**
2022
2025
  * Returns a copy with the seconds changed in the current time zone.
2023
- * @param {number} seconds The seconds. (0-59)
2024
- * @param {number} [milliseconds] The milliseconds.
2026
+ * @param {...number} args The seconds (0-59), optionally followed by
2027
+ * milliseconds.
2025
2028
  * @returns {DateTime} A new DateTime instance.
2026
2029
  */
2027
2030
  withSeconds(...args) {
@@ -2038,8 +2041,8 @@ export default class DateTime {
2038
2041
  */
2039
2042
  withTime(time) {
2040
2043
  return new this.constructor(time, {
2041
- locale: this._locale,
2042
- timeZone: this._timeZone,
2044
+ locale: this.#locale,
2045
+ timeZone: this.#timeZone,
2043
2046
  });
2044
2047
  }
2045
2048
 
@@ -2059,7 +2062,7 @@ export default class DateTime {
2059
2062
  */
2060
2063
  withTimeZone(timeZone) {
2061
2064
  return new this.constructor(this.getTime(), {
2062
- locale: this._locale,
2065
+ locale: this.#locale,
2063
2066
  timeZone,
2064
2067
  });
2065
2068
  }
@@ -2071,7 +2074,7 @@ export default class DateTime {
2071
2074
  */
2072
2075
  withTimeZoneOffset(offset) {
2073
2076
  return new this.constructor(this.getTime(), {
2074
- locale: this._locale,
2077
+ locale: this.#locale,
2075
2078
  timeZone: formatOffset(offset),
2076
2079
  });
2077
2080
  }
package/src/helpers.js CHANGED
@@ -1,8 +1,8 @@
1
+ /** @import DateTime from './date-time.js' */
2
+
1
3
  import { getDateFormatter } from './factory.js';
2
4
  import { diffMethods, thresholds } from './vars.js';
3
5
 
4
- /** @typedef {import('./date-time.js').default} DateTime */
5
-
6
6
  /**
7
7
  * DateTime Helpers
8
8
  */