@alexstukovnikov/oz-time 1.0.0 → 1.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.
- package/package.json +45 -48
- package/src/core/core.js +353 -0
- package/src/core/factory.js +194 -0
- package/src/index.js +22 -0
- package/src/modules/arithmetic.js +100 -0
- package/src/modules/compare.js +197 -0
- package/src/modules/duration.js +165 -0
- package/src/modules/format.js +162 -0
- package/src/modules/interval.js +190 -0
- package/src/modules/timezone.js +112 -0
- package/src/utils/calendar.js +277 -0
- package/src/utils/units.js +135 -0
- package/dist/oz-time.cjs +0 -1
- package/dist/oz-time.esm.js +0 -426
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Утилиты для нормализации и проверки единиц времени.
|
|
3
|
+
*
|
|
4
|
+
* @module utils/units
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Объект соотношений разных названий единиц времени к каноническим названиям.
|
|
9
|
+
*
|
|
10
|
+
* @type {Object.<string, string>}
|
|
11
|
+
*/
|
|
12
|
+
const UNIT_ALIASES = {
|
|
13
|
+
millisecond: 'millisecond',
|
|
14
|
+
milliseconds: 'millisecond',
|
|
15
|
+
ms: 'millisecond',
|
|
16
|
+
|
|
17
|
+
second: 'second',
|
|
18
|
+
seconds: 'second',
|
|
19
|
+
s: 'second',
|
|
20
|
+
|
|
21
|
+
minute: 'minute',
|
|
22
|
+
minutes: 'minute',
|
|
23
|
+
m: 'minute',
|
|
24
|
+
|
|
25
|
+
hour: 'hour',
|
|
26
|
+
hours: 'hour',
|
|
27
|
+
h: 'hour',
|
|
28
|
+
|
|
29
|
+
day: 'day',
|
|
30
|
+
days: 'day',
|
|
31
|
+
d: 'day',
|
|
32
|
+
|
|
33
|
+
month: 'month',
|
|
34
|
+
months: 'month',
|
|
35
|
+
|
|
36
|
+
year: 'year',
|
|
37
|
+
years: 'year',
|
|
38
|
+
y: 'year',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Точное количество миллисекунд для фиксированных единиц времени.
|
|
43
|
+
*
|
|
44
|
+
* @type {Object.<string, number>}
|
|
45
|
+
*/
|
|
46
|
+
export const FIXED_UNIT_TO_MS = {
|
|
47
|
+
millisecond: 1,
|
|
48
|
+
second: 1000,
|
|
49
|
+
minute: 60 * 1000,
|
|
50
|
+
hour: 60 * 60 * 1000,
|
|
51
|
+
day: 24 * 60 * 60 * 1000,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Массив поддерживаемых календарных единиц времени.
|
|
56
|
+
*
|
|
57
|
+
* @type {string[]}
|
|
58
|
+
*/
|
|
59
|
+
export const CALENDAR_UNITS = ['month', 'year'];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Нормализует единицу времени к каноническому виду.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} unit - Единица времени.
|
|
65
|
+
* @throws {TypeError} Выбрасывается, если unit пустой или не является строкой.
|
|
66
|
+
* @throws {Error} Выбрасывается, если unit не поддерживается.
|
|
67
|
+
* @returns {string} Каноническое название единицы времени.
|
|
68
|
+
* @example
|
|
69
|
+
* import { normalizeUnit } from './utils/units.js';
|
|
70
|
+
*
|
|
71
|
+
* console.log(normalizeUnit('hours')); // ожидаемый результат: hour
|
|
72
|
+
*/
|
|
73
|
+
export function normalizeUnit(unit) {
|
|
74
|
+
if (typeof unit !== 'string' || unit.trim() === '') {
|
|
75
|
+
throw new TypeError('normalizeUnit: unit must be a non-empty string');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const normalized = unit.toLowerCase().trim();
|
|
79
|
+
const canonical = UNIT_ALIASES[normalized];
|
|
80
|
+
|
|
81
|
+
if (!canonical) {
|
|
82
|
+
throw new Error(`Unsupported unit: ${unit}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return canonical;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Проверяет, является ли единица фиксированной.
|
|
90
|
+
*
|
|
91
|
+
* @param {string} unit - Единица времени.
|
|
92
|
+
* @returns {boolean} `true`, если единица является фиксированной.
|
|
93
|
+
* @example
|
|
94
|
+
* import { isFixedUnit } from './utils/units.js';
|
|
95
|
+
*
|
|
96
|
+
* console.log(isFixedUnit('minute')); // ожидаемый результат: true
|
|
97
|
+
*/
|
|
98
|
+
export function isFixedUnit(unit) {
|
|
99
|
+
return normalizeUnit(unit) in FIXED_UNIT_TO_MS;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Проверяет, является ли единица календарной.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} unit - Единица времени.
|
|
106
|
+
* @returns {boolean} `true`, если единица является календарной.
|
|
107
|
+
* @example
|
|
108
|
+
* import { isCalendarUnit } from './utils/units.js';
|
|
109
|
+
*
|
|
110
|
+
* console.log(isCalendarUnit('month')); // ожидаемый результат: true
|
|
111
|
+
*/
|
|
112
|
+
export function isCalendarUnit(unit) {
|
|
113
|
+
return CALENDAR_UNITS.includes(normalizeUnit(unit));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Возвращает точное количество миллисекунд в одной фиксированной единице времени.
|
|
118
|
+
*
|
|
119
|
+
* @param {string} unit - Фиксированная единица времени.
|
|
120
|
+
* @throws {Error} Выбрасывается, если unit нельзя точно перевести в миллисекунды.
|
|
121
|
+
* @returns {number} Количество миллисекунд в одной единице времени.
|
|
122
|
+
* @example
|
|
123
|
+
* import { unitToMilliseconds } from './utils/units.js';
|
|
124
|
+
*
|
|
125
|
+
* console.log(unitToMilliseconds('hour')); // ожидаемый результат: 3600000
|
|
126
|
+
*/
|
|
127
|
+
export function unitToMilliseconds(unit) {
|
|
128
|
+
const normalizedUnit = normalizeUnit(unit);
|
|
129
|
+
|
|
130
|
+
if (!(normalizedUnit in FIXED_UNIT_TO_MS)) {
|
|
131
|
+
throw new Error(`Unit cannot be converted to milliseconds exactly: ${unit}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return FIXED_UNIT_TO_MS[normalizedUnit];
|
|
135
|
+
}
|
package/dist/oz-time.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e={millisecond:`millisecond`,milliseconds:`millisecond`,ms:`millisecond`,second:`second`,seconds:`second`,s:`second`,minute:`minute`,minutes:`minute`,m:`minute`,hour:`hour`,hours:`hour`,h:`hour`,day:`day`,days:`day`,d:`day`,month:`month`,months:`month`,year:`year`,years:`year`,y:`year`},t={millisecond:1,second:1e3,minute:60*1e3,hour:3600*1e3,day:1440*60*1e3},n=[`month`,`year`];function r(t){if(typeof t!=`string`||t.trim()===``)throw TypeError(`normalizeUnit: unit must be a non-empty string`);let n=e[t.toLowerCase().trim()];if(!n)throw Error(`Unsupported unit: ${t}`);return n}function i(e){return r(e)in t}function a(e){return n.includes(r(e))}function o(e){let n=r(e);if(!(n in t))throw Error(`Unit cannot be converted to milliseconds exactly: ${e}`);return t[n]}function s(e,t){if(typeof e!=`number`||Number.isNaN(e))throw TypeError(`${t}: timestamp must be a valid number`)}function c(e,t){if(typeof e!=`number`||Number.isNaN(e))throw TypeError(`${t}: amount must be a valid number`)}function l(e,t){if(!(e instanceof B))throw TypeError(`${t} must be OzTime`)}function u(e,t){let n=new Date(e),r=new Date(t),i=(n.getUTCFullYear()-r.getUTCFullYear())*12+(n.getUTCMonth()-r.getUTCMonth()),a=n.getUTCDate(),o=r.getUTCDate();return i>0&&a<o?--i:i<0&&a>o&&(i+=1),i}function d(e,t){let n=new Date(e),r=new Date(t),i=n.getUTCFullYear()-r.getUTCFullYear(),a=n.getUTCMonth(),o=r.getUTCMonth(),s=n.getUTCDate(),c=r.getUTCDate();return i>0&&(a<o||a===o&&s<c)?--i:i<0&&(a>o||a===o&&s>c)&&(i+=1),i}function f(e){if(!Number.isInteger(e))throw TypeError(`isLeapYear: year must be an integer`);return e%4==0&&(e%100!=0||e%400==0)}function p(e,t){if(!Number.isInteger(e)||!Number.isInteger(t))throw TypeError(`daysInMonth: year and month must be integers`);if(t<1||t>12)throw RangeError(`daysInMonth: month must be between 1 and 12`);return new Date(Date.UTC(e,t,0)).getUTCDate()}function m(e,t,n){s(e,`addByFixedUnit`),c(t,`addByFixedUnit`);let a=r(n);if(!i(a))throw Error(`addByFixedUnit does not support calendar unit: ${n}`);return e+t*o(a)}function h(e,t,n){s(e,`addByCalendarUnit`),c(t,`addByCalendarUnit`);let i=r(n),a=new Date(e);if(i===`month`){let e=a.getUTCDate();a.setUTCDate(1),a.setUTCMonth(a.getUTCMonth()+t);let n=p(a.getUTCFullYear(),a.getUTCMonth()+1);return a.setUTCDate(Math.min(e,n)),a.getTime()}if(i===`year`){let e=a.getUTCMonth(),n=a.getUTCDate();a.setUTCDate(1),a.setUTCFullYear(a.getUTCFullYear()+t),a.setUTCMonth(e);let r=p(a.getUTCFullYear(),e+1);return a.setUTCDate(Math.min(n,r)),a.getTime()}throw Error(`addByCalendarUnit supports only month and year: ${n}`)}function g(e,t,n=`millisecond`){l(e,`left`),l(t,`right`);let a=r(n),s=e.getTimestamp(),c=t.getTimestamp();if(i(a))return(s-c)/o(a);if(a===`month`)return u(s,c);if(a===`year`)return d(s,c);throw Error(`Unsupported unit: ${n}`)}function _(e,t){if(!(e instanceof B))throw TypeError(`${t} must be OzTime`)}function v(e){if(typeof e!=`number`||Number.isNaN(e))throw TypeError(`amount must be a valid number`)}function y(e,t,n){_(e,`time`),v(t);let o=r(n),s=e.getTimestamp(),c;return i(o)?c=m(s,t,o):a(o)&&(c=h(s,t,o)),new B(c,e.getTimezone(),e.getLocale())}function b(e,t,n){return _(e,`time`),v(t),y(e,-t,n)}function x(e){if(!(e instanceof B))throw TypeError(`format: first argument must be OzTime`)}function S(e,t=2){return String(e).padStart(t,`0`)}function C(e,t){let n=new Intl.DateTimeFormat(t,{timeZone:e.getTimezone(),year:`numeric`,month:`numeric`,day:`numeric`,hour:`numeric`,minute:`2-digit`,second:`2-digit`,hour12:!1}).formatToParts(new Date(e.getTimestamp()));return Object.fromEntries(n.map(e=>[e.type,e.value]))}function w(e,t,n){return new Intl.DateTimeFormat(t,{timeZone:e.getTimezone(),month:n}).format(new Date(e.getTimestamp()))}function T(e,t,n){return new Intl.DateTimeFormat(t,{timeZone:e.getTimezone(),weekday:n}).format(new Date(e.getTimestamp()))}function E(e,t,n){if(x(e),typeof t!=`string`||t.trim()===``)throw TypeError(`format: template must be a non-empty string`);let r=n??e.getLocale(),i=C(e,r),a=Number(i.year),o=Number(i.month),s=Number(i.day),c=Number(i.hour),l=Number(i.minute),u=Number(i.second),d=new Date(e.getTimestamp()).getUTCMilliseconds(),f=c%12,p=f===0?12:f,m=c>=12?`PM`:`AM`,h={YYYY:String(a),YY:String(a).slice(-2),MMMM:w(e,r,`long`),MMM:w(e,r,`short`),MM:S(o),M:String(o),dddd:T(e,r,`long`),ddd:T(e,r,`short`),DD:S(s),D:String(s),HH:S(c),H:String(c),hh:S(p),h:String(p),mm:S(l),ss:S(u),SSS:S(d,3),A:m};return t.replace(/YYYY|MMMM|MMM|MM|M|dddd|ddd|DD|D|HH|H|hh|h|mm|ss|SSS|YY|A/g,e=>h[e]??e)}function D(e,t){if(!(e instanceof B))throw TypeError(`${t} must be OzTime`)}function O(e,t){let n=r(t),i=new Date(e);switch(n){case`millisecond`:return i.getTime();case`second`:i.setUTCMilliseconds(0);break;case`minute`:i.setUTCSeconds(0,0);break;case`hour`:i.setUTCMinutes(0,0,0);break;case`day`:i.setUTCHours(0,0,0,0);break;case`month`:i.setUTCHours(0,0,0,0),i.setUTCDate(1);break;case`year`:i.setUTCHours(0,0,0,0),i.setUTCDate(1),i.setUTCMonth(0);break;default:throw Error(`Unsupported unit for truncateToUnit: ${t}`)}return i.getTime()}function k(e,t,n=`millisecond`){return D(e,`a`),D(t,`b`),O(e.getTimestamp(),n)===O(t.getTimestamp(),n)}function A(e,t,n=`millisecond`){return D(e,`a`),D(t,`b`),O(e.getTimestamp(),n)<O(t.getTimestamp(),n)}function j(e,t,n=`millisecond`){return D(e,`a`),D(t,`b`),O(e.getTimestamp(),n)>O(t.getTimestamp(),n)}function M(e,t,n,r=`millisecond`,i=`[]`){D(e,`target`),D(t,`left`),D(n,`right`);let a=O(e.getTimestamp(),r),o=O(t.getTimestamp(),r),s=O(n.getTimestamp(),r),c=Math.min(o,s),l=Math.max(o,s);switch(i){case`[]`:return a>=c&&a<=l;case`[)`:return a>=c&&a<l;case`(]`:return a>c&&a<=l;case`()`:return a>c&&a<l;default:throw Error(`Invalid inclusivity value: ${i}`)}}function N(e){if(typeof e!=`string`||e.trim()===``)throw TypeError(`setTimezone: timezone must be a non-empty string`);if(!Intl.supportedValuesOf(`timeZone`).includes(e))throw Error(`Unsupported timezone: ${e}`)}function P(e,t){let n=new Date(e),r=new Intl.DateTimeFormat(`en-US`,{timeZone:t,year:`numeric`,month:`2-digit`,day:`2-digit`,hour:`2-digit`,minute:`2-digit`,second:`2-digit`,hour12:!1}).formatToParts(n),i=Object.fromEntries(r.map(e=>[e.type,e.value])),a=Number(i.year),o=Number(i.month),s=Number(i.day),c=Number(i.hour),l=Number(i.minute),u=Number(i.second);return(Date.UTC(a,o-1,s,c,l,u)-e)/6e4}function F(e,t){if(!(e instanceof B))throw TypeError(`tz: first argument must be OzTime`);return N(t),new B(e.getTimestamp(),t,e.getLocale())}function I(e){if(!(e instanceof B))throw TypeError(`getTimezoneOffset: argument must be OzTime`);return P(e.getTimestamp(),e.getTimezone())}function L(e){if(typeof e!=`number`||Number.isNaN(e))throw TypeError(`OzTime: timestamp must be a valid number`)}function R(e){if(typeof e!=`string`||e.trim()===``)throw TypeError(`OzTime: timezone must be a non-empty string`)}function z(e){if(typeof e!=`string`||e.trim()===``)throw TypeError(`OzTime: locale must be a non-empty string`)}var B=class{constructor(e,t=`UTC`,n=`en-US`){L(e),R(t),z(n),this._timestamp=e,this._timezone=t,this._locale=n}getTimestamp(){return this._timestamp}getTimezone(){return this._timezone}getLocale(){return this._locale}toTimestamp(){return this._timestamp}toISOString(){return new Date(this._timestamp).toISOString()}add(e,t){return y(this,e,t)}subtract(e,t){return b(this,e,t)}format(e,t){return E(this,e,t)}isSame(e,t=`millisecond`){return k(this,e,t)}isBefore(e,t=`millisecond`){return A(this,e,t)}isAfter(e,t=`millisecond`){return j(this,e,t)}isBetween(e,t,n=`millisecond`,r=`[]`){return M(this,e,t,n,r)}setTimezone(e){return F(this,e)}getTimezoneOffset(){return I(this)}diff(e,t=`millisecond`){return g(this,e,t)}};function V(e){if(typeof e!=`number`||Number.isNaN(e))throw TypeError(`fromTimestamp: timestamp must be a valid number`)}function H(e){if(!(e instanceof Date)||Number.isNaN(e.getTime()))throw TypeError(`fromDate: date must be a valid Date`)}function U(e,t){if(!Number.isInteger(e))throw TypeError(`${t} must be an integer`)}function W(e=`UTC`,t=`en-US`){return new B(Date.now(),e,t)}function G(e,t=`UTC`,n=`en-US`){return V(e),new B(e,t,n)}function K(e,t=`UTC`,n=`en-US`){return H(e),new B(e.getTime(),t,n)}function q(e,t=`UTC`,n=`en-US`){if(typeof e!=`string`||e.trim()===``)throw TypeError(`fromISO: isoString must be a non-empty string`);let r=Date.parse(e);if(Number.isNaN(r))throw Error(`Invalid ISO date string: ${e}`);return new B(r,t,n)}function J(e,t,n,r=0,i=0,a=0,o=0,s=`UTC`,c=`en-US`){if(U(e,`year`),U(t,`month`),U(n,`day`),U(r,`hour`),U(i,`minute`),U(a,`second`),U(o,`millisecond`),t<1||t>12)throw RangeError(`month must be between 1 and 12`);if(r<0||r>23)throw RangeError(`hour must be between 0 and 23`);if(i<0||i>59)throw RangeError(`minute must be between 0 and 59`);if(a<0||a>59)throw RangeError(`second must be between 0 and 59`);if(o<0||o>999)throw RangeError(`millisecond must be between 0 and 999`);let l=p(e,t);if(n<1||n>l)throw RangeError(`day must be between 1 and ${l} for ${e}-${t}`);return new B(Date.UTC(e,t-1,n,r,i,a,o),s,c)}function Y(e,t){if(!(e instanceof B))throw TypeError(`${t} must be OzTime`)}var X=class e{constructor(e,t){if(Y(e,`start`),Y(t,`end`),e.getTimestamp()>t.getTimestamp())throw RangeError(`Interval: start must be before or equal to end`);this._start=e,this._end=t}getStart(){return this._start}getEnd(){return this._end}contains(e){Y(e,`moment`);let t=e.getTimestamp();return t>=this._start.getTimestamp()&&t<=this._end.getTimestamp()}overlaps(t){if(!(t instanceof e))throw TypeError(`other must be Interval`);let n=this._start.getTimestamp(),r=this._end.getTimestamp(),i=t.getStart().getTimestamp();return n<=t.getEnd().getTimestamp()&&i<=r}duration(e=`millisecond`){let t=r(e);if(!i(t))throw Error(`Interval.duration supports only fixed units: ${e}`);return(this._end.getTimestamp()-this._start.getTimestamp())/o(t)}};function Z(e,t){return new X(e,t)}function Q(e){if(typeof e!=`number`||Number.isNaN(e))throw TypeError(`amount must be a valid number`)}var $=class e{constructor(e){if(typeof e!=`number`||Number.isNaN(e))throw TypeError(`Duration: milliseconds must be a valid number`);this._milliseconds=e}asMilliseconds(){return this._milliseconds}asSeconds(){return this._milliseconds/o(`second`)}asMinutes(){return this._milliseconds/o(`minute`)}asHours(){return this._milliseconds/o(`hour`)}asDays(){return this._milliseconds/o(`day`)}add(t){if(!(t instanceof e))throw TypeError(`Duration.add: other must be Duration`);return new e(this._milliseconds+t._milliseconds)}};function ee(e,t){Q(e);let n=r(t);if(!i(n))throw Error(`duration supports only fixed units: ${t}`);return new $(e*o(n))}exports.Duration=$,exports.Interval=X,exports.OzTime=B,exports.add=y,exports.daysInMonth=p,exports.duration=ee,exports.format=E,exports.fromComponents=J,exports.fromDate=K,exports.fromISO=q,exports.fromTimestamp=G,exports.getTimezoneOffset=I,exports.interval=Z,exports.isAfter=j,exports.isBefore=A,exports.isBetween=M,exports.isLeapYear=f,exports.isSame=k,exports.now=W,exports.setTimezone=F,exports.subtract=b;
|
package/dist/oz-time.esm.js
DELETED
|
@@ -1,426 +0,0 @@
|
|
|
1
|
-
//#region src/utils/units.js
|
|
2
|
-
var e = {
|
|
3
|
-
millisecond: "millisecond",
|
|
4
|
-
milliseconds: "millisecond",
|
|
5
|
-
ms: "millisecond",
|
|
6
|
-
second: "second",
|
|
7
|
-
seconds: "second",
|
|
8
|
-
s: "second",
|
|
9
|
-
minute: "minute",
|
|
10
|
-
minutes: "minute",
|
|
11
|
-
m: "minute",
|
|
12
|
-
hour: "hour",
|
|
13
|
-
hours: "hour",
|
|
14
|
-
h: "hour",
|
|
15
|
-
day: "day",
|
|
16
|
-
days: "day",
|
|
17
|
-
d: "day",
|
|
18
|
-
month: "month",
|
|
19
|
-
months: "month",
|
|
20
|
-
year: "year",
|
|
21
|
-
years: "year",
|
|
22
|
-
y: "year"
|
|
23
|
-
}, t = {
|
|
24
|
-
millisecond: 1,
|
|
25
|
-
second: 1e3,
|
|
26
|
-
minute: 60 * 1e3,
|
|
27
|
-
hour: 3600 * 1e3,
|
|
28
|
-
day: 1440 * 60 * 1e3
|
|
29
|
-
}, n = ["month", "year"];
|
|
30
|
-
function r(t) {
|
|
31
|
-
if (typeof t != "string" || t.trim() === "") throw TypeError("normalizeUnit: unit must be a non-empty string");
|
|
32
|
-
let n = e[t.toLowerCase().trim()];
|
|
33
|
-
if (!n) throw Error(`Unsupported unit: ${t}`);
|
|
34
|
-
return n;
|
|
35
|
-
}
|
|
36
|
-
function i(e) {
|
|
37
|
-
return r(e) in t;
|
|
38
|
-
}
|
|
39
|
-
function a(e) {
|
|
40
|
-
return n.includes(r(e));
|
|
41
|
-
}
|
|
42
|
-
function o(e) {
|
|
43
|
-
let n = r(e);
|
|
44
|
-
if (!(n in t)) throw Error(`Unit cannot be converted to milliseconds exactly: ${e}`);
|
|
45
|
-
return t[n];
|
|
46
|
-
}
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region src/utils/calendar.js
|
|
49
|
-
function s(e, t) {
|
|
50
|
-
if (typeof e != "number" || Number.isNaN(e)) throw TypeError(`${t}: timestamp must be a valid number`);
|
|
51
|
-
}
|
|
52
|
-
function c(e, t) {
|
|
53
|
-
if (typeof e != "number" || Number.isNaN(e)) throw TypeError(`${t}: amount must be a valid number`);
|
|
54
|
-
}
|
|
55
|
-
function l(e, t) {
|
|
56
|
-
if (!(e instanceof B)) throw TypeError(`${t} must be OzTime`);
|
|
57
|
-
}
|
|
58
|
-
function u(e, t) {
|
|
59
|
-
let n = new Date(e), r = new Date(t), i = (n.getUTCFullYear() - r.getUTCFullYear()) * 12 + (n.getUTCMonth() - r.getUTCMonth()), a = n.getUTCDate(), o = r.getUTCDate();
|
|
60
|
-
return i > 0 && a < o ? --i : i < 0 && a > o && (i += 1), i;
|
|
61
|
-
}
|
|
62
|
-
function d(e, t) {
|
|
63
|
-
let n = new Date(e), r = new Date(t), i = n.getUTCFullYear() - r.getUTCFullYear(), a = n.getUTCMonth(), o = r.getUTCMonth(), s = n.getUTCDate(), c = r.getUTCDate();
|
|
64
|
-
return i > 0 && (a < o || a === o && s < c) ? --i : i < 0 && (a > o || a === o && s > c) && (i += 1), i;
|
|
65
|
-
}
|
|
66
|
-
function f(e) {
|
|
67
|
-
if (!Number.isInteger(e)) throw TypeError("isLeapYear: year must be an integer");
|
|
68
|
-
return e % 4 == 0 && (e % 100 != 0 || e % 400 == 0);
|
|
69
|
-
}
|
|
70
|
-
function p(e, t) {
|
|
71
|
-
if (!Number.isInteger(e) || !Number.isInteger(t)) throw TypeError("daysInMonth: year and month must be integers");
|
|
72
|
-
if (t < 1 || t > 12) throw RangeError("daysInMonth: month must be between 1 and 12");
|
|
73
|
-
return new Date(Date.UTC(e, t, 0)).getUTCDate();
|
|
74
|
-
}
|
|
75
|
-
function m(e, t, n) {
|
|
76
|
-
s(e, "addByFixedUnit"), c(t, "addByFixedUnit");
|
|
77
|
-
let a = r(n);
|
|
78
|
-
if (!i(a)) throw Error(`addByFixedUnit does not support calendar unit: ${n}`);
|
|
79
|
-
return e + t * o(a);
|
|
80
|
-
}
|
|
81
|
-
function h(e, t, n) {
|
|
82
|
-
s(e, "addByCalendarUnit"), c(t, "addByCalendarUnit");
|
|
83
|
-
let i = r(n), a = new Date(e);
|
|
84
|
-
if (i === "month") {
|
|
85
|
-
let e = a.getUTCDate();
|
|
86
|
-
a.setUTCDate(1), a.setUTCMonth(a.getUTCMonth() + t);
|
|
87
|
-
let n = p(a.getUTCFullYear(), a.getUTCMonth() + 1);
|
|
88
|
-
return a.setUTCDate(Math.min(e, n)), a.getTime();
|
|
89
|
-
}
|
|
90
|
-
if (i === "year") {
|
|
91
|
-
let e = a.getUTCMonth(), n = a.getUTCDate();
|
|
92
|
-
a.setUTCDate(1), a.setUTCFullYear(a.getUTCFullYear() + t), a.setUTCMonth(e);
|
|
93
|
-
let r = p(a.getUTCFullYear(), e + 1);
|
|
94
|
-
return a.setUTCDate(Math.min(n, r)), a.getTime();
|
|
95
|
-
}
|
|
96
|
-
throw Error(`addByCalendarUnit supports only month and year: ${n}`);
|
|
97
|
-
}
|
|
98
|
-
function g(e, t, n = "millisecond") {
|
|
99
|
-
l(e, "left"), l(t, "right");
|
|
100
|
-
let a = r(n), s = e.getTimestamp(), c = t.getTimestamp();
|
|
101
|
-
if (i(a)) return (s - c) / o(a);
|
|
102
|
-
if (a === "month") return u(s, c);
|
|
103
|
-
if (a === "year") return d(s, c);
|
|
104
|
-
throw Error(`Unsupported unit: ${n}`);
|
|
105
|
-
}
|
|
106
|
-
//#endregion
|
|
107
|
-
//#region src/modules/arithmetic.js
|
|
108
|
-
function _(e, t) {
|
|
109
|
-
if (!(e instanceof B)) throw TypeError(`${t} must be OzTime`);
|
|
110
|
-
}
|
|
111
|
-
function v(e) {
|
|
112
|
-
if (typeof e != "number" || Number.isNaN(e)) throw TypeError("amount must be a valid number");
|
|
113
|
-
}
|
|
114
|
-
function y(e, t, n) {
|
|
115
|
-
_(e, "time"), v(t);
|
|
116
|
-
let o = r(n), s = e.getTimestamp(), c;
|
|
117
|
-
return i(o) ? c = m(s, t, o) : a(o) && (c = h(s, t, o)), new B(c, e.getTimezone(), e.getLocale());
|
|
118
|
-
}
|
|
119
|
-
function b(e, t, n) {
|
|
120
|
-
return _(e, "time"), v(t), y(e, -t, n);
|
|
121
|
-
}
|
|
122
|
-
//#endregion
|
|
123
|
-
//#region src/modules/format.js
|
|
124
|
-
function x(e) {
|
|
125
|
-
if (!(e instanceof B)) throw TypeError("format: first argument must be OzTime");
|
|
126
|
-
}
|
|
127
|
-
function S(e, t = 2) {
|
|
128
|
-
return String(e).padStart(t, "0");
|
|
129
|
-
}
|
|
130
|
-
function C(e, t) {
|
|
131
|
-
let n = new Intl.DateTimeFormat(t, {
|
|
132
|
-
timeZone: e.getTimezone(),
|
|
133
|
-
year: "numeric",
|
|
134
|
-
month: "numeric",
|
|
135
|
-
day: "numeric",
|
|
136
|
-
hour: "numeric",
|
|
137
|
-
minute: "2-digit",
|
|
138
|
-
second: "2-digit",
|
|
139
|
-
hour12: !1
|
|
140
|
-
}).formatToParts(new Date(e.getTimestamp()));
|
|
141
|
-
return Object.fromEntries(n.map((e) => [e.type, e.value]));
|
|
142
|
-
}
|
|
143
|
-
function w(e, t, n) {
|
|
144
|
-
return new Intl.DateTimeFormat(t, {
|
|
145
|
-
timeZone: e.getTimezone(),
|
|
146
|
-
month: n
|
|
147
|
-
}).format(new Date(e.getTimestamp()));
|
|
148
|
-
}
|
|
149
|
-
function T(e, t, n) {
|
|
150
|
-
return new Intl.DateTimeFormat(t, {
|
|
151
|
-
timeZone: e.getTimezone(),
|
|
152
|
-
weekday: n
|
|
153
|
-
}).format(new Date(e.getTimestamp()));
|
|
154
|
-
}
|
|
155
|
-
function E(e, t, n) {
|
|
156
|
-
if (x(e), typeof t != "string" || t.trim() === "") throw TypeError("format: template must be a non-empty string");
|
|
157
|
-
let r = n ?? e.getLocale(), i = C(e, r), a = Number(i.year), o = Number(i.month), s = Number(i.day), c = Number(i.hour), l = Number(i.minute), u = Number(i.second), d = new Date(e.getTimestamp()).getUTCMilliseconds(), f = c % 12, p = f === 0 ? 12 : f, m = c >= 12 ? "PM" : "AM", h = {
|
|
158
|
-
YYYY: String(a),
|
|
159
|
-
YY: String(a).slice(-2),
|
|
160
|
-
MMMM: w(e, r, "long"),
|
|
161
|
-
MMM: w(e, r, "short"),
|
|
162
|
-
MM: S(o),
|
|
163
|
-
M: String(o),
|
|
164
|
-
dddd: T(e, r, "long"),
|
|
165
|
-
ddd: T(e, r, "short"),
|
|
166
|
-
DD: S(s),
|
|
167
|
-
D: String(s),
|
|
168
|
-
HH: S(c),
|
|
169
|
-
H: String(c),
|
|
170
|
-
hh: S(p),
|
|
171
|
-
h: String(p),
|
|
172
|
-
mm: S(l),
|
|
173
|
-
ss: S(u),
|
|
174
|
-
SSS: S(d, 3),
|
|
175
|
-
A: m
|
|
176
|
-
};
|
|
177
|
-
return t.replace(/YYYY|MMMM|MMM|MM|M|dddd|ddd|DD|D|HH|H|hh|h|mm|ss|SSS|YY|A/g, (e) => h[e] ?? e);
|
|
178
|
-
}
|
|
179
|
-
//#endregion
|
|
180
|
-
//#region src/modules/compare.js
|
|
181
|
-
function D(e, t) {
|
|
182
|
-
if (!(e instanceof B)) throw TypeError(`${t} must be OzTime`);
|
|
183
|
-
}
|
|
184
|
-
function O(e, t) {
|
|
185
|
-
let n = r(t), i = new Date(e);
|
|
186
|
-
switch (n) {
|
|
187
|
-
case "millisecond": return i.getTime();
|
|
188
|
-
case "second":
|
|
189
|
-
i.setUTCMilliseconds(0);
|
|
190
|
-
break;
|
|
191
|
-
case "minute":
|
|
192
|
-
i.setUTCSeconds(0, 0);
|
|
193
|
-
break;
|
|
194
|
-
case "hour":
|
|
195
|
-
i.setUTCMinutes(0, 0, 0);
|
|
196
|
-
break;
|
|
197
|
-
case "day":
|
|
198
|
-
i.setUTCHours(0, 0, 0, 0);
|
|
199
|
-
break;
|
|
200
|
-
case "month":
|
|
201
|
-
i.setUTCHours(0, 0, 0, 0), i.setUTCDate(1);
|
|
202
|
-
break;
|
|
203
|
-
case "year":
|
|
204
|
-
i.setUTCHours(0, 0, 0, 0), i.setUTCDate(1), i.setUTCMonth(0);
|
|
205
|
-
break;
|
|
206
|
-
default: throw Error(`Unsupported unit for truncateToUnit: ${t}`);
|
|
207
|
-
}
|
|
208
|
-
return i.getTime();
|
|
209
|
-
}
|
|
210
|
-
function k(e, t, n = "millisecond") {
|
|
211
|
-
return D(e, "a"), D(t, "b"), O(e.getTimestamp(), n) === O(t.getTimestamp(), n);
|
|
212
|
-
}
|
|
213
|
-
function A(e, t, n = "millisecond") {
|
|
214
|
-
return D(e, "a"), D(t, "b"), O(e.getTimestamp(), n) < O(t.getTimestamp(), n);
|
|
215
|
-
}
|
|
216
|
-
function j(e, t, n = "millisecond") {
|
|
217
|
-
return D(e, "a"), D(t, "b"), O(e.getTimestamp(), n) > O(t.getTimestamp(), n);
|
|
218
|
-
}
|
|
219
|
-
function M(e, t, n, r = "millisecond", i = "[]") {
|
|
220
|
-
D(e, "target"), D(t, "left"), D(n, "right");
|
|
221
|
-
let a = O(e.getTimestamp(), r), o = O(t.getTimestamp(), r), s = O(n.getTimestamp(), r), c = Math.min(o, s), l = Math.max(o, s);
|
|
222
|
-
switch (i) {
|
|
223
|
-
case "[]": return a >= c && a <= l;
|
|
224
|
-
case "[)": return a >= c && a < l;
|
|
225
|
-
case "(]": return a > c && a <= l;
|
|
226
|
-
case "()": return a > c && a < l;
|
|
227
|
-
default: throw Error(`Invalid inclusivity value: ${i}`);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
//#endregion
|
|
231
|
-
//#region src/modules/timezone.js
|
|
232
|
-
function N(e) {
|
|
233
|
-
if (typeof e != "string" || e.trim() === "") throw TypeError("setTimezone: timezone must be a non-empty string");
|
|
234
|
-
if (!Intl.supportedValuesOf("timeZone").includes(e)) throw Error(`Unsupported timezone: ${e}`);
|
|
235
|
-
}
|
|
236
|
-
function P(e, t) {
|
|
237
|
-
let n = new Date(e), r = new Intl.DateTimeFormat("en-US", {
|
|
238
|
-
timeZone: t,
|
|
239
|
-
year: "numeric",
|
|
240
|
-
month: "2-digit",
|
|
241
|
-
day: "2-digit",
|
|
242
|
-
hour: "2-digit",
|
|
243
|
-
minute: "2-digit",
|
|
244
|
-
second: "2-digit",
|
|
245
|
-
hour12: !1
|
|
246
|
-
}).formatToParts(n), i = Object.fromEntries(r.map((e) => [e.type, e.value])), a = Number(i.year), o = Number(i.month), s = Number(i.day), c = Number(i.hour), l = Number(i.minute), u = Number(i.second);
|
|
247
|
-
return (Date.UTC(a, o - 1, s, c, l, u) - e) / 6e4;
|
|
248
|
-
}
|
|
249
|
-
function F(e, t) {
|
|
250
|
-
if (!(e instanceof B)) throw TypeError("tz: first argument must be OzTime");
|
|
251
|
-
return N(t), new B(e.getTimestamp(), t, e.getLocale());
|
|
252
|
-
}
|
|
253
|
-
function I(e) {
|
|
254
|
-
if (!(e instanceof B)) throw TypeError("getTimezoneOffset: argument must be OzTime");
|
|
255
|
-
return P(e.getTimestamp(), e.getTimezone());
|
|
256
|
-
}
|
|
257
|
-
//#endregion
|
|
258
|
-
//#region src/core/core.js
|
|
259
|
-
function L(e) {
|
|
260
|
-
if (typeof e != "number" || Number.isNaN(e)) throw TypeError("OzTime: timestamp must be a valid number");
|
|
261
|
-
}
|
|
262
|
-
function R(e) {
|
|
263
|
-
if (typeof e != "string" || e.trim() === "") throw TypeError("OzTime: timezone must be a non-empty string");
|
|
264
|
-
}
|
|
265
|
-
function z(e) {
|
|
266
|
-
if (typeof e != "string" || e.trim() === "") throw TypeError("OzTime: locale must be a non-empty string");
|
|
267
|
-
}
|
|
268
|
-
var B = class {
|
|
269
|
-
constructor(e, t = "UTC", n = "en-US") {
|
|
270
|
-
L(e), R(t), z(n), this._timestamp = e, this._timezone = t, this._locale = n;
|
|
271
|
-
}
|
|
272
|
-
getTimestamp() {
|
|
273
|
-
return this._timestamp;
|
|
274
|
-
}
|
|
275
|
-
getTimezone() {
|
|
276
|
-
return this._timezone;
|
|
277
|
-
}
|
|
278
|
-
getLocale() {
|
|
279
|
-
return this._locale;
|
|
280
|
-
}
|
|
281
|
-
toTimestamp() {
|
|
282
|
-
return this._timestamp;
|
|
283
|
-
}
|
|
284
|
-
toISOString() {
|
|
285
|
-
return new Date(this._timestamp).toISOString();
|
|
286
|
-
}
|
|
287
|
-
add(e, t) {
|
|
288
|
-
return y(this, e, t);
|
|
289
|
-
}
|
|
290
|
-
subtract(e, t) {
|
|
291
|
-
return b(this, e, t);
|
|
292
|
-
}
|
|
293
|
-
format(e, t) {
|
|
294
|
-
return E(this, e, t);
|
|
295
|
-
}
|
|
296
|
-
isSame(e, t = "millisecond") {
|
|
297
|
-
return k(this, e, t);
|
|
298
|
-
}
|
|
299
|
-
isBefore(e, t = "millisecond") {
|
|
300
|
-
return A(this, e, t);
|
|
301
|
-
}
|
|
302
|
-
isAfter(e, t = "millisecond") {
|
|
303
|
-
return j(this, e, t);
|
|
304
|
-
}
|
|
305
|
-
isBetween(e, t, n = "millisecond", r = "[]") {
|
|
306
|
-
return M(this, e, t, n, r);
|
|
307
|
-
}
|
|
308
|
-
setTimezone(e) {
|
|
309
|
-
return F(this, e);
|
|
310
|
-
}
|
|
311
|
-
getTimezoneOffset() {
|
|
312
|
-
return I(this);
|
|
313
|
-
}
|
|
314
|
-
diff(e, t = "millisecond") {
|
|
315
|
-
return g(this, e, t);
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
//#endregion
|
|
319
|
-
//#region src/core/factory.js
|
|
320
|
-
function V(e) {
|
|
321
|
-
if (typeof e != "number" || Number.isNaN(e)) throw TypeError("fromTimestamp: timestamp must be a valid number");
|
|
322
|
-
}
|
|
323
|
-
function H(e) {
|
|
324
|
-
if (!(e instanceof Date) || Number.isNaN(e.getTime())) throw TypeError("fromDate: date must be a valid Date");
|
|
325
|
-
}
|
|
326
|
-
function U(e, t) {
|
|
327
|
-
if (!Number.isInteger(e)) throw TypeError(`${t} must be an integer`);
|
|
328
|
-
}
|
|
329
|
-
function W(e = "UTC", t = "en-US") {
|
|
330
|
-
return new B(Date.now(), e, t);
|
|
331
|
-
}
|
|
332
|
-
function G(e, t = "UTC", n = "en-US") {
|
|
333
|
-
return V(e), new B(e, t, n);
|
|
334
|
-
}
|
|
335
|
-
function K(e, t = "UTC", n = "en-US") {
|
|
336
|
-
return H(e), new B(e.getTime(), t, n);
|
|
337
|
-
}
|
|
338
|
-
function q(e, t = "UTC", n = "en-US") {
|
|
339
|
-
if (typeof e != "string" || e.trim() === "") throw TypeError("fromISO: isoString must be a non-empty string");
|
|
340
|
-
let r = Date.parse(e);
|
|
341
|
-
if (Number.isNaN(r)) throw Error(`Invalid ISO date string: ${e}`);
|
|
342
|
-
return new B(r, t, n);
|
|
343
|
-
}
|
|
344
|
-
function J(e, t, n, r = 0, i = 0, a = 0, o = 0, s = "UTC", c = "en-US") {
|
|
345
|
-
if (U(e, "year"), U(t, "month"), U(n, "day"), U(r, "hour"), U(i, "minute"), U(a, "second"), U(o, "millisecond"), t < 1 || t > 12) throw RangeError("month must be between 1 and 12");
|
|
346
|
-
if (r < 0 || r > 23) throw RangeError("hour must be between 0 and 23");
|
|
347
|
-
if (i < 0 || i > 59) throw RangeError("minute must be between 0 and 59");
|
|
348
|
-
if (a < 0 || a > 59) throw RangeError("second must be between 0 and 59");
|
|
349
|
-
if (o < 0 || o > 999) throw RangeError("millisecond must be between 0 and 999");
|
|
350
|
-
let l = p(e, t);
|
|
351
|
-
if (n < 1 || n > l) throw RangeError(`day must be between 1 and ${l} for ${e}-${t}`);
|
|
352
|
-
return new B(Date.UTC(e, t - 1, n, r, i, a, o), s, c);
|
|
353
|
-
}
|
|
354
|
-
//#endregion
|
|
355
|
-
//#region src/modules/interval.js
|
|
356
|
-
function Y(e, t) {
|
|
357
|
-
if (!(e instanceof B)) throw TypeError(`${t} must be OzTime`);
|
|
358
|
-
}
|
|
359
|
-
var X = class e {
|
|
360
|
-
constructor(e, t) {
|
|
361
|
-
if (Y(e, "start"), Y(t, "end"), e.getTimestamp() > t.getTimestamp()) throw RangeError("Interval: start must be before or equal to end");
|
|
362
|
-
this._start = e, this._end = t;
|
|
363
|
-
}
|
|
364
|
-
getStart() {
|
|
365
|
-
return this._start;
|
|
366
|
-
}
|
|
367
|
-
getEnd() {
|
|
368
|
-
return this._end;
|
|
369
|
-
}
|
|
370
|
-
contains(e) {
|
|
371
|
-
Y(e, "moment");
|
|
372
|
-
let t = e.getTimestamp();
|
|
373
|
-
return t >= this._start.getTimestamp() && t <= this._end.getTimestamp();
|
|
374
|
-
}
|
|
375
|
-
overlaps(t) {
|
|
376
|
-
if (!(t instanceof e)) throw TypeError("other must be Interval");
|
|
377
|
-
let n = this._start.getTimestamp(), r = this._end.getTimestamp(), i = t.getStart().getTimestamp();
|
|
378
|
-
return n <= t.getEnd().getTimestamp() && i <= r;
|
|
379
|
-
}
|
|
380
|
-
duration(e = "millisecond") {
|
|
381
|
-
let t = r(e);
|
|
382
|
-
if (!i(t)) throw Error(`Interval.duration supports only fixed units: ${e}`);
|
|
383
|
-
return (this._end.getTimestamp() - this._start.getTimestamp()) / o(t);
|
|
384
|
-
}
|
|
385
|
-
};
|
|
386
|
-
function Z(e, t) {
|
|
387
|
-
return new X(e, t);
|
|
388
|
-
}
|
|
389
|
-
//#endregion
|
|
390
|
-
//#region src/modules/duration.js
|
|
391
|
-
function Q(e) {
|
|
392
|
-
if (typeof e != "number" || Number.isNaN(e)) throw TypeError("amount must be a valid number");
|
|
393
|
-
}
|
|
394
|
-
var $ = class e {
|
|
395
|
-
constructor(e) {
|
|
396
|
-
if (typeof e != "number" || Number.isNaN(e)) throw TypeError("Duration: milliseconds must be a valid number");
|
|
397
|
-
this._milliseconds = e;
|
|
398
|
-
}
|
|
399
|
-
asMilliseconds() {
|
|
400
|
-
return this._milliseconds;
|
|
401
|
-
}
|
|
402
|
-
asSeconds() {
|
|
403
|
-
return this._milliseconds / o("second");
|
|
404
|
-
}
|
|
405
|
-
asMinutes() {
|
|
406
|
-
return this._milliseconds / o("minute");
|
|
407
|
-
}
|
|
408
|
-
asHours() {
|
|
409
|
-
return this._milliseconds / o("hour");
|
|
410
|
-
}
|
|
411
|
-
asDays() {
|
|
412
|
-
return this._milliseconds / o("day");
|
|
413
|
-
}
|
|
414
|
-
add(t) {
|
|
415
|
-
if (!(t instanceof e)) throw TypeError("Duration.add: other must be Duration");
|
|
416
|
-
return new e(this._milliseconds + t._milliseconds);
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
function ee(e, t) {
|
|
420
|
-
Q(e);
|
|
421
|
-
let n = r(t);
|
|
422
|
-
if (!i(n)) throw Error(`duration supports only fixed units: ${t}`);
|
|
423
|
-
return new $(e * o(n));
|
|
424
|
-
}
|
|
425
|
-
//#endregion
|
|
426
|
-
export { $ as Duration, X as Interval, B as OzTime, y as add, p as daysInMonth, ee as duration, E as format, J as fromComponents, K as fromDate, q as fromISO, G as fromTimestamp, I as getTimezoneOffset, Z as interval, j as isAfter, A as isBefore, M as isBetween, f as isLeapYear, k as isSame, W as now, F as setTimezone, b as subtract };
|