neramjs 0.0.0-stage → 0.1.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/CHANGELOG.md +39 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/dist/calendar.d.ts +25 -0
- package/dist/calendar.js +59 -0
- package/dist/cjs/neram.cjs +3136 -0
- package/dist/duration.d.ts +69 -0
- package/dist/duration.js +271 -0
- package/dist/format.d.ts +13 -0
- package/dist/format.js +115 -0
- package/dist/index.cjs +9 -0
- package/dist/index.d.cts +35 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +2 -0
- package/dist/intl.d.ts +15 -0
- package/dist/intl.js +72 -0
- package/dist/locale.d.ts +53 -0
- package/dist/locale.js +107 -0
- package/dist/neram.d.ts +287 -0
- package/dist/neram.global.js +2 -0
- package/dist/neram.js +910 -0
- package/dist/neram.min.js +2 -0
- package/dist/neram.recur.global.js +3 -0
- package/dist/parse.d.ts +14 -0
- package/dist/parse.js +278 -0
- package/dist/recur/cron.d.ts +41 -0
- package/dist/recur/cron.js +275 -0
- package/dist/recur/index.cjs +2 -0
- package/dist/recur/index.d.cts +24 -0
- package/dist/recur/index.d.ts +6 -0
- package/dist/recur/index.js +3 -0
- package/dist/recur/rrule.d.ts +108 -0
- package/dist/recur/rrule.js +731 -0
- package/dist/recur/schedule.d.ts +85 -0
- package/dist/recur/schedule.js +169 -0
- package/dist/recur.min.js +3 -0
- package/dist/relative.d.ts +17 -0
- package/dist/relative.js +26 -0
- package/dist/units.d.ts +13 -0
- package/dist/units.js +44 -0
- package/dist/weekdata.d.ts +22 -0
- package/dist/weekdata.js +58 -0
- package/dist/zone.d.ts +48 -0
- package/dist/zone.js +247 -0
- package/package.json +89 -4
|
@@ -0,0 +1,3136 @@
|
|
|
1
|
+
/*! neramjs v0.1.0 | MIT | github.com/varksh05/neramjs */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// scripts/entries/cjs.ts
|
|
22
|
+
var cjs_exports = {};
|
|
23
|
+
__export(cjs_exports, {
|
|
24
|
+
Duration: () => Duration,
|
|
25
|
+
Neram: () => Neram,
|
|
26
|
+
duration: () => duration,
|
|
27
|
+
isDuration: () => isDuration,
|
|
28
|
+
neram: () => neram,
|
|
29
|
+
recur: () => recur_exports
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(cjs_exports);
|
|
32
|
+
|
|
33
|
+
// src/zone.ts
|
|
34
|
+
function utcFromParts(y, M, D, h = 0, m = 0, s = 0, ms = 0) {
|
|
35
|
+
if (y >= 100) return Date.UTC(y, M, D, h, m, s, ms);
|
|
36
|
+
const d = /* @__PURE__ */ new Date(0);
|
|
37
|
+
d.setUTCFullYear(y, M, D);
|
|
38
|
+
d.setUTCHours(h, m, s, ms);
|
|
39
|
+
return d.getTime();
|
|
40
|
+
}
|
|
41
|
+
function wallToUtcMs(w) {
|
|
42
|
+
return utcFromParts(w.y, w.M, w.D, w.h, w.m, w.s, w.ms);
|
|
43
|
+
}
|
|
44
|
+
var FIXED_RE = /^([+-])(\d{2}):?(\d{2})$/;
|
|
45
|
+
function parseOffset(str) {
|
|
46
|
+
if (str === "Z" || str === "z") return 0;
|
|
47
|
+
const m = FIXED_RE.exec(str) ?? /^([+-])(\d{2})$/.exec(str);
|
|
48
|
+
if (!m) return void 0;
|
|
49
|
+
const h = Number(m[2]);
|
|
50
|
+
const min = Number(m[3] ?? 0);
|
|
51
|
+
if (h > 23 || min > 59) return void 0;
|
|
52
|
+
const mins = h * 60 + min;
|
|
53
|
+
return m[1] === "-" ? -mins : mins;
|
|
54
|
+
}
|
|
55
|
+
function formatOffset(mins, sep = ":") {
|
|
56
|
+
const sign = mins < 0 ? "-" : "+";
|
|
57
|
+
const abs = Math.abs(mins);
|
|
58
|
+
return `${sign}${String(Math.floor(abs / 60)).padStart(2, "0")}${sep}${String(abs % 60).padStart(2, "0")}`;
|
|
59
|
+
}
|
|
60
|
+
function isFixedZone(zone) {
|
|
61
|
+
return FIXED_RE.test(zone);
|
|
62
|
+
}
|
|
63
|
+
var MODERN = {
|
|
64
|
+
"Asia/Calcutta": "Asia/Kolkata",
|
|
65
|
+
"Asia/Saigon": "Asia/Ho_Chi_Minh",
|
|
66
|
+
"Asia/Katmandu": "Asia/Kathmandu",
|
|
67
|
+
"Asia/Rangoon": "Asia/Yangon",
|
|
68
|
+
"Europe/Kiev": "Europe/Kyiv",
|
|
69
|
+
"Atlantic/Faeroe": "Atlantic/Faroe",
|
|
70
|
+
"America/Godthab": "America/Nuuk",
|
|
71
|
+
"Pacific/Enderbury": "Pacific/Kanton",
|
|
72
|
+
"Pacific/Truk": "Pacific/Chuuk",
|
|
73
|
+
"Pacific/Ponape": "Pacific/Pohnpei",
|
|
74
|
+
"Africa/Asmera": "Africa/Asmara",
|
|
75
|
+
"America/Buenos_Aires": "America/Argentina/Buenos_Aires",
|
|
76
|
+
"America/Catamarca": "America/Argentina/Catamarca",
|
|
77
|
+
"America/Cordoba": "America/Argentina/Cordoba",
|
|
78
|
+
"America/Jujuy": "America/Argentina/Jujuy",
|
|
79
|
+
"America/Mendoza": "America/Argentina/Mendoza",
|
|
80
|
+
"America/Indianapolis": "America/Indiana/Indianapolis",
|
|
81
|
+
"America/Louisville": "America/Kentucky/Louisville"
|
|
82
|
+
};
|
|
83
|
+
function zoneExists(zone) {
|
|
84
|
+
try {
|
|
85
|
+
new Intl.DateTimeFormat("en-US", { timeZone: zone });
|
|
86
|
+
return true;
|
|
87
|
+
} catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function modernZoneName(zone) {
|
|
92
|
+
const m = MODERN[zone];
|
|
93
|
+
return m && zoneExists(m) ? m : zone;
|
|
94
|
+
}
|
|
95
|
+
var validZones = /* @__PURE__ */ new Map();
|
|
96
|
+
function normalizeZone(zone) {
|
|
97
|
+
if (zone == null || zone === "local") return "local";
|
|
98
|
+
if (typeof zone !== "string" || zone.trim() === "") throw new RangeError(`neram: unknown time zone ${JSON.stringify(zone)}`);
|
|
99
|
+
const cached = validZones.get(zone);
|
|
100
|
+
if (cached) return cached;
|
|
101
|
+
let out;
|
|
102
|
+
const upper = zone.toUpperCase();
|
|
103
|
+
if (upper === "UTC" || upper === "Z" || upper === "GMT" || upper === "ETC/UTC" || upper === "ETC/GMT") {
|
|
104
|
+
out = "UTC";
|
|
105
|
+
} else {
|
|
106
|
+
const off = parseOffset(zone);
|
|
107
|
+
if (off !== void 0) {
|
|
108
|
+
out = off === 0 ? "UTC" : formatOffset(off);
|
|
109
|
+
} else {
|
|
110
|
+
try {
|
|
111
|
+
const resolved = new Intl.DateTimeFormat("en-US", { timeZone: zone }).resolvedOptions().timeZone;
|
|
112
|
+
out = modernZoneName(resolved.toLowerCase() === zone.toLowerCase() ? resolved : zone);
|
|
113
|
+
} catch {
|
|
114
|
+
throw new RangeError(`neram: unknown time zone "${zone}"`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
validZones.set(zone, out);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
var dtfCache = /* @__PURE__ */ new Map();
|
|
122
|
+
function zoneFormatter(zone) {
|
|
123
|
+
let f = dtfCache.get(zone);
|
|
124
|
+
if (!f) {
|
|
125
|
+
f = new Intl.DateTimeFormat("en-US", {
|
|
126
|
+
timeZone: zone,
|
|
127
|
+
hourCycle: "h23",
|
|
128
|
+
era: "short",
|
|
129
|
+
year: "numeric",
|
|
130
|
+
month: "numeric",
|
|
131
|
+
day: "numeric",
|
|
132
|
+
hour: "numeric",
|
|
133
|
+
minute: "numeric",
|
|
134
|
+
second: "numeric"
|
|
135
|
+
});
|
|
136
|
+
dtfCache.set(zone, f);
|
|
137
|
+
}
|
|
138
|
+
return f;
|
|
139
|
+
}
|
|
140
|
+
function ianaOffset(ms, zone) {
|
|
141
|
+
const parts = zoneFormatter(zone).formatToParts(new Date(ms));
|
|
142
|
+
let y = 0, M = 0, D = 0, h = 0, m = 0, s = 0, bc = false;
|
|
143
|
+
for (const p of parts) {
|
|
144
|
+
switch (p.type) {
|
|
145
|
+
case "year":
|
|
146
|
+
y = Number(p.value);
|
|
147
|
+
break;
|
|
148
|
+
case "month":
|
|
149
|
+
M = Number(p.value) - 1;
|
|
150
|
+
break;
|
|
151
|
+
case "day":
|
|
152
|
+
D = Number(p.value);
|
|
153
|
+
break;
|
|
154
|
+
case "hour":
|
|
155
|
+
h = Number(p.value) % 24;
|
|
156
|
+
break;
|
|
157
|
+
case "minute":
|
|
158
|
+
m = Number(p.value);
|
|
159
|
+
break;
|
|
160
|
+
case "second":
|
|
161
|
+
s = Number(p.value);
|
|
162
|
+
break;
|
|
163
|
+
case "era":
|
|
164
|
+
bc = /^b/i.test(p.value);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (bc) y = 1 - y;
|
|
169
|
+
const floored = ms - (ms % 1e3 + 1e3) % 1e3;
|
|
170
|
+
return Math.round((utcFromParts(y, M, D, h, m, s) - floored) / 6e4);
|
|
171
|
+
}
|
|
172
|
+
var DAY_MS = 864e5;
|
|
173
|
+
var MAX_BUCKETS = 5e4;
|
|
174
|
+
var TRANSITION = -1e9;
|
|
175
|
+
var offsetCache = /* @__PURE__ */ new Map();
|
|
176
|
+
function ianaOffsetCached(ms, zone) {
|
|
177
|
+
let zc = offsetCache.get(zone);
|
|
178
|
+
if (!zc) {
|
|
179
|
+
zc = /* @__PURE__ */ new Map();
|
|
180
|
+
offsetCache.set(zone, zc);
|
|
181
|
+
}
|
|
182
|
+
const bucket = Math.floor(ms / DAY_MS);
|
|
183
|
+
const hit = zc.get(bucket);
|
|
184
|
+
if (hit !== void 0) return hit === TRANSITION ? ianaOffset(ms, zone) : hit;
|
|
185
|
+
const start = ianaOffset(bucket * DAY_MS, zone);
|
|
186
|
+
const end = ianaOffset(bucket * DAY_MS + DAY_MS - 1e3, zone);
|
|
187
|
+
if (zc.size >= MAX_BUCKETS) zc.clear();
|
|
188
|
+
zc.set(bucket, start === end ? start : TRANSITION);
|
|
189
|
+
return start === end ? start : ianaOffset(ms, zone);
|
|
190
|
+
}
|
|
191
|
+
var fixedOffsets = /* @__PURE__ */ new Map();
|
|
192
|
+
function offsetAt(ms, zone) {
|
|
193
|
+
if (zone === "UTC") return 0;
|
|
194
|
+
if (zone === "local") return -new Date(ms).getTimezoneOffset() || 0;
|
|
195
|
+
let fixed = fixedOffsets.get(zone);
|
|
196
|
+
if (fixed === void 0 && !fixedOffsets.has(zone)) {
|
|
197
|
+
fixed = parseOffset(zone);
|
|
198
|
+
fixedOffsets.set(zone, fixed);
|
|
199
|
+
}
|
|
200
|
+
if (fixed !== void 0) return fixed;
|
|
201
|
+
return ianaOffsetCached(ms, zone);
|
|
202
|
+
}
|
|
203
|
+
function wallToEpoch(w, zone) {
|
|
204
|
+
if (zone === "local" && w.y >= 100) return new Date(w.y, w.M, w.D, w.h, w.m, w.s, w.ms).getTime();
|
|
205
|
+
const local = wallToUtcMs(w);
|
|
206
|
+
if (zone === "UTC") return local;
|
|
207
|
+
const fixed = parseOffset(zone);
|
|
208
|
+
if (fixed !== void 0) return local - fixed * 6e4;
|
|
209
|
+
const before = offsetAt(local - 864e5, zone);
|
|
210
|
+
const after = offsetAt(local + 864e5, zone);
|
|
211
|
+
const candidates = /* @__PURE__ */ new Set([offsetAt(local, zone), before, after]);
|
|
212
|
+
const valid = [];
|
|
213
|
+
for (const off of candidates) {
|
|
214
|
+
if (offsetAt(local - off * 6e4, zone) === off) valid.push(off);
|
|
215
|
+
}
|
|
216
|
+
if (valid.length) return local - Math.max(...valid) * 6e4;
|
|
217
|
+
return local - before * 6e4;
|
|
218
|
+
}
|
|
219
|
+
function wallOfMs(t) {
|
|
220
|
+
const day = Math.floor(t / 864e5);
|
|
221
|
+
let rem = t - day * 864e5;
|
|
222
|
+
const c = civilFromDays(day);
|
|
223
|
+
const h = Math.floor(rem / 36e5);
|
|
224
|
+
rem -= h * 36e5;
|
|
225
|
+
const m = Math.floor(rem / 6e4);
|
|
226
|
+
rem -= m * 6e4;
|
|
227
|
+
const s = Math.floor(rem / 1e3);
|
|
228
|
+
return { y: c.y, M: c.M, D: c.D, h, m, s, ms: rem - s * 1e3, dow: c.dow };
|
|
229
|
+
}
|
|
230
|
+
function wallFromEpoch(ms, offset) {
|
|
231
|
+
return wallOfMs(ms + offset * 6e4);
|
|
232
|
+
}
|
|
233
|
+
function guessZone() {
|
|
234
|
+
return modernZoneName(new Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/calendar.ts
|
|
238
|
+
function isLeapYear(y) {
|
|
239
|
+
return y % 4 === 0 && y % 100 !== 0 || y % 400 === 0;
|
|
240
|
+
}
|
|
241
|
+
function civilFromDays(n) {
|
|
242
|
+
const z = n + 719468;
|
|
243
|
+
const era = Math.floor(z / 146097);
|
|
244
|
+
const doe = z - era * 146097;
|
|
245
|
+
const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);
|
|
246
|
+
const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));
|
|
247
|
+
const mp = Math.floor((5 * doy + 2) / 153);
|
|
248
|
+
const D = doy - Math.floor((153 * mp + 2) / 5) + 1;
|
|
249
|
+
const M = mp < 10 ? mp + 2 : mp - 10;
|
|
250
|
+
return { y: yoe + era * 400 + (M <= 1 ? 1 : 0), M, D, dow: (n % 7 + 7 + 4) % 7 };
|
|
251
|
+
}
|
|
252
|
+
var MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
253
|
+
function daysInMonth(y, M) {
|
|
254
|
+
return M === 1 && isLeapYear(y) ? 29 : MONTH_DAYS[M];
|
|
255
|
+
}
|
|
256
|
+
function daysInYear(y) {
|
|
257
|
+
return isLeapYear(y) ? 366 : 365;
|
|
258
|
+
}
|
|
259
|
+
function dayOfYear(y, M, D) {
|
|
260
|
+
return Math.round((utcFromParts(y, M, D) - utcFromParts(y, 0, 1)) / 864e5) + 1;
|
|
261
|
+
}
|
|
262
|
+
function weekdayOf(y, M, D) {
|
|
263
|
+
return new Date(utcFromParts(y, M, D)).getUTCDay();
|
|
264
|
+
}
|
|
265
|
+
function weekOfYear(y, M, D, firstDay, minimalDays) {
|
|
266
|
+
const doy = dayOfYear(y, M, D);
|
|
267
|
+
const idx = (weekdayOf(y, M, D) - firstDay + 7) % 7;
|
|
268
|
+
const weekStart = doy - idx;
|
|
269
|
+
const jan1Idx = (weekdayOf(y, 0, 1) - firstDay + 7) % 7;
|
|
270
|
+
const week1Start = 7 - jan1Idx >= minimalDays ? 1 - jan1Idx : 8 - jan1Idx;
|
|
271
|
+
if (weekStart < week1Start) return weekOfYear(y - 1, 11, 31, firstDay, minimalDays);
|
|
272
|
+
const daysInNextYear = weekStart + 6 - daysInYear(y);
|
|
273
|
+
if (daysInNextYear >= minimalDays) return { week: 1, year: y + 1 };
|
|
274
|
+
return { week: Math.floor((weekStart - week1Start) / 7) + 1, year: y };
|
|
275
|
+
}
|
|
276
|
+
function weeksInYear(y, firstDay, minimalDays) {
|
|
277
|
+
for (let d = 31; d > 24; d--) {
|
|
278
|
+
const w = weekOfYear(y, 11, d, firstDay, minimalDays);
|
|
279
|
+
if (w.year === y) return w.week;
|
|
280
|
+
}
|
|
281
|
+
return 52;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// src/intl.ts
|
|
285
|
+
var intlSettings = { normalizeSpaces: true };
|
|
286
|
+
var SPACES = /[\u00a0\u2007\u2009\u202f]/g;
|
|
287
|
+
function clean(s) {
|
|
288
|
+
return intlSettings.normalizeSpaces ? s.replace(SPACES, " ") : s;
|
|
289
|
+
}
|
|
290
|
+
var I = Intl;
|
|
291
|
+
var listCache = /* @__PURE__ */ new Map();
|
|
292
|
+
function formatList(items, locale = "en", type = "conjunction", style = "long") {
|
|
293
|
+
if (I.ListFormat) {
|
|
294
|
+
const key = `${locale}|${type}|${style}`;
|
|
295
|
+
let lf = listCache.get(key);
|
|
296
|
+
if (!lf) {
|
|
297
|
+
lf = new I.ListFormat(locale, { type, style });
|
|
298
|
+
listCache.set(key, lf);
|
|
299
|
+
}
|
|
300
|
+
return clean(lf.format(items));
|
|
301
|
+
}
|
|
302
|
+
if (items.length <= 1) return items.join("");
|
|
303
|
+
if (type === "unit") return items.join(", ");
|
|
304
|
+
const word = type === "disjunction" ? "or" : "and";
|
|
305
|
+
return items.length === 2 ? `${items[0]} ${word} ${items[1]}` : `${items.slice(0, -1).join(", ")}, ${word} ${items.at(-1)}`;
|
|
306
|
+
}
|
|
307
|
+
var unitCache = /* @__PURE__ */ new Map();
|
|
308
|
+
function formatUnit(value, unit, locale, display = "long") {
|
|
309
|
+
const key = `${locale}|${unit}|${display}`;
|
|
310
|
+
let nf = unitCache.get(key);
|
|
311
|
+
if (nf === void 0) {
|
|
312
|
+
try {
|
|
313
|
+
nf = new Intl.NumberFormat(locale, { style: "unit", unit, unitDisplay: display });
|
|
314
|
+
} catch {
|
|
315
|
+
nf = null;
|
|
316
|
+
}
|
|
317
|
+
unitCache.set(key, nf);
|
|
318
|
+
}
|
|
319
|
+
if (nf) return clean(nf.format(value));
|
|
320
|
+
return `${value} ${unit}${Math.abs(value) === 1 ? "" : "s"}`;
|
|
321
|
+
}
|
|
322
|
+
var rtfCache = /* @__PURE__ */ new Map();
|
|
323
|
+
function formatRelativeTime(value, unit, locale, style, numeric) {
|
|
324
|
+
if (I.RelativeTimeFormat) {
|
|
325
|
+
const key = `${locale}|${style}|${numeric}`;
|
|
326
|
+
let rtf = rtfCache.get(key);
|
|
327
|
+
if (!rtf) {
|
|
328
|
+
rtf = new I.RelativeTimeFormat(locale, { style, numeric });
|
|
329
|
+
rtfCache.set(key, rtf);
|
|
330
|
+
}
|
|
331
|
+
return clean(rtf.format(value, unit));
|
|
332
|
+
}
|
|
333
|
+
if (numeric === "auto") {
|
|
334
|
+
if (value === 0) return unit === "day" ? "today" : "now";
|
|
335
|
+
if (unit === "day" && value === 1) return "tomorrow";
|
|
336
|
+
if (unit === "day" && value === -1) return "yesterday";
|
|
337
|
+
}
|
|
338
|
+
const abs = Math.abs(value);
|
|
339
|
+
const text = `${abs} ${unit}${abs === 1 ? "" : "s"}`;
|
|
340
|
+
return value < 0 || Object.is(value, -0) ? `${text} ago` : `in ${text}`;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/units.ts
|
|
344
|
+
var SHORT = {
|
|
345
|
+
ms: "millisecond",
|
|
346
|
+
s: "second",
|
|
347
|
+
m: "minute",
|
|
348
|
+
h: "hour",
|
|
349
|
+
d: "day",
|
|
350
|
+
D: "date",
|
|
351
|
+
w: "week",
|
|
352
|
+
W: "isoWeek",
|
|
353
|
+
M: "month",
|
|
354
|
+
Q: "quarter",
|
|
355
|
+
y: "year"
|
|
356
|
+
};
|
|
357
|
+
var LONG = /* @__PURE__ */ new Set([
|
|
358
|
+
"millisecond",
|
|
359
|
+
"second",
|
|
360
|
+
"minute",
|
|
361
|
+
"hour",
|
|
362
|
+
"day",
|
|
363
|
+
"date",
|
|
364
|
+
"week",
|
|
365
|
+
"isoWeek",
|
|
366
|
+
"month",
|
|
367
|
+
"quarter",
|
|
368
|
+
"year"
|
|
369
|
+
]);
|
|
370
|
+
function normalizeUnit(u) {
|
|
371
|
+
if (u == null) return void 0;
|
|
372
|
+
if (SHORT[u]) return SHORT[u];
|
|
373
|
+
if (LONG.has(u)) return u;
|
|
374
|
+
const lower = u.toLowerCase().replace(/s$/, "");
|
|
375
|
+
if (lower === "isoweek") return "isoWeek";
|
|
376
|
+
if (LONG.has(lower)) return lower;
|
|
377
|
+
throw new RangeError(`neram: unknown unit "${u}"`);
|
|
378
|
+
}
|
|
379
|
+
var MS_SECOND = 1e3;
|
|
380
|
+
var MS_MINUTE = 60 * MS_SECOND;
|
|
381
|
+
var MS_HOUR = 60 * MS_MINUTE;
|
|
382
|
+
var MS_DAY = 24 * MS_HOUR;
|
|
383
|
+
var MS_WEEK = 7 * MS_DAY;
|
|
384
|
+
var MS_YEAR = 365.2425 * MS_DAY;
|
|
385
|
+
var MS_MONTH = MS_YEAR / 12;
|
|
386
|
+
var FIXED_MS = {
|
|
387
|
+
millisecond: 1,
|
|
388
|
+
second: MS_SECOND,
|
|
389
|
+
minute: MS_MINUTE,
|
|
390
|
+
hour: MS_HOUR
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// src/relative.ts
|
|
394
|
+
function pickUnit(ms, months) {
|
|
395
|
+
const abs = Math.abs(ms);
|
|
396
|
+
const sign = ms < 0 ? -1 : 1;
|
|
397
|
+
const r = (n) => sign * Math.round(n);
|
|
398
|
+
if (abs < 45 * MS_SECOND) return { value: r(abs / MS_SECOND), unit: "second" };
|
|
399
|
+
if (abs < 45 * MS_MINUTE) return { value: r(abs / MS_MINUTE), unit: "minute" };
|
|
400
|
+
if (abs < 22 * MS_HOUR) return { value: r(abs / MS_HOUR), unit: "hour" };
|
|
401
|
+
if (abs < 26 * MS_DAY) return { value: r(abs / MS_DAY), unit: "day" };
|
|
402
|
+
const m = months !== void 0 ? Math.abs(months) : abs / MS_MONTH;
|
|
403
|
+
if (m < 10.5) return { value: r(Math.max(1, m)), unit: "month" };
|
|
404
|
+
return { value: r(Math.max(1, m / 12)), unit: "year" };
|
|
405
|
+
}
|
|
406
|
+
function formatRelative(value, unit, locale, opts = {}) {
|
|
407
|
+
const style = opts.style ?? "long";
|
|
408
|
+
if (opts.withoutSuffix) return formatUnit(Math.abs(value), unit, locale, style);
|
|
409
|
+
return formatRelativeTime(value, unit, locale, style, opts.numeric ?? "auto");
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// src/duration.ts
|
|
413
|
+
var DURATION_UNITS = [
|
|
414
|
+
"years",
|
|
415
|
+
"months",
|
|
416
|
+
"weeks",
|
|
417
|
+
"days",
|
|
418
|
+
"hours",
|
|
419
|
+
"minutes",
|
|
420
|
+
"seconds",
|
|
421
|
+
"milliseconds"
|
|
422
|
+
];
|
|
423
|
+
var INVALID = "Invalid Duration";
|
|
424
|
+
var UNIT_MS = {
|
|
425
|
+
years: MS_YEAR,
|
|
426
|
+
months: MS_MONTH,
|
|
427
|
+
weeks: MS_WEEK,
|
|
428
|
+
days: MS_DAY,
|
|
429
|
+
hours: MS_HOUR,
|
|
430
|
+
minutes: MS_MINUTE,
|
|
431
|
+
seconds: MS_SECOND,
|
|
432
|
+
milliseconds: 1
|
|
433
|
+
};
|
|
434
|
+
var ISO_DURATION = /^([+-])?P(?:(-?[\d.,]+)Y)?(?:(-?[\d.,]+)M)?(?:(-?[\d.,]+)W)?(?:(-?[\d.,]+)D)?(?:T(?:(-?[\d.,]+)H)?(?:(-?[\d.,]+)M)?(?:(-?[\d.,]+)S)?)?$/i;
|
|
435
|
+
function toDurationUnit(u) {
|
|
436
|
+
if (DURATION_UNITS.includes(u)) return u;
|
|
437
|
+
const n = normalizeUnit(u);
|
|
438
|
+
switch (n) {
|
|
439
|
+
case "year":
|
|
440
|
+
return "years";
|
|
441
|
+
case "quarter":
|
|
442
|
+
throw new RangeError("neram: use months for quarter durations");
|
|
443
|
+
case "month":
|
|
444
|
+
return "months";
|
|
445
|
+
case "week":
|
|
446
|
+
case "isoWeek":
|
|
447
|
+
return "weeks";
|
|
448
|
+
case "day":
|
|
449
|
+
case "date":
|
|
450
|
+
return "days";
|
|
451
|
+
case "hour":
|
|
452
|
+
return "hours";
|
|
453
|
+
case "minute":
|
|
454
|
+
return "minutes";
|
|
455
|
+
case "second":
|
|
456
|
+
return "seconds";
|
|
457
|
+
default:
|
|
458
|
+
return "milliseconds";
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
var Duration = class _Duration {
|
|
462
|
+
#v;
|
|
463
|
+
constructor(input, unit = "milliseconds") {
|
|
464
|
+
const v = {
|
|
465
|
+
years: 0,
|
|
466
|
+
months: 0,
|
|
467
|
+
weeks: 0,
|
|
468
|
+
days: 0,
|
|
469
|
+
hours: 0,
|
|
470
|
+
minutes: 0,
|
|
471
|
+
seconds: 0,
|
|
472
|
+
milliseconds: 0
|
|
473
|
+
};
|
|
474
|
+
if (input instanceof _Duration) {
|
|
475
|
+
Object.assign(v, input.toObject());
|
|
476
|
+
} else if (typeof input === "number") {
|
|
477
|
+
v[toDurationUnit(unit)] = input;
|
|
478
|
+
} else if (typeof input === "string") {
|
|
479
|
+
const m = ISO_DURATION.exec(input.trim());
|
|
480
|
+
if (!m || input.trim().toUpperCase() === "P" || /T$/i.test(input.trim())) {
|
|
481
|
+
throw new RangeError(`neram: invalid ISO 8601 duration "${input}"`);
|
|
482
|
+
}
|
|
483
|
+
const sign = m[1] === "-" ? -1 : 1;
|
|
484
|
+
DURATION_UNITS.forEach((u, i) => {
|
|
485
|
+
if (u === "milliseconds") return;
|
|
486
|
+
const raw = m[i + 2];
|
|
487
|
+
if (raw) v[u] = sign * Number(raw.replace(",", "."));
|
|
488
|
+
});
|
|
489
|
+
const whole = Math.trunc(v.seconds);
|
|
490
|
+
v.milliseconds = Math.round((v.seconds - whole) * 1e3);
|
|
491
|
+
v.seconds = whole;
|
|
492
|
+
} else if (input) {
|
|
493
|
+
for (const [k, n] of Object.entries(input)) {
|
|
494
|
+
if (n != null) v[toDurationUnit(k)] += n;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
this.#v = v;
|
|
498
|
+
}
|
|
499
|
+
/** False when any part is NaN or infinite, e.g. neram.duration(NaN) or the durationTo() of an invalid date. */
|
|
500
|
+
isValid() {
|
|
501
|
+
return DURATION_UNITS.every((u) => Number.isFinite(this.#v[u]));
|
|
502
|
+
}
|
|
503
|
+
get years() {
|
|
504
|
+
return this.#v.years;
|
|
505
|
+
}
|
|
506
|
+
get months() {
|
|
507
|
+
return this.#v.months;
|
|
508
|
+
}
|
|
509
|
+
get weeks() {
|
|
510
|
+
return this.#v.weeks;
|
|
511
|
+
}
|
|
512
|
+
get days() {
|
|
513
|
+
return this.#v.days;
|
|
514
|
+
}
|
|
515
|
+
get hours() {
|
|
516
|
+
return this.#v.hours;
|
|
517
|
+
}
|
|
518
|
+
get minutes() {
|
|
519
|
+
return this.#v.minutes;
|
|
520
|
+
}
|
|
521
|
+
get seconds() {
|
|
522
|
+
return this.#v.seconds;
|
|
523
|
+
}
|
|
524
|
+
get milliseconds() {
|
|
525
|
+
return this.#v.milliseconds;
|
|
526
|
+
}
|
|
527
|
+
get(unit) {
|
|
528
|
+
return this.#v[toDurationUnit(unit)];
|
|
529
|
+
}
|
|
530
|
+
/** Total length expressed in one unit (years/months use average lengths). */
|
|
531
|
+
as(unit) {
|
|
532
|
+
return this.valueOf() / UNIT_MS[toDurationUnit(unit)];
|
|
533
|
+
}
|
|
534
|
+
asMilliseconds() {
|
|
535
|
+
return this.as("milliseconds");
|
|
536
|
+
}
|
|
537
|
+
asSeconds() {
|
|
538
|
+
return this.as("seconds");
|
|
539
|
+
}
|
|
540
|
+
asMinutes() {
|
|
541
|
+
return this.as("minutes");
|
|
542
|
+
}
|
|
543
|
+
asHours() {
|
|
544
|
+
return this.as("hours");
|
|
545
|
+
}
|
|
546
|
+
asDays() {
|
|
547
|
+
return this.as("days");
|
|
548
|
+
}
|
|
549
|
+
asWeeks() {
|
|
550
|
+
return this.as("weeks");
|
|
551
|
+
}
|
|
552
|
+
asMonths() {
|
|
553
|
+
return this.as("months");
|
|
554
|
+
}
|
|
555
|
+
asYears() {
|
|
556
|
+
return this.as("years");
|
|
557
|
+
}
|
|
558
|
+
/** Approximate total in milliseconds. */
|
|
559
|
+
valueOf() {
|
|
560
|
+
if (!this.isValid()) return NaN;
|
|
561
|
+
let total = 0;
|
|
562
|
+
for (const u of DURATION_UNITS) total += this.#v[u] * UNIT_MS[u];
|
|
563
|
+
return total;
|
|
564
|
+
}
|
|
565
|
+
add(input, unit) {
|
|
566
|
+
const other = input instanceof _Duration ? input : new _Duration(input, unit);
|
|
567
|
+
const out = {};
|
|
568
|
+
for (const u of DURATION_UNITS) out[u] = this.#v[u] + other.get(u);
|
|
569
|
+
return new _Duration(out);
|
|
570
|
+
}
|
|
571
|
+
subtract(input, unit) {
|
|
572
|
+
const other = input instanceof _Duration ? input : new _Duration(input, unit);
|
|
573
|
+
return this.add(other.negate());
|
|
574
|
+
}
|
|
575
|
+
negate() {
|
|
576
|
+
const out = {};
|
|
577
|
+
for (const u of DURATION_UNITS) out[u] = -this.#v[u] || 0;
|
|
578
|
+
return new _Duration(out);
|
|
579
|
+
}
|
|
580
|
+
abs() {
|
|
581
|
+
return this.valueOf() < 0 ? this.negate() : this;
|
|
582
|
+
}
|
|
583
|
+
sign() {
|
|
584
|
+
const v = this.valueOf();
|
|
585
|
+
return v === 0 ? 0 : v < 0 ? -1 : 1;
|
|
586
|
+
}
|
|
587
|
+
isZero() {
|
|
588
|
+
return DURATION_UNITS.every((u) => this.#v[u] === 0);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Balance overflowing units: 90 minutes → 1 h 30 min, 14 months → 1 y 2 mo.
|
|
592
|
+
* Exact units roll up to days; months roll up to years. Weeks fold into days.
|
|
593
|
+
*/
|
|
594
|
+
normalize() {
|
|
595
|
+
if (!this.isValid()) return this;
|
|
596
|
+
const v = this.#v;
|
|
597
|
+
const exact = v.weeks * MS_WEEK + v.days * MS_DAY + v.hours * MS_HOUR + v.minutes * MS_MINUTE + v.seconds * MS_SECOND + v.milliseconds;
|
|
598
|
+
const totalMonths = v.years * 12 + v.months;
|
|
599
|
+
const sign = exact < 0 ? -1 : 1;
|
|
600
|
+
let rest = Math.abs(exact);
|
|
601
|
+
const take = (size) => {
|
|
602
|
+
const n = Math.floor(rest / size);
|
|
603
|
+
rest -= n * size;
|
|
604
|
+
return sign * n || 0;
|
|
605
|
+
};
|
|
606
|
+
const days = take(MS_DAY);
|
|
607
|
+
const hours = take(MS_HOUR);
|
|
608
|
+
const minutes = take(MS_MINUTE);
|
|
609
|
+
const seconds = take(MS_SECOND);
|
|
610
|
+
const milliseconds = sign * Math.round(rest) || 0;
|
|
611
|
+
return new _Duration({
|
|
612
|
+
years: Math.trunc(totalMonths / 12) || 0,
|
|
613
|
+
months: totalMonths % 12 || 0,
|
|
614
|
+
days,
|
|
615
|
+
hours,
|
|
616
|
+
minutes,
|
|
617
|
+
seconds,
|
|
618
|
+
milliseconds
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
/** "2 hours", or with suffix "in 2 hours" / "2 hours ago". Locale-aware via Intl. */
|
|
622
|
+
humanize(withSuffix = false, locale = "en") {
|
|
623
|
+
if (!this.isValid()) return INVALID;
|
|
624
|
+
const opts = typeof withSuffix === "object" ? withSuffix : { withoutSuffix: !withSuffix };
|
|
625
|
+
const months = this.#v.years * 12 + this.#v.months;
|
|
626
|
+
const exact = this.valueOf() - months * MS_MONTH;
|
|
627
|
+
const { value, unit } = pickUnit(this.valueOf(), exact === 0 && months !== 0 ? months : void 0);
|
|
628
|
+
return formatRelative(value, unit, opts.locale ?? locale, { numeric: "always", ...opts });
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Every non-zero unit, written out the way the locale lists them:
|
|
632
|
+
* "1 hour, 30 minutes" / "1 Stunde und 30 Minuten". Uses Intl.DurationFormat when present.
|
|
633
|
+
*/
|
|
634
|
+
toLocaleString(locale = "en", style = "long") {
|
|
635
|
+
if (!this.isValid()) return INVALID;
|
|
636
|
+
const DF = Intl.DurationFormat;
|
|
637
|
+
if (DF) return clean(new DF(locale, { style }).format(this.toObject()));
|
|
638
|
+
const display = style === "digital" ? "narrow" : style;
|
|
639
|
+
const parts = DURATION_UNITS.filter((u) => this.#v[u] !== 0).map((u) => formatUnit(this.#v[u], u.slice(0, -1), locale, display));
|
|
640
|
+
if (!parts.length) return formatUnit(0, "second", locale, display);
|
|
641
|
+
return formatList(parts, locale, "unit", display);
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Format with tokens: Y M W D H HH m mm s ss SSS (the units of the normalized duration).
|
|
645
|
+
* `[text]` is literal. Example: neram.duration(5025000).format('H[h] mm[m]') → "1h 23m".
|
|
646
|
+
*/
|
|
647
|
+
format(fmt = "HH:mm:ss") {
|
|
648
|
+
if (!this.isValid()) return INVALID;
|
|
649
|
+
const n = this.abs().normalize();
|
|
650
|
+
const pad2 = (x, l = 2) => String(x).padStart(l, "0");
|
|
651
|
+
const out = fmt.replace(/\[([^\]]*)\]|YYYY|Y|MM|M|DD|D|HH|H|mm|m|ss|s|SSS/g, (tok, lit) => {
|
|
652
|
+
if (lit !== void 0) return lit;
|
|
653
|
+
switch (tok) {
|
|
654
|
+
case "Y":
|
|
655
|
+
return String(n.years);
|
|
656
|
+
case "YYYY":
|
|
657
|
+
return pad2(n.years, 4);
|
|
658
|
+
case "M":
|
|
659
|
+
return String(n.months);
|
|
660
|
+
case "MM":
|
|
661
|
+
return pad2(n.months);
|
|
662
|
+
case "D":
|
|
663
|
+
return String(n.days);
|
|
664
|
+
case "DD":
|
|
665
|
+
return pad2(n.days);
|
|
666
|
+
case "H":
|
|
667
|
+
return String(n.hours);
|
|
668
|
+
case "HH":
|
|
669
|
+
return pad2(n.hours);
|
|
670
|
+
case "m":
|
|
671
|
+
return String(n.minutes);
|
|
672
|
+
case "mm":
|
|
673
|
+
return pad2(n.minutes);
|
|
674
|
+
case "s":
|
|
675
|
+
return String(n.seconds);
|
|
676
|
+
case "ss":
|
|
677
|
+
return pad2(n.seconds);
|
|
678
|
+
default:
|
|
679
|
+
return pad2(n.milliseconds, 3);
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
return this.sign() < 0 ? `-${out}` : out;
|
|
683
|
+
}
|
|
684
|
+
/** ISO 8601, e.g. "P1Y2M3DT4H5M6.5S". Weeks are folded into days. */
|
|
685
|
+
toISOString() {
|
|
686
|
+
if (!this.isValid()) throw new RangeError("neram: invalid duration");
|
|
687
|
+
const v = { ...this.#v };
|
|
688
|
+
v.days += v.weeks * 7;
|
|
689
|
+
const values = [v.years, v.months, v.days, v.hours, v.minutes, v.seconds + v.milliseconds / 1e3];
|
|
690
|
+
const allNeg = values.every((x) => x <= 0) && values.some((x) => x < 0);
|
|
691
|
+
const s = allNeg ? -1 : 1;
|
|
692
|
+
const n = (x) => String(Math.round(s * x * 1e3) / 1e3);
|
|
693
|
+
let date = "";
|
|
694
|
+
if (v.years) date += `${n(v.years)}Y`;
|
|
695
|
+
if (v.months) date += `${n(v.months)}M`;
|
|
696
|
+
if (v.days) date += `${n(v.days)}D`;
|
|
697
|
+
let time = "";
|
|
698
|
+
if (v.hours) time += `${n(v.hours)}H`;
|
|
699
|
+
if (v.minutes) time += `${n(v.minutes)}M`;
|
|
700
|
+
if (v.seconds || v.milliseconds) time += `${n(v.seconds + v.milliseconds / 1e3)}S`;
|
|
701
|
+
if (!date && !time) return "PT0S";
|
|
702
|
+
return `${allNeg ? "-" : ""}P${date}${time ? `T${time}` : ""}`;
|
|
703
|
+
}
|
|
704
|
+
/** ISO string, or null when invalid (like an invalid date). */
|
|
705
|
+
toJSON() {
|
|
706
|
+
return this.isValid() ? this.toISOString() : null;
|
|
707
|
+
}
|
|
708
|
+
toString() {
|
|
709
|
+
return this.isValid() ? this.toISOString() : INVALID;
|
|
710
|
+
}
|
|
711
|
+
toObject() {
|
|
712
|
+
return { ...this.#v };
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
function duration(input, unit) {
|
|
716
|
+
return new Duration(input, unit);
|
|
717
|
+
}
|
|
718
|
+
function isDuration(x) {
|
|
719
|
+
return x instanceof Duration;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// src/weekdata.ts
|
|
723
|
+
var FIRST_DAY = {
|
|
724
|
+
...regions(5, "MV"),
|
|
725
|
+
...regions(6, "AF BH DJ DZ EG IQ IR JO KW LY OM QA SD SY"),
|
|
726
|
+
...regions(0, "AG AS BD BR BS BT BW BZ CA CO DM DO ET GT GU HK HN ID IL IN IS JM JP KE KH KR LA MH MM MO MT MX MZ NI NP PA PE PH PK PR PT PY SA SG SV TH TT TW UM US VE VI WS YE ZA ZW")
|
|
727
|
+
};
|
|
728
|
+
var WEEKEND = {
|
|
729
|
+
...regions([5], "IR"),
|
|
730
|
+
...regions([0], "IN UG"),
|
|
731
|
+
...regions([4, 5], "AF"),
|
|
732
|
+
...regions([5, 6], "BH DZ EG IL IQ JO KW LY OM QA SA SD SY YE")
|
|
733
|
+
};
|
|
734
|
+
var MIN_DAYS_4 = new Set(
|
|
735
|
+
"AD AN AT AX BE BG CH CZ DE DK EE ES FI FJ FO FR GB GF GG GI GP GR HU IE IM IS IT JE LI LT LU MC MQ NL NO PL RE RU SE SJ SK SM VA".split(" ")
|
|
736
|
+
);
|
|
737
|
+
var DAY_CODES = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
|
|
738
|
+
function regions(value, list2) {
|
|
739
|
+
return Object.fromEntries(list2.split(" ").map((r) => [r, value]));
|
|
740
|
+
}
|
|
741
|
+
function regionOf(locale) {
|
|
742
|
+
try {
|
|
743
|
+
const l = new Intl.Locale(locale);
|
|
744
|
+
return l.region ?? (typeof l.maximize === "function" ? l.maximize().region : void 0);
|
|
745
|
+
} catch {
|
|
746
|
+
const m = /^[a-z]{2,3}[-_]([A-Z]{2})\b/i.exec(locale);
|
|
747
|
+
return m?.[1].toUpperCase();
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
function weekRules(locale) {
|
|
751
|
+
const region = regionOf(locale) ?? "001";
|
|
752
|
+
let firstDay = FIRST_DAY[region] ?? 1;
|
|
753
|
+
const fw = /-u(?:-[a-z0-9]{2,8})*?-fw-([a-z]{3})\b/i.exec(locale);
|
|
754
|
+
if (fw) {
|
|
755
|
+
const i = DAY_CODES.indexOf(fw[1].toLowerCase());
|
|
756
|
+
if (i >= 0) firstDay = i;
|
|
757
|
+
}
|
|
758
|
+
return {
|
|
759
|
+
firstDay,
|
|
760
|
+
minimalDays: MIN_DAYS_4.has(region) ? 4 : 1,
|
|
761
|
+
weekend: WEEKEND[region] ?? [6, 0]
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
// src/locale.ts
|
|
766
|
+
var cache = /* @__PURE__ */ new Map();
|
|
767
|
+
function canonicalLocale(locale) {
|
|
768
|
+
try {
|
|
769
|
+
return Intl.getCanonicalLocales(locale)[0] ?? "en";
|
|
770
|
+
} catch {
|
|
771
|
+
throw new RangeError(`neram: invalid locale "${locale}"`);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
function weekInfo(locale) {
|
|
775
|
+
return weekRules(locale);
|
|
776
|
+
}
|
|
777
|
+
function localeData(locale) {
|
|
778
|
+
let data = cache.get(locale);
|
|
779
|
+
if (data) return data;
|
|
780
|
+
const base = (opts) => new Intl.DateTimeFormat(locale, { ...opts, timeZone: "UTC" });
|
|
781
|
+
const fmt = (opts) => {
|
|
782
|
+
const f = base(opts);
|
|
783
|
+
return {
|
|
784
|
+
format: (t) => clean(f.format(t)),
|
|
785
|
+
formatToParts: (t) => f.formatToParts(t).map((p) => ({ ...p, value: clean(p.value) }))
|
|
786
|
+
};
|
|
787
|
+
};
|
|
788
|
+
const mLong = fmt({ month: "long" });
|
|
789
|
+
const mShort = fmt({ month: "short" });
|
|
790
|
+
const mLongInDate = fmt({ month: "long", day: "numeric" });
|
|
791
|
+
const months = [];
|
|
792
|
+
const monthsShort = [];
|
|
793
|
+
for (let i = 0; i < 12; i++) {
|
|
794
|
+
const t = Date.UTC(2021, i, 15);
|
|
795
|
+
const inDate = mLongInDate.formatToParts(t).find((p) => p.type === "month")?.value;
|
|
796
|
+
months.push(inDate ?? mLong.format(t));
|
|
797
|
+
monthsShort.push(mShort.format(t).replace(/\.$/, ""));
|
|
798
|
+
}
|
|
799
|
+
const wLong = fmt({ weekday: "long" });
|
|
800
|
+
const wShort = fmt({ weekday: "short" });
|
|
801
|
+
const wNarrow = fmt({ weekday: "narrow" });
|
|
802
|
+
const isEnglish = locale.toLowerCase().startsWith("en");
|
|
803
|
+
const weekdays = [];
|
|
804
|
+
const weekdaysShort = [];
|
|
805
|
+
const weekdaysMin = [];
|
|
806
|
+
for (let i = 0; i < 7; i++) {
|
|
807
|
+
const t = Date.UTC(2021, 0, 3 + i);
|
|
808
|
+
weekdays.push(wLong.format(t));
|
|
809
|
+
const short = wShort.format(t).replace(/\.$/, "");
|
|
810
|
+
weekdaysShort.push(short);
|
|
811
|
+
weekdaysMin.push(isEnglish ? short.slice(0, 2) : wNarrow.format(t));
|
|
812
|
+
}
|
|
813
|
+
const hour12 = fmt({ hour: "numeric", hour12: true });
|
|
814
|
+
const dp = (t) => hour12.formatToParts(t).find((p) => p.type === "dayPeriod")?.value;
|
|
815
|
+
const meridiem = [
|
|
816
|
+
dp(Date.UTC(2021, 0, 1, 9)) ?? "AM",
|
|
817
|
+
dp(Date.UTC(2021, 0, 1, 15)) ?? "PM"
|
|
818
|
+
];
|
|
819
|
+
data = { months, monthsShort, weekdays, weekdaysShort, weekdaysMin, meridiem, week: weekInfo(locale) };
|
|
820
|
+
cache.set(locale, data);
|
|
821
|
+
return data;
|
|
822
|
+
}
|
|
823
|
+
var ordinalRules = /* @__PURE__ */ new Map();
|
|
824
|
+
var EN_SUFFIX = { one: "st", two: "nd", few: "rd", other: "th" };
|
|
825
|
+
function ordinal(n, locale) {
|
|
826
|
+
if (!locale.toLowerCase().startsWith("en")) return `${n}.`;
|
|
827
|
+
let pr = ordinalRules.get(locale);
|
|
828
|
+
if (!pr) {
|
|
829
|
+
pr = new Intl.PluralRules(locale, { type: "ordinal" });
|
|
830
|
+
ordinalRules.set(locale, pr);
|
|
831
|
+
}
|
|
832
|
+
return `${n}${EN_SUFFIX[pr.select(n)] ?? "th"}`;
|
|
833
|
+
}
|
|
834
|
+
var publicCache = /* @__PURE__ */ new Map();
|
|
835
|
+
function publicLocaleData(locale) {
|
|
836
|
+
let out = publicCache.get(locale);
|
|
837
|
+
if (out) return out;
|
|
838
|
+
const d = localeData(locale);
|
|
839
|
+
const pick = (list2, at) => (x) => x === void 0 ? list2.slice() : list2[at(x)];
|
|
840
|
+
const byMonth = (x) => x.month();
|
|
841
|
+
const byDay = (x) => x.day();
|
|
842
|
+
out = Object.freeze({
|
|
843
|
+
name: locale,
|
|
844
|
+
firstDayOfWeek: () => d.week.firstDay,
|
|
845
|
+
months: pick(d.months, byMonth),
|
|
846
|
+
monthsShort: pick(d.monthsShort, byMonth),
|
|
847
|
+
weekdays: pick(d.weekdays, byDay),
|
|
848
|
+
weekdaysShort: pick(d.weekdaysShort, byDay),
|
|
849
|
+
weekdaysMin: pick(d.weekdaysMin, byDay),
|
|
850
|
+
meridiem: (hour, _minute, isLowercase) => {
|
|
851
|
+
const m = d.meridiem[hour < 12 ? 0 : 1];
|
|
852
|
+
return isLowercase ? m.toLocaleLowerCase(locale) : m;
|
|
853
|
+
},
|
|
854
|
+
ordinal: (n) => ordinal(n, locale),
|
|
855
|
+
week: () => ({ ...d.week, weekend: d.week.weekend.slice() })
|
|
856
|
+
});
|
|
857
|
+
publicCache.set(locale, out);
|
|
858
|
+
return out;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// src/format.ts
|
|
862
|
+
var TOKEN = /\[([^\]]*)\]|LTS|LT|LLLL|LLL|LL|L|llll|lll|ll|l|YYYY|YY|GGGG|gggg|MMMM|MMM|MM|M|Qo|Q|DDDD|DDD|Do|DD|D|dddd|ddd|dd|d|E|HH|H|hh|h|kk|k|mm|m|ss|s|S{1,9}|A|a|ZZ|Z|X|x|WW|W|ww|w|zzz|z/g;
|
|
863
|
+
var pad = (n, len = 2) => {
|
|
864
|
+
const s = String(Math.abs(n)).padStart(len, "0");
|
|
865
|
+
return n < 0 ? `-${s}` : s;
|
|
866
|
+
};
|
|
867
|
+
var LOCALIZED = {
|
|
868
|
+
LT: { timeStyle: "short" },
|
|
869
|
+
LTS: { timeStyle: "medium" },
|
|
870
|
+
L: { year: "numeric", month: "2-digit", day: "2-digit" },
|
|
871
|
+
LL: { dateStyle: "long" },
|
|
872
|
+
LLL: { dateStyle: "long", timeStyle: "short" },
|
|
873
|
+
LLLL: { dateStyle: "full", timeStyle: "short" },
|
|
874
|
+
l: { year: "numeric", month: "numeric", day: "numeric" },
|
|
875
|
+
ll: { dateStyle: "medium" },
|
|
876
|
+
lll: { dateStyle: "medium", timeStyle: "short" },
|
|
877
|
+
llll: { weekday: "short", year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "2-digit" }
|
|
878
|
+
};
|
|
879
|
+
function formatDate(ctx, fmt) {
|
|
880
|
+
const { wall: w, locale } = ctx;
|
|
881
|
+
let ld;
|
|
882
|
+
const names = () => ld ??= localeData(locale);
|
|
883
|
+
const iso = () => weekOfYear(w.y, w.M, w.D, 1, 4);
|
|
884
|
+
const loc = () => weekOfYear(w.y, w.M, w.D, ctx.weekStart, names().week.minimalDays);
|
|
885
|
+
const h12 = w.h % 12 || 12;
|
|
886
|
+
const value = (tok) => {
|
|
887
|
+
if (LOCALIZED[tok]) return ctx.intl(LOCALIZED[tok]);
|
|
888
|
+
switch (tok) {
|
|
889
|
+
case "YYYY":
|
|
890
|
+
return w.y >= 0 && w.y <= 9999 ? pad(w.y, 4) : (w.y < 0 ? "-" : "+") + pad(Math.abs(w.y), 6);
|
|
891
|
+
case "YY":
|
|
892
|
+
return pad((w.y % 100 + 100) % 100);
|
|
893
|
+
case "GGGG":
|
|
894
|
+
return pad(iso().year, 4);
|
|
895
|
+
case "gggg":
|
|
896
|
+
return pad(loc().year, 4);
|
|
897
|
+
case "M":
|
|
898
|
+
return String(w.M + 1);
|
|
899
|
+
case "MM":
|
|
900
|
+
return pad(w.M + 1);
|
|
901
|
+
case "MMM":
|
|
902
|
+
return names().monthsShort[w.M];
|
|
903
|
+
case "MMMM":
|
|
904
|
+
return names().months[w.M];
|
|
905
|
+
case "Q":
|
|
906
|
+
return String(Math.floor(w.M / 3) + 1);
|
|
907
|
+
case "Qo":
|
|
908
|
+
return ordinal(Math.floor(w.M / 3) + 1, locale);
|
|
909
|
+
case "D":
|
|
910
|
+
return String(w.D);
|
|
911
|
+
case "DD":
|
|
912
|
+
return pad(w.D);
|
|
913
|
+
case "Do":
|
|
914
|
+
return ordinal(w.D, locale);
|
|
915
|
+
case "DDD":
|
|
916
|
+
return String(dayOfYear(w.y, w.M, w.D));
|
|
917
|
+
case "DDDD":
|
|
918
|
+
return pad(dayOfYear(w.y, w.M, w.D), 3);
|
|
919
|
+
case "d":
|
|
920
|
+
return String(w.dow);
|
|
921
|
+
case "E":
|
|
922
|
+
return String(w.dow || 7);
|
|
923
|
+
case "dd":
|
|
924
|
+
return names().weekdaysMin[w.dow];
|
|
925
|
+
case "ddd":
|
|
926
|
+
return names().weekdaysShort[w.dow];
|
|
927
|
+
case "dddd":
|
|
928
|
+
return names().weekdays[w.dow];
|
|
929
|
+
case "H":
|
|
930
|
+
return String(w.h);
|
|
931
|
+
case "HH":
|
|
932
|
+
return pad(w.h);
|
|
933
|
+
case "h":
|
|
934
|
+
return String(h12);
|
|
935
|
+
case "hh":
|
|
936
|
+
return pad(h12);
|
|
937
|
+
case "k":
|
|
938
|
+
return String(w.h || 24);
|
|
939
|
+
case "kk":
|
|
940
|
+
return pad(w.h || 24);
|
|
941
|
+
case "m":
|
|
942
|
+
return String(w.m);
|
|
943
|
+
case "mm":
|
|
944
|
+
return pad(w.m);
|
|
945
|
+
case "s":
|
|
946
|
+
return String(w.s);
|
|
947
|
+
case "ss":
|
|
948
|
+
return pad(w.s);
|
|
949
|
+
case "A":
|
|
950
|
+
return names().meridiem[w.h < 12 ? 0 : 1];
|
|
951
|
+
case "a":
|
|
952
|
+
return names().meridiem[w.h < 12 ? 0 : 1].toLowerCase();
|
|
953
|
+
case "Z":
|
|
954
|
+
return formatOffset(ctx.offset);
|
|
955
|
+
case "ZZ":
|
|
956
|
+
return formatOffset(ctx.offset, "");
|
|
957
|
+
case "X":
|
|
958
|
+
return String(Math.floor(ctx.ms / 1e3));
|
|
959
|
+
case "x":
|
|
960
|
+
return String(ctx.ms);
|
|
961
|
+
case "W":
|
|
962
|
+
return String(iso().week);
|
|
963
|
+
case "WW":
|
|
964
|
+
return pad(iso().week);
|
|
965
|
+
case "w":
|
|
966
|
+
return String(loc().week);
|
|
967
|
+
case "ww":
|
|
968
|
+
return pad(loc().week);
|
|
969
|
+
case "z":
|
|
970
|
+
return ctx.intl({ timeZoneName: "short" }).split(/,?\s/).pop() ?? "";
|
|
971
|
+
case "zzz":
|
|
972
|
+
return zoneLongName(ctx);
|
|
973
|
+
default:
|
|
974
|
+
return pad(w.ms, 3).padEnd(tok.length, "0").slice(0, tok.length);
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
const parts = compile(fmt);
|
|
978
|
+
let out = parts[0];
|
|
979
|
+
for (let i = 1; i < parts.length; i += 2) out += value(parts[i]) + parts[i + 1];
|
|
980
|
+
return out;
|
|
981
|
+
}
|
|
982
|
+
var compiled = /* @__PURE__ */ new Map();
|
|
983
|
+
function compile(fmt) {
|
|
984
|
+
let parts = compiled.get(fmt);
|
|
985
|
+
if (parts) return parts;
|
|
986
|
+
parts = [];
|
|
987
|
+
let lit = "";
|
|
988
|
+
let last = 0;
|
|
989
|
+
for (const m of fmt.matchAll(TOKEN)) {
|
|
990
|
+
lit += fmt.slice(last, m.index);
|
|
991
|
+
last = m.index + m[0].length;
|
|
992
|
+
if (m[1] !== void 0) {
|
|
993
|
+
lit += m[1];
|
|
994
|
+
} else {
|
|
995
|
+
parts.push(lit, m[0]);
|
|
996
|
+
lit = "";
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
parts.push(lit + fmt.slice(last));
|
|
1000
|
+
if (compiled.size > 500) compiled.clear();
|
|
1001
|
+
compiled.set(fmt, parts);
|
|
1002
|
+
return parts;
|
|
1003
|
+
}
|
|
1004
|
+
function zoneLongName(ctx) {
|
|
1005
|
+
const full = ctx.intl({ year: "numeric", timeZoneName: "long" });
|
|
1006
|
+
const year = ctx.intl({ year: "numeric" });
|
|
1007
|
+
return full.replace(year, "").replace(/^[\s,]+|[\s,]+$/g, "");
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
// src/parse.ts
|
|
1011
|
+
var INVALID2 = { kind: "epoch", ms: NaN };
|
|
1012
|
+
var ISO_RE = /^([+-]\d{6}|\d{4})(?:-?(\d{2})(?:-?(\d{2}))?)?(?:[T\s](\d{2})(?::?(\d{2})(?::?(\d{2})(?:[.,](\d{1,9}))?)?)?)?\s*(Z|[+-]\d{2}(?::?\d{2})?)?(?:\[!?([^\]=]+)\])?(?:\[[^\]]*\])*$/i;
|
|
1013
|
+
function validWall(w) {
|
|
1014
|
+
return w.M >= 0 && w.M <= 11 && w.D >= 1 && w.D <= daysInMonth(w.y, w.M) && w.h >= 0 && (w.h < 24 || w.h === 24 && w.m === 0 && w.s === 0 && w.ms === 0) && w.m >= 0 && w.m < 60 && w.s >= 0 && w.s < 61 && w.ms >= 0 && w.ms < 1e3;
|
|
1015
|
+
}
|
|
1016
|
+
function d2(s, i) {
|
|
1017
|
+
const a = s.charCodeAt(i) - 48;
|
|
1018
|
+
const b = s.charCodeAt(i + 1) - 48;
|
|
1019
|
+
return a >= 0 && a <= 9 && b >= 0 && b <= 9 ? a * 10 + b : -1;
|
|
1020
|
+
}
|
|
1021
|
+
function fastISO(s) {
|
|
1022
|
+
const len = s.length;
|
|
1023
|
+
if (len < 10 || s.charCodeAt(4) !== 45 || s.charCodeAt(7) !== 45) return void 0;
|
|
1024
|
+
const yHi = d2(s, 0);
|
|
1025
|
+
const yLo = d2(s, 2);
|
|
1026
|
+
const M = d2(s, 5);
|
|
1027
|
+
const D = d2(s, 8);
|
|
1028
|
+
if (yHi < 0 || yLo < 0 || M < 0 || D < 0) return void 0;
|
|
1029
|
+
let h = 0, m = 0, sec = 0, ms = 0;
|
|
1030
|
+
let offset;
|
|
1031
|
+
let i = 10;
|
|
1032
|
+
if (i < len) {
|
|
1033
|
+
const t = s.charCodeAt(i);
|
|
1034
|
+
if (t !== 84 && t !== 116 && t !== 32 || len < 16 || s.charCodeAt(13) !== 58) return void 0;
|
|
1035
|
+
h = d2(s, 11);
|
|
1036
|
+
m = d2(s, 14);
|
|
1037
|
+
if (h < 0 || m < 0) return void 0;
|
|
1038
|
+
i = 16;
|
|
1039
|
+
if (s.charCodeAt(i) === 58) {
|
|
1040
|
+
sec = d2(s, 17);
|
|
1041
|
+
if (sec < 0) return void 0;
|
|
1042
|
+
i = 19;
|
|
1043
|
+
if (s.charCodeAt(i) === 46) {
|
|
1044
|
+
let j = i + 1;
|
|
1045
|
+
let frac = 0;
|
|
1046
|
+
let digits = 0;
|
|
1047
|
+
for (; j < len; j++) {
|
|
1048
|
+
const c = s.charCodeAt(j) - 48;
|
|
1049
|
+
if (c < 0 || c > 9) break;
|
|
1050
|
+
if (digits < 3) frac = frac * 10 + c;
|
|
1051
|
+
digits++;
|
|
1052
|
+
}
|
|
1053
|
+
if (digits === 0 || digits > 9) return void 0;
|
|
1054
|
+
ms = digits === 1 ? frac * 100 : digits === 2 ? frac * 10 : frac;
|
|
1055
|
+
i = j;
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
if (i < len) {
|
|
1059
|
+
const c = s.charCodeAt(i);
|
|
1060
|
+
if ((c === 90 || c === 122) && i === len - 1) {
|
|
1061
|
+
offset = 0;
|
|
1062
|
+
i++;
|
|
1063
|
+
} else if ((c === 43 || c === 45) && len - i === 6 && s.charCodeAt(i + 3) === 58) {
|
|
1064
|
+
const oh = d2(s, i + 1);
|
|
1065
|
+
const om = d2(s, i + 4);
|
|
1066
|
+
if (oh < 0 || om < 0) return void 0;
|
|
1067
|
+
if (oh > 23 || om > 59) return INVALID2;
|
|
1068
|
+
offset = (c === 45 ? -1 : 1) * (oh * 60 + om);
|
|
1069
|
+
i = len;
|
|
1070
|
+
} else {
|
|
1071
|
+
return void 0;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
const wall = { y: yHi * 100 + yLo, M: M - 1, D, h, m, s: sec, ms };
|
|
1076
|
+
if (!validWall(wall)) return INVALID2;
|
|
1077
|
+
if (wall.s === 60) wall.s = 59;
|
|
1078
|
+
return { kind: "wall", wall, offset };
|
|
1079
|
+
}
|
|
1080
|
+
function parseISO(str) {
|
|
1081
|
+
const fast = fastISO(str);
|
|
1082
|
+
if (fast) return fast;
|
|
1083
|
+
const m = ISO_RE.exec(str.trim());
|
|
1084
|
+
if (!m) return void 0;
|
|
1085
|
+
const wall = {
|
|
1086
|
+
y: Number(m[1]),
|
|
1087
|
+
M: m[2] ? Number(m[2]) - 1 : 0,
|
|
1088
|
+
D: m[3] ? Number(m[3]) : 1,
|
|
1089
|
+
h: m[4] ? Number(m[4]) : 0,
|
|
1090
|
+
m: m[5] ? Number(m[5]) : 0,
|
|
1091
|
+
s: m[6] ? Number(m[6]) : 0,
|
|
1092
|
+
ms: m[7] ? Math.floor(Number(`0.${m[7]}`) * 1e3) : 0
|
|
1093
|
+
};
|
|
1094
|
+
if (!validWall(wall)) return INVALID2;
|
|
1095
|
+
if (wall.s === 60) wall.s = 59;
|
|
1096
|
+
const offset = m[8] ? parseOffset(m[8].replace(/^([+-]\d{2})$/, "$1:00")) : void 0;
|
|
1097
|
+
if (m[8] && offset === void 0) return INVALID2;
|
|
1098
|
+
return { kind: "wall", wall, offset, zone: m[9] };
|
|
1099
|
+
}
|
|
1100
|
+
function parseString(str) {
|
|
1101
|
+
const iso = parseISO(str);
|
|
1102
|
+
if (iso) return iso;
|
|
1103
|
+
const native = Date.parse(str);
|
|
1104
|
+
return Number.isNaN(native) ? INVALID2 : { kind: "epoch", ms: native };
|
|
1105
|
+
}
|
|
1106
|
+
var PARSE_TOKEN = /\[([^\]]*)\]|YYYY|YY|MMMM|MMM|MM|M|Do|DD|D|dddd|ddd|dd|HH|H|hh|h|kk|k|mm|m|ss|s|S{1,3}|A|a|ZZ|Z|X|x/g;
|
|
1107
|
+
function escapeRe(s) {
|
|
1108
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1109
|
+
}
|
|
1110
|
+
function namesRe(names) {
|
|
1111
|
+
return `(${[...names].sort((a, b) => b.length - a.length).map(escapeRe).join("|")})`;
|
|
1112
|
+
}
|
|
1113
|
+
var num = (k) => (v, a) => {
|
|
1114
|
+
;
|
|
1115
|
+
a[k] = Number(v);
|
|
1116
|
+
};
|
|
1117
|
+
function parseFormat(input, format, locale) {
|
|
1118
|
+
const ld = localeData(locale);
|
|
1119
|
+
const setters = [];
|
|
1120
|
+
let pattern = "";
|
|
1121
|
+
let last = 0;
|
|
1122
|
+
for (const match of format.matchAll(PARSE_TOKEN)) {
|
|
1123
|
+
pattern += escapeRe(format.slice(last, match.index)).replace(/\s+/g, "\\s+");
|
|
1124
|
+
last = match.index + match[0].length;
|
|
1125
|
+
const tok = match[0];
|
|
1126
|
+
if (match[1] !== void 0) {
|
|
1127
|
+
pattern += escapeRe(match[1]);
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
switch (tok) {
|
|
1131
|
+
case "YYYY":
|
|
1132
|
+
pattern += "([+-]?\\d{4,6})";
|
|
1133
|
+
setters.push(num("y"));
|
|
1134
|
+
break;
|
|
1135
|
+
case "YY":
|
|
1136
|
+
pattern += "(\\d{2})";
|
|
1137
|
+
setters.push((v, a) => {
|
|
1138
|
+
const n = Number(v);
|
|
1139
|
+
a.y = n + (n > 68 ? 1900 : 2e3);
|
|
1140
|
+
});
|
|
1141
|
+
break;
|
|
1142
|
+
case "MMMM":
|
|
1143
|
+
case "MMM": {
|
|
1144
|
+
pattern += namesRe([...ld.months, ...ld.monthsShort]);
|
|
1145
|
+
setters.push((v, a) => {
|
|
1146
|
+
const lv = v.toLowerCase();
|
|
1147
|
+
let i = ld.months.findIndex((n) => n.toLowerCase() === lv);
|
|
1148
|
+
if (i < 0) i = ld.monthsShort.findIndex((n) => n.toLowerCase() === lv);
|
|
1149
|
+
a.M = i + 1;
|
|
1150
|
+
});
|
|
1151
|
+
break;
|
|
1152
|
+
}
|
|
1153
|
+
case "MM":
|
|
1154
|
+
case "M":
|
|
1155
|
+
pattern += tok === "MM" ? "(\\d{2})" : "(\\d{1,2})";
|
|
1156
|
+
setters.push(num("M"));
|
|
1157
|
+
break;
|
|
1158
|
+
case "Do":
|
|
1159
|
+
pattern += "(\\d{1,2})(?:st|nd|rd|th|\\.)?";
|
|
1160
|
+
setters.push(num("D"));
|
|
1161
|
+
break;
|
|
1162
|
+
case "DD":
|
|
1163
|
+
case "D":
|
|
1164
|
+
pattern += tok === "DD" ? "(\\d{2})" : "(\\d{1,2})";
|
|
1165
|
+
setters.push(num("D"));
|
|
1166
|
+
break;
|
|
1167
|
+
case "dddd":
|
|
1168
|
+
case "ddd":
|
|
1169
|
+
case "dd":
|
|
1170
|
+
pattern += namesRe([...ld.weekdays, ...ld.weekdaysShort, ...ld.weekdaysMin]);
|
|
1171
|
+
setters.push(() => {
|
|
1172
|
+
});
|
|
1173
|
+
break;
|
|
1174
|
+
case "HH":
|
|
1175
|
+
case "H":
|
|
1176
|
+
case "hh":
|
|
1177
|
+
case "h":
|
|
1178
|
+
case "kk":
|
|
1179
|
+
case "k":
|
|
1180
|
+
pattern += tok.length === 2 ? "(\\d{2})" : "(\\d{1,2})";
|
|
1181
|
+
setters.push(tok[0] === "k" ? (v, a) => {
|
|
1182
|
+
a.h = Number(v) % 24;
|
|
1183
|
+
} : num("h"));
|
|
1184
|
+
break;
|
|
1185
|
+
case "mm":
|
|
1186
|
+
case "m":
|
|
1187
|
+
pattern += tok === "mm" ? "(\\d{2})" : "(\\d{1,2})";
|
|
1188
|
+
setters.push(num("m"));
|
|
1189
|
+
break;
|
|
1190
|
+
case "ss":
|
|
1191
|
+
case "s":
|
|
1192
|
+
pattern += tok === "ss" ? "(\\d{2})" : "(\\d{1,2})";
|
|
1193
|
+
setters.push(num("s"));
|
|
1194
|
+
break;
|
|
1195
|
+
case "A":
|
|
1196
|
+
case "a":
|
|
1197
|
+
pattern += `(${escapeRe(ld.meridiem[0])}|${escapeRe(ld.meridiem[1])}|am|pm|a\\.m\\.|p\\.m\\.)`;
|
|
1198
|
+
setters.push((v, a) => {
|
|
1199
|
+
const lv = v.toLowerCase();
|
|
1200
|
+
a.pm = lv === ld.meridiem[1].toLowerCase() || lv.startsWith("p");
|
|
1201
|
+
});
|
|
1202
|
+
break;
|
|
1203
|
+
case "ZZ":
|
|
1204
|
+
case "Z":
|
|
1205
|
+
pattern += "(Z|[+-]\\d{2}:?\\d{2})";
|
|
1206
|
+
setters.push((v, a) => {
|
|
1207
|
+
a.offset = parseOffset(v) ?? NaN;
|
|
1208
|
+
});
|
|
1209
|
+
break;
|
|
1210
|
+
case "X":
|
|
1211
|
+
pattern += "(-?\\d+(?:\\.\\d+)?)";
|
|
1212
|
+
setters.push((v, a) => {
|
|
1213
|
+
a.epoch = Number(v) * 1e3;
|
|
1214
|
+
});
|
|
1215
|
+
break;
|
|
1216
|
+
case "x":
|
|
1217
|
+
pattern += "(-?\\d+)";
|
|
1218
|
+
setters.push((v, a) => {
|
|
1219
|
+
a.epoch = Number(v);
|
|
1220
|
+
});
|
|
1221
|
+
break;
|
|
1222
|
+
default:
|
|
1223
|
+
pattern += `(\\d{1,${tok.length}})`;
|
|
1224
|
+
setters.push((v, a) => {
|
|
1225
|
+
a.ms = Math.round(Number(`0.${v}`) * 1e3);
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
pattern += escapeRe(format.slice(last)).replace(/\s+/g, "\\s+");
|
|
1230
|
+
const m = new RegExp(`^\\s*${pattern}\\s*$`, "i").exec(input);
|
|
1231
|
+
if (!m) return INVALID2;
|
|
1232
|
+
const acc = {};
|
|
1233
|
+
setters.forEach((set, i) => set(m[i + 1], acc));
|
|
1234
|
+
if (acc.epoch !== void 0) return { kind: "epoch", ms: acc.epoch };
|
|
1235
|
+
let h = acc.h ?? 0;
|
|
1236
|
+
if (acc.pm !== void 0) {
|
|
1237
|
+
if (h < 1 || h > 12) return INVALID2;
|
|
1238
|
+
if (acc.pm && h < 12) h += 12;
|
|
1239
|
+
if (!acc.pm && h === 12) h = 0;
|
|
1240
|
+
}
|
|
1241
|
+
const now = /* @__PURE__ */ new Date();
|
|
1242
|
+
const wall = {
|
|
1243
|
+
y: acc.y ?? now.getFullYear(),
|
|
1244
|
+
M: acc.M !== void 0 ? acc.M - 1 : acc.y !== void 0 ? 0 : now.getMonth(),
|
|
1245
|
+
D: acc.D ?? 1,
|
|
1246
|
+
h,
|
|
1247
|
+
m: acc.m ?? 0,
|
|
1248
|
+
s: acc.s ?? 0,
|
|
1249
|
+
ms: acc.ms ?? 0
|
|
1250
|
+
};
|
|
1251
|
+
if (!validWall(wall)) return INVALID2;
|
|
1252
|
+
return { kind: "wall", wall, offset: acc.offset };
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// src/neram.ts
|
|
1256
|
+
var config = { locale: "en", zone: "local", normalizeSpaces: true };
|
|
1257
|
+
var intlCache = /* @__PURE__ */ new Map();
|
|
1258
|
+
var Neram = class _Neram {
|
|
1259
|
+
#ms;
|
|
1260
|
+
#zone;
|
|
1261
|
+
#locale;
|
|
1262
|
+
#weekStart;
|
|
1263
|
+
#wallCache;
|
|
1264
|
+
#offCache;
|
|
1265
|
+
/** @internal Use `neram()` instead. */
|
|
1266
|
+
constructor(ms, zone, locale, weekStart) {
|
|
1267
|
+
this.#ms = Number.isFinite(ms) && Math.abs(ms) <= 864e13 ? ms : NaN;
|
|
1268
|
+
this.#zone = zone;
|
|
1269
|
+
this.#locale = locale;
|
|
1270
|
+
this.#weekStart = weekStart;
|
|
1271
|
+
}
|
|
1272
|
+
#make(ms, zone = this.#zone, locale = this.#locale, weekStart = this.#weekStart) {
|
|
1273
|
+
return new _Neram(ms, zone, locale, weekStart);
|
|
1274
|
+
}
|
|
1275
|
+
#offset() {
|
|
1276
|
+
return this.#offCache ??= this.isValid() ? offsetAt(this.#ms, this.#zone) : NaN;
|
|
1277
|
+
}
|
|
1278
|
+
#wall() {
|
|
1279
|
+
if (!this.#wallCache) {
|
|
1280
|
+
this.#wallCache = this.isValid() ? wallFromEpoch(this.#ms, this.#offset()) : { y: NaN, M: NaN, D: NaN, h: NaN, m: NaN, s: NaN, ms: NaN, dow: NaN };
|
|
1281
|
+
}
|
|
1282
|
+
return this.#wallCache;
|
|
1283
|
+
}
|
|
1284
|
+
#fromWall(w) {
|
|
1285
|
+
return this.#make(wallToEpoch(w, this.#zone));
|
|
1286
|
+
}
|
|
1287
|
+
#weekStartDay() {
|
|
1288
|
+
return this.#weekStart ?? config.weekStart ?? localeData(this.#locale).week.firstDay;
|
|
1289
|
+
}
|
|
1290
|
+
#addMonths(n) {
|
|
1291
|
+
const w = this.#wall();
|
|
1292
|
+
const total = w.y * 12 + w.M + roundCalendar(n);
|
|
1293
|
+
const y = Math.floor(total / 12);
|
|
1294
|
+
const M = total - y * 12;
|
|
1295
|
+
return this.#fromWall({ ...w, y, M, D: Math.min(w.D, daysInMonth(y, M)) });
|
|
1296
|
+
}
|
|
1297
|
+
#addDays(n) {
|
|
1298
|
+
const w = this.#wall();
|
|
1299
|
+
return this.#fromWall({ ...w, D: w.D + roundCalendar(n) });
|
|
1300
|
+
}
|
|
1301
|
+
#intl(opts, locale = this.#locale) {
|
|
1302
|
+
if (!this.isValid()) return "Invalid Date";
|
|
1303
|
+
const fixed = isFixedZone(this.#zone);
|
|
1304
|
+
const timeZone = this.#zone === "local" ? void 0 : fixed ? "UTC" : this.#zone;
|
|
1305
|
+
const key = `${locale}|${timeZone}|${JSON.stringify(opts)}`;
|
|
1306
|
+
let f = intlCache.get(key);
|
|
1307
|
+
if (!f) {
|
|
1308
|
+
f = new Intl.DateTimeFormat(locale, { ...opts, timeZone });
|
|
1309
|
+
intlCache.set(key, f);
|
|
1310
|
+
}
|
|
1311
|
+
const out = clean(f.format(fixed ? this.#ms + this.#offset() * 6e4 : this.#ms));
|
|
1312
|
+
return fixed && opts.timeZoneName ? out.replace(/UTC|GMT/, `GMT${formatOffset(this.#offset())}`) : out;
|
|
1313
|
+
}
|
|
1314
|
+
// -- validity & identity ---------------------------------------------------
|
|
1315
|
+
isValid() {
|
|
1316
|
+
return !Number.isNaN(this.#ms);
|
|
1317
|
+
}
|
|
1318
|
+
clone() {
|
|
1319
|
+
return this;
|
|
1320
|
+
}
|
|
1321
|
+
// -- getters / setters -----------------------------------------------------
|
|
1322
|
+
get(unit) {
|
|
1323
|
+
const w = this.#wall();
|
|
1324
|
+
switch (normalizeUnit(unit)) {
|
|
1325
|
+
case "year":
|
|
1326
|
+
return w.y;
|
|
1327
|
+
case "quarter":
|
|
1328
|
+
return Math.floor(w.M / 3) + 1;
|
|
1329
|
+
case "month":
|
|
1330
|
+
return w.M;
|
|
1331
|
+
case "week":
|
|
1332
|
+
return this.week();
|
|
1333
|
+
case "isoWeek":
|
|
1334
|
+
return this.isoWeek();
|
|
1335
|
+
case "date":
|
|
1336
|
+
return w.D;
|
|
1337
|
+
case "day":
|
|
1338
|
+
return w.dow;
|
|
1339
|
+
case "hour":
|
|
1340
|
+
return w.h;
|
|
1341
|
+
case "minute":
|
|
1342
|
+
return w.m;
|
|
1343
|
+
case "second":
|
|
1344
|
+
return w.s;
|
|
1345
|
+
default:
|
|
1346
|
+
return w.ms;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
set(unitOrValues, value) {
|
|
1350
|
+
if (!this.isValid()) return this;
|
|
1351
|
+
if (typeof unitOrValues === "object") {
|
|
1352
|
+
let r = this;
|
|
1353
|
+
const order = ["year", "month", "date", "day", "hour", "minute", "second", "millisecond"];
|
|
1354
|
+
for (const k of order) {
|
|
1355
|
+
const v2 = unitOrValues[k];
|
|
1356
|
+
if (v2 !== void 0) r = r.set(k, v2);
|
|
1357
|
+
}
|
|
1358
|
+
return r;
|
|
1359
|
+
}
|
|
1360
|
+
const v = value;
|
|
1361
|
+
const w = { ...this.#wall() };
|
|
1362
|
+
switch (normalizeUnit(unitOrValues)) {
|
|
1363
|
+
case "year":
|
|
1364
|
+
return this.#fromWall({ ...w, y: v, D: Math.min(w.D, daysInMonth(v, w.M)) });
|
|
1365
|
+
case "quarter":
|
|
1366
|
+
return this.#addMonths((v - (Math.floor(w.M / 3) + 1)) * 3);
|
|
1367
|
+
case "month": {
|
|
1368
|
+
const y = w.y + Math.floor(v / 12);
|
|
1369
|
+
const M = (v % 12 + 12) % 12;
|
|
1370
|
+
return this.#fromWall({ ...w, y, M, D: Math.min(w.D, daysInMonth(y, M)) });
|
|
1371
|
+
}
|
|
1372
|
+
case "week":
|
|
1373
|
+
return this.#addDays((v - this.week()) * 7);
|
|
1374
|
+
case "isoWeek":
|
|
1375
|
+
return this.#addDays((v - this.isoWeek()) * 7);
|
|
1376
|
+
case "date":
|
|
1377
|
+
return this.#fromWall({ ...w, D: v });
|
|
1378
|
+
case "day":
|
|
1379
|
+
return this.#addDays(v - w.dow);
|
|
1380
|
+
case "hour":
|
|
1381
|
+
return this.#fromWall({ ...w, h: v });
|
|
1382
|
+
case "minute":
|
|
1383
|
+
return this.#fromWall({ ...w, m: v });
|
|
1384
|
+
case "second":
|
|
1385
|
+
return this.#fromWall({ ...w, s: v });
|
|
1386
|
+
default:
|
|
1387
|
+
return this.#fromWall({ ...w, ms: v });
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
year(v) {
|
|
1391
|
+
return v === void 0 ? this.get("year") : this.set("year", v);
|
|
1392
|
+
}
|
|
1393
|
+
month(v) {
|
|
1394
|
+
return v === void 0 ? this.get("month") : this.set("month", v);
|
|
1395
|
+
}
|
|
1396
|
+
date(v) {
|
|
1397
|
+
return v === void 0 ? this.get("date") : this.set("date", v);
|
|
1398
|
+
}
|
|
1399
|
+
day(v) {
|
|
1400
|
+
return v === void 0 ? this.get("day") : this.set("day", v);
|
|
1401
|
+
}
|
|
1402
|
+
hour(v) {
|
|
1403
|
+
return v === void 0 ? this.get("hour") : this.set("hour", v);
|
|
1404
|
+
}
|
|
1405
|
+
minute(v) {
|
|
1406
|
+
return v === void 0 ? this.get("minute") : this.set("minute", v);
|
|
1407
|
+
}
|
|
1408
|
+
second(v) {
|
|
1409
|
+
return v === void 0 ? this.get("second") : this.set("second", v);
|
|
1410
|
+
}
|
|
1411
|
+
millisecond(v) {
|
|
1412
|
+
return v === void 0 ? this.get("millisecond") : this.set("millisecond", v);
|
|
1413
|
+
}
|
|
1414
|
+
quarter(v) {
|
|
1415
|
+
return v === void 0 ? this.get("quarter") : this.set("quarter", v);
|
|
1416
|
+
}
|
|
1417
|
+
dayOfYear(v) {
|
|
1418
|
+
const w = this.#wall();
|
|
1419
|
+
const doy = dayOfYear(w.y, w.M, w.D);
|
|
1420
|
+
return v === void 0 ? doy : this.#addDays(v - doy);
|
|
1421
|
+
}
|
|
1422
|
+
isoWeekday(v) {
|
|
1423
|
+
const iso = this.#wall().dow || 7;
|
|
1424
|
+
return v === void 0 ? iso : this.#addDays(v - iso);
|
|
1425
|
+
}
|
|
1426
|
+
weekday(v) {
|
|
1427
|
+
const wd = (this.#wall().dow - this.#weekStartDay() + 7) % 7;
|
|
1428
|
+
return v === void 0 ? wd : this.#addDays(v - wd);
|
|
1429
|
+
}
|
|
1430
|
+
/** Locale week of the year (uses the locale's first day and minimal-days rule). */
|
|
1431
|
+
week() {
|
|
1432
|
+
const w = this.#wall();
|
|
1433
|
+
return weekOfYear(w.y, w.M, w.D, this.#weekStartDay(), localeData(this.#locale).week.minimalDays).week;
|
|
1434
|
+
}
|
|
1435
|
+
weekYear() {
|
|
1436
|
+
const w = this.#wall();
|
|
1437
|
+
return weekOfYear(w.y, w.M, w.D, this.#weekStartDay(), localeData(this.#locale).week.minimalDays).year;
|
|
1438
|
+
}
|
|
1439
|
+
/** ISO 8601 week number, 1-53. */
|
|
1440
|
+
isoWeek() {
|
|
1441
|
+
const w = this.#wall();
|
|
1442
|
+
return weekOfYear(w.y, w.M, w.D, 1, 4).week;
|
|
1443
|
+
}
|
|
1444
|
+
isoWeekYear() {
|
|
1445
|
+
const w = this.#wall();
|
|
1446
|
+
return weekOfYear(w.y, w.M, w.D, 1, 4).year;
|
|
1447
|
+
}
|
|
1448
|
+
isoWeeksInYear() {
|
|
1449
|
+
return weeksInYear(this.year(), 1, 4);
|
|
1450
|
+
}
|
|
1451
|
+
daysInMonth() {
|
|
1452
|
+
const w = this.#wall();
|
|
1453
|
+
return daysInMonth(w.y, w.M);
|
|
1454
|
+
}
|
|
1455
|
+
isLeapYear() {
|
|
1456
|
+
return isLeapYear(this.year());
|
|
1457
|
+
}
|
|
1458
|
+
add(amount, unit) {
|
|
1459
|
+
if (!this.isValid()) return this;
|
|
1460
|
+
if (typeof amount !== "number") {
|
|
1461
|
+
const dur = isDuration(amount) ? amount : duration(amount);
|
|
1462
|
+
if (!dur.isValid()) return this.#make(NaN);
|
|
1463
|
+
const o = dur.toObject();
|
|
1464
|
+
let r = this;
|
|
1465
|
+
if (o.years || o.months) r = r.#addMonths(o.years * 12 + o.months);
|
|
1466
|
+
if (o.weeks || o.days) r = r.#addDays(o.weeks * 7 + o.days);
|
|
1467
|
+
const exact = o.hours * 36e5 + o.minutes * 6e4 + o.seconds * 1e3 + o.milliseconds;
|
|
1468
|
+
return exact ? r.#make(r.#ms + exact) : r;
|
|
1469
|
+
}
|
|
1470
|
+
const u = normalizeUnit(unit ?? "millisecond");
|
|
1471
|
+
switch (u) {
|
|
1472
|
+
case "year":
|
|
1473
|
+
return this.#addMonths(amount * 12);
|
|
1474
|
+
case "quarter":
|
|
1475
|
+
return this.#addMonths(amount * 3);
|
|
1476
|
+
case "month":
|
|
1477
|
+
return this.#addMonths(amount);
|
|
1478
|
+
case "week":
|
|
1479
|
+
case "isoWeek":
|
|
1480
|
+
return this.#addDays(amount * 7);
|
|
1481
|
+
case "day":
|
|
1482
|
+
case "date":
|
|
1483
|
+
return this.#addDays(amount);
|
|
1484
|
+
default:
|
|
1485
|
+
return this.#make(this.#ms + amount * (FIXED_MS[u] ?? 1));
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
subtract(amount, unit) {
|
|
1489
|
+
if (typeof amount === "number") return this.add(-amount, unit);
|
|
1490
|
+
return this.add((isDuration(amount) ? amount : duration(amount)).negate());
|
|
1491
|
+
}
|
|
1492
|
+
startOf(unit) {
|
|
1493
|
+
if (!this.isValid()) return this;
|
|
1494
|
+
const u = normalizeUnit(unit);
|
|
1495
|
+
const w = { ...this.#wall() };
|
|
1496
|
+
const midnight = { h: 0, m: 0, s: 0, ms: 0 };
|
|
1497
|
+
switch (u) {
|
|
1498
|
+
case "year":
|
|
1499
|
+
return this.#fromWall({ ...w, ...midnight, M: 0, D: 1 });
|
|
1500
|
+
case "quarter":
|
|
1501
|
+
return this.#fromWall({ ...w, ...midnight, M: Math.floor(w.M / 3) * 3, D: 1 });
|
|
1502
|
+
case "month":
|
|
1503
|
+
return this.#fromWall({ ...w, ...midnight, D: 1 });
|
|
1504
|
+
case "week":
|
|
1505
|
+
return this.#fromWall({ ...w, ...midnight, D: w.D - (w.dow - this.#weekStartDay() + 7) % 7 });
|
|
1506
|
+
case "isoWeek":
|
|
1507
|
+
return this.#fromWall({ ...w, ...midnight, D: w.D - (w.dow + 6) % 7 });
|
|
1508
|
+
case "day":
|
|
1509
|
+
case "date":
|
|
1510
|
+
return this.#fromWall({ ...w, ...midnight });
|
|
1511
|
+
// Sub-day units: trim elapsed time so repeated DST hours stay on the right side.
|
|
1512
|
+
case "hour":
|
|
1513
|
+
return this.#make(this.#ms - (w.m * 6e4 + w.s * 1e3 + w.ms));
|
|
1514
|
+
case "minute":
|
|
1515
|
+
return this.#make(this.#ms - (w.s * 1e3 + w.ms));
|
|
1516
|
+
case "second":
|
|
1517
|
+
return this.#make(this.#ms - w.ms);
|
|
1518
|
+
default:
|
|
1519
|
+
return this;
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
endOf(unit) {
|
|
1523
|
+
const u = normalizeUnit(unit);
|
|
1524
|
+
if (u === "millisecond") return this;
|
|
1525
|
+
return this.startOf(u).add(1, u === "date" ? "day" : u).subtract(1, "millisecond");
|
|
1526
|
+
}
|
|
1527
|
+
/** Round to the nearest unit boundary: neram('10:40').round('hour') → 11:00. */
|
|
1528
|
+
round(unit) {
|
|
1529
|
+
if (!this.isValid()) return this;
|
|
1530
|
+
const u = normalizeUnit(unit);
|
|
1531
|
+
const start = this.startOf(u);
|
|
1532
|
+
const next = start.add(1, u === "date" ? "day" : u);
|
|
1533
|
+
return this.#ms - start.#ms < next.#ms - this.#ms ? start : next;
|
|
1534
|
+
}
|
|
1535
|
+
/** Keep the date inside [min, max]. */
|
|
1536
|
+
clamp(min, max) {
|
|
1537
|
+
const lo = this.#coerce(min);
|
|
1538
|
+
const hi = this.#coerce(max);
|
|
1539
|
+
if (this.#ms < lo.#ms) return lo.tz(this.#zone);
|
|
1540
|
+
if (this.#ms > hi.#ms) return hi.tz(this.#zone);
|
|
1541
|
+
return this;
|
|
1542
|
+
}
|
|
1543
|
+
// -- zones -----------------------------------------------------------------
|
|
1544
|
+
/**
|
|
1545
|
+
* The same instant in another zone, or (keepLocalTime) the same wall time in another zone.
|
|
1546
|
+
* neram('2026-12-04T09:00Z').tz('Asia/Kolkata').format('HH:mm') → "14:30"
|
|
1547
|
+
*/
|
|
1548
|
+
tz(zone, keepLocalTime = false) {
|
|
1549
|
+
const z = normalizeZone(zone);
|
|
1550
|
+
if (!this.isValid()) return this.#make(NaN, z);
|
|
1551
|
+
if (keepLocalTime) return this.#make(wallToEpoch(this.#wall(), z), z);
|
|
1552
|
+
return this.#make(this.#ms, z);
|
|
1553
|
+
}
|
|
1554
|
+
utc(keepLocalTime = false) {
|
|
1555
|
+
return this.tz("UTC", keepLocalTime);
|
|
1556
|
+
}
|
|
1557
|
+
local(keepLocalTime = false) {
|
|
1558
|
+
return this.tz("local", keepLocalTime);
|
|
1559
|
+
}
|
|
1560
|
+
/** The IANA name (or "UTC" / "+05:30") of this instance's zone. */
|
|
1561
|
+
timezone() {
|
|
1562
|
+
return this.#zone === "local" ? guessZone() : this.#zone;
|
|
1563
|
+
}
|
|
1564
|
+
isUTC() {
|
|
1565
|
+
return this.#zone === "UTC";
|
|
1566
|
+
}
|
|
1567
|
+
isLocal() {
|
|
1568
|
+
return this.#zone === "local";
|
|
1569
|
+
}
|
|
1570
|
+
utcOffset(offset, keepLocalTime = false) {
|
|
1571
|
+
if (offset === void 0) return this.#offset();
|
|
1572
|
+
let mins;
|
|
1573
|
+
if (typeof offset === "string") {
|
|
1574
|
+
mins = normalizeZone(offset) === "UTC" ? 0 : Number.NaN;
|
|
1575
|
+
const m = /^([+-])(\d{2}):?(\d{2})$/.exec(offset);
|
|
1576
|
+
if (m) mins = (m[1] === "-" ? -1 : 1) * (Number(m[2]) * 60 + Number(m[3]));
|
|
1577
|
+
} else {
|
|
1578
|
+
mins = Math.abs(offset) < 16 ? offset * 60 : offset;
|
|
1579
|
+
}
|
|
1580
|
+
if (Number.isNaN(mins)) throw new RangeError(`neram: invalid offset "${offset}"`);
|
|
1581
|
+
return this.tz(mins === 0 ? "UTC" : formatOffset(mins), keepLocalTime);
|
|
1582
|
+
}
|
|
1583
|
+
/** True when daylight-saving time is in effect in this instance's zone. */
|
|
1584
|
+
isDST() {
|
|
1585
|
+
if (!this.isValid()) return false;
|
|
1586
|
+
const y = this.year();
|
|
1587
|
+
const jan = offsetAt(Date.UTC(y, 0, 1), this.#zone);
|
|
1588
|
+
const jul = offsetAt(Date.UTC(y, 6, 1), this.#zone);
|
|
1589
|
+
return jan !== jul && this.#offset() === Math.max(jan, jul);
|
|
1590
|
+
}
|
|
1591
|
+
// -- locale ----------------------------------------------------------------
|
|
1592
|
+
/** Month and weekday names and week rules for this value's locale. Day.js-compatible. */
|
|
1593
|
+
localeData() {
|
|
1594
|
+
return publicLocaleData(this.#locale);
|
|
1595
|
+
}
|
|
1596
|
+
locale(locale) {
|
|
1597
|
+
if (locale === void 0) return this.#locale;
|
|
1598
|
+
return this.#make(this.#ms, this.#zone, canonicalLocale(locale));
|
|
1599
|
+
}
|
|
1600
|
+
weekStart(day) {
|
|
1601
|
+
if (day === void 0) return this.#weekStartDay();
|
|
1602
|
+
return this.#make(this.#ms, this.#zone, this.#locale, day);
|
|
1603
|
+
}
|
|
1604
|
+
// -- display ---------------------------------------------------------------
|
|
1605
|
+
/**
|
|
1606
|
+
* Format with Day.js tokens, plus:
|
|
1607
|
+
* W/WW ISO week · GGGG ISO week-year · E ISO weekday · Qo ordinal quarter
|
|
1608
|
+
* z/zzz zone name · S…SSSSSSSSS fraction · L LL LLL LLLL LT LTS (via Intl)
|
|
1609
|
+
*/
|
|
1610
|
+
format(fmt = "YYYY-MM-DDTHH:mm:ssZ") {
|
|
1611
|
+
if (!this.isValid()) return "Invalid Date";
|
|
1612
|
+
return formatDate(
|
|
1613
|
+
{
|
|
1614
|
+
wall: this.#wall(),
|
|
1615
|
+
ms: this.#ms,
|
|
1616
|
+
offset: this.#offset(),
|
|
1617
|
+
locale: this.#locale,
|
|
1618
|
+
weekStart: this.#weekStartDay(),
|
|
1619
|
+
intl: (o) => this.#intl(o)
|
|
1620
|
+
},
|
|
1621
|
+
fmt
|
|
1622
|
+
);
|
|
1623
|
+
}
|
|
1624
|
+
/** Format with Intl options: neram().toLocaleString({ dateStyle: 'full' }, 'ta') */
|
|
1625
|
+
toLocaleString(opts = { dateStyle: "medium", timeStyle: "medium" }, locale) {
|
|
1626
|
+
return this.#intl(opts, locale ? canonicalLocale(locale) : this.#locale);
|
|
1627
|
+
}
|
|
1628
|
+
/** "in 3 hours", "yesterday", "2 months ago" — relative to another date. */
|
|
1629
|
+
from(other, opts = {}) {
|
|
1630
|
+
if (!this.isValid()) return "Invalid Date";
|
|
1631
|
+
const that = this.#coerce(other);
|
|
1632
|
+
if (!that.isValid()) return "Invalid Date";
|
|
1633
|
+
const ms = this.#ms - that.#ms;
|
|
1634
|
+
const { value, unit } = pickUnit(ms, this.diff(that, "month", true));
|
|
1635
|
+
return formatRelative(value, unit, opts.locale ?? this.#locale, opts);
|
|
1636
|
+
}
|
|
1637
|
+
fromNow(opts = {}) {
|
|
1638
|
+
return this.from(this.#make(Date.now()), opts);
|
|
1639
|
+
}
|
|
1640
|
+
to(other, opts = {}) {
|
|
1641
|
+
if (!this.isValid()) return "Invalid Date";
|
|
1642
|
+
return this.#coerce(other).locale(this.#locale).from(this, opts);
|
|
1643
|
+
}
|
|
1644
|
+
toNow(opts = {}) {
|
|
1645
|
+
return this.to(this.#make(Date.now()), opts);
|
|
1646
|
+
}
|
|
1647
|
+
/** "Today at 3:00 PM", "Tomorrow at 9:00 AM", "Last Friday at …", or a date. */
|
|
1648
|
+
calendar(reference, opts = {}) {
|
|
1649
|
+
if (!this.isValid()) return "Invalid Date";
|
|
1650
|
+
const ref = (reference === void 0 ? this.#make(Date.now()) : this.#coerce(reference)).tz(this.#zone);
|
|
1651
|
+
const days = this.startOf("day").diff(ref.startOf("day"), "day");
|
|
1652
|
+
const en = this.#locale.toLowerCase().startsWith("en");
|
|
1653
|
+
const at = en ? " at " : " ";
|
|
1654
|
+
const time = this.format("LT");
|
|
1655
|
+
const cap = (s) => s.charAt(0).toLocaleUpperCase(this.#locale) + s.slice(1);
|
|
1656
|
+
if (Math.abs(days) <= 1) return cap(formatRelative(days, "day", this.#locale)) + at + time;
|
|
1657
|
+
const wd = localeData(this.#locale).weekdays[this.day()];
|
|
1658
|
+
if (days > 1 && days < 7) return cap(wd) + at + time;
|
|
1659
|
+
if (days < -1 && days > -7) return (en ? `Last ${wd}` : cap(wd)) + at + time;
|
|
1660
|
+
return this.format(opts.sameElse ?? "L");
|
|
1661
|
+
}
|
|
1662
|
+
// -- comparison ------------------------------------------------------------
|
|
1663
|
+
#coerce(input) {
|
|
1664
|
+
return input instanceof _Neram ? input : neram(input, { zone: this.#zone, locale: this.#locale });
|
|
1665
|
+
}
|
|
1666
|
+
/**
|
|
1667
|
+
* Difference in a unit, truncated toward zero unless `float`.
|
|
1668
|
+
* Months and years are calendar-aware; days and weeks count local calendar days (DST-safe).
|
|
1669
|
+
*/
|
|
1670
|
+
diff(other, unit = "millisecond", float = false) {
|
|
1671
|
+
const that = this.#coerce(other);
|
|
1672
|
+
if (!this.isValid() || !that.isValid()) return NaN;
|
|
1673
|
+
const u = normalizeUnit(unit);
|
|
1674
|
+
const wallDelta = this.#ms + this.#offset() * 6e4 - (that.#ms + that.#offset() * 6e4);
|
|
1675
|
+
let r;
|
|
1676
|
+
switch (u) {
|
|
1677
|
+
case "year":
|
|
1678
|
+
r = monthDiff(this, that) / 12;
|
|
1679
|
+
break;
|
|
1680
|
+
case "quarter":
|
|
1681
|
+
r = monthDiff(this, that) / 3;
|
|
1682
|
+
break;
|
|
1683
|
+
case "month":
|
|
1684
|
+
r = monthDiff(this, that);
|
|
1685
|
+
break;
|
|
1686
|
+
case "week":
|
|
1687
|
+
case "isoWeek":
|
|
1688
|
+
r = wallDelta / MS_WEEK;
|
|
1689
|
+
break;
|
|
1690
|
+
case "day":
|
|
1691
|
+
case "date":
|
|
1692
|
+
r = wallDelta / MS_DAY;
|
|
1693
|
+
break;
|
|
1694
|
+
default:
|
|
1695
|
+
r = (this.#ms - that.#ms) / (FIXED_MS[u] ?? 1);
|
|
1696
|
+
}
|
|
1697
|
+
return float ? r : Math.trunc(r) || 0;
|
|
1698
|
+
}
|
|
1699
|
+
/** Whole Duration between two dates, calendar-correct: 1 year 2 months 3 days … */
|
|
1700
|
+
durationTo(other) {
|
|
1701
|
+
const that = this.#coerce(other);
|
|
1702
|
+
if (!this.isValid() || !that.isValid()) return duration(NaN);
|
|
1703
|
+
const sign = that.#ms < this.#ms ? -1 : 1;
|
|
1704
|
+
let [a, b] = sign > 0 ? [this, that] : [that, this];
|
|
1705
|
+
const months = Math.trunc(monthDiff(b, a));
|
|
1706
|
+
a = a.add(months, "month");
|
|
1707
|
+
const days = Math.trunc(b.diff(a, "day"));
|
|
1708
|
+
a = a.add(days, "day");
|
|
1709
|
+
const rest = b.#ms - a.#ms;
|
|
1710
|
+
return duration({ months: sign * months, days: sign * days, milliseconds: sign * rest }).normalize();
|
|
1711
|
+
}
|
|
1712
|
+
isBefore(other, unit) {
|
|
1713
|
+
const that = this.#coerce(other);
|
|
1714
|
+
return unit ? this.endOf(unit).#ms < that.#ms : this.#ms < that.#ms;
|
|
1715
|
+
}
|
|
1716
|
+
isAfter(other, unit) {
|
|
1717
|
+
const that = this.#coerce(other);
|
|
1718
|
+
return unit ? this.startOf(unit).#ms > that.#ms : this.#ms > that.#ms;
|
|
1719
|
+
}
|
|
1720
|
+
isSame(other, unit) {
|
|
1721
|
+
const that = this.#coerce(other);
|
|
1722
|
+
if (!unit) return this.#ms === that.#ms;
|
|
1723
|
+
return this.startOf(unit).#ms <= that.#ms && that.#ms <= this.endOf(unit).#ms;
|
|
1724
|
+
}
|
|
1725
|
+
isSameOrBefore(other, unit) {
|
|
1726
|
+
return this.isSame(other, unit) || this.isBefore(other, unit);
|
|
1727
|
+
}
|
|
1728
|
+
isSameOrAfter(other, unit) {
|
|
1729
|
+
return this.isSame(other, unit) || this.isAfter(other, unit);
|
|
1730
|
+
}
|
|
1731
|
+
isBetween(a, b, unit, inclusivity = "()") {
|
|
1732
|
+
const [lo, hi] = [this.#coerce(a), this.#coerce(b)].sort((x, y) => x.#ms - y.#ms);
|
|
1733
|
+
const left = inclusivity[0] === "[" ? this.isSameOrAfter(lo, unit) : this.isAfter(lo, unit);
|
|
1734
|
+
const right = inclusivity[1] === "]" ? this.isSameOrBefore(hi, unit) : this.isBefore(hi, unit);
|
|
1735
|
+
return left && right;
|
|
1736
|
+
}
|
|
1737
|
+
isToday() {
|
|
1738
|
+
return this.isSame(this.#make(Date.now()), "day");
|
|
1739
|
+
}
|
|
1740
|
+
isTomorrow() {
|
|
1741
|
+
return this.isSame(this.#make(Date.now()).add(1, "day"), "day");
|
|
1742
|
+
}
|
|
1743
|
+
isYesterday() {
|
|
1744
|
+
return this.isSame(this.#make(Date.now()).subtract(1, "day"), "day");
|
|
1745
|
+
}
|
|
1746
|
+
isPast() {
|
|
1747
|
+
return this.#ms < Date.now();
|
|
1748
|
+
}
|
|
1749
|
+
isFuture() {
|
|
1750
|
+
return this.#ms > Date.now();
|
|
1751
|
+
}
|
|
1752
|
+
// -- business days ---------------------------------------------------------
|
|
1753
|
+
isWeekend(weekend) {
|
|
1754
|
+
return (weekend === void 0 ? localeData(this.#locale).week.weekend : checkWeekend(weekend)).includes(this.day());
|
|
1755
|
+
}
|
|
1756
|
+
isBusinessDay(opts = {}) {
|
|
1757
|
+
if (this.isWeekend(opts.weekend)) return false;
|
|
1758
|
+
if (!opts.holidays?.length) return true;
|
|
1759
|
+
return !this.#holidaySet(opts).has(this.format("YYYY-MM-DD"));
|
|
1760
|
+
}
|
|
1761
|
+
/** Step over weekends and holidays. Negative n goes backwards. */
|
|
1762
|
+
addBusinessDays(n, opts = {}) {
|
|
1763
|
+
if (!Number.isInteger(n)) throw new RangeError(`neram: business days must be a whole number, got ${n}`);
|
|
1764
|
+
if (opts.weekend !== void 0) checkWeekend(opts.weekend);
|
|
1765
|
+
const set = this.#holidaySet(opts);
|
|
1766
|
+
if (!this.isValid()) return this;
|
|
1767
|
+
const step = n < 0 ? -1 : 1;
|
|
1768
|
+
let left = Math.abs(n);
|
|
1769
|
+
let r = this;
|
|
1770
|
+
while (left > 0) {
|
|
1771
|
+
r = r.#addDays(step);
|
|
1772
|
+
if (!r.isWeekend(opts.weekend) && !set.has(r.format("YYYY-MM-DD"))) left--;
|
|
1773
|
+
}
|
|
1774
|
+
return r;
|
|
1775
|
+
}
|
|
1776
|
+
subtractBusinessDays(n, opts = {}) {
|
|
1777
|
+
return this.addBusinessDays(-n, opts);
|
|
1778
|
+
}
|
|
1779
|
+
/** Business days from this date (exclusive) to `other` (inclusive); negative if earlier. */
|
|
1780
|
+
businessDaysUntil(other, opts = {}) {
|
|
1781
|
+
if (opts.weekend !== void 0) checkWeekend(opts.weekend);
|
|
1782
|
+
const target = this.#coerce(other);
|
|
1783
|
+
if (!this.isValid() || !target.isValid()) return NaN;
|
|
1784
|
+
const that = target.tz(this.#zone).startOf("day");
|
|
1785
|
+
let cur = this.startOf("day");
|
|
1786
|
+
const step = that.#ms < cur.#ms ? -1 : 1;
|
|
1787
|
+
const set = this.#holidaySet(opts);
|
|
1788
|
+
let count = 0;
|
|
1789
|
+
while (step > 0 ? cur.#ms < that.#ms : cur.#ms > that.#ms) {
|
|
1790
|
+
cur = cur.#addDays(step);
|
|
1791
|
+
if (!cur.isWeekend(opts.weekend) && !set.has(cur.format("YYYY-MM-DD"))) count += step;
|
|
1792
|
+
}
|
|
1793
|
+
return count;
|
|
1794
|
+
}
|
|
1795
|
+
#holidaySet(opts) {
|
|
1796
|
+
return new Set(
|
|
1797
|
+
(opts.holidays ?? []).map((h) => {
|
|
1798
|
+
const d = this.#coerce(h);
|
|
1799
|
+
if (!d.isValid()) throw new RangeError(`neram: invalid holiday ${JSON.stringify(h)}`);
|
|
1800
|
+
return d.tz(this.#zone).format("YYYY-MM-DD");
|
|
1801
|
+
})
|
|
1802
|
+
);
|
|
1803
|
+
}
|
|
1804
|
+
// -- conversion ------------------------------------------------------------
|
|
1805
|
+
valueOf() {
|
|
1806
|
+
return this.#ms;
|
|
1807
|
+
}
|
|
1808
|
+
/** Seconds since the epoch. */
|
|
1809
|
+
unix() {
|
|
1810
|
+
return Math.floor(this.#ms / 1e3);
|
|
1811
|
+
}
|
|
1812
|
+
toDate() {
|
|
1813
|
+
return new Date(this.#ms);
|
|
1814
|
+
}
|
|
1815
|
+
toISOString() {
|
|
1816
|
+
return new Date(this.#ms).toISOString();
|
|
1817
|
+
}
|
|
1818
|
+
/**
|
|
1819
|
+
* RFC 9557 / Temporal string with the zone kept:
|
|
1820
|
+
* "2026-12-04T14:30:00+05:30[Asia/Kolkata]"
|
|
1821
|
+
*/
|
|
1822
|
+
toZonedISOString() {
|
|
1823
|
+
if (!this.isValid()) return "Invalid Date";
|
|
1824
|
+
const frac = this.#wall().ms ? ".SSS" : "";
|
|
1825
|
+
return `${this.format(`YYYY-MM-DDTHH:mm:ss${frac}Z`)}[${this.timezone()}]`;
|
|
1826
|
+
}
|
|
1827
|
+
/** A Temporal.ZonedDateTime, where the runtime has Temporal. */
|
|
1828
|
+
toTemporal() {
|
|
1829
|
+
const T = globalThis.Temporal;
|
|
1830
|
+
if (!T) throw new Error("neram: Temporal is not available in this runtime");
|
|
1831
|
+
return T.ZonedDateTime.from(this.toZonedISOString());
|
|
1832
|
+
}
|
|
1833
|
+
toJSON() {
|
|
1834
|
+
return this.isValid() ? this.toISOString() : null;
|
|
1835
|
+
}
|
|
1836
|
+
toString() {
|
|
1837
|
+
return this.isValid() ? new Date(this.#ms).toUTCString() : "Invalid Date";
|
|
1838
|
+
}
|
|
1839
|
+
toObject() {
|
|
1840
|
+
const w = this.#wall();
|
|
1841
|
+
return { year: w.y, month: w.M, date: w.D, hour: w.h, minute: w.m, second: w.s, millisecond: w.ms };
|
|
1842
|
+
}
|
|
1843
|
+
toArray() {
|
|
1844
|
+
const w = this.#wall();
|
|
1845
|
+
return [w.y, w.M, w.D, w.h, w.m, w.s, w.ms];
|
|
1846
|
+
}
|
|
1847
|
+
[Symbol.toPrimitive](hint) {
|
|
1848
|
+
return hint === "number" ? this.#ms : this.toString();
|
|
1849
|
+
}
|
|
1850
|
+
get [Symbol.toStringTag]() {
|
|
1851
|
+
return "Neram";
|
|
1852
|
+
}
|
|
1853
|
+
};
|
|
1854
|
+
function monthDiff(a, b) {
|
|
1855
|
+
if (a.date() < b.date()) return -monthDiff(b, a);
|
|
1856
|
+
const whole = (b.year() - a.year()) * 12 + (b.month() - a.month());
|
|
1857
|
+
const anchor = a.add(whole, "month").valueOf();
|
|
1858
|
+
const behind = b.valueOf() - anchor < 0;
|
|
1859
|
+
const anchor2 = a.add(whole + (behind ? -1 : 1), "month").valueOf();
|
|
1860
|
+
return -(whole + (b.valueOf() - anchor) / (behind ? anchor - anchor2 : anchor2 - anchor)) || 0;
|
|
1861
|
+
}
|
|
1862
|
+
function resolve(parsed, zone, zoneGiven) {
|
|
1863
|
+
if (parsed.kind === "epoch") return { ms: parsed.ms, zone };
|
|
1864
|
+
let z = zone;
|
|
1865
|
+
if (parsed.zone && !zoneGiven) z = normalizeZone(parsed.zone);
|
|
1866
|
+
if (parsed.offset !== void 0) return { ms: wallToUtcMs(parsed.wall) - parsed.offset * 6e4, zone: z };
|
|
1867
|
+
return { ms: wallToEpoch(parsed.wall, z), zone: z };
|
|
1868
|
+
}
|
|
1869
|
+
function isEpochLike(x) {
|
|
1870
|
+
return typeof x === "object" && x !== null && typeof x.epochMilliseconds === "number";
|
|
1871
|
+
}
|
|
1872
|
+
function create(input, formatOrOpts, localeOrStrict, strict) {
|
|
1873
|
+
let opts = {};
|
|
1874
|
+
if (typeof formatOrOpts === "string" || Array.isArray(formatOrOpts)) opts.format = formatOrOpts;
|
|
1875
|
+
else if (formatOrOpts) opts = { ...formatOrOpts };
|
|
1876
|
+
if (typeof localeOrStrict === "boolean") opts.strict = localeOrStrict;
|
|
1877
|
+
else if (typeof localeOrStrict === "string") opts.locale = localeOrStrict;
|
|
1878
|
+
if (typeof strict === "boolean") opts.strict = strict;
|
|
1879
|
+
const locale = opts.locale ? canonicalLocale(opts.locale) : config.locale;
|
|
1880
|
+
const zoneGiven = opts.utc === true || opts.zone !== void 0;
|
|
1881
|
+
let zone = normalizeZone(opts.utc ? "UTC" : opts.zone ?? config.zone);
|
|
1882
|
+
const ws = opts.weekStart;
|
|
1883
|
+
if (input instanceof Neram) {
|
|
1884
|
+
let r = input;
|
|
1885
|
+
if (zoneGiven) r = r.tz(zone);
|
|
1886
|
+
if (opts.locale) r = r.locale(locale);
|
|
1887
|
+
if (ws !== void 0) r = r.weekStart(ws);
|
|
1888
|
+
return r;
|
|
1889
|
+
}
|
|
1890
|
+
let ms;
|
|
1891
|
+
if (input === void 0) ms = Date.now();
|
|
1892
|
+
else if (input === null) ms = NaN;
|
|
1893
|
+
else if (input instanceof Date) ms = input.getTime();
|
|
1894
|
+
else if (typeof input === "number") ms = input;
|
|
1895
|
+
else if (typeof input === "string") {
|
|
1896
|
+
const formats = opts.format === void 0 ? void 0 : Array.isArray(opts.format) ? opts.format : [opts.format];
|
|
1897
|
+
if (!formats) {
|
|
1898
|
+
;
|
|
1899
|
+
({ ms, zone } = resolve(parseString(input), zone, zoneGiven));
|
|
1900
|
+
} else {
|
|
1901
|
+
ms = NaN;
|
|
1902
|
+
for (const f of formats) {
|
|
1903
|
+
const r = resolve(parseFormat(input, f, locale), zone, zoneGiven);
|
|
1904
|
+
if (Number.isNaN(r.ms)) continue;
|
|
1905
|
+
if (opts.strict && new Neram(r.ms, r.zone, locale, ws).format(f) !== input.trim()) continue;
|
|
1906
|
+
({ ms, zone } = r);
|
|
1907
|
+
break;
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
} else if (Array.isArray(input)) {
|
|
1911
|
+
const [y = (/* @__PURE__ */ new Date()).getFullYear(), M = 0, D = 1, h = 0, m = 0, s = 0, mss = 0] = input;
|
|
1912
|
+
const w = { y, M, D, h, m, s, ms: mss };
|
|
1913
|
+
ms = input.length >= 1 && input.length <= 7 && isValidWall(w) ? wallToEpoch(w, zone) : NaN;
|
|
1914
|
+
} else if (isEpochLike(input)) {
|
|
1915
|
+
ms = input.epochMilliseconds;
|
|
1916
|
+
if (input.timeZoneId && !zoneGiven) zone = normalizeZone(input.timeZoneId);
|
|
1917
|
+
} else if (typeof input === "object") {
|
|
1918
|
+
const o = objectFields(input);
|
|
1919
|
+
const w = o && {
|
|
1920
|
+
y: o.year ?? (/* @__PURE__ */ new Date()).getFullYear(),
|
|
1921
|
+
M: o.month ?? 0,
|
|
1922
|
+
D: o.date ?? 1,
|
|
1923
|
+
h: o.hour ?? 0,
|
|
1924
|
+
m: o.minute ?? 0,
|
|
1925
|
+
s: o.second ?? 0,
|
|
1926
|
+
ms: o.millisecond ?? 0
|
|
1927
|
+
};
|
|
1928
|
+
ms = w && isValidWall(w) ? wallToEpoch(w, zone) : NaN;
|
|
1929
|
+
} else {
|
|
1930
|
+
ms = NaN;
|
|
1931
|
+
}
|
|
1932
|
+
return new Neram(ms, zone, locale, ws);
|
|
1933
|
+
}
|
|
1934
|
+
function roundCalendar(n) {
|
|
1935
|
+
return n < 0 ? -Math.round(-n) : Math.round(n);
|
|
1936
|
+
}
|
|
1937
|
+
function checkWeekend(weekend) {
|
|
1938
|
+
if (!Array.isArray(weekend) || weekend.some((d) => !Number.isInteger(d) || d < 0 || d > 6)) {
|
|
1939
|
+
throw new RangeError(`neram: weekend must list weekday numbers 0-6 (0 = Sunday), got ${JSON.stringify(weekend)}`);
|
|
1940
|
+
}
|
|
1941
|
+
if (new Set(weekend).size === 7) throw new RangeError("neram: weekend cannot include every day of the week");
|
|
1942
|
+
return weekend;
|
|
1943
|
+
}
|
|
1944
|
+
var OBJECT_KEYS = {
|
|
1945
|
+
year: "year",
|
|
1946
|
+
years: "year",
|
|
1947
|
+
y: "year",
|
|
1948
|
+
month: "month",
|
|
1949
|
+
months: "month",
|
|
1950
|
+
M: "month",
|
|
1951
|
+
date: "date",
|
|
1952
|
+
dates: "date",
|
|
1953
|
+
D: "date",
|
|
1954
|
+
d: "date",
|
|
1955
|
+
day: "date",
|
|
1956
|
+
days: "date",
|
|
1957
|
+
hour: "hour",
|
|
1958
|
+
hours: "hour",
|
|
1959
|
+
h: "hour",
|
|
1960
|
+
minute: "minute",
|
|
1961
|
+
minutes: "minute",
|
|
1962
|
+
m: "minute",
|
|
1963
|
+
second: "second",
|
|
1964
|
+
seconds: "second",
|
|
1965
|
+
s: "second",
|
|
1966
|
+
millisecond: "millisecond",
|
|
1967
|
+
milliseconds: "millisecond",
|
|
1968
|
+
ms: "millisecond"
|
|
1969
|
+
};
|
|
1970
|
+
function objectFields(input) {
|
|
1971
|
+
const out = {};
|
|
1972
|
+
let any = false;
|
|
1973
|
+
for (const [k, v] of Object.entries(input)) {
|
|
1974
|
+
if (v === void 0) continue;
|
|
1975
|
+
const key = OBJECT_KEYS[k];
|
|
1976
|
+
if (!key || typeof v !== "number" || out[key] !== void 0) return void 0;
|
|
1977
|
+
out[key] = v;
|
|
1978
|
+
any = true;
|
|
1979
|
+
}
|
|
1980
|
+
return any ? out : void 0;
|
|
1981
|
+
}
|
|
1982
|
+
function isValidWall(w) {
|
|
1983
|
+
const ints = [w.y, w.M, w.D, w.h, w.m, w.s, w.ms];
|
|
1984
|
+
if (!ints.every(Number.isInteger)) return false;
|
|
1985
|
+
return w.M >= 0 && w.M <= 11 && w.D >= 1 && w.D <= daysInMonth(w.y, w.M) && w.h >= 0 && w.h <= 23 && w.m >= 0 && w.m <= 59 && w.s >= 0 && w.s <= 59 && w.ms >= 0 && w.ms <= 999;
|
|
1986
|
+
}
|
|
1987
|
+
var installed = /* @__PURE__ */ new Set();
|
|
1988
|
+
function toNerams(args) {
|
|
1989
|
+
return (args.length === 1 && Array.isArray(args[0]) && !(typeof args[0][0] === "number") ? args[0] : args).map((a) => create(a)).filter((d) => d.isValid());
|
|
1990
|
+
}
|
|
1991
|
+
var neram = Object.assign(create, {
|
|
1992
|
+
/** Parse in UTC: neram.utc('2026-12-04 10:00') */
|
|
1993
|
+
utc(input, format, strict) {
|
|
1994
|
+
return create(input, { format, strict, utc: true });
|
|
1995
|
+
},
|
|
1996
|
+
/** Parse a wall time in a zone, or convert an instant to it: neram.tz('2026-03-08 02:30', 'America/New_York') */
|
|
1997
|
+
tz(input, zone, format) {
|
|
1998
|
+
return create(input, { format, zone });
|
|
1999
|
+
},
|
|
2000
|
+
/** From seconds since the epoch. */
|
|
2001
|
+
unix(seconds) {
|
|
2002
|
+
return create(seconds * 1e3);
|
|
2003
|
+
},
|
|
2004
|
+
now() {
|
|
2005
|
+
return create();
|
|
2006
|
+
},
|
|
2007
|
+
duration,
|
|
2008
|
+
isDuration,
|
|
2009
|
+
Duration,
|
|
2010
|
+
Neram,
|
|
2011
|
+
isNeram(x) {
|
|
2012
|
+
return x instanceof Neram;
|
|
2013
|
+
},
|
|
2014
|
+
min(...dates) {
|
|
2015
|
+
const list2 = toNerams(dates);
|
|
2016
|
+
return list2.length ? list2.reduce((a, b) => b.valueOf() < a.valueOf() ? b : a) : null;
|
|
2017
|
+
},
|
|
2018
|
+
max(...dates) {
|
|
2019
|
+
const list2 = toNerams(dates);
|
|
2020
|
+
return list2.length ? list2.reduce((a, b) => b.valueOf() > a.valueOf() ? b : a) : null;
|
|
2021
|
+
},
|
|
2022
|
+
/** Sorted copy, earliest first. */
|
|
2023
|
+
sort(dates) {
|
|
2024
|
+
return dates.map((d) => create(d)).sort((a, b) => a.valueOf() - b.valueOf());
|
|
2025
|
+
},
|
|
2026
|
+
/**
|
|
2027
|
+
* Every date from start to end (inclusive) in steps:
|
|
2028
|
+
* neram.range('2026-12-01', '2026-12-07') — 7 days
|
|
2029
|
+
* neram.range(start, end, { unit: 'week', step: 2 })
|
|
2030
|
+
*/
|
|
2031
|
+
range(start, end, opts = {}) {
|
|
2032
|
+
const unit = normalizeUnit(opts.unit ?? "day");
|
|
2033
|
+
const size = opts.step ?? 1;
|
|
2034
|
+
if (typeof size !== "number" || !Number.isFinite(size) || size <= 0) {
|
|
2035
|
+
throw new RangeError(`neram: range step must be a positive number, got ${String(size)}`);
|
|
2036
|
+
}
|
|
2037
|
+
if (!(unit in FIXED_MS) && !Number.isInteger(size)) {
|
|
2038
|
+
throw new RangeError(`neram: range step must be a whole number of ${unit}s, got ${size}`);
|
|
2039
|
+
}
|
|
2040
|
+
const limit = opts.limit ?? 1e5;
|
|
2041
|
+
if (!Number.isInteger(limit) || limit < 0) throw new RangeError(`neram: range limit must be a whole number, got ${limit}`);
|
|
2042
|
+
const a = create(start);
|
|
2043
|
+
const b = create(end);
|
|
2044
|
+
if (!a.isValid() || !b.isValid()) return [];
|
|
2045
|
+
const step = size * (b.valueOf() < a.valueOf() ? -1 : 1);
|
|
2046
|
+
const out = [];
|
|
2047
|
+
for (let i = 0; ; i++) {
|
|
2048
|
+
const cur = a.add(step * i, unit);
|
|
2049
|
+
const past = step > 0 ? cur.valueOf() > b.valueOf() : cur.valueOf() < b.valueOf();
|
|
2050
|
+
if (past || opts.exclusive && cur.valueOf() === b.valueOf()) break;
|
|
2051
|
+
if (out.length === limit) {
|
|
2052
|
+
throw new RangeError(`neram: range has more than ${limit} dates; use a larger step or pass { limit }`);
|
|
2053
|
+
}
|
|
2054
|
+
out.push(cur);
|
|
2055
|
+
}
|
|
2056
|
+
return out;
|
|
2057
|
+
},
|
|
2058
|
+
/** Get or set the default locale for new instances. */
|
|
2059
|
+
locale(locale) {
|
|
2060
|
+
if (locale !== void 0) config.locale = canonicalLocale(locale);
|
|
2061
|
+
return config.locale;
|
|
2062
|
+
},
|
|
2063
|
+
/** Month and weekday names and week rules for a locale (the default locale if omitted). Day.js-compatible. */
|
|
2064
|
+
localeData(locale) {
|
|
2065
|
+
return publicLocaleData(locale ? canonicalLocale(locale) : config.locale);
|
|
2066
|
+
},
|
|
2067
|
+
/**
|
|
2068
|
+
* Versions and features of this runtime's date data. Log it on the server and in the browser:
|
|
2069
|
+
* if icu/cldr/tzdata or the zone differ, localized text or future DST changes may differ too.
|
|
2070
|
+
*/
|
|
2071
|
+
environment() {
|
|
2072
|
+
const v = globalThis.process?.versions ?? {};
|
|
2073
|
+
const I2 = Intl;
|
|
2074
|
+
let unitFormat = true;
|
|
2075
|
+
try {
|
|
2076
|
+
new Intl.NumberFormat("en", { style: "unit", unit: "hour" });
|
|
2077
|
+
} catch {
|
|
2078
|
+
unitFormat = false;
|
|
2079
|
+
}
|
|
2080
|
+
return {
|
|
2081
|
+
zone: guessZone(),
|
|
2082
|
+
defaultZone: config.zone === "local" ? guessZone() : config.zone,
|
|
2083
|
+
locale: config.locale,
|
|
2084
|
+
runtimeLocale: new Intl.DateTimeFormat().resolvedOptions().locale,
|
|
2085
|
+
icu: v.icu,
|
|
2086
|
+
cldr: v.cldr,
|
|
2087
|
+
tzdata: v.tz,
|
|
2088
|
+
features: {
|
|
2089
|
+
relativeTimeFormat: typeof I2.RelativeTimeFormat === "function",
|
|
2090
|
+
listFormat: typeof I2.ListFormat === "function",
|
|
2091
|
+
durationFormat: typeof I2.DurationFormat === "function",
|
|
2092
|
+
supportedValuesOf: typeof I2.supportedValuesOf === "function",
|
|
2093
|
+
unitFormat
|
|
2094
|
+
}
|
|
2095
|
+
};
|
|
2096
|
+
},
|
|
2097
|
+
/** The runtime's IANA time zone. */
|
|
2098
|
+
guess() {
|
|
2099
|
+
return guessZone();
|
|
2100
|
+
},
|
|
2101
|
+
/** Every IANA zone the runtime knows (where Intl.supportedValuesOf exists). */
|
|
2102
|
+
zones() {
|
|
2103
|
+
const sv = Intl.supportedValuesOf;
|
|
2104
|
+
return sv ? sv("timeZone") : [];
|
|
2105
|
+
},
|
|
2106
|
+
/** Set defaults: neram.config({ locale: 'ta', zone: 'Asia/Kolkata', weekStart: 1 }) */
|
|
2107
|
+
config(next = {}) {
|
|
2108
|
+
if (next.locale !== void 0) config.locale = canonicalLocale(next.locale);
|
|
2109
|
+
if (next.zone !== void 0) config.zone = normalizeZone(next.zone);
|
|
2110
|
+
if ("weekStart" in next) config.weekStart = next.weekStart;
|
|
2111
|
+
if (next.normalizeSpaces !== void 0) intlSettings.normalizeSpaces = config.normalizeSpaces = next.normalizeSpaces;
|
|
2112
|
+
return { ...config };
|
|
2113
|
+
},
|
|
2114
|
+
/** Day.js-style plugins: (option, NeramClass, neram) => void. Installed once. */
|
|
2115
|
+
extend(plugin, option) {
|
|
2116
|
+
if (!installed.has(plugin)) {
|
|
2117
|
+
plugin(option, Neram, neram);
|
|
2118
|
+
installed.add(plugin);
|
|
2119
|
+
}
|
|
2120
|
+
return neram;
|
|
2121
|
+
}
|
|
2122
|
+
});
|
|
2123
|
+
|
|
2124
|
+
// src/recur/index.ts
|
|
2125
|
+
var recur_exports = {};
|
|
2126
|
+
__export(recur_exports, {
|
|
2127
|
+
Cron: () => Cron,
|
|
2128
|
+
RRule: () => RRule,
|
|
2129
|
+
Schedule: () => Schedule,
|
|
2130
|
+
cron: () => cron,
|
|
2131
|
+
parseRRule: () => parseRRule,
|
|
2132
|
+
rrule: () => rrule
|
|
2133
|
+
});
|
|
2134
|
+
|
|
2135
|
+
// src/recur/schedule.ts
|
|
2136
|
+
var MAX_SCAN = 400 * 366 * 864e5;
|
|
2137
|
+
var Schedule = class {
|
|
2138
|
+
coerce(input) {
|
|
2139
|
+
const ms = neram(input, { zone: this.tz }).valueOf();
|
|
2140
|
+
if (Number.isNaN(ms)) throw new RangeError(`neram: invalid date ${JSON.stringify(input)}`);
|
|
2141
|
+
return ms;
|
|
2142
|
+
}
|
|
2143
|
+
out(ms, as) {
|
|
2144
|
+
const d = neram(ms, { zone: this.tz });
|
|
2145
|
+
if (typeof as === "function") return as(d);
|
|
2146
|
+
const local = () => d.format(this.allDay ? "YYYY-MM-DD" : "YYYY-MM-DDTHH:mm:ss");
|
|
2147
|
+
let v;
|
|
2148
|
+
switch (as ?? "neram") {
|
|
2149
|
+
case "neram":
|
|
2150
|
+
v = d;
|
|
2151
|
+
break;
|
|
2152
|
+
case "date":
|
|
2153
|
+
v = d.toDate();
|
|
2154
|
+
break;
|
|
2155
|
+
case "epoch":
|
|
2156
|
+
v = ms;
|
|
2157
|
+
break;
|
|
2158
|
+
case "unix":
|
|
2159
|
+
v = Math.floor(ms / 1e3);
|
|
2160
|
+
break;
|
|
2161
|
+
case "iso":
|
|
2162
|
+
v = d.toISOString();
|
|
2163
|
+
break;
|
|
2164
|
+
case "zoned":
|
|
2165
|
+
v = d.toZonedISOString();
|
|
2166
|
+
break;
|
|
2167
|
+
case "local":
|
|
2168
|
+
v = local();
|
|
2169
|
+
break;
|
|
2170
|
+
case "record":
|
|
2171
|
+
v = { utc: d.toISOString(), local: local(), tz: d.timezone(), epoch: ms, allDay: this.allDay };
|
|
2172
|
+
break;
|
|
2173
|
+
default:
|
|
2174
|
+
throw new RangeError(`neram: unknown output format "${String(as)}"`);
|
|
2175
|
+
}
|
|
2176
|
+
return v;
|
|
2177
|
+
}
|
|
2178
|
+
/**
|
|
2179
|
+
* Every occurrence. Infinite schedules need a `limit`.
|
|
2180
|
+
* rule.all({ limit: 10, as: 'iso' })
|
|
2181
|
+
*/
|
|
2182
|
+
all(opts = {}) {
|
|
2183
|
+
checkCount("limit", opts.limit);
|
|
2184
|
+
if (opts.limit === void 0 && this.isInfinite()) {
|
|
2185
|
+
throw new RangeError("neram: this schedule never ends; pass { limit } or use between()");
|
|
2186
|
+
}
|
|
2187
|
+
const res = [];
|
|
2188
|
+
for (const ms of this.iterate(this.startMs())) {
|
|
2189
|
+
if (opts.limit !== void 0 && res.length >= opts.limit) break;
|
|
2190
|
+
res.push(this.out(ms, opts.as));
|
|
2191
|
+
}
|
|
2192
|
+
return res;
|
|
2193
|
+
}
|
|
2194
|
+
/** Occurrences between two dates, inclusive by default. Strings without an offset are read in the schedule's zone. */
|
|
2195
|
+
between(from, to, opts = {}) {
|
|
2196
|
+
checkCount("limit", opts.limit);
|
|
2197
|
+
const inc = opts.inclusive ?? true;
|
|
2198
|
+
const f = this.coerce(from);
|
|
2199
|
+
const t = this.coerce(to);
|
|
2200
|
+
const res = [];
|
|
2201
|
+
for (const ms of this.iterate(f)) {
|
|
2202
|
+
if (ms > t || !inc && ms === t) break;
|
|
2203
|
+
if (!inc && ms === f) continue;
|
|
2204
|
+
if (opts.limit !== void 0 && res.length >= opts.limit) break;
|
|
2205
|
+
res.push(this.out(ms, opts.as));
|
|
2206
|
+
}
|
|
2207
|
+
return res;
|
|
2208
|
+
}
|
|
2209
|
+
/** The first occurrence after `date` (default: now). */
|
|
2210
|
+
after(date, opts = {}) {
|
|
2211
|
+
const f = date === void 0 ? Date.now() : this.coerce(date);
|
|
2212
|
+
for (const ms of this.iterate(f)) {
|
|
2213
|
+
if (ms === f && !opts.inclusive) continue;
|
|
2214
|
+
return this.out(ms, opts.as);
|
|
2215
|
+
}
|
|
2216
|
+
return null;
|
|
2217
|
+
}
|
|
2218
|
+
/** The last occurrence before `date` (default: now). */
|
|
2219
|
+
before(date, opts = {}) {
|
|
2220
|
+
const t = date === void 0 ? Date.now() : this.coerce(date);
|
|
2221
|
+
const floor = Math.max(this.startMs(), t - MAX_SCAN);
|
|
2222
|
+
let window = 7 * 864e5;
|
|
2223
|
+
for (; ; ) {
|
|
2224
|
+
const lo = Math.max(floor, t - window);
|
|
2225
|
+
let found;
|
|
2226
|
+
for (const ms of this.iterate(lo)) {
|
|
2227
|
+
if (ms > t || ms === t && !opts.inclusive) break;
|
|
2228
|
+
found = ms;
|
|
2229
|
+
}
|
|
2230
|
+
if (found !== void 0) return this.out(found, opts.as);
|
|
2231
|
+
if (lo <= floor) return null;
|
|
2232
|
+
window *= 8;
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
/** The next `n` occurrences strictly after `from` (default: now). */
|
|
2236
|
+
take(n, opts = {}) {
|
|
2237
|
+
checkCount("take(n)", n);
|
|
2238
|
+
const f = opts.from === void 0 ? Date.now() : this.coerce(opts.from);
|
|
2239
|
+
const res = [];
|
|
2240
|
+
if (n === 0) return res;
|
|
2241
|
+
for (const ms of this.iterate(f)) {
|
|
2242
|
+
if (ms === f) continue;
|
|
2243
|
+
res.push(this.out(ms, opts.as));
|
|
2244
|
+
if (res.length >= n) break;
|
|
2245
|
+
}
|
|
2246
|
+
return res;
|
|
2247
|
+
}
|
|
2248
|
+
/** True when `date` is exactly an occurrence. */
|
|
2249
|
+
includes(date) {
|
|
2250
|
+
const t = this.coerce(date);
|
|
2251
|
+
for (const ms of this.iterate(t)) return ms === t;
|
|
2252
|
+
return false;
|
|
2253
|
+
}
|
|
2254
|
+
/**
|
|
2255
|
+
* Event instances with a start and end that overlap [from, to): what a calendar view needs.
|
|
2256
|
+
* Includes events that started before `from` but are still running.
|
|
2257
|
+
* rule.instances('2026-12-01', '2027-01-01', { duration: { hours: 1 }, as: 'iso' })
|
|
2258
|
+
*/
|
|
2259
|
+
instances(from, to, opts) {
|
|
2260
|
+
const dur = duration(opts.duration);
|
|
2261
|
+
if (!dur.isValid() || dur.valueOf() < 0) throw new RangeError("neram: instances() needs a duration of zero or more");
|
|
2262
|
+
const f = this.coerce(from);
|
|
2263
|
+
const t = this.coerce(to);
|
|
2264
|
+
const slack = Math.abs(dur.asMilliseconds()) + 2 * 864e5;
|
|
2265
|
+
const res = [];
|
|
2266
|
+
for (const ms of this.iterate(f - slack)) {
|
|
2267
|
+
if (ms >= t) break;
|
|
2268
|
+
const start = neram(ms, { zone: this.tz });
|
|
2269
|
+
const end = start.add(dur);
|
|
2270
|
+
if (end.valueOf() <= f && ms < f) continue;
|
|
2271
|
+
res.push({ start: this.out(ms, opts.as), end: this.out(end.valueOf(), opts.as) });
|
|
2272
|
+
}
|
|
2273
|
+
return res;
|
|
2274
|
+
}
|
|
2275
|
+
*[Symbol.iterator]() {
|
|
2276
|
+
for (const ms of this.iterate(this.startMs())) yield neram(ms, { zone: this.tz });
|
|
2277
|
+
}
|
|
2278
|
+
};
|
|
2279
|
+
function checkCount(name, n) {
|
|
2280
|
+
if (n !== void 0 && (!Number.isInteger(n) || n < 0)) {
|
|
2281
|
+
throw new RangeError(`neram: ${name} must be a whole number of zero or more, got ${String(n)}`);
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
|
|
2285
|
+
// src/recur/rrule.ts
|
|
2286
|
+
var FREQS = ["yearly", "monthly", "weekly", "daily", "hourly", "minutely", "secondly"];
|
|
2287
|
+
var OPTION_KEYS = /* @__PURE__ */ new Set([
|
|
2288
|
+
"freq",
|
|
2289
|
+
"dtstart",
|
|
2290
|
+
"tz",
|
|
2291
|
+
"allDay",
|
|
2292
|
+
"interval",
|
|
2293
|
+
"count",
|
|
2294
|
+
"until",
|
|
2295
|
+
"wkst",
|
|
2296
|
+
"byMonth",
|
|
2297
|
+
"byWeekNo",
|
|
2298
|
+
"byYearDay",
|
|
2299
|
+
"byMonthDay",
|
|
2300
|
+
"byDay",
|
|
2301
|
+
"byHour",
|
|
2302
|
+
"byMinute",
|
|
2303
|
+
"bySecond",
|
|
2304
|
+
"bySetPos",
|
|
2305
|
+
"rdate",
|
|
2306
|
+
"exdate"
|
|
2307
|
+
]);
|
|
2308
|
+
var YEARLY = 0;
|
|
2309
|
+
var MONTHLY = 1;
|
|
2310
|
+
var WEEKLY = 2;
|
|
2311
|
+
var DAILY = 3;
|
|
2312
|
+
var HOURLY = 4;
|
|
2313
|
+
var MINUTELY = 5;
|
|
2314
|
+
var SECONDLY = 6;
|
|
2315
|
+
var WEEKDAYS = ["SU", "MO", "TU", "WE", "TH", "FR", "SA"];
|
|
2316
|
+
var DAY = 864e5;
|
|
2317
|
+
var SUB_UNIT = [0, 0, 0, 0, 36e5, 6e4, 1e3];
|
|
2318
|
+
var list = (v, name, min, max, zero = true) => {
|
|
2319
|
+
if (v === void 0) return void 0;
|
|
2320
|
+
const arr = (Array.isArray(v) ? v : [v]).map(Number);
|
|
2321
|
+
for (const n of arr) {
|
|
2322
|
+
if (!Number.isInteger(n) || Math.abs(n) > max || min >= 0 && n < min || !zero && n === 0 || min < 0 && n < min) {
|
|
2323
|
+
throw new RangeError(`neram: ${name} value ${n} is out of range`);
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
return arr.length ? [...new Set(arr)].sort((a, b) => a - b) : void 0;
|
|
2327
|
+
};
|
|
2328
|
+
function parseByDay(v) {
|
|
2329
|
+
if (typeof v === "object") {
|
|
2330
|
+
const w = WEEKDAYS.indexOf(v.day.toUpperCase());
|
|
2331
|
+
if (w < 0) throw new RangeError(`neram: invalid weekday "${v.day}"`);
|
|
2332
|
+
return { w, n: v.n ?? 0 };
|
|
2333
|
+
}
|
|
2334
|
+
const m = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/i.exec(v.trim());
|
|
2335
|
+
if (!m) throw new RangeError(`neram: invalid BYDAY "${v}"`);
|
|
2336
|
+
const n = m[1] ? Number(m[1]) : 0;
|
|
2337
|
+
if (Math.abs(n) > 53) throw new RangeError(`neram: invalid BYDAY "${v}"`);
|
|
2338
|
+
return { w: WEEKDAYS.indexOf(m[2].toUpperCase()), n };
|
|
2339
|
+
}
|
|
2340
|
+
var dayNum = (y, M, D) => Math.floor(utcFromParts(y, M, D) / DAY);
|
|
2341
|
+
var fromDay = civilFromDays;
|
|
2342
|
+
function wallOf(ms) {
|
|
2343
|
+
const w = wallOfMs(ms);
|
|
2344
|
+
return { y: w.y, M: w.M, D: w.D, h: w.h, m: w.m, s: w.s, ms: 0 };
|
|
2345
|
+
}
|
|
2346
|
+
var isDateOnly = (v) => typeof v === "string" && /^\d{4}-?\d{2}-?\d{2}$/.test(v.trim());
|
|
2347
|
+
var RRule = class _RRule extends Schedule {
|
|
2348
|
+
tz;
|
|
2349
|
+
allDay;
|
|
2350
|
+
#o;
|
|
2351
|
+
#eff;
|
|
2352
|
+
#dtWallMs;
|
|
2353
|
+
#dtMs;
|
|
2354
|
+
#dt;
|
|
2355
|
+
#exdates;
|
|
2356
|
+
#rdates;
|
|
2357
|
+
constructor(opts) {
|
|
2358
|
+
super();
|
|
2359
|
+
for (const key of Object.keys(opts)) {
|
|
2360
|
+
if (!OPTION_KEYS.has(key)) {
|
|
2361
|
+
const hint = [...OPTION_KEYS].find((k) => k.toLowerCase() === key.toLowerCase());
|
|
2362
|
+
throw new RangeError(`neram: unknown rrule option "${key}"${hint ? `; did you mean "${hint}"?` : ""}`);
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
const freq = FREQS.indexOf(String(opts.freq).toLowerCase());
|
|
2366
|
+
if (freq < 0) throw new RangeError(`neram: invalid freq "${opts.freq}"`);
|
|
2367
|
+
if (opts.dtstart === void 0 || opts.dtstart === null) throw new RangeError("neram: dtstart is required");
|
|
2368
|
+
let start = opts.tz ? neram(opts.dtstart, { zone: opts.tz }) : neram(opts.dtstart);
|
|
2369
|
+
if (!start.isValid()) throw new RangeError(`neram: invalid dtstart ${JSON.stringify(opts.dtstart)}`);
|
|
2370
|
+
this.tz = normalizeZone(opts.tz ?? start.timezone());
|
|
2371
|
+
this.allDay = opts.allDay ?? false;
|
|
2372
|
+
start = start.tz(this.tz).millisecond(0);
|
|
2373
|
+
if (this.allDay) start = start.startOf("day");
|
|
2374
|
+
const [y, M, D, h, m, s] = start.toArray();
|
|
2375
|
+
this.#dt = { y, M, D, h, m, s, dow: start.day() };
|
|
2376
|
+
this.#dtWallMs = utcFromParts(y, M, D, h, m, s);
|
|
2377
|
+
this.#dtMs = start.valueOf();
|
|
2378
|
+
const interval = opts.interval ?? 1;
|
|
2379
|
+
if (!Number.isInteger(interval) || interval < 1) throw new RangeError("neram: interval must be a positive integer");
|
|
2380
|
+
if (opts.count !== void 0 && (!Number.isInteger(opts.count) || opts.count < 1)) {
|
|
2381
|
+
throw new RangeError("neram: count must be a positive integer");
|
|
2382
|
+
}
|
|
2383
|
+
if (opts.count !== void 0 && opts.until !== void 0 && opts.until !== null) {
|
|
2384
|
+
throw new RangeError("neram: use count or until, not both (RFC 5545)");
|
|
2385
|
+
}
|
|
2386
|
+
let wkst = 1;
|
|
2387
|
+
if (opts.wkst) {
|
|
2388
|
+
wkst = WEEKDAYS.indexOf(opts.wkst.toUpperCase());
|
|
2389
|
+
if (wkst < 0) throw new RangeError(`neram: invalid wkst "${opts.wkst}"`);
|
|
2390
|
+
}
|
|
2391
|
+
let untilMs;
|
|
2392
|
+
if (opts.until !== void 0 && opts.until !== null) {
|
|
2393
|
+
const u = neram(opts.until, { zone: this.tz });
|
|
2394
|
+
if (!u.isValid()) throw new RangeError(`neram: invalid until ${JSON.stringify(opts.until)}`);
|
|
2395
|
+
untilMs = (this.allDay || isDateOnly(opts.until) ? u.endOf("day") : u).valueOf();
|
|
2396
|
+
}
|
|
2397
|
+
const byDay = opts.byDay === void 0 ? void 0 : (Array.isArray(opts.byDay) ? opts.byDay : [opts.byDay]).map(parseByDay);
|
|
2398
|
+
if (byDay?.some((r) => r.n !== 0)) {
|
|
2399
|
+
if (freq !== MONTHLY && freq !== YEARLY) {
|
|
2400
|
+
throw new RangeError(`neram: byDay with a number ("1MO", "-1FR") needs freq 'monthly' or 'yearly'`);
|
|
2401
|
+
}
|
|
2402
|
+
if (freq === YEARLY && opts.byWeekNo !== void 0) {
|
|
2403
|
+
throw new RangeError("neram: byDay with a number cannot be combined with byWeekNo (RFC 5545)");
|
|
2404
|
+
}
|
|
2405
|
+
}
|
|
2406
|
+
this.#o = {
|
|
2407
|
+
freq,
|
|
2408
|
+
interval,
|
|
2409
|
+
count: opts.count,
|
|
2410
|
+
untilMs,
|
|
2411
|
+
wkst,
|
|
2412
|
+
wkstGiven: opts.wkst !== void 0,
|
|
2413
|
+
byMonth: list(opts.byMonth, "byMonth", 1, 12),
|
|
2414
|
+
byWeekNo: list(opts.byWeekNo, "byWeekNo", -53, 53, false),
|
|
2415
|
+
byYearDay: list(opts.byYearDay, "byYearDay", -366, 366, false),
|
|
2416
|
+
byMonthDay: list(opts.byMonthDay, "byMonthDay", -31, 31, false),
|
|
2417
|
+
byDay: byDay?.length ? byDay : void 0,
|
|
2418
|
+
byHour: list(opts.byHour, "byHour", 0, 23),
|
|
2419
|
+
byMinute: list(opts.byMinute, "byMinute", 0, 59),
|
|
2420
|
+
bySecond: list(opts.bySecond, "bySecond", 0, 60),
|
|
2421
|
+
bySetPos: list(opts.bySetPos, "bySetPos", -366, 366, false)
|
|
2422
|
+
};
|
|
2423
|
+
const e = { ...this.#o };
|
|
2424
|
+
const noDayParts = !e.byWeekNo && !e.byYearDay && !e.byMonthDay && !e.byDay;
|
|
2425
|
+
if (freq === YEARLY && noDayParts) {
|
|
2426
|
+
e.byMonth ??= [M + 1];
|
|
2427
|
+
e.byMonthDay = [D];
|
|
2428
|
+
} else if (freq === MONTHLY && noDayParts) {
|
|
2429
|
+
e.byMonthDay = [D];
|
|
2430
|
+
} else if (freq === WEEKLY && !e.byDay && !e.byMonthDay && !e.byYearDay) {
|
|
2431
|
+
e.byDay = [{ w: this.#dt.dow, n: 0 }];
|
|
2432
|
+
}
|
|
2433
|
+
if (this.allDay) {
|
|
2434
|
+
e.byHour = [0];
|
|
2435
|
+
e.byMinute = [0];
|
|
2436
|
+
e.bySecond = [0];
|
|
2437
|
+
} else {
|
|
2438
|
+
if (freq < HOURLY) e.byHour ??= [h];
|
|
2439
|
+
if (freq < MINUTELY) e.byMinute ??= [m];
|
|
2440
|
+
if (freq < SECONDLY) e.bySecond ??= [s];
|
|
2441
|
+
}
|
|
2442
|
+
this.#eff = e;
|
|
2443
|
+
const toMs = (d) => {
|
|
2444
|
+
const n = neram(d, { zone: this.tz });
|
|
2445
|
+
if (!n.isValid()) throw new RangeError(`neram: invalid date ${JSON.stringify(d)}`);
|
|
2446
|
+
return (this.allDay ? n.startOf("day") : n.millisecond(0)).valueOf();
|
|
2447
|
+
};
|
|
2448
|
+
this.#exdates = new Set((opts.exdate ?? []).map(toMs));
|
|
2449
|
+
this.#rdates = [...new Set((opts.rdate ?? []).map(toMs))].sort((a, b) => a - b);
|
|
2450
|
+
}
|
|
2451
|
+
isInfinite() {
|
|
2452
|
+
return this.#o.count === void 0 && this.#o.untilMs === void 0;
|
|
2453
|
+
}
|
|
2454
|
+
startMs() {
|
|
2455
|
+
return Math.min(this.#dtMs, this.#rdates[0] ?? Infinity);
|
|
2456
|
+
}
|
|
2457
|
+
get freq() {
|
|
2458
|
+
return FREQS[this.#o.freq];
|
|
2459
|
+
}
|
|
2460
|
+
/** The first occurrence's start, in the rule's zone. */
|
|
2461
|
+
get dtstart() {
|
|
2462
|
+
return neram(this.#dtMs, { zone: this.tz });
|
|
2463
|
+
}
|
|
2464
|
+
// -- expansion -------------------------------------------------------------
|
|
2465
|
+
#matchDay(dn, periodYear) {
|
|
2466
|
+
const e = this.#eff;
|
|
2467
|
+
const { y, M, D, dow } = fromDay(dn);
|
|
2468
|
+
if (e.byMonth && !e.byMonth.includes(M + 1)) return false;
|
|
2469
|
+
if (e.byWeekNo) {
|
|
2470
|
+
const wk = weekOfYear(y, M, D, e.wkst, 4);
|
|
2471
|
+
if (e.freq === YEARLY && wk.year !== periodYear) return false;
|
|
2472
|
+
const nw = weeksInYear(wk.year, e.wkst, 4);
|
|
2473
|
+
if (!e.byWeekNo.some((n) => (n > 0 ? n : nw + n + 1) === wk.week)) return false;
|
|
2474
|
+
}
|
|
2475
|
+
if (e.byYearDay) {
|
|
2476
|
+
const doy = dayOfYear(y, M, D);
|
|
2477
|
+
const diy = daysInYear(y);
|
|
2478
|
+
if (!e.byYearDay.some((n) => (n > 0 ? n : diy + n + 1) === doy)) return false;
|
|
2479
|
+
}
|
|
2480
|
+
if (e.byMonthDay) {
|
|
2481
|
+
const dim = daysInMonth(y, M);
|
|
2482
|
+
if (!e.byMonthDay.some((n) => (n > 0 ? n : dim + n + 1) === D)) return false;
|
|
2483
|
+
}
|
|
2484
|
+
if (e.byDay) {
|
|
2485
|
+
const monthScope = e.freq === MONTHLY || e.freq === YEARLY && !!e.byMonth;
|
|
2486
|
+
const yearScope = e.freq === YEARLY && !e.byMonth;
|
|
2487
|
+
let ok = false;
|
|
2488
|
+
for (const r of e.byDay) {
|
|
2489
|
+
if (r.w !== dow) continue;
|
|
2490
|
+
if (r.n === 0 || !monthScope && !yearScope) {
|
|
2491
|
+
ok = true;
|
|
2492
|
+
break;
|
|
2493
|
+
}
|
|
2494
|
+
let idx, last;
|
|
2495
|
+
if (monthScope) {
|
|
2496
|
+
idx = Math.floor((D - 1) / 7) + 1;
|
|
2497
|
+
last = -(Math.floor((daysInMonth(y, M) - D) / 7) + 1);
|
|
2498
|
+
} else {
|
|
2499
|
+
const doy = dayOfYear(y, M, D);
|
|
2500
|
+
idx = Math.floor((doy - 1) / 7) + 1;
|
|
2501
|
+
last = -(Math.floor((daysInYear(y) - doy) / 7) + 1);
|
|
2502
|
+
}
|
|
2503
|
+
if (r.n === idx || r.n === last) {
|
|
2504
|
+
ok = true;
|
|
2505
|
+
break;
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
if (!ok) return false;
|
|
2509
|
+
}
|
|
2510
|
+
return true;
|
|
2511
|
+
}
|
|
2512
|
+
#times() {
|
|
2513
|
+
const e = this.#eff;
|
|
2514
|
+
const out = [];
|
|
2515
|
+
for (const h of e.byHour) for (const m of e.byMinute) for (const s of e.bySecond) {
|
|
2516
|
+
out.push(h * 36e5 + m * 6e4 + s * 1e3);
|
|
2517
|
+
}
|
|
2518
|
+
return out.sort((a, b) => a - b);
|
|
2519
|
+
}
|
|
2520
|
+
/** Candidate wall times (as UTC-ms of the wall clock) for period p. */
|
|
2521
|
+
#period(p, times) {
|
|
2522
|
+
const e = this.#eff;
|
|
2523
|
+
const dt = this.#dt;
|
|
2524
|
+
const iv = e.interval;
|
|
2525
|
+
let first, last, year;
|
|
2526
|
+
if (e.freq >= HOURLY) {
|
|
2527
|
+
const unit = SUB_UNIT[e.freq];
|
|
2528
|
+
const base = this.#dtWallMs - (this.#dtWallMs % unit + unit) % unit;
|
|
2529
|
+
const pw = base + p * iv * unit;
|
|
2530
|
+
const dn = Math.floor(pw / DAY);
|
|
2531
|
+
year = fromDay(dn).y;
|
|
2532
|
+
const jump = (to) => Math.ceil((to - base) / (iv * unit));
|
|
2533
|
+
if (!this.#matchDay(dn, year)) return { year, walls: [], skipTo: jump((dn + 1) * DAY) };
|
|
2534
|
+
const w = wallOf(pw);
|
|
2535
|
+
if (e.byHour && !e.byHour.includes(w.h)) {
|
|
2536
|
+
return e.freq === HOURLY ? { year, walls: [] } : { year, walls: [], skipTo: jump((Math.floor(pw / 36e5) + 1) * 36e5) };
|
|
2537
|
+
}
|
|
2538
|
+
if (e.freq === SECONDLY && e.byMinute && !e.byMinute.includes(w.m)) {
|
|
2539
|
+
return { year, walls: [], skipTo: jump((Math.floor(pw / 6e4) + 1) * 6e4) };
|
|
2540
|
+
}
|
|
2541
|
+
const walls2 = [];
|
|
2542
|
+
if (e.freq === HOURLY) {
|
|
2543
|
+
for (const m of e.byMinute ?? [dt.m]) for (const s of e.bySecond ?? [dt.s]) walls2.push(pw + m * 6e4 + s * 1e3);
|
|
2544
|
+
} else if (e.freq === MINUTELY) {
|
|
2545
|
+
if (e.byMinute && !e.byMinute.includes(w.m)) return { year, walls: [] };
|
|
2546
|
+
for (const s of e.bySecond ?? [dt.s]) walls2.push(pw + s * 1e3);
|
|
2547
|
+
} else if (!e.bySecond || e.bySecond.includes(w.s)) {
|
|
2548
|
+
walls2.push(pw);
|
|
2549
|
+
}
|
|
2550
|
+
return { year, walls: walls2.sort((a, b) => a - b) };
|
|
2551
|
+
}
|
|
2552
|
+
switch (e.freq) {
|
|
2553
|
+
case YEARLY:
|
|
2554
|
+
year = dt.y + p * iv;
|
|
2555
|
+
first = e.byWeekNo ? dayNum(year - 1, 11, 25) : dayNum(year, 0, 1);
|
|
2556
|
+
last = e.byWeekNo ? dayNum(year + 1, 0, 7) : dayNum(year, 11, 31);
|
|
2557
|
+
break;
|
|
2558
|
+
case MONTHLY: {
|
|
2559
|
+
const total = dt.y * 12 + dt.M + p * iv;
|
|
2560
|
+
year = Math.floor(total / 12);
|
|
2561
|
+
const M = total - year * 12;
|
|
2562
|
+
first = dayNum(year, M, 1);
|
|
2563
|
+
last = dayNum(year, M, daysInMonth(year, M));
|
|
2564
|
+
break;
|
|
2565
|
+
}
|
|
2566
|
+
case WEEKLY: {
|
|
2567
|
+
const d0 = dayNum(dt.y, dt.M, dt.D);
|
|
2568
|
+
first = d0 - (dt.dow - e.wkst + 7) % 7 + p * iv * 7;
|
|
2569
|
+
last = first + 6;
|
|
2570
|
+
year = fromDay(first).y;
|
|
2571
|
+
break;
|
|
2572
|
+
}
|
|
2573
|
+
default:
|
|
2574
|
+
first = last = dayNum(dt.y, dt.M, dt.D) + p * iv;
|
|
2575
|
+
year = fromDay(first).y;
|
|
2576
|
+
}
|
|
2577
|
+
const walls = [];
|
|
2578
|
+
for (let dn = first; dn <= last; dn++) {
|
|
2579
|
+
if (!this.#matchDay(dn, year)) continue;
|
|
2580
|
+
for (const t of times) walls.push(dn * DAY + t);
|
|
2581
|
+
}
|
|
2582
|
+
return { year, walls };
|
|
2583
|
+
}
|
|
2584
|
+
#seek(fromMs) {
|
|
2585
|
+
const e = this.#eff;
|
|
2586
|
+
const dt = this.#dt;
|
|
2587
|
+
const target = neram(fromMs - 2 * DAY, { zone: this.tz });
|
|
2588
|
+
const [ty, tM, tD, th, tm, ts] = target.toArray();
|
|
2589
|
+
const iv = e.interval;
|
|
2590
|
+
let p;
|
|
2591
|
+
switch (e.freq) {
|
|
2592
|
+
case YEARLY:
|
|
2593
|
+
p = Math.floor((ty - dt.y) / iv) - 1;
|
|
2594
|
+
break;
|
|
2595
|
+
case MONTHLY:
|
|
2596
|
+
p = Math.floor((ty * 12 + tM - (dt.y * 12 + dt.M)) / iv) - 1;
|
|
2597
|
+
break;
|
|
2598
|
+
case WEEKLY: {
|
|
2599
|
+
const ws0 = dayNum(dt.y, dt.M, dt.D) - (dt.dow - e.wkst + 7) % 7;
|
|
2600
|
+
p = Math.floor((dayNum(ty, tM, tD) - ws0) / (7 * iv)) - 1;
|
|
2601
|
+
break;
|
|
2602
|
+
}
|
|
2603
|
+
case DAILY:
|
|
2604
|
+
p = Math.floor((dayNum(ty, tM, tD) - dayNum(dt.y, dt.M, dt.D)) / iv) - 1;
|
|
2605
|
+
break;
|
|
2606
|
+
default: {
|
|
2607
|
+
const unit = SUB_UNIT[e.freq];
|
|
2608
|
+
const base = this.#dtWallMs - (this.#dtWallMs % unit + unit) % unit;
|
|
2609
|
+
p = Math.floor((utcFromParts(ty, tM, tD, th, tm, ts) - base) / (iv * unit)) - 1;
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
return Math.max(0, p);
|
|
2613
|
+
}
|
|
2614
|
+
/** Instants produced by the RRULE part alone (COUNT counts these). */
|
|
2615
|
+
*#rule(fromMs) {
|
|
2616
|
+
const e = this.#eff;
|
|
2617
|
+
const times = e.freq < HOURLY ? this.#times() : [];
|
|
2618
|
+
const seeking = e.count === void 0 && Number.isFinite(fromMs) && fromMs > this.#dtMs;
|
|
2619
|
+
let p = seeking ? this.#seek(fromMs) : 0;
|
|
2620
|
+
let lastHitYear = seeking ? neram(fromMs, { zone: this.tz }).year() : this.#dt.y;
|
|
2621
|
+
let emitted = 0;
|
|
2622
|
+
let lastInst = -Infinity;
|
|
2623
|
+
for (; ; p++) {
|
|
2624
|
+
const res = this.#period(p, times);
|
|
2625
|
+
if (res.year > 9999 || res.year > lastHitYear + 400) return;
|
|
2626
|
+
if (res.skipTo !== void 0) {
|
|
2627
|
+
p = res.skipTo - 1;
|
|
2628
|
+
continue;
|
|
2629
|
+
}
|
|
2630
|
+
let walls = res.walls;
|
|
2631
|
+
if (e.bySetPos && walls.length) {
|
|
2632
|
+
const n = walls.length;
|
|
2633
|
+
walls = [...new Set(e.bySetPos.map((i) => walls[i > 0 ? i - 1 : n + i]).filter((x) => x !== void 0))].sort((a, b) => a - b);
|
|
2634
|
+
}
|
|
2635
|
+
for (const wt of walls) {
|
|
2636
|
+
if (wt < this.#dtWallMs) continue;
|
|
2637
|
+
const inst = wallToEpoch(wallOf(wt), this.tz);
|
|
2638
|
+
if (e.untilMs !== void 0 && inst > e.untilMs) return;
|
|
2639
|
+
if (inst <= lastInst) continue;
|
|
2640
|
+
lastInst = inst;
|
|
2641
|
+
lastHitYear = res.year;
|
|
2642
|
+
emitted++;
|
|
2643
|
+
if (inst >= fromMs) yield inst;
|
|
2644
|
+
if (e.count !== void 0 && emitted >= e.count) return;
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
/** RRULE ∪ RDATE − EXDATE, ascending. */
|
|
2649
|
+
*iterate(fromMs) {
|
|
2650
|
+
const rd = this.#rdates.filter((r) => r >= fromMs);
|
|
2651
|
+
let i = 0;
|
|
2652
|
+
let last = -Infinity;
|
|
2653
|
+
const emit = (ms) => {
|
|
2654
|
+
if (ms === last || this.#exdates.has(ms)) return false;
|
|
2655
|
+
last = ms;
|
|
2656
|
+
return true;
|
|
2657
|
+
};
|
|
2658
|
+
for (const ms of this.#rule(fromMs)) {
|
|
2659
|
+
while (i < rd.length && rd[i] <= ms) {
|
|
2660
|
+
if (emit(rd[i])) yield rd[i];
|
|
2661
|
+
i++;
|
|
2662
|
+
}
|
|
2663
|
+
if (emit(ms)) yield ms;
|
|
2664
|
+
}
|
|
2665
|
+
for (; i < rd.length; i++) if (emit(rd[i])) yield rd[i];
|
|
2666
|
+
}
|
|
2667
|
+
// -- serialisation ---------------------------------------------------------
|
|
2668
|
+
#ical(ms) {
|
|
2669
|
+
const d = neram(ms, { zone: this.tz });
|
|
2670
|
+
if (this.allDay) return d.format("YYYYMMDD");
|
|
2671
|
+
if (this.tz === "UTC") return d.format("YYYYMMDD[T]HHmmss[Z]");
|
|
2672
|
+
return d.format("YYYYMMDD[T]HHmmss");
|
|
2673
|
+
}
|
|
2674
|
+
#dateProp(name, values) {
|
|
2675
|
+
const params = this.allDay ? ";VALUE=DATE" : this.tz === "UTC" ? "" : `;TZID=${this.tz}`;
|
|
2676
|
+
return `${name}${params}:${values.map((v) => this.#ical(v)).join(",")}`;
|
|
2677
|
+
}
|
|
2678
|
+
/** The RRULE line alone: "FREQ=WEEKLY;BYDAY=MO,WE;COUNT=10" */
|
|
2679
|
+
rruleString() {
|
|
2680
|
+
const o = this.#o;
|
|
2681
|
+
const parts = [`FREQ=${FREQS[o.freq].toUpperCase()}`];
|
|
2682
|
+
if (o.interval !== 1) parts.push(`INTERVAL=${o.interval}`);
|
|
2683
|
+
if (o.count !== void 0) parts.push(`COUNT=${o.count}`);
|
|
2684
|
+
if (o.untilMs !== void 0) {
|
|
2685
|
+
const u = neram(o.untilMs, { zone: this.allDay ? this.tz : "UTC" });
|
|
2686
|
+
parts.push(`UNTIL=${this.allDay ? u.format("YYYYMMDD") : u.format("YYYYMMDD[T]HHmmss[Z]")}`);
|
|
2687
|
+
}
|
|
2688
|
+
if (o.wkstGiven) parts.push(`WKST=${WEEKDAYS[o.wkst]}`);
|
|
2689
|
+
const add = (k, v) => v && parts.push(`${k}=${v.join(",")}`);
|
|
2690
|
+
add("BYMONTH", o.byMonth);
|
|
2691
|
+
add("BYWEEKNO", o.byWeekNo);
|
|
2692
|
+
add("BYYEARDAY", o.byYearDay);
|
|
2693
|
+
add("BYMONTHDAY", o.byMonthDay);
|
|
2694
|
+
add("BYDAY", o.byDay?.map((r) => `${r.n || ""}${WEEKDAYS[r.w]}`));
|
|
2695
|
+
add("BYHOUR", o.byHour);
|
|
2696
|
+
add("BYMINUTE", o.byMinute);
|
|
2697
|
+
add("BYSECOND", o.bySecond);
|
|
2698
|
+
add("BYSETPOS", o.bySetPos);
|
|
2699
|
+
return parts.join(";");
|
|
2700
|
+
}
|
|
2701
|
+
/**
|
|
2702
|
+
* RFC 5545 text — store this in your database and load it back with rrule(text).
|
|
2703
|
+
* DTSTART;TZID=Asia/Kolkata:20261207T090000
|
|
2704
|
+
* RRULE:FREQ=WEEKLY;BYDAY=MO,WE
|
|
2705
|
+
* EXDATE;TZID=Asia/Kolkata:20261209T090000
|
|
2706
|
+
*/
|
|
2707
|
+
toString() {
|
|
2708
|
+
const lines = [this.#dateProp("DTSTART", [this.#dtMs]), `RRULE:${this.rruleString()}`];
|
|
2709
|
+
if (this.#rdates.length) lines.push(this.#dateProp("RDATE", this.#rdates));
|
|
2710
|
+
if (this.#exdates.size) lines.push(this.#dateProp("EXDATE", [...this.#exdates].sort((a, b) => a - b)));
|
|
2711
|
+
return lines.join("\n");
|
|
2712
|
+
}
|
|
2713
|
+
/** Plain-object form with zoned ISO strings; rrule(json) restores it. */
|
|
2714
|
+
toJSON() {
|
|
2715
|
+
const o = this.#o;
|
|
2716
|
+
const z = (ms) => {
|
|
2717
|
+
const d = neram(ms, { zone: this.tz });
|
|
2718
|
+
return this.allDay ? d.format("YYYY-MM-DD") : d.toZonedISOString();
|
|
2719
|
+
};
|
|
2720
|
+
const out = { freq: FREQS[o.freq], dtstart: z(this.#dtMs), tz: this.tz };
|
|
2721
|
+
if (this.allDay) out.allDay = true;
|
|
2722
|
+
if (o.interval !== 1) out.interval = o.interval;
|
|
2723
|
+
if (o.count !== void 0) out.count = o.count;
|
|
2724
|
+
if (o.untilMs !== void 0) out.until = z(o.untilMs);
|
|
2725
|
+
if (o.wkstGiven) out.wkst = WEEKDAYS[o.wkst];
|
|
2726
|
+
for (const k of ["byMonth", "byWeekNo", "byYearDay", "byMonthDay", "byHour", "byMinute", "bySecond", "bySetPos"]) {
|
|
2727
|
+
if (o[k]) out[k] = [...o[k]];
|
|
2728
|
+
}
|
|
2729
|
+
if (o.byDay) out.byDay = o.byDay.map((r) => `${r.n || ""}${WEEKDAYS[r.w]}`);
|
|
2730
|
+
if (this.#rdates.length) out.rdate = this.#rdates.map(z);
|
|
2731
|
+
if (this.#exdates.size) out.exdate = [...this.#exdates].sort((a, b) => a - b).map(z);
|
|
2732
|
+
return out;
|
|
2733
|
+
}
|
|
2734
|
+
/** A copy with some options changed: rule.with({ exdate: [...] }) */
|
|
2735
|
+
with(changes) {
|
|
2736
|
+
const base = this.toJSON();
|
|
2737
|
+
if ("until" in changes) delete base.count;
|
|
2738
|
+
if ("count" in changes) delete base.until;
|
|
2739
|
+
return new _RRule({ ...base, ...changes });
|
|
2740
|
+
}
|
|
2741
|
+
/**
|
|
2742
|
+
* Plain-English description: "every 2 weeks on Monday and Wednesday at 9:00 AM, 10 times".
|
|
2743
|
+
* Weekday, month and time names follow `locale`; the sentence itself is English.
|
|
2744
|
+
*/
|
|
2745
|
+
toText(locale = "en") {
|
|
2746
|
+
const o = this.#o;
|
|
2747
|
+
const ld = localeData(locale);
|
|
2748
|
+
const listFmt = (xs) => formatList(xs, "en");
|
|
2749
|
+
const unit = ["year", "month", "week", "day", "hour", "minute", "second"][o.freq];
|
|
2750
|
+
const ORD = { 1: "first", 2: "second", 3: "third", 4: "fourth", 5: "fifth", [-1]: "last", [-2]: "second-to-last" };
|
|
2751
|
+
let text = o.interval === 1 ? `every ${unit}` : `every ${o.interval} ${unit}s`;
|
|
2752
|
+
if (o.freq === DAILY && o.byDay && o.byDay.every((r) => !r.n)) {
|
|
2753
|
+
const set = o.byDay.map((r) => r.w).sort().join();
|
|
2754
|
+
if (set === "1,2,3,4,5" && o.interval === 1) text = "every weekday";
|
|
2755
|
+
}
|
|
2756
|
+
if (o.byMonth) text += ` in ${listFmt(o.byMonth.map((m) => ld.months[m - 1]))}`;
|
|
2757
|
+
const noDayParts = !o.byWeekNo && !o.byYearDay && !o.byMonthDay && !o.byDay;
|
|
2758
|
+
const dt = this.#dt;
|
|
2759
|
+
if (noDayParts && o.freq === YEARLY && !o.byMonth) text += ` on ${ld.months[dt.M]} ${dt.D}`;
|
|
2760
|
+
else if (noDayParts && o.freq === YEARLY) text += ` on the ${ordinal(dt.D, "en")}`;
|
|
2761
|
+
else if (noDayParts && o.freq === MONTHLY) text += ` on the ${ordinal(dt.D, "en")}`;
|
|
2762
|
+
else if (o.freq === WEEKLY && !o.byDay && !o.byMonthDay && !o.byYearDay) text += ` on ${ld.weekdays[dt.dow]}`;
|
|
2763
|
+
if (o.byWeekNo) text += ` in week ${listFmt(o.byWeekNo.map(String))}`;
|
|
2764
|
+
if (o.byYearDay) text += ` on day ${listFmt(o.byYearDay.map(String))} of the year`;
|
|
2765
|
+
if (o.byMonthDay) {
|
|
2766
|
+
text += ` on the ${listFmt(o.byMonthDay.map((d) => d === -1 ? "last day" : d < 0 ? `${ordinal(-d, "en")}-to-last day` : ordinal(d, "en")))}`;
|
|
2767
|
+
}
|
|
2768
|
+
const plainDays = o.byDay && o.byDay.every((r) => !r.n);
|
|
2769
|
+
const isWorkweek = plainDays && o.byDay.map((r) => r.w).sort().join() === "1,2,3,4,5";
|
|
2770
|
+
if (o.bySetPos && plainDays && !o.byMonthDay) {
|
|
2771
|
+
const what = isWorkweek ? "weekday" : formatList(o.byDay.map((r) => ld.weekdays[r.w]), "en", "disjunction");
|
|
2772
|
+
text += ` on the ${listFmt(o.bySetPos.map((n) => ORD[n] ?? ordinal(n, "en")))} ${what}`;
|
|
2773
|
+
} else {
|
|
2774
|
+
if (o.byDay && text !== "every weekday") {
|
|
2775
|
+
const days = o.byDay.map((r) => (r.n ? `${ORD[r.n] ?? ordinal(Math.abs(r.n), "en") + (r.n < 0 ? " last" : "")} ` : "") + ld.weekdays[r.w]);
|
|
2776
|
+
text += `${o.byMonthDay ? " if it is" : " on"} ${o.byDay.some((r) => r.n) ? "the " : ""}${listFmt(days)}`;
|
|
2777
|
+
}
|
|
2778
|
+
if (o.bySetPos) text += ` (occurrence ${listFmt(o.bySetPos.map(String))} of each ${unit})`;
|
|
2779
|
+
}
|
|
2780
|
+
if (!this.allDay && o.freq <= DAILY) {
|
|
2781
|
+
const e = this.#eff;
|
|
2782
|
+
const times = this.#times().slice(0, 6).map((t) => {
|
|
2783
|
+
const ms = utcFromParts(2e3, 0, 1) + t;
|
|
2784
|
+
return neram(ms, { zone: "UTC", locale }).format(e.bySecond.some((s) => s) ? "LTS" : "LT");
|
|
2785
|
+
});
|
|
2786
|
+
text += ` at ${listFmt(times)}`;
|
|
2787
|
+
}
|
|
2788
|
+
if (o.untilMs !== void 0) text += ` until ${neram(o.untilMs, { zone: this.tz, locale }).format("LL")}`;
|
|
2789
|
+
if (o.count !== void 0) text += `, ${o.count} time${o.count === 1 ? "" : "s"}`;
|
|
2790
|
+
return text;
|
|
2791
|
+
}
|
|
2792
|
+
};
|
|
2793
|
+
var ICAL_DATE = /^(\d{4})(\d{2})(\d{2})(?:T(\d{2})(\d{2})(\d{2})(Z)?)?$/i;
|
|
2794
|
+
function icalDate(v, tz) {
|
|
2795
|
+
const m = ICAL_DATE.exec(v.trim());
|
|
2796
|
+
if (!m) throw new RangeError(`neram: invalid iCalendar date "${v}"`);
|
|
2797
|
+
const dateOnly = !m[4];
|
|
2798
|
+
const iso = dateOnly ? `${m[1]}-${m[2]}-${m[3]}` : `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}`;
|
|
2799
|
+
const utc = !!m[7];
|
|
2800
|
+
const value = utc ? neram(`${iso}Z`, { zone: tz ?? "UTC" }) : neram(iso, { zone: tz ?? "UTC" });
|
|
2801
|
+
return { value, dateOnly, utc };
|
|
2802
|
+
}
|
|
2803
|
+
function parseRRule(text, defaults = {}) {
|
|
2804
|
+
const lines = text.replace(/\r?\n[ \t]/g, "").split(/\r?\n|\\n/).map((l) => l.trim()).filter(Boolean);
|
|
2805
|
+
const opts = {};
|
|
2806
|
+
let tz = defaults.tz;
|
|
2807
|
+
let sawRule = false;
|
|
2808
|
+
const rdate = [];
|
|
2809
|
+
const exdate = [];
|
|
2810
|
+
const dated = [];
|
|
2811
|
+
for (const line of lines) {
|
|
2812
|
+
const colon = line.indexOf(":");
|
|
2813
|
+
const isBare = colon < 0 || /^FREQ=/i.test(line);
|
|
2814
|
+
const head = isBare ? "RRULE" : line.slice(0, colon);
|
|
2815
|
+
const value = isBare ? line : line.slice(colon + 1);
|
|
2816
|
+
const [name, ...rawParams] = head.split(";");
|
|
2817
|
+
const params = {};
|
|
2818
|
+
for (const p of rawParams) {
|
|
2819
|
+
const eq = p.indexOf("=");
|
|
2820
|
+
params[p.slice(0, eq).toUpperCase()] = p.slice(eq + 1).replace(/^"|"$/g, "");
|
|
2821
|
+
}
|
|
2822
|
+
const N = name.toUpperCase();
|
|
2823
|
+
if (N === "RRULE") {
|
|
2824
|
+
if (sawRule) throw new RangeError("neram: only one RRULE per rule is supported; use several rules for more");
|
|
2825
|
+
sawRule = true;
|
|
2826
|
+
for (const part of value.split(";").filter(Boolean)) {
|
|
2827
|
+
const eq = part.indexOf("=");
|
|
2828
|
+
if (eq < 1) throw new RangeError(`neram: invalid RRULE part "${part}"`);
|
|
2829
|
+
const k = part.slice(0, eq).toUpperCase();
|
|
2830
|
+
const v = part.slice(eq + 1);
|
|
2831
|
+
const nums = () => v.split(",").map(Number);
|
|
2832
|
+
switch (k) {
|
|
2833
|
+
case "FREQ":
|
|
2834
|
+
opts.freq = v.toLowerCase();
|
|
2835
|
+
break;
|
|
2836
|
+
case "INTERVAL":
|
|
2837
|
+
opts.interval = Number(v);
|
|
2838
|
+
break;
|
|
2839
|
+
case "COUNT":
|
|
2840
|
+
opts.count = Number(v);
|
|
2841
|
+
break;
|
|
2842
|
+
case "UNTIL":
|
|
2843
|
+
opts.until = v;
|
|
2844
|
+
break;
|
|
2845
|
+
// resolved below, once the zone is known
|
|
2846
|
+
case "WKST":
|
|
2847
|
+
opts.wkst = v.toUpperCase();
|
|
2848
|
+
break;
|
|
2849
|
+
case "BYMONTH":
|
|
2850
|
+
opts.byMonth = nums();
|
|
2851
|
+
break;
|
|
2852
|
+
case "BYWEEKNO":
|
|
2853
|
+
opts.byWeekNo = nums();
|
|
2854
|
+
break;
|
|
2855
|
+
case "BYYEARDAY":
|
|
2856
|
+
opts.byYearDay = nums();
|
|
2857
|
+
break;
|
|
2858
|
+
case "BYMONTHDAY":
|
|
2859
|
+
opts.byMonthDay = nums();
|
|
2860
|
+
break;
|
|
2861
|
+
case "BYDAY":
|
|
2862
|
+
opts.byDay = v.split(",");
|
|
2863
|
+
break;
|
|
2864
|
+
case "BYHOUR":
|
|
2865
|
+
opts.byHour = nums();
|
|
2866
|
+
break;
|
|
2867
|
+
case "BYMINUTE":
|
|
2868
|
+
opts.byMinute = nums();
|
|
2869
|
+
break;
|
|
2870
|
+
case "BYSECOND":
|
|
2871
|
+
opts.bySecond = nums();
|
|
2872
|
+
break;
|
|
2873
|
+
case "BYSETPOS":
|
|
2874
|
+
opts.bySetPos = nums();
|
|
2875
|
+
break;
|
|
2876
|
+
default:
|
|
2877
|
+
if (k.startsWith("X-")) break;
|
|
2878
|
+
throw new RangeError(`neram: unsupported RRULE part "${k}"`);
|
|
2879
|
+
}
|
|
2880
|
+
}
|
|
2881
|
+
} else if (N === "DTSTART" || N === "RDATE" || N === "EXDATE") {
|
|
2882
|
+
dated.push({ name: N, params, values: value.split(",") });
|
|
2883
|
+
}
|
|
2884
|
+
}
|
|
2885
|
+
for (const d of dated) {
|
|
2886
|
+
if (d.name !== "DTSTART") continue;
|
|
2887
|
+
if (d.params.TZID) tz ??= d.params.TZID;
|
|
2888
|
+
const parsed = icalDate(d.values[0], tz);
|
|
2889
|
+
if (parsed.utc && !tz) tz = "UTC";
|
|
2890
|
+
opts.dtstart = parsed.value;
|
|
2891
|
+
if (parsed.dateOnly || d.params.VALUE?.toUpperCase() === "DATE") opts.allDay = true;
|
|
2892
|
+
}
|
|
2893
|
+
for (const d of dated) {
|
|
2894
|
+
if (d.name === "DTSTART") continue;
|
|
2895
|
+
const zone = d.params.TZID ?? tz;
|
|
2896
|
+
for (const v of d.values) (d.name === "RDATE" ? rdate : exdate).push(icalDate(v, zone).value);
|
|
2897
|
+
}
|
|
2898
|
+
if (typeof opts.until === "string") {
|
|
2899
|
+
const parsed = icalDate(opts.until, tz);
|
|
2900
|
+
opts.until = parsed.dateOnly ? parsed.value.format("YYYY-MM-DD") : parsed.value;
|
|
2901
|
+
}
|
|
2902
|
+
if (!sawRule && !defaults.freq) throw new RangeError("neram: no RRULE found");
|
|
2903
|
+
if (sawRule && opts.freq === void 0 && !defaults.freq) throw new RangeError("neram: RRULE has no FREQ");
|
|
2904
|
+
const merged = { ...opts, ...defaults };
|
|
2905
|
+
if (tz) merged.tz = tz;
|
|
2906
|
+
if (rdate.length || defaults.rdate) merged.rdate = [...rdate, ...defaults.rdate ?? []];
|
|
2907
|
+
if (exdate.length || defaults.exdate) merged.exdate = [...exdate, ...defaults.exdate ?? []];
|
|
2908
|
+
if (merged.dtstart === void 0) throw new RangeError("neram: no DTSTART; pass { dtstart } as the second argument");
|
|
2909
|
+
return merged;
|
|
2910
|
+
}
|
|
2911
|
+
function rrule(input, defaults = {}) {
|
|
2912
|
+
if (typeof input === "string") return new RRule(parseRRule(input, defaults));
|
|
2913
|
+
return new RRule({ ...input, ...defaults });
|
|
2914
|
+
}
|
|
2915
|
+
|
|
2916
|
+
// src/recur/cron.ts
|
|
2917
|
+
var MACROS = {
|
|
2918
|
+
"@yearly": "0 0 1 1 *",
|
|
2919
|
+
"@annually": "0 0 1 1 *",
|
|
2920
|
+
"@monthly": "0 0 1 * *",
|
|
2921
|
+
"@weekly": "0 0 * * 0",
|
|
2922
|
+
"@daily": "0 0 * * *",
|
|
2923
|
+
"@midnight": "0 0 * * *",
|
|
2924
|
+
"@hourly": "0 * * * *"
|
|
2925
|
+
};
|
|
2926
|
+
var MONTHS = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
|
|
2927
|
+
var DAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
|
|
2928
|
+
function parseField(src, name, min, max, names) {
|
|
2929
|
+
const values = /* @__PURE__ */ new Set();
|
|
2930
|
+
const any = src.startsWith("*") || src === "?";
|
|
2931
|
+
const toNum = (s) => {
|
|
2932
|
+
const up = s.toUpperCase();
|
|
2933
|
+
const i = names ? names.indexOf(up) : -1;
|
|
2934
|
+
const n = i >= 0 ? i + (names === MONTHS ? 1 : 0) : Number(s);
|
|
2935
|
+
if (!/^\d+$/.test(s) && i < 0) throw new RangeError(`neram: invalid cron ${name} "${s}"`);
|
|
2936
|
+
return n;
|
|
2937
|
+
};
|
|
2938
|
+
for (const part of src.split(",")) {
|
|
2939
|
+
if (!part) throw new RangeError(`neram: empty item in cron ${name}`);
|
|
2940
|
+
const [range, stepStr] = part.split("/");
|
|
2941
|
+
const step = stepStr === void 0 ? 1 : Number(stepStr);
|
|
2942
|
+
if (!Number.isInteger(step) || step < 1) throw new RangeError(`neram: invalid cron step "${part}"`);
|
|
2943
|
+
let lo, hi;
|
|
2944
|
+
if (range === "*" || range === "?") {
|
|
2945
|
+
lo = min;
|
|
2946
|
+
hi = max;
|
|
2947
|
+
} else if (range.includes("-")) {
|
|
2948
|
+
const [a, b] = range.split("-");
|
|
2949
|
+
lo = toNum(a);
|
|
2950
|
+
hi = toNum(b);
|
|
2951
|
+
} else {
|
|
2952
|
+
lo = toNum(range);
|
|
2953
|
+
hi = stepStr === void 0 ? lo : max;
|
|
2954
|
+
}
|
|
2955
|
+
const span = (v) => {
|
|
2956
|
+
if (v < min || v > max) throw new RangeError(`neram: cron ${name} value ${v} is out of range ${min}-${max}`);
|
|
2957
|
+
};
|
|
2958
|
+
span(lo);
|
|
2959
|
+
span(hi);
|
|
2960
|
+
if (lo <= hi) {
|
|
2961
|
+
for (let v = lo; v <= hi; v += step) values.add(v);
|
|
2962
|
+
} else {
|
|
2963
|
+
const size = max - min + 1;
|
|
2964
|
+
const len = (hi - lo + size) % size;
|
|
2965
|
+
for (let i = 0; i <= len; i += step) values.add(min + (lo - min + i) % size);
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
return { values, any };
|
|
2969
|
+
}
|
|
2970
|
+
var Cron = class extends Schedule {
|
|
2971
|
+
tz;
|
|
2972
|
+
allDay = false;
|
|
2973
|
+
expression;
|
|
2974
|
+
hasSeconds;
|
|
2975
|
+
#sec;
|
|
2976
|
+
#min;
|
|
2977
|
+
#hour;
|
|
2978
|
+
#dom;
|
|
2979
|
+
#month;
|
|
2980
|
+
#dow;
|
|
2981
|
+
#domX = { last: [], nearestWeekday: [], lastWeekday: false };
|
|
2982
|
+
#dowX = { nth: [], last: [] };
|
|
2983
|
+
#startMs;
|
|
2984
|
+
#endMs;
|
|
2985
|
+
constructor(expression, opts = {}) {
|
|
2986
|
+
super();
|
|
2987
|
+
this.expression = expression.trim();
|
|
2988
|
+
this.tz = normalizeZone(opts.tz ?? neram().timezone());
|
|
2989
|
+
const expanded = MACROS[this.expression.toLowerCase()] ?? this.expression;
|
|
2990
|
+
const parts = expanded.split(/\s+/);
|
|
2991
|
+
if (parts.length !== 5 && parts.length !== 6) {
|
|
2992
|
+
throw new RangeError(`neram: cron needs 5 or 6 fields, got ${parts.length} in "${expression}"`);
|
|
2993
|
+
}
|
|
2994
|
+
this.hasSeconds = parts.length === 6;
|
|
2995
|
+
const [sec, min, hour, dom, month, dow] = this.hasSeconds ? parts : ["0", ...parts];
|
|
2996
|
+
this.#sec = parseField(sec, "second", 0, 59);
|
|
2997
|
+
this.#min = parseField(min, "minute", 0, 59);
|
|
2998
|
+
this.#hour = parseField(hour, "hour", 0, 23);
|
|
2999
|
+
this.#month = parseField(month, "month", 1, 12, MONTHS);
|
|
3000
|
+
const domPlain = [];
|
|
3001
|
+
for (const item of dom.split(",")) {
|
|
3002
|
+
const up = item.toUpperCase();
|
|
3003
|
+
let m;
|
|
3004
|
+
if (up === "L") this.#domX.last.push(0);
|
|
3005
|
+
else if (m = /^L-(\d+)$/.exec(up)) {
|
|
3006
|
+
if (Number(m[1]) > 30) throw new RangeError(`neram: invalid cron day of month "${item}" (L-n needs n from 0 to 30)`);
|
|
3007
|
+
this.#domX.last.push(Number(m[1]));
|
|
3008
|
+
} else if (up === "LW") this.#domX.lastWeekday = true;
|
|
3009
|
+
else if (m = /^(\d+)W$/.exec(up)) {
|
|
3010
|
+
const n = Number(m[1]);
|
|
3011
|
+
if (n < 1 || n > 31) throw new RangeError(`neram: invalid cron day of month "${item}" (nW needs n from 1 to 31)`);
|
|
3012
|
+
this.#domX.nearestWeekday.push(n);
|
|
3013
|
+
} else domPlain.push(item);
|
|
3014
|
+
}
|
|
3015
|
+
this.#dom = domPlain.length ? parseField(domPlain.join(","), "day of month", 1, 31) : { values: /* @__PURE__ */ new Set(), any: false };
|
|
3016
|
+
if (domPlain.length !== dom.split(",").length) this.#dom.any = false;
|
|
3017
|
+
const dowPlain = [];
|
|
3018
|
+
for (const item of dow.split(",")) {
|
|
3019
|
+
const up = item.toUpperCase();
|
|
3020
|
+
const dayNum2 = (s) => {
|
|
3021
|
+
const i = DAYS.indexOf(s);
|
|
3022
|
+
const n = i >= 0 ? i : Number(s);
|
|
3023
|
+
if (!Number.isInteger(n) || n < 0 || n > 7) throw new RangeError(`neram: invalid cron weekday "${s}"`);
|
|
3024
|
+
return n % 7;
|
|
3025
|
+
};
|
|
3026
|
+
let m;
|
|
3027
|
+
if (m = /^(\w+)#([1-5])$/.exec(up)) this.#dowX.nth.push([dayNum2(m[1]), Number(m[2])]);
|
|
3028
|
+
else if (m = /^(\w+)L$/.exec(up)) this.#dowX.last.push(dayNum2(m[1]));
|
|
3029
|
+
else dowPlain.push(item);
|
|
3030
|
+
}
|
|
3031
|
+
this.#dow = dowPlain.length ? parseField(dowPlain.join(","), "day of week", 0, 7, DAYS) : { values: /* @__PURE__ */ new Set(), any: false };
|
|
3032
|
+
if (dowPlain.length !== dow.split(",").length) this.#dow.any = false;
|
|
3033
|
+
if (this.#dow.values.delete(7)) this.#dow.values.add(0);
|
|
3034
|
+
this.#startMs = opts.start === void 0 ? void 0 : this.coerce(opts.start);
|
|
3035
|
+
this.#endMs = opts.end === void 0 ? void 0 : this.coerce(opts.end);
|
|
3036
|
+
}
|
|
3037
|
+
isInfinite() {
|
|
3038
|
+
return this.#endMs === void 0;
|
|
3039
|
+
}
|
|
3040
|
+
startMs() {
|
|
3041
|
+
return this.#startMs ?? Date.now();
|
|
3042
|
+
}
|
|
3043
|
+
#dayMatches(y, M, D) {
|
|
3044
|
+
const dow = (Math.floor(utcFromParts(y, M, D) / 864e5) % 7 + 11) % 7;
|
|
3045
|
+
const dim = daysInMonth(y, M);
|
|
3046
|
+
const domOk = () => {
|
|
3047
|
+
if (this.#dom.values.has(D)) return true;
|
|
3048
|
+
if (this.#domX.last.some((off) => D === dim - off)) return true;
|
|
3049
|
+
const weekdayNear = (target) => {
|
|
3050
|
+
let t = Math.min(target, dim);
|
|
3051
|
+
const wd = new Date(utcFromParts(y, M, t)).getUTCDay();
|
|
3052
|
+
if (wd === 6) t = t === 1 ? 3 : t - 1;
|
|
3053
|
+
else if (wd === 0) t = t === dim ? t - 2 : t + 1;
|
|
3054
|
+
return t;
|
|
3055
|
+
};
|
|
3056
|
+
if (this.#domX.nearestWeekday.some((n) => D === weekdayNear(n))) return true;
|
|
3057
|
+
if (this.#domX.lastWeekday) {
|
|
3058
|
+
let t = dim;
|
|
3059
|
+
while ([0, 6].includes(new Date(utcFromParts(y, M, t)).getUTCDay())) t--;
|
|
3060
|
+
if (D === t) return true;
|
|
3061
|
+
}
|
|
3062
|
+
return false;
|
|
3063
|
+
};
|
|
3064
|
+
const dowOk = () => this.#dow.values.has(dow) || this.#dowX.nth.some(([w, n]) => w === dow && Math.floor((D - 1) / 7) + 1 === n) || this.#dowX.last.some((w) => w === dow && D + 7 > dim);
|
|
3065
|
+
if (this.#dom.any || this.#dow.any) return domOk() && dowOk();
|
|
3066
|
+
return domOk() || dowOk();
|
|
3067
|
+
}
|
|
3068
|
+
/** Next matching wall time (UTC-ms of the wall clock) at or after `cur`, or null. */
|
|
3069
|
+
#nextWall(cur) {
|
|
3070
|
+
const limitYear = new Date(cur).getUTCFullYear() + 400;
|
|
3071
|
+
for (; ; ) {
|
|
3072
|
+
const { y, M, D, h, m, s } = wallOfMs(cur);
|
|
3073
|
+
if (y > limitYear || y > 9999) return null;
|
|
3074
|
+
if (!this.#month.values.has(M + 1)) {
|
|
3075
|
+
cur = utcFromParts(y, M + 1, 1);
|
|
3076
|
+
continue;
|
|
3077
|
+
}
|
|
3078
|
+
if (!this.#dayMatches(y, M, D)) {
|
|
3079
|
+
cur = utcFromParts(y, M, D + 1);
|
|
3080
|
+
continue;
|
|
3081
|
+
}
|
|
3082
|
+
if (!this.#hour.values.has(h)) {
|
|
3083
|
+
cur = utcFromParts(y, M, D, h + 1);
|
|
3084
|
+
continue;
|
|
3085
|
+
}
|
|
3086
|
+
if (!this.#min.values.has(m)) {
|
|
3087
|
+
cur = utcFromParts(y, M, D, h, m + 1);
|
|
3088
|
+
continue;
|
|
3089
|
+
}
|
|
3090
|
+
if (!this.#sec.values.has(s)) {
|
|
3091
|
+
cur = utcFromParts(y, M, D, h, m, s + 1);
|
|
3092
|
+
continue;
|
|
3093
|
+
}
|
|
3094
|
+
return cur;
|
|
3095
|
+
}
|
|
3096
|
+
}
|
|
3097
|
+
*iterate(fromMs) {
|
|
3098
|
+
let from = fromMs;
|
|
3099
|
+
if (this.#startMs !== void 0) from = Math.max(from, this.#startMs);
|
|
3100
|
+
if (!Number.isFinite(from)) from = Date.now();
|
|
3101
|
+
const unit = this.hasSeconds ? 1e3 : 6e4;
|
|
3102
|
+
const w = neram(from, { zone: this.tz });
|
|
3103
|
+
const [y, M, D, h, m, s, ms] = w.toArray();
|
|
3104
|
+
let cur = utcFromParts(y, M, D, h, m, this.hasSeconds ? s : 0);
|
|
3105
|
+
if (ms > 0 || !this.hasSeconds && (s > 0 || ms > 0)) cur += unit;
|
|
3106
|
+
let last = -Infinity;
|
|
3107
|
+
for (; ; ) {
|
|
3108
|
+
const next = this.#nextWall(cur);
|
|
3109
|
+
if (next === null) return;
|
|
3110
|
+
const inst = wallToEpoch(wallOfMs(next), this.tz);
|
|
3111
|
+
cur = next + unit;
|
|
3112
|
+
if (inst < from || inst <= last) continue;
|
|
3113
|
+
if (this.#endMs !== void 0 && inst > this.#endMs) return;
|
|
3114
|
+
last = inst;
|
|
3115
|
+
yield inst;
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
toString() {
|
|
3119
|
+
return this.expression;
|
|
3120
|
+
}
|
|
3121
|
+
toJSON() {
|
|
3122
|
+
return { expression: this.expression, tz: this.tz };
|
|
3123
|
+
}
|
|
3124
|
+
};
|
|
3125
|
+
function cron(expression, opts = {}) {
|
|
3126
|
+
return new Cron(expression, opts);
|
|
3127
|
+
}
|
|
3128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
3129
|
+
0 && (module.exports = {
|
|
3130
|
+
Duration,
|
|
3131
|
+
Neram,
|
|
3132
|
+
duration,
|
|
3133
|
+
isDuration,
|
|
3134
|
+
neram,
|
|
3135
|
+
recur
|
|
3136
|
+
});
|