@alexstukovnikov/oz-time 1.0.0 → 1.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.
@@ -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 };