@bereasoftware/time-guard 2.7.2 → 2.9.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.
- package/README.en.md +5 -1
- package/README.md +5 -1
- package/dist/adapters/temporal.adapter.d.ts +6 -2
- package/dist/angular/index.cjs +1 -148
- package/dist/angular/index.es.js +63 -112
- package/dist/calendars/index.cjs +1 -357
- package/dist/calendars/index.es.js +66 -99
- package/dist/chunk-C6qsp3rV.cjs +1 -0
- package/dist/core-BDQnStek.cjs +1 -0
- package/dist/core-GHK7isoa.js +1200 -0
- package/dist/core.d.ts +27 -1
- package/dist/index.d.ts +4 -2
- package/dist/locales/index.cjs +1 -7
- package/dist/locales/index.d.ts +11 -0
- package/dist/locales/index.es.js +3 -3
- package/dist/{locales-CGdn0DJN.cjs → locales-Dwe5oGoy.js} +201 -82
- package/dist/locales-bc-5kDuZ.cjs +1 -0
- package/dist/native/index.cjs +1 -48
- package/dist/native/index.es.js +8 -18
- package/dist/plugins/advanced-format/index.d.ts +8 -0
- package/dist/plugins/advanced-format.cjs +1 -105
- package/dist/plugins/advanced-format.es.js +48 -63
- package/dist/plugins/duration/index.d.ts +6 -0
- package/dist/plugins/duration.cjs +1 -258
- package/dist/plugins/duration.es.js +63 -165
- package/dist/plugins/manager.d.ts +6 -2
- package/dist/plugins/relative-time/index.d.ts +6 -0
- package/dist/plugins/relative-time.cjs +1 -166
- package/dist/plugins/relative-time.es.js +39 -74
- package/dist/qwik/index.cjs +1 -104
- package/dist/qwik/index.es.js +36 -94
- package/dist/qwik.d.ts +5 -1
- package/dist/react/index.cjs +1 -112
- package/dist/react/index.es.js +57 -91
- package/dist/solid/index.cjs +1 -127
- package/dist/solid/index.es.js +36 -116
- package/dist/solid.d.ts +5 -2
- package/dist/svelte/index.cjs +1 -398
- package/dist/svelte/index.es.js +85 -344
- package/dist/time-guard.cjs +3 -7359
- package/dist/time-guard.es.js +3412 -3625
- package/dist/time-guard.iife.js +3 -13549
- package/dist/time-guard.umd.js +3 -13550
- package/dist/types/index.d.ts +10 -0
- package/dist/vue/index.cjs +1 -143
- package/dist/vue/index.es.js +62 -116
- package/dist/vue.d.ts +5 -3
- package/package.json +3 -4
- package/dist/chunk-S8Y6Ng9i.cjs +0 -36
- package/dist/core-BIDjudpO.cjs +0 -1892
- package/dist/core-DWPXk3rz.js +0 -1809
- package/dist/locales-BUE_aq8K.js +0 -3514
|
@@ -1,105 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperties(exports, {
|
|
3
|
-
__esModule: { value: true },
|
|
4
|
-
[Symbol.toStringTag]: { value: "Module" }
|
|
5
|
-
});
|
|
6
|
-
//#region src/plugins/advanced-format/index.ts
|
|
7
|
-
var AdvancedFormatPlugin = class {
|
|
8
|
-
name = "advanced-format";
|
|
9
|
-
version = "1.0.0";
|
|
10
|
-
/**
|
|
11
|
-
* Install plugin into TimeGuard
|
|
12
|
-
*/
|
|
13
|
-
install(TimeGuardClass) {
|
|
14
|
-
const originalFormat = TimeGuardClass.prototype.format;
|
|
15
|
-
TimeGuardClass.prototype.format = function(pattern) {
|
|
16
|
-
if (!pattern || typeof pattern !== "string") return originalFormat.call(this, pattern);
|
|
17
|
-
if (!/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/.test(pattern)) return originalFormat.call(this, pattern);
|
|
18
|
-
const temporal = this.toTemporal();
|
|
19
|
-
const temporal_dt = "toPlainDateTime" in temporal ? temporal.toPlainDateTime() : temporal;
|
|
20
|
-
const ordinalFn = (n) => {
|
|
21
|
-
const suffixes = [
|
|
22
|
-
"th",
|
|
23
|
-
"st",
|
|
24
|
-
"nd",
|
|
25
|
-
"rd"
|
|
26
|
-
];
|
|
27
|
-
const v = n % 100;
|
|
28
|
-
return n + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]);
|
|
29
|
-
};
|
|
30
|
-
const padFn = (n, length) => String(n).padStart(length, "0");
|
|
31
|
-
const getISOWeek = (temporal) => {
|
|
32
|
-
const jan4 = new Date(temporal.year, 0, 4);
|
|
33
|
-
const week1Start = new Date(jan4);
|
|
34
|
-
week1Start.setDate(jan4.getDate() - jan4.getDay() + (jan4.getDay() === 0 ? -6 : 1));
|
|
35
|
-
const currentDate = new Date(temporal.year, temporal.month - 1, temporal.day);
|
|
36
|
-
const weekNum = Math.floor((currentDate.getTime() - week1Start.getTime()) / (10080 * 60 * 1e3)) + 1;
|
|
37
|
-
return Math.max(1, weekNum);
|
|
38
|
-
};
|
|
39
|
-
const getWeekOfYear = (temporal) => {
|
|
40
|
-
const weekNum = Math.ceil((temporal.day + new Date(temporal.year, temporal.month - 1, 1).getDay()) / 7);
|
|
41
|
-
return Math.max(1, weekNum);
|
|
42
|
-
};
|
|
43
|
-
const getISOWeekYear = (temporal) => {
|
|
44
|
-
const currentDate = new Date(temporal.year, temporal.month - 1, temporal.day);
|
|
45
|
-
const yearAdjust = currentDate.getTime() < new Date(temporal.year, 0, 1).getTime() ? -1 : currentDate.getTime() >= new Date(temporal.year + 1, 0, 1).getTime() ? 1 : 0;
|
|
46
|
-
return temporal.year + yearAdjust;
|
|
47
|
-
};
|
|
48
|
-
const getWeekYear = (temporal) => {
|
|
49
|
-
const yearAdjust = temporal.month === 1 && temporal.day < 4 ? -1 : temporal.month === 12 && temporal.day > 28 ? 1 : 0;
|
|
50
|
-
return temporal.year + yearAdjust;
|
|
51
|
-
};
|
|
52
|
-
const result = pattern.replace(/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/g, (match) => {
|
|
53
|
-
let replacement = "";
|
|
54
|
-
switch (match) {
|
|
55
|
-
case "Q":
|
|
56
|
-
replacement = String(Math.ceil(temporal_dt.month / 3));
|
|
57
|
-
break;
|
|
58
|
-
case "Do":
|
|
59
|
-
replacement = ordinalFn(temporal_dt.day);
|
|
60
|
-
break;
|
|
61
|
-
case "W":
|
|
62
|
-
case "WW":
|
|
63
|
-
replacement = padFn(getISOWeek(temporal_dt), match === "W" ? 1 : 2);
|
|
64
|
-
break;
|
|
65
|
-
case "w":
|
|
66
|
-
case "ww":
|
|
67
|
-
replacement = padFn(getWeekOfYear(temporal_dt), match === "w" ? 1 : 2);
|
|
68
|
-
break;
|
|
69
|
-
case "GGGG":
|
|
70
|
-
replacement = String(getISOWeekYear(temporal_dt));
|
|
71
|
-
break;
|
|
72
|
-
case "gggg":
|
|
73
|
-
replacement = String(getWeekYear(temporal_dt));
|
|
74
|
-
break;
|
|
75
|
-
case "k":
|
|
76
|
-
case "kk":
|
|
77
|
-
replacement = padFn(temporal_dt.hour === 0 ? 24 : temporal_dt.hour, match === "k" ? 1 : 2);
|
|
78
|
-
break;
|
|
79
|
-
case "X":
|
|
80
|
-
replacement = String(Math.floor(this.valueOf() / 1e3));
|
|
81
|
-
break;
|
|
82
|
-
case "x":
|
|
83
|
-
replacement = String(this.valueOf());
|
|
84
|
-
break;
|
|
85
|
-
case "z":
|
|
86
|
-
replacement = `${this.getTimezoneOffset()}`;
|
|
87
|
-
break;
|
|
88
|
-
case "zzz":
|
|
89
|
-
replacement = `${this.getTimezoneOffsetLong()}`;
|
|
90
|
-
break;
|
|
91
|
-
default: return match;
|
|
92
|
-
}
|
|
93
|
-
return `[${replacement}]`;
|
|
94
|
-
});
|
|
95
|
-
return originalFormat.call(this, result);
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
/**
|
|
100
|
-
* Create and export default instance
|
|
101
|
-
*/
|
|
102
|
-
var advanced_format_default = new AdvancedFormatPlugin();
|
|
103
|
-
//#endregion
|
|
104
|
-
exports.AdvancedFormatPlugin = AdvancedFormatPlugin;
|
|
105
|
-
exports.default = advanced_format_default;
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=class{name=`advanced-format`;version=`1.0.0`;originalFormats=new WeakMap;install(e){let t=e.prototype.format;this.originalFormats.set(e,t),e.prototype.format=function(e){if(!e||typeof e!=`string`||!/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz|z/.test(e))return t.call(this,e);let n=this.toTemporal(),r=`toPlainDateTime`in n?n.toPlainDateTime():n,i=e=>{let t=[`th`,`st`,`nd`,`rd`],n=e%100;return e+(t[(n-20)%10]||t[n]||t[0])},a=(e,t)=>String(e).padStart(t,`0`),o=e=>{let t=new Date(e.year,0,4),n=new Date(t);n.setDate(t.getDate()-t.getDay()+(t.getDay()===0?-6:1));let r=new Date(e.year,e.month-1,e.day),i=Math.floor((r.getTime()-n.getTime())/(10080*60*1e3))+1;return Math.max(1,i)},s=e=>{let t=Math.ceil((e.day+new Date(e.year,e.month-1,1).getDay())/7);return Math.max(1,t)},c=e=>{let t=new Date(e.year,e.month-1,e.day),n=t.getTime()<new Date(e.year,0,1).getTime()?-1:+(t.getTime()>=new Date(e.year+1,0,1).getTime());return e.year+n},l=e=>{let t=e.month===1&&e.day<4?-1:+(e.month===12&&e.day>28);return e.year+t},u=e.replace(/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz|z/g,e=>{let t=``;switch(e){case`Q`:t=String(Math.ceil(r.month/3));break;case`Do`:t=i(r.day);break;case`W`:case`WW`:t=a(o(r),e===`W`?1:2);break;case`w`:case`ww`:t=a(s(r),e===`w`?1:2);break;case`GGGG`:t=String(c(r));break;case`gggg`:t=String(l(r));break;case`k`:case`kk`:t=a(r.hour===0?24:r.hour,e===`k`?1:2);break;case`X`:t=String(Math.floor(this.valueOf()/1e3));break;case`x`:t=String(this.valueOf());break;case`z`:t=this.getOffset();break;case`zzz`:t=this.getTimeZoneId()??``;break;default:return e}return`[${t}]`});return t.call(this,u)}}uninstall(e){let t=this.originalFormats.get(e);t&&(e.prototype.format=t,this.originalFormats.delete(e))}},t=new e;exports.AdvancedFormatPlugin=e,exports.default=t;
|
|
@@ -1,100 +1,85 @@
|
|
|
1
|
-
/*! time-guard v2.
|
|
1
|
+
/*! time-guard v2.9.0 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/plugins/advanced-format/index.ts
|
|
3
|
-
var
|
|
3
|
+
var e = class {
|
|
4
4
|
name = "advanced-format";
|
|
5
5
|
version = "1.0.0";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/.test(pattern)) return originalFormat.call(this, pattern);
|
|
14
|
-
const temporal = this.toTemporal();
|
|
15
|
-
const temporal_dt = "toPlainDateTime" in temporal ? temporal.toPlainDateTime() : temporal;
|
|
16
|
-
const ordinalFn = (n) => {
|
|
17
|
-
const suffixes = [
|
|
6
|
+
originalFormats = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
install(e) {
|
|
8
|
+
let t = e.prototype.format;
|
|
9
|
+
this.originalFormats.set(e, t), e.prototype.format = function(e) {
|
|
10
|
+
if (!e || typeof e != "string" || !/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz|z/.test(e)) return t.call(this, e);
|
|
11
|
+
let n = this.toTemporal(), r = "toPlainDateTime" in n ? n.toPlainDateTime() : n, i = (e) => {
|
|
12
|
+
let t = [
|
|
18
13
|
"th",
|
|
19
14
|
"st",
|
|
20
15
|
"nd",
|
|
21
16
|
"rd"
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const yearAdjust = currentDate.getTime() < new Date(temporal.year, 0, 1).getTime() ? -1 : currentDate.getTime() >= new Date(temporal.year + 1, 0, 1).getTime() ? 1 : 0;
|
|
42
|
-
return temporal.year + yearAdjust;
|
|
43
|
-
};
|
|
44
|
-
const getWeekYear = (temporal) => {
|
|
45
|
-
const yearAdjust = temporal.month === 1 && temporal.day < 4 ? -1 : temporal.month === 12 && temporal.day > 28 ? 1 : 0;
|
|
46
|
-
return temporal.year + yearAdjust;
|
|
47
|
-
};
|
|
48
|
-
const result = pattern.replace(/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/g, (match) => {
|
|
49
|
-
let replacement = "";
|
|
50
|
-
switch (match) {
|
|
17
|
+
], n = e % 100;
|
|
18
|
+
return e + (t[(n - 20) % 10] || t[n] || t[0]);
|
|
19
|
+
}, a = (e, t) => String(e).padStart(t, "0"), o = (e) => {
|
|
20
|
+
let t = new Date(e.year, 0, 4), n = new Date(t);
|
|
21
|
+
n.setDate(t.getDate() - t.getDay() + (t.getDay() === 0 ? -6 : 1));
|
|
22
|
+
let r = new Date(e.year, e.month - 1, e.day), i = Math.floor((r.getTime() - n.getTime()) / (10080 * 60 * 1e3)) + 1;
|
|
23
|
+
return Math.max(1, i);
|
|
24
|
+
}, s = (e) => {
|
|
25
|
+
let t = Math.ceil((e.day + new Date(e.year, e.month - 1, 1).getDay()) / 7);
|
|
26
|
+
return Math.max(1, t);
|
|
27
|
+
}, c = (e) => {
|
|
28
|
+
let t = new Date(e.year, e.month - 1, e.day), n = t.getTime() < new Date(e.year, 0, 1).getTime() ? -1 : +(t.getTime() >= new Date(e.year + 1, 0, 1).getTime());
|
|
29
|
+
return e.year + n;
|
|
30
|
+
}, l = (e) => {
|
|
31
|
+
let t = e.month === 1 && e.day < 4 ? -1 : +(e.month === 12 && e.day > 28);
|
|
32
|
+
return e.year + t;
|
|
33
|
+
}, u = e.replace(/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz|z/g, (e) => {
|
|
34
|
+
let t = "";
|
|
35
|
+
switch (e) {
|
|
51
36
|
case "Q":
|
|
52
|
-
|
|
37
|
+
t = String(Math.ceil(r.month / 3));
|
|
53
38
|
break;
|
|
54
39
|
case "Do":
|
|
55
|
-
|
|
40
|
+
t = i(r.day);
|
|
56
41
|
break;
|
|
57
42
|
case "W":
|
|
58
43
|
case "WW":
|
|
59
|
-
|
|
44
|
+
t = a(o(r), e === "W" ? 1 : 2);
|
|
60
45
|
break;
|
|
61
46
|
case "w":
|
|
62
47
|
case "ww":
|
|
63
|
-
|
|
48
|
+
t = a(s(r), e === "w" ? 1 : 2);
|
|
64
49
|
break;
|
|
65
50
|
case "GGGG":
|
|
66
|
-
|
|
51
|
+
t = String(c(r));
|
|
67
52
|
break;
|
|
68
53
|
case "gggg":
|
|
69
|
-
|
|
54
|
+
t = String(l(r));
|
|
70
55
|
break;
|
|
71
56
|
case "k":
|
|
72
57
|
case "kk":
|
|
73
|
-
|
|
58
|
+
t = a(r.hour === 0 ? 24 : r.hour, e === "k" ? 1 : 2);
|
|
74
59
|
break;
|
|
75
60
|
case "X":
|
|
76
|
-
|
|
61
|
+
t = String(Math.floor(this.valueOf() / 1e3));
|
|
77
62
|
break;
|
|
78
63
|
case "x":
|
|
79
|
-
|
|
64
|
+
t = String(this.valueOf());
|
|
80
65
|
break;
|
|
81
66
|
case "z":
|
|
82
|
-
|
|
67
|
+
t = this.getOffset();
|
|
83
68
|
break;
|
|
84
69
|
case "zzz":
|
|
85
|
-
|
|
70
|
+
t = this.getTimeZoneId() ?? "";
|
|
86
71
|
break;
|
|
87
|
-
default: return
|
|
72
|
+
default: return e;
|
|
88
73
|
}
|
|
89
|
-
return `[${
|
|
74
|
+
return `[${t}]`;
|
|
90
75
|
});
|
|
91
|
-
return
|
|
76
|
+
return t.call(this, u);
|
|
92
77
|
};
|
|
93
78
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
79
|
+
uninstall(e) {
|
|
80
|
+
let t = this.originalFormats.get(e);
|
|
81
|
+
t && (e.prototype.format = t, this.originalFormats.delete(e));
|
|
82
|
+
}
|
|
83
|
+
}, t = new e();
|
|
99
84
|
//#endregion
|
|
100
|
-
export { AdvancedFormatPlugin,
|
|
85
|
+
export { e as AdvancedFormatPlugin, t as default };
|
|
@@ -99,6 +99,12 @@ export declare class DurationPlugin implements ITimeGuardPlugin {
|
|
|
99
99
|
* Install plugin into TimeGuard
|
|
100
100
|
*/
|
|
101
101
|
install(TimeGuardClass: typeof TimeGuard): void;
|
|
102
|
+
/**
|
|
103
|
+
* Reverse install() — none of these (instance `.duration()`, static
|
|
104
|
+
* `Duration`/`duration`) pre-exist on TimeGuard, so undoing this is
|
|
105
|
+
* just deleting them.
|
|
106
|
+
*/
|
|
107
|
+
uninstall(TimeGuardClass: typeof TimeGuard): void;
|
|
102
108
|
}
|
|
103
109
|
/**
|
|
104
110
|
* Create and export default instance
|
|
@@ -1,258 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperties(exports, {
|
|
3
|
-
__esModule: { value: true },
|
|
4
|
-
[Symbol.toStringTag]: { value: "Module" }
|
|
5
|
-
});
|
|
6
|
-
//#region src/plugins/duration/index.ts
|
|
7
|
-
/**
|
|
8
|
-
* Duration class - represents time span following ISO 8601 standard
|
|
9
|
-
*/
|
|
10
|
-
var Duration = class Duration {
|
|
11
|
-
years = 0;
|
|
12
|
-
months = 0;
|
|
13
|
-
weeks = 0;
|
|
14
|
-
days = 0;
|
|
15
|
-
hours = 0;
|
|
16
|
-
minutes = 0;
|
|
17
|
-
seconds = 0;
|
|
18
|
-
milliseconds = 0;
|
|
19
|
-
constructor(input) {
|
|
20
|
-
this.years = input.years || 0;
|
|
21
|
-
this.months = input.months || 0;
|
|
22
|
-
this.weeks = input.weeks || 0;
|
|
23
|
-
this.days = input.days || 0;
|
|
24
|
-
this.hours = input.hours || 0;
|
|
25
|
-
this.minutes = input.minutes || 0;
|
|
26
|
-
this.seconds = input.seconds || 0;
|
|
27
|
-
this.milliseconds = input.milliseconds || 0;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Create Duration from ISO 8601 string
|
|
31
|
-
* @example Duration.fromISO("P3Y6M4DT12H30M5S")
|
|
32
|
-
*/
|
|
33
|
-
static fromISO(iso) {
|
|
34
|
-
const match = iso.match(/^(-)?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:([\d.]+)S)?)?$/);
|
|
35
|
-
if (!match) throw new Error(`Invalid ISO 8601 duration: ${iso}`);
|
|
36
|
-
const [, negative, years, months, , days, hours, minutes, seconds] = match;
|
|
37
|
-
const multiplier = negative ? -1 : 1;
|
|
38
|
-
return new Duration({
|
|
39
|
-
years: parseInt(years || "0", 10) * multiplier,
|
|
40
|
-
months: parseInt(months || "0", 10) * multiplier,
|
|
41
|
-
days: parseInt(days || "0", 10) * multiplier,
|
|
42
|
-
hours: parseInt(hours || "0", 10) * multiplier,
|
|
43
|
-
minutes: parseInt(minutes || "0", 10) * multiplier,
|
|
44
|
-
seconds: parseFloat(seconds || "0") * multiplier
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Create a Duration between this date and another
|
|
49
|
-
*/
|
|
50
|
-
static between(from, to) {
|
|
51
|
-
const fromDT = from.toTemporal();
|
|
52
|
-
const duration = to.toTemporal().since(fromDT);
|
|
53
|
-
return new Duration({
|
|
54
|
-
years: duration.years || 0,
|
|
55
|
-
months: duration.months || 0,
|
|
56
|
-
weeks: 0,
|
|
57
|
-
days: duration.days || 0,
|
|
58
|
-
hours: duration.hours || 0,
|
|
59
|
-
minutes: duration.minutes || 0,
|
|
60
|
-
seconds: duration.seconds || 0,
|
|
61
|
-
milliseconds: duration.milliseconds || 0
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Create Duration from milliseconds
|
|
66
|
-
*/
|
|
67
|
-
static fromMilliseconds(ms) {
|
|
68
|
-
const isNegative = ms < 0;
|
|
69
|
-
const absMilhs = Math.abs(ms);
|
|
70
|
-
const years = Math.floor(absMilhs / (1e3 * 60 * 60 * 24 * 365));
|
|
71
|
-
const months = Math.floor(absMilhs % (1e3 * 60 * 60 * 24 * 365) / (1e3 * 60 * 60 * 24 * 30));
|
|
72
|
-
const days = Math.floor(absMilhs % (1e3 * 60 * 60 * 24 * 30) / (1e3 * 60 * 60 * 24));
|
|
73
|
-
const hours = Math.floor(absMilhs % (1e3 * 60 * 60 * 24) / (1e3 * 60 * 60));
|
|
74
|
-
const minutes = Math.floor(absMilhs % (1e3 * 60 * 60) / (1e3 * 60));
|
|
75
|
-
const seconds = Math.floor(absMilhs % (1e3 * 60) / 1e3);
|
|
76
|
-
const milliseconds = Math.floor(absMilhs % 1e3);
|
|
77
|
-
const multiplier = isNegative ? -1 : 1;
|
|
78
|
-
return new Duration({
|
|
79
|
-
years: years * multiplier,
|
|
80
|
-
months: months * multiplier,
|
|
81
|
-
days: days * multiplier,
|
|
82
|
-
hours: hours * multiplier,
|
|
83
|
-
minutes: minutes * multiplier,
|
|
84
|
-
seconds: seconds * multiplier,
|
|
85
|
-
milliseconds: milliseconds * multiplier
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Get duration in specific unit
|
|
90
|
-
*/
|
|
91
|
-
as(unit) {
|
|
92
|
-
return this.asMilliseconds() / {
|
|
93
|
-
milliseconds: 1,
|
|
94
|
-
seconds: 1e3,
|
|
95
|
-
minutes: 1e3 * 60,
|
|
96
|
-
hours: 1e3 * 60 * 60,
|
|
97
|
-
days: 1e3 * 60 * 60 * 24,
|
|
98
|
-
weeks: 1e3 * 60 * 60 * 24 * 7,
|
|
99
|
-
months: 1e3 * 60 * 60 * 24 * 30,
|
|
100
|
-
years: 1e3 * 60 * 60 * 24 * 365
|
|
101
|
-
}[unit];
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Get duration as milliseconds
|
|
105
|
-
*/
|
|
106
|
-
asMilliseconds() {
|
|
107
|
-
return this.years * 1e3 * 60 * 60 * 24 * 365 + this.months * 1e3 * 60 * 60 * 24 * 30 + this.weeks * 1e3 * 60 * 60 * 24 * 7 + this.days * 1e3 * 60 * 60 * 24 + this.hours * 1e3 * 60 * 60 + this.minutes * 1e3 * 60 + this.seconds * 1e3 + this.milliseconds;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Get duration as seconds
|
|
111
|
-
*/
|
|
112
|
-
asSeconds() {
|
|
113
|
-
return this.asMilliseconds() / 1e3;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Get duration as minutes
|
|
117
|
-
*/
|
|
118
|
-
asMinutes() {
|
|
119
|
-
return this.asSeconds() / 60;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Get duration as hours
|
|
123
|
-
*/
|
|
124
|
-
asHours() {
|
|
125
|
-
return this.asMinutes() / 60;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Get duration as days
|
|
129
|
-
*/
|
|
130
|
-
asDays() {
|
|
131
|
-
return this.asHours() / 24;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Get duration as weeks
|
|
135
|
-
*/
|
|
136
|
-
asWeeks() {
|
|
137
|
-
return this.asDays() / 7;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Get duration as months
|
|
141
|
-
*/
|
|
142
|
-
asMonths() {
|
|
143
|
-
return this.asDays() / 30;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Get duration as years
|
|
147
|
-
*/
|
|
148
|
-
asYears() {
|
|
149
|
-
return this.asDays() / 365;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Get all components
|
|
153
|
-
*/
|
|
154
|
-
toObject() {
|
|
155
|
-
return {
|
|
156
|
-
years: this.years,
|
|
157
|
-
months: this.months,
|
|
158
|
-
weeks: this.weeks,
|
|
159
|
-
days: this.days,
|
|
160
|
-
hours: this.hours,
|
|
161
|
-
minutes: this.minutes,
|
|
162
|
-
seconds: this.seconds,
|
|
163
|
-
milliseconds: this.milliseconds
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Get ISO 8601 string representation
|
|
168
|
-
* @example "P3Y6M4DT12H30M5S"
|
|
169
|
-
*/
|
|
170
|
-
toISO() {
|
|
171
|
-
let datePart = "";
|
|
172
|
-
if (this.years) datePart += `${this.years}Y`;
|
|
173
|
-
if (this.months) datePart += `${this.months}M`;
|
|
174
|
-
if (this.weeks || this.days) datePart += `${this.weeks * 7 + this.days}D`;
|
|
175
|
-
let timePart = "";
|
|
176
|
-
if (this.hours) timePart += `${this.hours}H`;
|
|
177
|
-
if (this.minutes) timePart += `${this.minutes}M`;
|
|
178
|
-
if (this.seconds || this.milliseconds) timePart += `${this.seconds + this.milliseconds / 1e3}S`;
|
|
179
|
-
return `${this.isNegative() ? "-" : ""}P${datePart}${timePart ? `T${timePart}` : ""}`;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Get human-readable string
|
|
183
|
-
*/
|
|
184
|
-
humanize() {
|
|
185
|
-
const parts = [];
|
|
186
|
-
if (this.years) parts.push(`${Math.abs(this.years)} year${Math.abs(this.years) !== 1 ? "s" : ""}`);
|
|
187
|
-
if (this.months) parts.push(`${Math.abs(this.months)} month${Math.abs(this.months) !== 1 ? "s" : ""}`);
|
|
188
|
-
if (this.weeks) parts.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks) !== 1 ? "s" : ""}`);
|
|
189
|
-
if (this.days) parts.push(`${Math.abs(this.days)} day${Math.abs(this.days) !== 1 ? "s" : ""}`);
|
|
190
|
-
if (this.hours) parts.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours) !== 1 ? "s" : ""}`);
|
|
191
|
-
if (this.minutes) parts.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes) !== 1 ? "s" : ""}`);
|
|
192
|
-
if (this.seconds) parts.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds) !== 1 ? "s" : ""}`);
|
|
193
|
-
if (this.milliseconds) parts.push(`${Math.abs(this.milliseconds)} ms`);
|
|
194
|
-
if (parts.length === 0) return "0 seconds";
|
|
195
|
-
const text = parts.join(", ");
|
|
196
|
-
return this.isNegative() ? `-${text}` : text;
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Check if duration is negative
|
|
200
|
-
*/
|
|
201
|
-
isNegative() {
|
|
202
|
-
return this.years < 0 || this.months < 0 || this.weeks < 0 || this.days < 0 || this.hours < 0 || this.minutes < 0 || this.seconds < 0 || this.milliseconds < 0;
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Get absolute duration
|
|
206
|
-
*/
|
|
207
|
-
abs() {
|
|
208
|
-
return new Duration({
|
|
209
|
-
years: Math.abs(this.years),
|
|
210
|
-
months: Math.abs(this.months),
|
|
211
|
-
weeks: Math.abs(this.weeks),
|
|
212
|
-
days: Math.abs(this.days),
|
|
213
|
-
hours: Math.abs(this.hours),
|
|
214
|
-
minutes: Math.abs(this.minutes),
|
|
215
|
-
seconds: Math.abs(this.seconds),
|
|
216
|
-
milliseconds: Math.abs(this.milliseconds)
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* String representation
|
|
221
|
-
*/
|
|
222
|
-
toString() {
|
|
223
|
-
return this.toISO();
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
/**
|
|
227
|
-
* Duration Plugin
|
|
228
|
-
*/
|
|
229
|
-
var DurationPlugin = class {
|
|
230
|
-
name = "duration";
|
|
231
|
-
version = "1.0.0";
|
|
232
|
-
/**
|
|
233
|
-
* Install plugin into TimeGuard
|
|
234
|
-
*/
|
|
235
|
-
install(TimeGuardClass) {
|
|
236
|
-
/**
|
|
237
|
-
* Create a Duration between this date and another
|
|
238
|
-
*/
|
|
239
|
-
TimeGuardClass.prototype.duration = function(other) {
|
|
240
|
-
return Duration.between(this, other);
|
|
241
|
-
};
|
|
242
|
-
TimeGuardClass.Duration = Duration;
|
|
243
|
-
TimeGuardClass.duration = {
|
|
244
|
-
fromISO: (iso) => Duration.fromISO(iso),
|
|
245
|
-
between: (from, to) => Duration.between(from, to),
|
|
246
|
-
fromMilliseconds: (ms) => Duration.fromMilliseconds(ms)
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
/**
|
|
251
|
-
* Create and export default instance
|
|
252
|
-
*/
|
|
253
|
-
var durationPlugin = new DurationPlugin();
|
|
254
|
-
//#endregion
|
|
255
|
-
exports.Duration = Duration;
|
|
256
|
-
exports.DurationPlugin = DurationPlugin;
|
|
257
|
-
exports.default = durationPlugin;
|
|
258
|
-
exports.durationPlugin = durationPlugin;
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=class e{years=0;months=0;weeks=0;days=0;hours=0;minutes=0;seconds=0;milliseconds=0;constructor(e){this.years=e.years||0,this.months=e.months||0,this.weeks=e.weeks||0,this.days=e.days||0,this.hours=e.hours||0,this.minutes=e.minutes||0,this.seconds=e.seconds||0,this.milliseconds=e.milliseconds||0}static fromISO(t){let n=t.match(/^(-)?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:([\d.]+)S)?)?$/);if(!n)throw Error(`Invalid ISO 8601 duration: ${t}`);let[,r,i,a,o,s,c,l,u]=n,d=r?-1:1;return new e({years:parseInt(i||`0`,10)*d,months:parseInt(a||`0`,10)*d,weeks:parseInt(o||`0`,10)*d,days:parseInt(s||`0`,10)*d,hours:parseInt(c||`0`,10)*d,minutes:parseInt(l||`0`,10)*d,seconds:parseFloat(u||`0`)*d})}static between(t,n){let r=t.toTemporal(),i=n.toTemporal().since(r);return new e({years:i.years||0,months:i.months||0,weeks:0,days:i.days||0,hours:i.hours||0,minutes:i.minutes||0,seconds:i.seconds||0,milliseconds:i.milliseconds||0})}static fromMilliseconds(t){let n=t<0,r=Math.abs(t),i=Math.floor(r/(1e3*60*60*24*365)),a=Math.floor(r%(1e3*60*60*24*365)/(1e3*60*60*24*30)),o=Math.floor(r%(1e3*60*60*24*30)/(1e3*60*60*24)),s=Math.floor(r%(1e3*60*60*24)/(1e3*60*60)),c=Math.floor(r%(1e3*60*60)/(1e3*60)),l=Math.floor(r%(1e3*60)/1e3),u=Math.floor(r%1e3),d=n?-1:1;return new e({years:i*d,months:a*d,days:o*d,hours:s*d,minutes:c*d,seconds:l*d,milliseconds:u*d})}as(e){return this.asMilliseconds()/{milliseconds:1,seconds:1e3,minutes:1e3*60,hours:1e3*60*60,days:1e3*60*60*24,weeks:1e3*60*60*24*7,months:1e3*60*60*24*30,years:1e3*60*60*24*365}[e]}asMilliseconds(){return this.years*1e3*60*60*24*365+this.months*1e3*60*60*24*30+this.weeks*1e3*60*60*24*7+this.days*1e3*60*60*24+this.hours*1e3*60*60+this.minutes*1e3*60+this.seconds*1e3+this.milliseconds}asSeconds(){return this.asMilliseconds()/1e3}asMinutes(){return this.asSeconds()/60}asHours(){return this.asMinutes()/60}asDays(){return this.asHours()/24}asWeeks(){return this.asDays()/7}asMonths(){return this.asDays()/30}asYears(){return this.asDays()/365}toObject(){return{years:this.years,months:this.months,weeks:this.weeks,days:this.days,hours:this.hours,minutes:this.minutes,seconds:this.seconds,milliseconds:this.milliseconds}}toISO(){let e=``;this.years&&(e+=`${this.years}Y`),this.months&&(e+=`${this.months}M`),(this.weeks||this.days)&&(e+=`${this.weeks*7+this.days}D`);let t=``;return this.hours&&(t+=`${this.hours}H`),this.minutes&&(t+=`${this.minutes}M`),(this.seconds||this.milliseconds)&&(t+=`${this.seconds+this.milliseconds/1e3}S`),`${this.isNegative()?`-`:``}P${e}${t?`T${t}`:``}`}humanize(){let e=[];if(this.years&&e.push(`${Math.abs(this.years)} year${Math.abs(this.years)===1?``:`s`}`),this.months&&e.push(`${Math.abs(this.months)} month${Math.abs(this.months)===1?``:`s`}`),this.weeks&&e.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks)===1?``:`s`}`),this.days&&e.push(`${Math.abs(this.days)} day${Math.abs(this.days)===1?``:`s`}`),this.hours&&e.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours)===1?``:`s`}`),this.minutes&&e.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes)===1?``:`s`}`),this.seconds&&e.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds)===1?``:`s`}`),this.milliseconds&&e.push(`${Math.abs(this.milliseconds)} ms`),e.length===0)return`0 seconds`;let t=e.join(`, `);return this.isNegative()?`-${t}`:t}isNegative(){return this.years<0||this.months<0||this.weeks<0||this.days<0||this.hours<0||this.minutes<0||this.seconds<0||this.milliseconds<0}abs(){return new e({years:Math.abs(this.years),months:Math.abs(this.months),weeks:Math.abs(this.weeks),days:Math.abs(this.days),hours:Math.abs(this.hours),minutes:Math.abs(this.minutes),seconds:Math.abs(this.seconds),milliseconds:Math.abs(this.milliseconds)})}toString(){return this.toISO()}},t=class{name=`duration`;version=`1.0.0`;install(t){t.prototype.duration=function(t){return e.between(this,t)},t.Duration=e,t.duration={fromISO:t=>e.fromISO(t),between:(t,n)=>e.between(t,n),fromMilliseconds:t=>e.fromMilliseconds(t)}}uninstall(e){delete e.prototype.duration,delete e.Duration,delete e.duration}},n=new t;exports.Duration=e,exports.DurationPlugin=t,exports.default=n,exports.durationPlugin=n;
|