rrule-temporal 1.6.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -34
- package/dist/index.cjs +5 -2761
- package/dist/index.d.cts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +1 -2757
- package/dist/src-B0RsDEOV.cjs +7577 -0
- package/dist/src-DEpYefSo.js +7554 -0
- package/dist/totext.cjs +19 -21
- package/dist/totext.js +13 -15
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,2758 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/RRuleTemporal.ts
|
|
3
|
-
const allowedFreq = [
|
|
4
|
-
"YEARLY",
|
|
5
|
-
"MONTHLY",
|
|
6
|
-
"WEEKLY",
|
|
7
|
-
"DAILY",
|
|
8
|
-
"HOURLY",
|
|
9
|
-
"MINUTELY",
|
|
10
|
-
"SECONDLY"
|
|
11
|
-
];
|
|
12
|
-
const allowedWeekdays = [
|
|
13
|
-
"MO",
|
|
14
|
-
"TU",
|
|
15
|
-
"WE",
|
|
16
|
-
"TH",
|
|
17
|
-
"FR",
|
|
18
|
-
"SA",
|
|
19
|
-
"SU"
|
|
20
|
-
];
|
|
21
|
-
const weekdayToIsoDay = {
|
|
22
|
-
MO: 1,
|
|
23
|
-
TU: 2,
|
|
24
|
-
WE: 3,
|
|
25
|
-
TH: 4,
|
|
26
|
-
FR: 5,
|
|
27
|
-
SA: 6,
|
|
28
|
-
SU: 7
|
|
29
|
-
};
|
|
30
|
-
const allowedFreqSet = new Set(allowedFreq);
|
|
31
|
-
const allowedWeekdaysSet = new Set(allowedWeekdays);
|
|
32
|
-
const byDayTokenRegex = new RegExp(`^([+-]?\\d{1,2})?(${allowedWeekdays.join("|")})$`);
|
|
33
|
-
const byDayWeekdaySuffixRegex = new RegExp(`(${allowedWeekdays.join("|")})$`);
|
|
34
|
-
const MS_PER_SECOND = 1e3;
|
|
35
|
-
const MS_PER_DAY = 24 * (60 * (60 * MS_PER_SECOND));
|
|
36
|
-
const MS_PER_WEEK = 7 * MS_PER_DAY;
|
|
37
|
-
const GREGORIAN_MONTH_LENGTHS = [
|
|
38
|
-
31,
|
|
39
|
-
28,
|
|
40
|
-
31,
|
|
41
|
-
30,
|
|
42
|
-
31,
|
|
43
|
-
30,
|
|
44
|
-
31,
|
|
45
|
-
31,
|
|
46
|
-
30,
|
|
47
|
-
31,
|
|
48
|
-
30,
|
|
49
|
-
31
|
|
50
|
-
];
|
|
51
|
-
const GREGORIAN_WEEKDAY_OFFSETS = [
|
|
52
|
-
0,
|
|
53
|
-
3,
|
|
54
|
-
2,
|
|
55
|
-
5,
|
|
56
|
-
0,
|
|
57
|
-
3,
|
|
58
|
-
5,
|
|
59
|
-
1,
|
|
60
|
-
4,
|
|
61
|
-
6,
|
|
62
|
-
2,
|
|
63
|
-
4
|
|
64
|
-
];
|
|
65
|
-
const NS_PER_MILLISECOND = BigInt(1e6);
|
|
66
|
-
const NS_PER_SECOND = BigInt(1e9);
|
|
67
|
-
const NS_PER_MINUTE = BigInt(60) * NS_PER_SECOND;
|
|
68
|
-
const NS_PER_HOUR = BigInt(60) * NS_PER_MINUTE;
|
|
69
|
-
const NS_PER_DAY = BigInt(24) * NS_PER_HOUR;
|
|
70
|
-
const NS_PER_WEEK = BigInt(7) * NS_PER_DAY;
|
|
71
|
-
function addIsoDays(dayOfWeek, deltaDays) {
|
|
72
|
-
return (dayOfWeek - 1 + deltaDays % 7 + 7) % 7 + 1;
|
|
73
|
-
}
|
|
74
|
-
function extractWeekdayToken(token) {
|
|
75
|
-
const weekday = token.toUpperCase().match(byDayWeekdaySuffixRegex)?.[1];
|
|
76
|
-
if (!weekday || !allowedWeekdaysSet.has(weekday)) return null;
|
|
77
|
-
return weekday;
|
|
78
|
-
}
|
|
79
|
-
function parseByDayToken(token) {
|
|
80
|
-
const m = token.toUpperCase().match(byDayTokenRegex);
|
|
81
|
-
if (!m) return null;
|
|
82
|
-
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
83
|
-
const weekday = m[2];
|
|
84
|
-
if (!weekday || !allowedWeekdaysSet.has(weekday)) return null;
|
|
85
|
-
return {
|
|
86
|
-
ord,
|
|
87
|
-
weekday
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function isTemporalZonedDateTimeInput(value) {
|
|
91
|
-
return typeof value === "object" && value !== null && typeof value.timeZoneId === "string" && typeof value.toString === "function";
|
|
92
|
-
}
|
|
93
|
-
function normalizeZonedDateTime(value, label) {
|
|
94
|
-
if (!isTemporalZonedDateTimeInput(value)) throw new Error(`${label} must be a ZonedDateTime`);
|
|
95
|
-
try {
|
|
96
|
-
return Temporal.ZonedDateTime.from(value.toString());
|
|
97
|
-
} catch {
|
|
98
|
-
throw new Error(`${label} must be a ZonedDateTime`);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
function normalizeZonedDateTimeList(values, label) {
|
|
102
|
-
return values?.map((value) => normalizeZonedDateTime(value, label));
|
|
103
|
-
}
|
|
104
|
-
function isIcsOpts(opts) {
|
|
105
|
-
return typeof opts.rruleString === "string";
|
|
106
|
-
}
|
|
107
|
-
function mergeDateLists(parsedDates, suppliedDates) {
|
|
108
|
-
const merged = [...parsedDates ?? [], ...normalizeZonedDateTimeList(suppliedDates, "Manual date") ?? []];
|
|
109
|
-
return merged.length > 0 ? merged : void 0;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Unfold lines according to RFC 5545 specification.
|
|
113
|
-
* Lines can be folded by inserting CRLF followed by a single linear white-space character.
|
|
114
|
-
* This function removes such folding by removing CRLF and the immediately following space/tab.
|
|
115
|
-
*/
|
|
116
|
-
function unfoldLine(foldedLine) {
|
|
117
|
-
return foldedLine.replace(/\r?\n[ \t]/g, "");
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Parse a single ICS date-time string into a Temporal.ZonedDateTime
|
|
121
|
-
*/
|
|
122
|
-
function parseIcsDateTime(dateStr, tzid, valueType) {
|
|
123
|
-
const isDate = valueType === "DATE" || !dateStr.includes("T");
|
|
124
|
-
const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}`;
|
|
125
|
-
if (isDate) return Temporal.PlainDate.from(isoDate).toZonedDateTime({ timeZone: tzid });
|
|
126
|
-
if (dateStr.endsWith("Z")) {
|
|
127
|
-
const iso = `${isoDate}T${dateStr.slice(9, 15)}Z`;
|
|
128
|
-
return Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
|
|
129
|
-
} else {
|
|
130
|
-
const iso = `${isoDate}T${dateStr.slice(9)}`;
|
|
131
|
-
return Temporal.PlainDateTime.from(iso).toZonedDateTime(tzid);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Parse date values from EXDATE or RDATE lines
|
|
136
|
-
*/
|
|
137
|
-
function parseDateLines(lines, linePrefix, defaultTzid) {
|
|
138
|
-
const dates = [];
|
|
139
|
-
for (const line of lines) {
|
|
140
|
-
const parsed = parseIcsDatePropertyLine(line, linePrefix);
|
|
141
|
-
if (!parsed) continue;
|
|
142
|
-
const timezone = parsed.tzid || defaultTzid;
|
|
143
|
-
const dateValues = parsed.value.split(",");
|
|
144
|
-
dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, parsed.valueType)));
|
|
145
|
-
}
|
|
146
|
-
return dates;
|
|
147
|
-
}
|
|
148
|
-
function parseIcsDatePropertyLine(line, propertyName) {
|
|
149
|
-
const colonIndex = line.indexOf(":");
|
|
150
|
-
if (colonIndex === -1) return null;
|
|
151
|
-
const head = line.slice(0, colonIndex);
|
|
152
|
-
const value = line.slice(colonIndex + 1);
|
|
153
|
-
const [name, ...params] = head.split(";");
|
|
154
|
-
if (name?.toUpperCase() !== propertyName) return null;
|
|
155
|
-
let valueType;
|
|
156
|
-
let tzid;
|
|
157
|
-
for (const param of params) {
|
|
158
|
-
const equalsIndex = param.indexOf("=");
|
|
159
|
-
if (equalsIndex === -1) continue;
|
|
160
|
-
const paramName = param.slice(0, equalsIndex).toUpperCase();
|
|
161
|
-
const paramValue = param.slice(equalsIndex + 1);
|
|
162
|
-
if (paramName === "VALUE") valueType = paramValue.toUpperCase();
|
|
163
|
-
else if (paramName === "TZID") tzid = paramValue;
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
valueType,
|
|
167
|
-
tzid,
|
|
168
|
-
value
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
function parseIntegerToken(token, label, strict) {
|
|
172
|
-
const trimmed = token.trim();
|
|
173
|
-
if (strict && !/^[+-]?\d+$/.test(trimmed)) throw new Error(`Invalid ${label} value: ${token}`);
|
|
174
|
-
return parseInt(trimmed, 10);
|
|
175
|
-
}
|
|
176
|
-
function parseNumberArray(val, sort = false, strict = false, label = "number") {
|
|
177
|
-
const arr = val.split(",").map((n) => parseIntegerToken(n, label, strict));
|
|
178
|
-
if (sort) return arr.sort((a, b) => a - b);
|
|
179
|
-
return arr;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Parse BYMONTH values, supporting RFC 7529 leap-month tokens with an "L" suffix (e.g., "5L").
|
|
183
|
-
* Returns a heterogeneous array keeping original tokens for serialization.
|
|
184
|
-
*/
|
|
185
|
-
function parseByMonthArray(val, strict = false) {
|
|
186
|
-
return val.split(",").map((tok) => {
|
|
187
|
-
const t = tok.trim();
|
|
188
|
-
if (/^\d+L$/i.test(t)) return t.toUpperCase();
|
|
189
|
-
const n = parseIntegerToken(t, "BYMONTH", strict);
|
|
190
|
-
return Number.isFinite(n) ? n : t;
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Parse either a full ICS snippet or an RRULE line into ManualOpts.
|
|
195
|
-
*
|
|
196
|
-
* @param input - String containing a `DTSTART` line followed by `RRULE` and
|
|
197
|
-
* optional `EXDATE`/`RDATE` lines. Can also be just an `RRULE:` line or
|
|
198
|
-
* recurrence pattern without DTSTART (dtstart must be provided separately).
|
|
199
|
-
* @param targetTimezone - Optional IANA time zone identifier used when the
|
|
200
|
-
* `DTSTART` line omits `TZID`. Floating times are interpreted in this zone
|
|
201
|
-
* and the resulting `tzid` field in the returned options will be set to this
|
|
202
|
-
* value. If `DTSTART` already specifies a `TZID` this parameter is ignored.
|
|
203
|
-
* @param dtstart - Optional DTSTART to use when input doesn't contain one.
|
|
204
|
-
*
|
|
205
|
-
* Examples:
|
|
206
|
-
* ```ts
|
|
207
|
-
* parseRRuleString(
|
|
208
|
-
* `DTSTART:20240101T090000\nRRULE:FREQ=DAILY`,
|
|
209
|
-
* 'America/New_York'
|
|
210
|
-
* );
|
|
211
|
-
* // => opts.tzid === 'America/New_York'
|
|
212
|
-
*
|
|
213
|
-
* parseRRuleString(
|
|
214
|
-
* `DTSTART;TZID=Europe/Paris:20240101T090000\nRRULE:FREQ=DAILY`
|
|
215
|
-
* );
|
|
216
|
-
* // => opts.tzid === 'Europe/Paris' (targetTimezone ignored)
|
|
217
|
-
*
|
|
218
|
-
* parseRRuleString(
|
|
219
|
-
* `FREQ=DAILY;COUNT=5`,
|
|
220
|
-
* 'UTC',
|
|
221
|
-
* Temporal.ZonedDateTime.from('2025-01-01T09:00:00[UTC]')
|
|
222
|
-
* );
|
|
223
|
-
* // => opts.dtstart from parameter
|
|
224
|
-
* ```
|
|
225
|
-
*/
|
|
226
|
-
function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
227
|
-
const unfoldedInput = unfoldLine(input).trim();
|
|
228
|
-
let parsedDtstart;
|
|
229
|
-
let tzid = targetTimezone;
|
|
230
|
-
let dtstartValueType = "DATE-TIME";
|
|
231
|
-
let dtstartHasTzid = false;
|
|
232
|
-
let dtstartIsUtc = false;
|
|
233
|
-
let rruleLine;
|
|
234
|
-
let exDate = [];
|
|
235
|
-
let rDate = [];
|
|
236
|
-
if (/^DTSTART/im.test(unfoldedInput)) {
|
|
237
|
-
const lines = unfoldedInput.split(/\s+/);
|
|
238
|
-
const dtLine = lines.find((line) => line.match(/^DTSTART/i));
|
|
239
|
-
const rrLine = lines.find((line) => line.match(/^RRULE:/i));
|
|
240
|
-
const exLines = lines.filter((line) => line.match(/^EXDATE/i));
|
|
241
|
-
const rLines = lines.filter((line) => line.match(/^RDATE/i));
|
|
242
|
-
const parsedDtLine = parseIcsDatePropertyLine(dtLine, "DTSTART");
|
|
243
|
-
if (!parsedDtLine) throw new Error("Invalid DTSTART in ICS snippet");
|
|
244
|
-
const { valueType, tzid: dtTzid, value: dtValue } = parsedDtLine;
|
|
245
|
-
dtstartValueType = (valueType || (dtValue?.includes("T") ? "DATE-TIME" : "DATE")).toUpperCase() === "DATE" ? "DATE" : "DATE-TIME";
|
|
246
|
-
dtstartHasTzid = Boolean(dtTzid);
|
|
247
|
-
dtstartIsUtc = Boolean(dtValue?.endsWith("Z"));
|
|
248
|
-
parsedDtstart = parseIcsDateTime(dtValue, dtTzid ?? targetTimezone ?? tzid ?? "UTC", dtstartValueType);
|
|
249
|
-
tzid = dtTzid ?? parsedDtstart.timeZoneId ?? targetTimezone ?? tzid ?? "UTC";
|
|
250
|
-
rruleLine = rrLine;
|
|
251
|
-
exDate = parseDateLines(exLines, "EXDATE", tzid ?? "UTC");
|
|
252
|
-
rDate = parseDateLines(rLines, "RDATE", tzid ?? "UTC");
|
|
253
|
-
} else {
|
|
254
|
-
parsedDtstart = dtstart ? normalizeZonedDateTime(dtstart, "dtstart") : void 0;
|
|
255
|
-
rruleLine = unfoldedInput;
|
|
256
|
-
if (parsedDtstart) {
|
|
257
|
-
tzid = parsedDtstart.timeZoneId;
|
|
258
|
-
dtstartValueType = "DATE-TIME";
|
|
259
|
-
dtstartHasTzid = true;
|
|
260
|
-
dtstartIsUtc = parsedDtstart.timeZoneId === "UTC";
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
const parts = rruleLine ? rruleLine.replace(/^RRULE:/i, "").split(";") : [];
|
|
264
|
-
const opts = {
|
|
265
|
-
dtstart: parsedDtstart,
|
|
266
|
-
tzid,
|
|
267
|
-
exDate: exDate.length > 0 ? exDate : void 0,
|
|
268
|
-
rDate: rDate.length > 0 ? rDate : void 0
|
|
269
|
-
};
|
|
270
|
-
let pendingSkip;
|
|
271
|
-
for (const part of parts) {
|
|
272
|
-
const [key, val] = part.split("=");
|
|
273
|
-
if (!key) continue;
|
|
274
|
-
switch (key.toUpperCase()) {
|
|
275
|
-
case "RSCALE":
|
|
276
|
-
if (val) {
|
|
277
|
-
opts.rscale = val.toUpperCase();
|
|
278
|
-
if (pendingSkip && !opts.skip) {
|
|
279
|
-
opts.skip = pendingSkip;
|
|
280
|
-
pendingSkip = void 0;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
break;
|
|
284
|
-
case "SKIP": {
|
|
285
|
-
const v = (val || "").toUpperCase();
|
|
286
|
-
if (![
|
|
287
|
-
"OMIT",
|
|
288
|
-
"BACKWARD",
|
|
289
|
-
"FORWARD"
|
|
290
|
-
].includes(v)) throw new Error(`Invalid SKIP value: ${val}`);
|
|
291
|
-
if (opts.rscale) opts.skip = v;
|
|
292
|
-
else pendingSkip = v;
|
|
293
|
-
break;
|
|
294
|
-
}
|
|
295
|
-
case "FREQ":
|
|
296
|
-
opts.freq = val.toUpperCase();
|
|
297
|
-
break;
|
|
298
|
-
case "INTERVAL":
|
|
299
|
-
opts.interval = parseIntegerToken(val, "INTERVAL", strict);
|
|
300
|
-
break;
|
|
301
|
-
case "COUNT":
|
|
302
|
-
opts.count = parseIntegerToken(val, "COUNT", strict);
|
|
303
|
-
break;
|
|
304
|
-
case "UNTIL": {
|
|
305
|
-
const untilHasTime = val.includes("T");
|
|
306
|
-
if (dtstartValueType === "DATE") {
|
|
307
|
-
if (untilHasTime) throw new Error("UNTIL rule part MUST have the same value type as DTSTART");
|
|
308
|
-
opts.until = parseIcsDateTime(val, tzid || "UTC", "DATE");
|
|
309
|
-
break;
|
|
310
|
-
}
|
|
311
|
-
if (!untilHasTime) {
|
|
312
|
-
if (strict) throw new Error("UNTIL rule part MUST have the same value type as DTSTART");
|
|
313
|
-
const localEndOfDay = parseIcsDateTime(val, tzid || "UTC", "DATE").with({
|
|
314
|
-
hour: 23,
|
|
315
|
-
minute: 59,
|
|
316
|
-
second: 59,
|
|
317
|
-
millisecond: 0,
|
|
318
|
-
microsecond: 0,
|
|
319
|
-
nanosecond: 0
|
|
320
|
-
});
|
|
321
|
-
opts.until = dtstartHasTzid || dtstartIsUtc ? localEndOfDay.withTimeZone("UTC") : localEndOfDay;
|
|
322
|
-
break;
|
|
323
|
-
}
|
|
324
|
-
if ((dtstartHasTzid || dtstartIsUtc) && !val.endsWith("Z")) throw new Error("UNTIL rule part MUST always be specified as a date with UTC time");
|
|
325
|
-
opts.until = parseIcsDateTime(val, tzid || "UTC", "DATE-TIME");
|
|
326
|
-
break;
|
|
327
|
-
}
|
|
328
|
-
case "BYHOUR":
|
|
329
|
-
opts.byHour = parseNumberArray(val, true, strict, "BYHOUR");
|
|
330
|
-
break;
|
|
331
|
-
case "BYMINUTE":
|
|
332
|
-
opts.byMinute = parseNumberArray(val, true, strict, "BYMINUTE");
|
|
333
|
-
break;
|
|
334
|
-
case "BYSECOND":
|
|
335
|
-
opts.bySecond = parseNumberArray(val, true, strict, "BYSECOND");
|
|
336
|
-
break;
|
|
337
|
-
case "BYDAY":
|
|
338
|
-
opts.byDay = val.split(",").map((token) => token.toUpperCase());
|
|
339
|
-
break;
|
|
340
|
-
case "BYMONTH":
|
|
341
|
-
opts.byMonth = parseByMonthArray(val, strict);
|
|
342
|
-
break;
|
|
343
|
-
case "BYMONTHDAY":
|
|
344
|
-
opts.byMonthDay = parseNumberArray(val, false, strict, "BYMONTHDAY");
|
|
345
|
-
break;
|
|
346
|
-
case "BYYEARDAY":
|
|
347
|
-
opts.byYearDay = parseNumberArray(val, false, strict, "BYYEARDAY");
|
|
348
|
-
break;
|
|
349
|
-
case "BYWEEKNO":
|
|
350
|
-
opts.byWeekNo = parseNumberArray(val, false, strict, "BYWEEKNO");
|
|
351
|
-
break;
|
|
352
|
-
case "BYSETPOS":
|
|
353
|
-
opts.bySetPos = parseNumberArray(val, false, strict, "BYSETPOS");
|
|
354
|
-
break;
|
|
355
|
-
case "WKST":
|
|
356
|
-
opts.wkst = val?.toUpperCase();
|
|
357
|
-
break;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
if (pendingSkip && !opts.rscale) throw new Error("SKIP MUST NOT be present unless RSCALE is present");
|
|
361
|
-
if (pendingSkip && opts.rscale && !opts.skip) opts.skip = pendingSkip;
|
|
362
|
-
return opts;
|
|
363
|
-
}
|
|
364
|
-
var RRuleTemporal = class RRuleTemporal {
|
|
365
|
-
static {
|
|
366
|
-
this.rscaleCalendarSupport = {};
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Normalize a ZonedDateTime to the polyfill implementation.
|
|
370
|
-
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
371
|
-
*/
|
|
372
|
-
static normalizeToPolyfill(zdt) {
|
|
373
|
-
return normalizeZonedDateTime(zdt, "Date");
|
|
374
|
-
}
|
|
375
|
-
static toInternalIterator(iterator) {
|
|
376
|
-
return iterator;
|
|
377
|
-
}
|
|
378
|
-
static toPublicDate(date) {
|
|
379
|
-
return date;
|
|
380
|
-
}
|
|
381
|
-
static toPublicDates(dates) {
|
|
382
|
-
return dates;
|
|
383
|
-
}
|
|
384
|
-
constructor(params) {
|
|
385
|
-
let manual;
|
|
386
|
-
if (isIcsOpts(params)) {
|
|
387
|
-
const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, params.strict ?? false);
|
|
388
|
-
if (!parsed.dtstart) throw new Error("dtstart is required - provide it either in rruleString or as a separate parameter");
|
|
389
|
-
const dtstart = RRuleTemporal.normalizeToPolyfill(parsed.dtstart);
|
|
390
|
-
this.tzid = parsed.tzid ?? params.tzid ?? "UTC";
|
|
391
|
-
this.originalDtstart = dtstart;
|
|
392
|
-
manual = {
|
|
393
|
-
...parsed,
|
|
394
|
-
dtstart,
|
|
395
|
-
rDate: mergeDateLists(parsed.rDate, params.rDate),
|
|
396
|
-
exDate: mergeDateLists(parsed.exDate, params.exDate),
|
|
397
|
-
count: params.count ?? parsed.count,
|
|
398
|
-
until: params.until ? RRuleTemporal.normalizeToPolyfill(params.until) : parsed.until,
|
|
399
|
-
strict: params.strict,
|
|
400
|
-
maxIterations: params.maxIterations,
|
|
401
|
-
includeDtstart: params.includeDtstart,
|
|
402
|
-
tzid: this.tzid
|
|
403
|
-
};
|
|
404
|
-
} else {
|
|
405
|
-
const dtstart = normalizeZonedDateTime(params.dtstart, "Manual dtstart");
|
|
406
|
-
manual = {
|
|
407
|
-
...params,
|
|
408
|
-
dtstart,
|
|
409
|
-
until: params.until ? normalizeZonedDateTime(params.until, "Manual until") : void 0,
|
|
410
|
-
rDate: normalizeZonedDateTimeList(params.rDate, "Manual rDate"),
|
|
411
|
-
exDate: normalizeZonedDateTimeList(params.exDate, "Manual exDate")
|
|
412
|
-
};
|
|
413
|
-
manual.tzid = manual.tzid || dtstart.timeZoneId;
|
|
414
|
-
this.tzid = manual.tzid;
|
|
415
|
-
this.originalDtstart = dtstart;
|
|
416
|
-
}
|
|
417
|
-
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
418
|
-
manual.interval = manual.interval ?? 1;
|
|
419
|
-
if (manual.interval <= 0) throw new Error("Cannot create RRule: interval must be greater than 0");
|
|
420
|
-
this.opts = this.sanitizeOpts(manual);
|
|
421
|
-
this.maxIterations = manual.maxIterations ?? 1e4;
|
|
422
|
-
this.includeDtstart = manual.includeDtstart ?? false;
|
|
423
|
-
this.parsedByDayTokens = this.buildParsedByDayTokens(this.opts.byDay);
|
|
424
|
-
this.simpleByDayIsoDays = this.buildByDayIsoDays(this.parsedByDayTokens, false);
|
|
425
|
-
this.allByDayIsoDays = this.buildByDayIsoDays(this.parsedByDayTokens, true);
|
|
426
|
-
this.hasOrdinalByDay = this.parsedByDayTokens?.some((token) => token.ord !== 0) ?? false;
|
|
427
|
-
this.canUseEpochMillisecondsPrecisionFlag = this.originalDtstart.microsecond === 0 && this.originalDtstart.nanosecond === 0 && (!this.opts.until || this.opts.until.microsecond === 0 && this.opts.until.nanosecond === 0);
|
|
428
|
-
this.timeSlotOffsetsMs = this.buildTimeSlotOffsetsMs();
|
|
429
|
-
this.numericByMonths = this.opts.byMonth?.filter((value) => typeof value === "number");
|
|
430
|
-
}
|
|
431
|
-
buildParsedByDayTokens(byDay) {
|
|
432
|
-
if (!byDay?.length) return void 0;
|
|
433
|
-
const tokens = byDay.map((tok) => {
|
|
434
|
-
const parsed = parseByDayToken(tok);
|
|
435
|
-
if (!parsed) return null;
|
|
436
|
-
return {
|
|
437
|
-
ord: parsed.ord,
|
|
438
|
-
weekday: parsed.weekday,
|
|
439
|
-
isoDay: weekdayToIsoDay[parsed.weekday]
|
|
440
|
-
};
|
|
441
|
-
}).filter((token) => token !== null);
|
|
442
|
-
return tokens.length > 0 ? tokens : void 0;
|
|
443
|
-
}
|
|
444
|
-
buildByDayIsoDays(tokens, includeOrdinals) {
|
|
445
|
-
if (!tokens?.length) return void 0;
|
|
446
|
-
const isoDays = tokens.filter((token) => includeOrdinals || token.ord === 0).map((token) => token.isoDay);
|
|
447
|
-
if (!isoDays.length) return void 0;
|
|
448
|
-
return [...new Set(isoDays)].sort((a, b) => a - b);
|
|
449
|
-
}
|
|
450
|
-
sanitizeNumericArray(arr, min, max, allowZero = false, sort = false) {
|
|
451
|
-
if (!arr) return void 0;
|
|
452
|
-
const sanitized = arr.filter((n) => Number.isInteger(n) && n >= min && n <= max && (allowZero || n !== 0));
|
|
453
|
-
if (sanitized.length === 0) return void 0;
|
|
454
|
-
return sort ? sanitized.sort((a, b) => a - b) : sanitized;
|
|
455
|
-
}
|
|
456
|
-
sanitizeByDay(byDay) {
|
|
457
|
-
const days = (byDay ?? []).filter((day) => Boolean(day) && typeof day === "string");
|
|
458
|
-
const normalized = [];
|
|
459
|
-
for (const day of days) {
|
|
460
|
-
const token = day.toUpperCase();
|
|
461
|
-
const parsed = parseByDayToken(token);
|
|
462
|
-
if (!parsed) throw new Error(`Invalid BYDAY value: ${day}`);
|
|
463
|
-
if (parsed.ord === 0 && /^[+-]?\d/.test(token)) throw new Error(`Invalid BYDAY value: ${day}`);
|
|
464
|
-
normalized.push(token);
|
|
465
|
-
}
|
|
466
|
-
return normalized.length > 0 ? normalized : void 0;
|
|
467
|
-
}
|
|
468
|
-
enforceStrictRfc(opts) {
|
|
469
|
-
if (!opts.strict) return;
|
|
470
|
-
const freq = opts.freq;
|
|
471
|
-
if (opts.byWeekNo && freq !== "YEARLY") throw new Error("BYWEEKNO MUST NOT be used unless FREQ=YEARLY");
|
|
472
|
-
if (opts.byYearDay && [
|
|
473
|
-
"DAILY",
|
|
474
|
-
"WEEKLY",
|
|
475
|
-
"MONTHLY"
|
|
476
|
-
].includes(freq)) throw new Error("BYYEARDAY MUST NOT be used when FREQ is DAILY, WEEKLY, or MONTHLY");
|
|
477
|
-
if (opts.byMonthDay && freq === "WEEKLY") throw new Error("BYMONTHDAY MUST NOT be used when FREQ is WEEKLY");
|
|
478
|
-
const hasNumericByDay = (opts.byDay ?? []).some((day) => /^[+-]?\d/.test(day));
|
|
479
|
-
if (hasNumericByDay && !["MONTHLY", "YEARLY"].includes(freq)) throw new Error("BYDAY with numeric value MUST NOT be used unless FREQ is MONTHLY or YEARLY");
|
|
480
|
-
if (hasNumericByDay && freq === "YEARLY" && opts.byWeekNo) throw new Error("BYDAY with numeric value MUST NOT be used with FREQ=YEARLY when BYWEEKNO is present");
|
|
481
|
-
const hasOtherBy = Boolean(opts.byDay || opts.byMonth || opts.byMonthDay || opts.byYearDay || opts.byWeekNo || opts.byHour || opts.byMinute || opts.bySecond);
|
|
482
|
-
if (opts.bySetPos && !hasOtherBy) throw new Error("BYSETPOS MUST be used with another BYxxx rule part");
|
|
483
|
-
}
|
|
484
|
-
sanitizeOpts(opts) {
|
|
485
|
-
if (!allowedFreqSet.has(opts.freq)) throw new Error(`Invalid FREQ value: ${opts.freq}`);
|
|
486
|
-
opts.byDay = this.sanitizeByDay(opts.byDay);
|
|
487
|
-
if (opts.wkst) {
|
|
488
|
-
const wkst = opts.wkst.toUpperCase();
|
|
489
|
-
if (!allowedWeekdaysSet.has(wkst)) throw new Error(`Invalid WKST value: ${opts.wkst}`);
|
|
490
|
-
opts.wkst = wkst;
|
|
491
|
-
}
|
|
492
|
-
if (opts.byMonth) {
|
|
493
|
-
const numeric = opts.byMonth.filter((v) => typeof v === "number");
|
|
494
|
-
const stringy = opts.byMonth.filter((v) => typeof v === "string");
|
|
495
|
-
const merged = [...this.sanitizeNumericArray(numeric, 1, 12, false, false) ?? [], ...stringy];
|
|
496
|
-
opts.byMonth = merged.length > 0 ? merged : void 0;
|
|
497
|
-
}
|
|
498
|
-
if (opts.rscale && !opts.skip) opts.skip = "OMIT";
|
|
499
|
-
opts.byMonthDay = this.sanitizeNumericArray(opts.byMonthDay, -31, 31, false, false);
|
|
500
|
-
opts.byYearDay = this.sanitizeNumericArray(opts.byYearDay, -366, 366, false, false);
|
|
501
|
-
opts.byWeekNo = this.sanitizeNumericArray(opts.byWeekNo, -53, 53, false, false);
|
|
502
|
-
opts.byHour = this.sanitizeNumericArray(opts.byHour, 0, 23, true, true);
|
|
503
|
-
opts.byMinute = this.sanitizeNumericArray(opts.byMinute, 0, 59, true, true);
|
|
504
|
-
opts.bySecond = this.sanitizeNumericArray(opts.bySecond, 0, 59, true, true);
|
|
505
|
-
if (opts.bySetPos) {
|
|
506
|
-
if (opts.bySetPos.some((p) => p === 0)) throw new Error("bySetPos may not contain 0");
|
|
507
|
-
opts.bySetPos = this.sanitizeNumericArray(opts.bySetPos, -Infinity, Infinity, false, false);
|
|
508
|
-
}
|
|
509
|
-
this.enforceStrictRfc(opts);
|
|
510
|
-
return opts;
|
|
511
|
-
}
|
|
512
|
-
rawAdvance(zdt) {
|
|
513
|
-
const { freq, interval } = this.opts;
|
|
514
|
-
switch (freq) {
|
|
515
|
-
case "DAILY": return zdt.add({ days: interval });
|
|
516
|
-
case "WEEKLY": return zdt.add({ weeks: interval });
|
|
517
|
-
case "MONTHLY": return zdt.add({ months: interval });
|
|
518
|
-
case "YEARLY": return zdt.add({ years: interval });
|
|
519
|
-
case "HOURLY": {
|
|
520
|
-
const originalHour = zdt.hour;
|
|
521
|
-
let next = zdt.add({ hours: interval });
|
|
522
|
-
if (next.hour === originalHour && interval === 1) next = next.add({ hours: interval });
|
|
523
|
-
return next;
|
|
524
|
-
}
|
|
525
|
-
case "MINUTELY": return zdt.add({ minutes: interval });
|
|
526
|
-
case "SECONDLY": return zdt.add({ seconds: interval });
|
|
527
|
-
default: throw new Error(`Unsupported FREQ: ${freq}`);
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
/** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE × BYSECOND
|
|
531
|
-
* combinations, keeping chronological order. If the options are not
|
|
532
|
-
* present the original date is returned unchanged.
|
|
533
|
-
*/
|
|
534
|
-
expandByTime(base) {
|
|
535
|
-
if (!this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond) return [base];
|
|
536
|
-
const hours = this.opts.byHour ?? [base.hour];
|
|
537
|
-
const minutes = this.opts.byMinute ?? [base.minute];
|
|
538
|
-
const seconds = this.opts.bySecond ?? [base.second];
|
|
539
|
-
if (hours.length === 1 && minutes.length === 1 && seconds.length === 1) {
|
|
540
|
-
const hour = hours[0];
|
|
541
|
-
const minute = minutes[0];
|
|
542
|
-
const second = seconds[0];
|
|
543
|
-
if (hour === base.hour && minute === base.minute && second === base.second) return [base];
|
|
544
|
-
return [base.with({
|
|
545
|
-
hour,
|
|
546
|
-
minute,
|
|
547
|
-
second
|
|
548
|
-
})];
|
|
549
|
-
}
|
|
550
|
-
const out = [];
|
|
551
|
-
for (const h of hours) for (const m of minutes) for (const s of seconds) out.push(base.with({
|
|
552
|
-
hour: h,
|
|
553
|
-
minute: m,
|
|
554
|
-
second: s
|
|
555
|
-
}));
|
|
556
|
-
return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
557
|
-
}
|
|
558
|
-
nextCandidateSameDate(zdt) {
|
|
559
|
-
const { freq, interval = 1, byHour, byMinute, bySecond } = this.opts;
|
|
560
|
-
if (freq === "HOURLY" && byHour && byHour.length === 1) return this.applyTimeOverride(zdt.add({ days: interval }));
|
|
561
|
-
if (freq === "MINUTELY" && byMinute && byMinute.length === 1) return this.applyTimeOverride(zdt.add({ hours: interval }));
|
|
562
|
-
if (bySecond && bySecond.length > 1) {
|
|
563
|
-
const idx = bySecond.indexOf(zdt.second);
|
|
564
|
-
if (idx !== -1 && idx < bySecond.length - 1) return zdt.with({ second: bySecond[idx + 1] });
|
|
565
|
-
}
|
|
566
|
-
if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute) {
|
|
567
|
-
const next = zdt.add({ minutes: interval });
|
|
568
|
-
if (byHour.includes(next.hour)) return next.with({ second: bySecond ? bySecond[0] : zdt.second });
|
|
569
|
-
const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
|
|
570
|
-
if (nextHour && nextHour > zdt.hour) return zdt.with({
|
|
571
|
-
hour: nextHour,
|
|
572
|
-
minute: 0,
|
|
573
|
-
second: bySecond ? bySecond[0] : zdt.second
|
|
574
|
-
});
|
|
575
|
-
return this.applyTimeOverride(zdt.add({ days: 1 }));
|
|
576
|
-
}
|
|
577
|
-
if (freq === "SECONDLY") {
|
|
578
|
-
let candidate = zdt;
|
|
579
|
-
if (bySecond && bySecond.length > 0) {
|
|
580
|
-
const nextSecondInList = bySecond.find((s) => s > candidate.second);
|
|
581
|
-
if (nextSecondInList !== void 0) return candidate.with({ second: nextSecondInList });
|
|
582
|
-
candidate = candidate.with({ second: bySecond[0] }).add({ minutes: 1 });
|
|
583
|
-
} else candidate = candidate.add({ seconds: interval });
|
|
584
|
-
if (byMinute && byMinute.length > 0) {
|
|
585
|
-
if (!byMinute.includes(candidate.minute) || candidate.minute === zdt.minute && candidate.second < zdt.second) {
|
|
586
|
-
const nextMinuteInList = byMinute.find((m) => m > candidate.minute);
|
|
587
|
-
if (nextMinuteInList !== void 0) return candidate.with({
|
|
588
|
-
minute: nextMinuteInList,
|
|
589
|
-
second: bySecond ? bySecond[0] : 0
|
|
590
|
-
});
|
|
591
|
-
candidate = candidate.with({
|
|
592
|
-
minute: byMinute[0],
|
|
593
|
-
second: bySecond ? bySecond[0] : 0
|
|
594
|
-
}).add({ hours: 1 });
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
if (byHour && byHour.length > 0) {
|
|
598
|
-
if (!byHour.includes(candidate.hour) || candidate.hour === zdt.hour && candidate.minute < zdt.minute) {
|
|
599
|
-
const nextHourInList = byHour.find((h) => h > candidate.hour);
|
|
600
|
-
if (nextHourInList !== void 0) return candidate.with({
|
|
601
|
-
hour: nextHourInList,
|
|
602
|
-
minute: byMinute ? byMinute[0] : 0,
|
|
603
|
-
second: bySecond ? bySecond[0] : 0
|
|
604
|
-
});
|
|
605
|
-
candidate = candidate.with({
|
|
606
|
-
hour: byHour[0],
|
|
607
|
-
minute: byMinute ? byMinute[0] : 0,
|
|
608
|
-
second: bySecond ? bySecond[0] : 0
|
|
609
|
-
}).add({ days: 1 });
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
return candidate;
|
|
613
|
-
}
|
|
614
|
-
if (byMinute && byMinute.length > 1) {
|
|
615
|
-
const idx = byMinute.indexOf(zdt.minute);
|
|
616
|
-
if (idx !== -1 && idx < byMinute.length - 1) return zdt.with({
|
|
617
|
-
minute: byMinute[idx + 1],
|
|
618
|
-
second: bySecond ? bySecond[0] : zdt.second
|
|
619
|
-
});
|
|
620
|
-
if (freq === "MINUTELY" && idx === byMinute.length - 1) {
|
|
621
|
-
if (byHour && byHour.length > 0) {
|
|
622
|
-
const currentHourIdx = byHour.indexOf(zdt.hour);
|
|
623
|
-
if (currentHourIdx !== -1 && currentHourIdx < byHour.length - 1) return zdt.with({
|
|
624
|
-
hour: byHour[currentHourIdx + 1],
|
|
625
|
-
minute: byMinute[0],
|
|
626
|
-
second: bySecond ? bySecond[0] : zdt.second
|
|
627
|
-
});
|
|
628
|
-
else return this.applyTimeOverride(zdt.add({ days: 1 }));
|
|
629
|
-
}
|
|
630
|
-
return zdt.add({ hours: interval }).with({
|
|
631
|
-
minute: byMinute[0],
|
|
632
|
-
second: bySecond ? bySecond[0] : zdt.second
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
if (byHour && byHour.length > 1) {
|
|
637
|
-
const idx = byHour.indexOf(zdt.hour);
|
|
638
|
-
if (idx !== -1 && idx < byHour.length - 1) return zdt.with({
|
|
639
|
-
hour: byHour[idx + 1],
|
|
640
|
-
minute: byMinute ? byMinute[0] : zdt.minute,
|
|
641
|
-
second: bySecond ? bySecond[0] : zdt.second
|
|
642
|
-
});
|
|
643
|
-
}
|
|
644
|
-
if (freq === "HOURLY" && byHour && byHour.length > 1) return this.applyTimeOverride(zdt.add({ days: 1 }));
|
|
645
|
-
return this.applyTimeOverride(this.rawAdvance(zdt));
|
|
646
|
-
}
|
|
647
|
-
applyTimeOverride(zdt) {
|
|
648
|
-
const { byHour, byMinute, bySecond } = this.opts;
|
|
649
|
-
let dt = zdt;
|
|
650
|
-
if (byHour) dt = dt.with({ hour: byHour[0] });
|
|
651
|
-
if (byMinute) dt = dt.with({ minute: byMinute[0] });
|
|
652
|
-
if (bySecond) dt = dt.with({ second: bySecond[0] });
|
|
653
|
-
return dt;
|
|
654
|
-
}
|
|
655
|
-
computeFirst() {
|
|
656
|
-
let zdt = this.originalDtstart;
|
|
657
|
-
if (this.opts.byWeekNo?.length && [
|
|
658
|
-
"DAILY",
|
|
659
|
-
"HOURLY",
|
|
660
|
-
"MINUTELY",
|
|
661
|
-
"SECONDLY"
|
|
662
|
-
].includes(this.opts.freq)) {
|
|
663
|
-
let targetWeek = this.opts.byWeekNo[0];
|
|
664
|
-
let targetYear = zdt.year;
|
|
665
|
-
while (targetYear <= zdt.year + 10) {
|
|
666
|
-
const jan1 = zdt.with({
|
|
667
|
-
year: targetYear,
|
|
668
|
-
month: 1,
|
|
669
|
-
day: 1
|
|
670
|
-
});
|
|
671
|
-
const dec31 = zdt.with({
|
|
672
|
-
year: targetYear,
|
|
673
|
-
month: 12,
|
|
674
|
-
day: 31
|
|
675
|
-
});
|
|
676
|
-
let hasTargetWeek = false;
|
|
677
|
-
if (targetWeek > 0) {
|
|
678
|
-
let maxWeek = 52;
|
|
679
|
-
if (jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4) maxWeek = 53;
|
|
680
|
-
hasTargetWeek = targetWeek <= maxWeek;
|
|
681
|
-
} else {
|
|
682
|
-
let maxWeek = 52;
|
|
683
|
-
if (jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4) maxWeek = 53;
|
|
684
|
-
hasTargetWeek = -targetWeek <= maxWeek;
|
|
685
|
-
}
|
|
686
|
-
if (hasTargetWeek) {
|
|
687
|
-
const firstThursday = jan1.add({ days: (4 - jan1.dayOfWeek + 7) % 7 });
|
|
688
|
-
let weekStart;
|
|
689
|
-
if (targetWeek > 0) weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: targetWeek - 1 });
|
|
690
|
-
else {
|
|
691
|
-
const lastWeek = jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4 ? 53 : 52;
|
|
692
|
-
weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: lastWeek + targetWeek });
|
|
693
|
-
}
|
|
694
|
-
if (this.opts.byDay?.length) {
|
|
695
|
-
const dayMap = weekdayToIsoDay;
|
|
696
|
-
const targetDays = this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null).map((day) => dayMap[day]).filter(Boolean);
|
|
697
|
-
if (targetDays.length) {
|
|
698
|
-
const firstCandidate = targetDays.map((dayOfWeek) => {
|
|
699
|
-
const delta = (dayOfWeek - weekStart.dayOfWeek + 7) % 7;
|
|
700
|
-
return weekStart.add({ days: delta });
|
|
701
|
-
}).sort((a, b) => Temporal.ZonedDateTime.compare(a, b))[0];
|
|
702
|
-
if (firstCandidate && Temporal.ZonedDateTime.compare(firstCandidate, this.originalDtstart) >= 0) {
|
|
703
|
-
zdt = firstCandidate;
|
|
704
|
-
break;
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
} else if (Temporal.ZonedDateTime.compare(weekStart, this.originalDtstart) >= 0) {
|
|
708
|
-
zdt = weekStart;
|
|
709
|
-
break;
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
targetYear++;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
if (this.opts.byDay?.length && !this.opts.byWeekNo) {
|
|
716
|
-
const dayMap = weekdayToIsoDay;
|
|
717
|
-
if (this.opts.byDay.some((tok) => /^[+-]?\d/.test(tok)) && this.opts.byMonth && (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY")) {
|
|
718
|
-
const months = this.opts.byMonth.filter((v) => typeof v === "number").sort((a, b) => a - b);
|
|
719
|
-
let foundFirst = false;
|
|
720
|
-
for (let year = zdt.year; year <= zdt.year + 10 && !foundFirst; year++) for (const month of months) {
|
|
721
|
-
if (year === zdt.year && month < zdt.month) continue;
|
|
722
|
-
const monthSample = zdt.with({
|
|
723
|
-
year,
|
|
724
|
-
month,
|
|
725
|
-
day: 1
|
|
726
|
-
});
|
|
727
|
-
const monthlyOccs = this.generateMonthlyOccurrences(monthSample);
|
|
728
|
-
for (const occ of monthlyOccs) if (Temporal.ZonedDateTime.compare(occ, zdt) >= 0) {
|
|
729
|
-
if (!occ.toPlainDate().equals(zdt.toPlainDate())) zdt = this.applyTimeOverride(occ.with({
|
|
730
|
-
hour: 0,
|
|
731
|
-
minute: 0,
|
|
732
|
-
second: 0
|
|
733
|
-
}));
|
|
734
|
-
else zdt = occ;
|
|
735
|
-
foundFirst = true;
|
|
736
|
-
break;
|
|
737
|
-
}
|
|
738
|
-
if (foundFirst) break;
|
|
739
|
-
}
|
|
740
|
-
} else {
|
|
741
|
-
let deltas;
|
|
742
|
-
const weekdayTokens = this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((tok) => tok !== null);
|
|
743
|
-
if ([
|
|
744
|
-
"DAILY",
|
|
745
|
-
"HOURLY",
|
|
746
|
-
"MINUTELY",
|
|
747
|
-
"SECONDLY"
|
|
748
|
-
].includes(this.opts.freq) && weekdayTokens.length === this.opts.byDay.length) deltas = weekdayTokens.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
|
|
749
|
-
else deltas = weekdayTokens.map((wdTok) => (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7);
|
|
750
|
-
if (deltas.length) zdt = zdt.add({ days: Math.min(...deltas) });
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
const { byHour, byMinute, bySecond } = this.opts;
|
|
754
|
-
if (this.opts.freq === "HOURLY" && !byHour && Temporal.ZonedDateTime.compare(zdt.with({
|
|
755
|
-
hour: 0,
|
|
756
|
-
minute: 0,
|
|
757
|
-
second: 0,
|
|
758
|
-
microsecond: 0,
|
|
759
|
-
nanosecond: 0
|
|
760
|
-
}), this.originalDtstart) > 0) zdt = zdt.with({
|
|
761
|
-
hour: 0,
|
|
762
|
-
minute: 0,
|
|
763
|
-
second: 0,
|
|
764
|
-
microsecond: 0,
|
|
765
|
-
nanosecond: 0
|
|
766
|
-
});
|
|
767
|
-
if (this.opts.freq === "MINUTELY" && !byMinute && Temporal.ZonedDateTime.compare(zdt.with({
|
|
768
|
-
hour: 0,
|
|
769
|
-
minute: 0,
|
|
770
|
-
second: 0,
|
|
771
|
-
microsecond: 0,
|
|
772
|
-
nanosecond: 0
|
|
773
|
-
}), this.originalDtstart) > 0) zdt = zdt.with({
|
|
774
|
-
hour: 0,
|
|
775
|
-
minute: 0,
|
|
776
|
-
second: 0,
|
|
777
|
-
microsecond: 0,
|
|
778
|
-
nanosecond: 0
|
|
779
|
-
});
|
|
780
|
-
if (this.opts.freq === "SECONDLY" && this.opts.byWeekNo?.length && !bySecond && Temporal.ZonedDateTime.compare(zdt.with({
|
|
781
|
-
hour: 0,
|
|
782
|
-
minute: 0,
|
|
783
|
-
second: 0,
|
|
784
|
-
microsecond: 0,
|
|
785
|
-
nanosecond: 0
|
|
786
|
-
}), this.originalDtstart) > 0) zdt = zdt.with({
|
|
787
|
-
hour: 0,
|
|
788
|
-
minute: 0,
|
|
789
|
-
second: 0,
|
|
790
|
-
microsecond: 0,
|
|
791
|
-
nanosecond: 0
|
|
792
|
-
});
|
|
793
|
-
if (byHour || byMinute || bySecond) {
|
|
794
|
-
const candidates = this.expandByTime(zdt);
|
|
795
|
-
for (const candidate of candidates) if (Temporal.ZonedDateTime.compare(candidate, this.originalDtstart) >= 0) return candidate;
|
|
796
|
-
zdt = this.applyTimeOverride(this.rawAdvance(zdt));
|
|
797
|
-
}
|
|
798
|
-
return zdt;
|
|
799
|
-
}
|
|
800
|
-
matchesByDay(zdt) {
|
|
801
|
-
const { byDay, freq } = this.opts;
|
|
802
|
-
if (!byDay) return true;
|
|
803
|
-
if (!this.hasOrdinalByDay) return this.simpleByDayIsoDays?.includes(zdt.dayOfWeek) ?? false;
|
|
804
|
-
for (const token of this.parsedByDayTokens ?? []) {
|
|
805
|
-
if (freq === "DAILY" && zdt.dayOfWeek === token.isoDay) return true;
|
|
806
|
-
if (token.ord === 0) {
|
|
807
|
-
if (zdt.dayOfWeek === token.isoDay) return true;
|
|
808
|
-
continue;
|
|
809
|
-
}
|
|
810
|
-
const month = zdt.month;
|
|
811
|
-
let dt = zdt.with({ day: 1 });
|
|
812
|
-
const candidates = [];
|
|
813
|
-
while (dt.month === month) {
|
|
814
|
-
if (dt.dayOfWeek === token.isoDay) candidates.push(dt.day);
|
|
815
|
-
dt = dt.add({ days: 1 });
|
|
816
|
-
}
|
|
817
|
-
if (candidates[token.ord > 0 ? token.ord - 1 : candidates.length + token.ord] === zdt.day) return true;
|
|
818
|
-
}
|
|
819
|
-
return false;
|
|
820
|
-
}
|
|
821
|
-
matchesByMonth(zdt) {
|
|
822
|
-
const { byMonth } = this.opts;
|
|
823
|
-
if (!byMonth) return true;
|
|
824
|
-
const nums = byMonth.filter((v) => typeof v === "number");
|
|
825
|
-
if (nums.length === 0) return true;
|
|
826
|
-
return nums.includes(zdt.month);
|
|
827
|
-
}
|
|
828
|
-
matchesNumericConstraint(value, constraints, maxPositiveValue) {
|
|
829
|
-
return constraints.some((c) => {
|
|
830
|
-
return value === (c > 0 ? c : maxPositiveValue + c + 1);
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
matchesByMonthDay(zdt) {
|
|
834
|
-
const { byMonthDay } = this.opts;
|
|
835
|
-
if (!byMonthDay) return true;
|
|
836
|
-
const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
837
|
-
return this.matchesNumericConstraint(zdt.day, byMonthDay, lastDay);
|
|
838
|
-
}
|
|
839
|
-
matchesByHour(zdt) {
|
|
840
|
-
const { byHour } = this.opts;
|
|
841
|
-
if (!byHour) return true;
|
|
842
|
-
if (byHour.includes(zdt.hour)) return true;
|
|
843
|
-
for (const h of byHour) if (zdt.with({ hour: h }).hour === zdt.hour) return true;
|
|
844
|
-
return false;
|
|
845
|
-
}
|
|
846
|
-
matchesByMinute(zdt) {
|
|
847
|
-
const { byMinute } = this.opts;
|
|
848
|
-
if (!byMinute) return true;
|
|
849
|
-
return byMinute.includes(zdt.minute);
|
|
850
|
-
}
|
|
851
|
-
matchesBySecond(zdt) {
|
|
852
|
-
const { bySecond } = this.opts;
|
|
853
|
-
if (!bySecond) return true;
|
|
854
|
-
return bySecond.includes(zdt.second);
|
|
855
|
-
}
|
|
856
|
-
matchesAll(zdt) {
|
|
857
|
-
return this.matchesByMonth(zdt) && this.matchesByWeekNo(zdt) && this.matchesByYearDay(zdt) && this.matchesByMonthDay(zdt) && this.matchesByDay(zdt) && this.matchesByHour(zdt) && this.matchesByMinute(zdt) && this.matchesBySecond(zdt);
|
|
858
|
-
}
|
|
859
|
-
matchesByYearDay(zdt) {
|
|
860
|
-
const { byYearDay } = this.opts;
|
|
861
|
-
if (!byYearDay) return true;
|
|
862
|
-
const dayOfYear = zdt.dayOfYear;
|
|
863
|
-
const last = zdt.with({
|
|
864
|
-
month: 12,
|
|
865
|
-
day: 31
|
|
866
|
-
}).dayOfYear;
|
|
867
|
-
return this.matchesNumericConstraint(dayOfYear, byYearDay, last);
|
|
868
|
-
}
|
|
869
|
-
getIsoWeekInfo(zdt) {
|
|
870
|
-
const thursday = zdt.add({ days: 4 - zdt.dayOfWeek });
|
|
871
|
-
const year = thursday.year;
|
|
872
|
-
const jan1 = zdt.with({
|
|
873
|
-
year,
|
|
874
|
-
month: 1,
|
|
875
|
-
day: 1
|
|
876
|
-
});
|
|
877
|
-
const firstThursday = jan1.add({ days: (4 - jan1.dayOfWeek + 7) % 7 });
|
|
878
|
-
const diffDays = thursday.toPlainDate().since(firstThursday.toPlainDate()).days;
|
|
879
|
-
return {
|
|
880
|
-
week: Math.floor(diffDays / 7) + 1,
|
|
881
|
-
year
|
|
882
|
-
};
|
|
883
|
-
}
|
|
884
|
-
matchesByWeekNo(zdt) {
|
|
885
|
-
const { byWeekNo } = this.opts;
|
|
886
|
-
if (!byWeekNo) return true;
|
|
887
|
-
const { week, year } = this.getIsoWeekInfo(zdt);
|
|
888
|
-
const jan1 = zdt.with({
|
|
889
|
-
year,
|
|
890
|
-
month: 1,
|
|
891
|
-
day: 1
|
|
892
|
-
});
|
|
893
|
-
const isLeapYear = jan1.inLeapYear;
|
|
894
|
-
const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
|
|
895
|
-
return byWeekNo.some((wn) => {
|
|
896
|
-
if (wn > 0) return week === wn;
|
|
897
|
-
else return week === lastWeek + wn + 1;
|
|
898
|
-
});
|
|
899
|
-
}
|
|
900
|
-
options() {
|
|
901
|
-
return this.cloneOptions();
|
|
902
|
-
}
|
|
903
|
-
cloneOptions() {
|
|
904
|
-
const { byHour, byMinute, bySecond, byDay, byMonth, byMonthDay, byYearDay, byWeekNo, bySetPos, rDate, exDate, ...rest } = this.opts;
|
|
905
|
-
return {
|
|
906
|
-
...rest,
|
|
907
|
-
byHour: byHour ? [...byHour] : void 0,
|
|
908
|
-
byMinute: byMinute ? [...byMinute] : void 0,
|
|
909
|
-
bySecond: bySecond ? [...bySecond] : void 0,
|
|
910
|
-
byDay: byDay ? [...byDay] : void 0,
|
|
911
|
-
byMonth: byMonth ? [...byMonth] : void 0,
|
|
912
|
-
byMonthDay: byMonthDay ? [...byMonthDay] : void 0,
|
|
913
|
-
byYearDay: byYearDay ? [...byYearDay] : void 0,
|
|
914
|
-
byWeekNo: byWeekNo ? [...byWeekNo] : void 0,
|
|
915
|
-
bySetPos: bySetPos ? [...bySetPos] : void 0,
|
|
916
|
-
rDate: rDate ? [...rDate] : void 0,
|
|
917
|
-
exDate: exDate ? [...exDate] : void 0
|
|
918
|
-
};
|
|
919
|
-
}
|
|
920
|
-
cloneUpdateOptions(updates) {
|
|
921
|
-
const cloned = {};
|
|
922
|
-
if (Object.prototype.hasOwnProperty.call(updates, "byHour")) cloned.byHour = Array.isArray(updates.byHour) ? [...updates.byHour] : updates.byHour;
|
|
923
|
-
if (Object.prototype.hasOwnProperty.call(updates, "byMinute")) cloned.byMinute = Array.isArray(updates.byMinute) ? [...updates.byMinute] : updates.byMinute;
|
|
924
|
-
if (Object.prototype.hasOwnProperty.call(updates, "bySecond")) cloned.bySecond = Array.isArray(updates.bySecond) ? [...updates.bySecond] : updates.bySecond;
|
|
925
|
-
if (Object.prototype.hasOwnProperty.call(updates, "byDay")) cloned.byDay = Array.isArray(updates.byDay) ? [...updates.byDay] : updates.byDay;
|
|
926
|
-
if (Object.prototype.hasOwnProperty.call(updates, "byMonth")) cloned.byMonth = Array.isArray(updates.byMonth) ? [...updates.byMonth] : updates.byMonth;
|
|
927
|
-
if (Object.prototype.hasOwnProperty.call(updates, "byMonthDay")) cloned.byMonthDay = Array.isArray(updates.byMonthDay) ? [...updates.byMonthDay] : updates.byMonthDay;
|
|
928
|
-
if (Object.prototype.hasOwnProperty.call(updates, "byYearDay")) cloned.byYearDay = Array.isArray(updates.byYearDay) ? [...updates.byYearDay] : updates.byYearDay;
|
|
929
|
-
if (Object.prototype.hasOwnProperty.call(updates, "byWeekNo")) cloned.byWeekNo = Array.isArray(updates.byWeekNo) ? [...updates.byWeekNo] : updates.byWeekNo;
|
|
930
|
-
if (Object.prototype.hasOwnProperty.call(updates, "bySetPos")) cloned.bySetPos = Array.isArray(updates.bySetPos) ? [...updates.bySetPos] : updates.bySetPos;
|
|
931
|
-
if (Object.prototype.hasOwnProperty.call(updates, "rDate")) cloned.rDate = Array.isArray(updates.rDate) ? [...updates.rDate] : updates.rDate;
|
|
932
|
-
if (Object.prototype.hasOwnProperty.call(updates, "exDate")) cloned.exDate = Array.isArray(updates.exDate) ? [...updates.exDate] : updates.exDate;
|
|
933
|
-
return cloned;
|
|
934
|
-
}
|
|
935
|
-
/**
|
|
936
|
-
* Create a new {@link RRuleTemporal} instance with modified options while keeping the current one unchanged.
|
|
937
|
-
*
|
|
938
|
-
* @example
|
|
939
|
-
* ```ts
|
|
940
|
-
* const updated = rule.with({byMonthDay: [3]});
|
|
941
|
-
* ```
|
|
942
|
-
*/
|
|
943
|
-
with(updates) {
|
|
944
|
-
return new RRuleTemporal({
|
|
945
|
-
...this.cloneOptions(),
|
|
946
|
-
...updates,
|
|
947
|
-
...this.cloneUpdateOptions(updates),
|
|
948
|
-
tzid: updates.tzid ?? this.opts.tzid,
|
|
949
|
-
dtstart: updates.dtstart ?? this.opts.dtstart
|
|
950
|
-
});
|
|
951
|
-
}
|
|
952
|
-
addDtstartIfNeeded(dates, iterator) {
|
|
953
|
-
if (this.includeDtstart && !this.matchesAll(this.originalDtstart)) {
|
|
954
|
-
if (iterator && this.isExcluded(this.originalDtstart)) return true;
|
|
955
|
-
if (iterator && !iterator(this.originalDtstart, dates.length)) return false;
|
|
956
|
-
dates.push(this.originalDtstart);
|
|
957
|
-
if (this.shouldBreakForCountLimit(dates.length)) return false;
|
|
958
|
-
}
|
|
959
|
-
return true;
|
|
960
|
-
}
|
|
961
|
-
canUseUtcLinearFastPath(iterator) {
|
|
962
|
-
if (iterator || this.tzid !== "UTC" || this.opts.rscale || this.opts.rDate || this.opts.exDate) return false;
|
|
963
|
-
if (this.opts.byMonth || this.opts.byMonthDay || this.opts.byYearDay || this.opts.byWeekNo || this.opts.bySetPos) return false;
|
|
964
|
-
switch (this.opts.freq) {
|
|
965
|
-
case "DAILY": return !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond && !this.hasOrdinalByDay;
|
|
966
|
-
case "HOURLY":
|
|
967
|
-
case "MINUTELY": return !this.opts.byDay && !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond;
|
|
968
|
-
default: return false;
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
canUseUtcWeeklyFastPath(iterator) {
|
|
972
|
-
return !iterator && this.tzid === "UTC" && this.opts.freq === "WEEKLY" && !this.opts.rscale && !this.opts.rDate && !this.opts.exDate && !this.opts.byMonth && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo && !this.opts.bySetPos && !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond && !this.hasOrdinalByDay;
|
|
973
|
-
}
|
|
974
|
-
canUseUtcMonthlyFastPath(iterator) {
|
|
975
|
-
return !iterator && this.tzid === "UTC" && this.opts.freq === "MONTHLY" && !this.opts.rscale && !this.opts.rDate && !this.opts.exDate && !this.opts.byYearDay && !this.opts.byWeekNo && this.canUseEpochMillisecondsPrecisionFlag && !!(this.opts.byDay || this.opts.byMonthDay);
|
|
976
|
-
}
|
|
977
|
-
utcZdtFromEpochNanoseconds(epochNanoseconds) {
|
|
978
|
-
return Temporal.Instant.fromEpochNanoseconds(epochNanoseconds).toZonedDateTimeISO("UTC");
|
|
979
|
-
}
|
|
980
|
-
utcZdtFromEpochMilliseconds(epochMilliseconds) {
|
|
981
|
-
return Temporal.Instant.fromEpochMilliseconds(epochMilliseconds).toZonedDateTimeISO("UTC");
|
|
982
|
-
}
|
|
983
|
-
canUseUtcEpochMillisecondsPrecision() {
|
|
984
|
-
return this.canUseEpochMillisecondsPrecisionFlag;
|
|
985
|
-
}
|
|
986
|
-
buildTimeSlotOffsetsMs() {
|
|
987
|
-
if (!this.canUseEpochMillisecondsPrecisionFlag) return void 0;
|
|
988
|
-
const hours = this.opts.byHour ?? [this.originalDtstart.hour];
|
|
989
|
-
const minutes = this.opts.byMinute ?? [this.originalDtstart.minute];
|
|
990
|
-
const seconds = this.opts.bySecond ?? [this.originalDtstart.second];
|
|
991
|
-
const baseMilliseconds = this.originalDtstart.millisecond;
|
|
992
|
-
const offsets = [];
|
|
993
|
-
for (const hour of hours) for (const minute of minutes) for (const second of seconds) offsets.push(((hour * 60 + minute) * 60 + second) * MS_PER_SECOND + baseMilliseconds);
|
|
994
|
-
return offsets;
|
|
995
|
-
}
|
|
996
|
-
findFirstMatchingDailyStep(startDayOfWeek, stepDays, allowedDays) {
|
|
997
|
-
let dayOfWeek = startDayOfWeek;
|
|
998
|
-
for (let steps = 0; steps < 7; steps++) {
|
|
999
|
-
if (allowedDays.includes(dayOfWeek)) return steps;
|
|
1000
|
-
dayOfWeek = addIsoDays(dayOfWeek, stepDays);
|
|
1001
|
-
}
|
|
1002
|
-
return null;
|
|
1003
|
-
}
|
|
1004
|
-
allUtcFastPath(iterator) {
|
|
1005
|
-
if (this.canUseUtcLinearFastPath(iterator)) switch (this.opts.freq) {
|
|
1006
|
-
case "DAILY": return this._allUtcDailySimple();
|
|
1007
|
-
case "HOURLY": return this._allUtcFixedStepSimple(NS_PER_HOUR * BigInt(this.opts.interval));
|
|
1008
|
-
case "MINUTELY": return this._allUtcFixedStepSimple(NS_PER_MINUTE * BigInt(this.opts.interval));
|
|
1009
|
-
}
|
|
1010
|
-
if (this.canUseUtcMonthlyFastPath(iterator)) return this._allUtcMonthlyByDayOrMonthDay();
|
|
1011
|
-
if (this.canUseUtcWeeklyFastPath(iterator)) return this._allUtcWeeklySimple();
|
|
1012
|
-
return null;
|
|
1013
|
-
}
|
|
1014
|
-
_allUtcFixedStepSimple(stepNanoseconds) {
|
|
1015
|
-
const dates = [];
|
|
1016
|
-
if (!this.addDtstartIfNeeded(dates)) return dates;
|
|
1017
|
-
let iterationCount = 0;
|
|
1018
|
-
if (this.canUseUtcEpochMillisecondsPrecision()) {
|
|
1019
|
-
let currentMilliseconds = this.originalDtstart.epochMilliseconds;
|
|
1020
|
-
const stepMilliseconds = Number(stepNanoseconds / NS_PER_MILLISECOND);
|
|
1021
|
-
const untilMilliseconds = this.opts.until?.epochMilliseconds;
|
|
1022
|
-
while (true) {
|
|
1023
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1024
|
-
if (untilMilliseconds !== void 0 && currentMilliseconds > untilMilliseconds) break;
|
|
1025
|
-
dates.push(this.utcZdtFromEpochMilliseconds(currentMilliseconds));
|
|
1026
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1027
|
-
currentMilliseconds += stepMilliseconds;
|
|
1028
|
-
}
|
|
1029
|
-
return dates;
|
|
1030
|
-
}
|
|
1031
|
-
let currentNanoseconds = this.originalDtstart.epochNanoseconds;
|
|
1032
|
-
const untilNanoseconds = this.opts.until?.epochNanoseconds;
|
|
1033
|
-
while (true) {
|
|
1034
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1035
|
-
if (untilNanoseconds !== void 0 && currentNanoseconds > untilNanoseconds) break;
|
|
1036
|
-
dates.push(this.utcZdtFromEpochNanoseconds(currentNanoseconds));
|
|
1037
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1038
|
-
currentNanoseconds += stepNanoseconds;
|
|
1039
|
-
}
|
|
1040
|
-
return dates;
|
|
1041
|
-
}
|
|
1042
|
-
_allUtcDailySimple() {
|
|
1043
|
-
const dates = [];
|
|
1044
|
-
if (!this.addDtstartIfNeeded(dates)) return dates;
|
|
1045
|
-
const stepDays = this.opts.interval;
|
|
1046
|
-
const allowedDays = this.simpleByDayIsoDays;
|
|
1047
|
-
let iterationCount = 0;
|
|
1048
|
-
if (this.canUseUtcEpochMillisecondsPrecision()) {
|
|
1049
|
-
const stepMilliseconds = stepDays * MS_PER_DAY;
|
|
1050
|
-
const untilMilliseconds = this.opts.until?.epochMilliseconds;
|
|
1051
|
-
let currentMilliseconds = this.originalDtstart.epochMilliseconds;
|
|
1052
|
-
let currentDayOfWeek = this.originalDtstart.dayOfWeek;
|
|
1053
|
-
if (allowedDays?.length) {
|
|
1054
|
-
const firstMatchingStep = this.findFirstMatchingDailyStep(currentDayOfWeek, stepDays, allowedDays);
|
|
1055
|
-
if (firstMatchingStep === null) return dates;
|
|
1056
|
-
currentMilliseconds += firstMatchingStep * stepMilliseconds;
|
|
1057
|
-
currentDayOfWeek = addIsoDays(currentDayOfWeek, firstMatchingStep * stepDays);
|
|
1058
|
-
}
|
|
1059
|
-
while (true) {
|
|
1060
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1061
|
-
if (untilMilliseconds !== void 0 && currentMilliseconds > untilMilliseconds) break;
|
|
1062
|
-
if (!allowedDays || allowedDays.includes(currentDayOfWeek)) {
|
|
1063
|
-
dates.push(this.utcZdtFromEpochMilliseconds(currentMilliseconds));
|
|
1064
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1065
|
-
}
|
|
1066
|
-
currentMilliseconds += stepMilliseconds;
|
|
1067
|
-
currentDayOfWeek = addIsoDays(currentDayOfWeek, stepDays);
|
|
1068
|
-
}
|
|
1069
|
-
return dates;
|
|
1070
|
-
}
|
|
1071
|
-
const stepNanoseconds = BigInt(stepDays) * NS_PER_DAY;
|
|
1072
|
-
const untilNanoseconds = this.opts.until?.epochNanoseconds;
|
|
1073
|
-
let currentNanoseconds = this.originalDtstart.epochNanoseconds;
|
|
1074
|
-
let currentDayOfWeek = this.originalDtstart.dayOfWeek;
|
|
1075
|
-
if (allowedDays?.length) {
|
|
1076
|
-
const firstMatchingStep = this.findFirstMatchingDailyStep(currentDayOfWeek, stepDays, allowedDays);
|
|
1077
|
-
if (firstMatchingStep === null) return dates;
|
|
1078
|
-
currentNanoseconds += BigInt(firstMatchingStep) * stepNanoseconds;
|
|
1079
|
-
currentDayOfWeek = addIsoDays(currentDayOfWeek, firstMatchingStep * stepDays);
|
|
1080
|
-
}
|
|
1081
|
-
while (true) {
|
|
1082
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1083
|
-
if (untilNanoseconds !== void 0 && currentNanoseconds > untilNanoseconds) break;
|
|
1084
|
-
if (!allowedDays || allowedDays.includes(currentDayOfWeek)) {
|
|
1085
|
-
dates.push(this.utcZdtFromEpochNanoseconds(currentNanoseconds));
|
|
1086
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1087
|
-
}
|
|
1088
|
-
currentNanoseconds += stepNanoseconds;
|
|
1089
|
-
currentDayOfWeek = addIsoDays(currentDayOfWeek, stepDays);
|
|
1090
|
-
}
|
|
1091
|
-
return dates;
|
|
1092
|
-
}
|
|
1093
|
-
_allUtcWeeklySimple() {
|
|
1094
|
-
const dates = [];
|
|
1095
|
-
if (!this.addDtstartIfNeeded(dates)) return dates;
|
|
1096
|
-
const start = this.originalDtstart;
|
|
1097
|
-
const wkstDay = weekdayToIsoDay[extractWeekdayToken(this.opts.wkst || "MO") ?? "MO"] ?? 1;
|
|
1098
|
-
const dayOffsets = (this.opts.byDay ? [...this.allByDayIsoDays ?? []] : [start.dayOfWeek]).map((day) => (day - wkstDay + 7) % 7).sort((a, b) => a - b);
|
|
1099
|
-
const weekStartOffset = (start.dayOfWeek - wkstDay + 7) % 7;
|
|
1100
|
-
let iterationCount = 0;
|
|
1101
|
-
if (this.canUseUtcEpochMillisecondsPrecision()) {
|
|
1102
|
-
const startMilliseconds = start.epochMilliseconds;
|
|
1103
|
-
const untilMilliseconds = this.opts.until?.epochMilliseconds;
|
|
1104
|
-
const weekStepMilliseconds = this.opts.interval * MS_PER_WEEK;
|
|
1105
|
-
let weekStartMilliseconds = startMilliseconds - weekStartOffset * MS_PER_DAY;
|
|
1106
|
-
while (true) {
|
|
1107
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1108
|
-
for (const dayOffset of dayOffsets) {
|
|
1109
|
-
const occurrenceMilliseconds = weekStartMilliseconds + dayOffset * MS_PER_DAY;
|
|
1110
|
-
if (occurrenceMilliseconds < startMilliseconds) continue;
|
|
1111
|
-
if (untilMilliseconds !== void 0 && occurrenceMilliseconds > untilMilliseconds) return dates;
|
|
1112
|
-
dates.push(this.utcZdtFromEpochMilliseconds(occurrenceMilliseconds));
|
|
1113
|
-
if (this.shouldBreakForCountLimit(dates.length)) return dates;
|
|
1114
|
-
}
|
|
1115
|
-
weekStartMilliseconds += weekStepMilliseconds;
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
const startNanoseconds = start.epochNanoseconds;
|
|
1119
|
-
const untilNanoseconds = this.opts.until?.epochNanoseconds;
|
|
1120
|
-
const weekStepNanoseconds = BigInt(this.opts.interval) * NS_PER_WEEK;
|
|
1121
|
-
let weekStartNanoseconds = startNanoseconds - BigInt(weekStartOffset) * NS_PER_DAY;
|
|
1122
|
-
while (true) {
|
|
1123
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1124
|
-
for (const dayOffset of dayOffsets) {
|
|
1125
|
-
const occurrenceNanoseconds = weekStartNanoseconds + BigInt(dayOffset) * NS_PER_DAY;
|
|
1126
|
-
if (occurrenceNanoseconds < startNanoseconds) continue;
|
|
1127
|
-
if (untilNanoseconds !== void 0 && occurrenceNanoseconds > untilNanoseconds) return dates;
|
|
1128
|
-
dates.push(this.utcZdtFromEpochNanoseconds(occurrenceNanoseconds));
|
|
1129
|
-
if (this.shouldBreakForCountLimit(dates.length)) return dates;
|
|
1130
|
-
}
|
|
1131
|
-
weekStartNanoseconds += weekStepNanoseconds;
|
|
1132
|
-
}
|
|
1133
|
-
}
|
|
1134
|
-
hasSingleExpandedTimeSlot() {
|
|
1135
|
-
if (this.timeSlotOffsetsMs) return this.timeSlotOffsetsMs.length === 1;
|
|
1136
|
-
const hours = this.opts.byHour ?? [this.originalDtstart.hour];
|
|
1137
|
-
const minutes = this.opts.byMinute ?? [this.originalDtstart.minute];
|
|
1138
|
-
const seconds = this.opts.bySecond ?? [this.originalDtstart.second];
|
|
1139
|
-
return hours.length === 1 && minutes.length === 1 && seconds.length === 1;
|
|
1140
|
-
}
|
|
1141
|
-
buildMonthlyOccurrenceOnDay(monthStart, day) {
|
|
1142
|
-
const base = monthStart.day === day ? monthStart : monthStart.with({ day });
|
|
1143
|
-
return this.applyTimeOverride(base);
|
|
1144
|
-
}
|
|
1145
|
-
applyBySetPosToSortedList(list) {
|
|
1146
|
-
const { bySetPos } = this.opts;
|
|
1147
|
-
if (!bySetPos || !bySetPos.length || list.length === 0) return list;
|
|
1148
|
-
const out = [];
|
|
1149
|
-
const len = list.length;
|
|
1150
|
-
for (const pos of bySetPos) {
|
|
1151
|
-
const idx = pos > 0 ? pos - 1 : len + pos;
|
|
1152
|
-
if (idx >= 0 && idx < len) out.push(list[idx]);
|
|
1153
|
-
}
|
|
1154
|
-
return out;
|
|
1155
|
-
}
|
|
1156
|
-
generateMonthlyOccurrenceDays(sample) {
|
|
1157
|
-
const { byDay, byMonth, byMonthDay } = this.opts;
|
|
1158
|
-
const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
|
|
1159
|
-
if (byMonth && !byMonth.includes(sample.month)) return [];
|
|
1160
|
-
const lastDay = monthStart.add({ months: 1 }).subtract({ days: 1 }).day;
|
|
1161
|
-
let byMonthDayHits = [];
|
|
1162
|
-
if (byMonthDay && byMonthDay.length > 0) {
|
|
1163
|
-
byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
|
|
1164
|
-
byMonthDayHits = [...new Set(byMonthDayHits)].sort((a, b) => a - b);
|
|
1165
|
-
}
|
|
1166
|
-
if (!byDay && byMonthDay && byMonthDay.length > 0) return byMonthDayHits;
|
|
1167
|
-
if (!byDay) return [sample.day];
|
|
1168
|
-
const tokens = this.parsedByDayTokens;
|
|
1169
|
-
if (!tokens?.length) return [];
|
|
1170
|
-
const firstDayOfWeek = monthStart.dayOfWeek;
|
|
1171
|
-
const lastDayOfWeek = (firstDayOfWeek - 1 + lastDay - 1) % 7 + 1;
|
|
1172
|
-
const byDayHits = /* @__PURE__ */ new Set();
|
|
1173
|
-
for (const { ord, isoDay } of tokens) if (ord === 0) {
|
|
1174
|
-
let day = 1 + (isoDay - firstDayOfWeek + 7) % 7;
|
|
1175
|
-
while (day <= lastDay) {
|
|
1176
|
-
byDayHits.add(day);
|
|
1177
|
-
day += 7;
|
|
1178
|
-
}
|
|
1179
|
-
} else {
|
|
1180
|
-
let day;
|
|
1181
|
-
if (ord > 0) day = 1 + (isoDay - firstDayOfWeek + 7) % 7 + 7 * (ord - 1);
|
|
1182
|
-
else day = lastDay - (lastDayOfWeek - isoDay + 7) % 7 + 7 * (ord + 1);
|
|
1183
|
-
if (day >= 1 && day <= lastDay) byDayHits.add(day);
|
|
1184
|
-
}
|
|
1185
|
-
let finalDays = [...byDayHits].sort((a, b) => a - b);
|
|
1186
|
-
if (byMonthDay && byMonthDay.length > 0) {
|
|
1187
|
-
if (byMonthDayHits.length === 0) return [];
|
|
1188
|
-
const byMonthDayHitSet = new Set(byMonthDayHits);
|
|
1189
|
-
finalDays = finalDays.filter((d) => byMonthDayHitSet.has(d));
|
|
1190
|
-
}
|
|
1191
|
-
return finalDays;
|
|
1192
|
-
}
|
|
1193
|
-
generateMonthlyOccurrencesOptimizedBySetPos(sample) {
|
|
1194
|
-
if (!this.opts.bySetPos || !this.hasSingleExpandedTimeSlot()) return null;
|
|
1195
|
-
const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
|
|
1196
|
-
const days = this.generateMonthlyOccurrenceDays(monthStart);
|
|
1197
|
-
if (days.length === 0) return [];
|
|
1198
|
-
const selectedDays = this.applyBySetPosToSortedList(days);
|
|
1199
|
-
if (selectedDays.length === 0) return [];
|
|
1200
|
-
return selectedDays.sort((a, b) => a - b).map((day) => this.buildMonthlyOccurrenceOnDay(monthStart, day));
|
|
1201
|
-
}
|
|
1202
|
-
isGregorianLeapYear(year) {
|
|
1203
|
-
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
1204
|
-
}
|
|
1205
|
-
daysInGregorianMonth(year, month) {
|
|
1206
|
-
if (month === 2 && this.isGregorianLeapYear(year)) return 29;
|
|
1207
|
-
return GREGORIAN_MONTH_LENGTHS[month - 1];
|
|
1208
|
-
}
|
|
1209
|
-
gregorianIsoDayOfWeek(year, month, day) {
|
|
1210
|
-
let adjustedYear = year;
|
|
1211
|
-
if (month < 3) adjustedYear -= 1;
|
|
1212
|
-
const sundayZero = (adjustedYear + Math.floor(adjustedYear / 4) - Math.floor(adjustedYear / 100) + Math.floor(adjustedYear / 400) + GREGORIAN_WEEKDAY_OFFSETS[month - 1] + day) % 7;
|
|
1213
|
-
return sundayZero === 0 ? 7 : sundayZero;
|
|
1214
|
-
}
|
|
1215
|
-
monthIndexToYearMonth(monthIndex) {
|
|
1216
|
-
const year = Math.floor(monthIndex / 12);
|
|
1217
|
-
return {
|
|
1218
|
-
year,
|
|
1219
|
-
month: monthIndex - year * 12 + 1
|
|
1220
|
-
};
|
|
1221
|
-
}
|
|
1222
|
-
generateMonthlyOccurrenceDaysUtc(year, month) {
|
|
1223
|
-
if (this.numericByMonths && this.numericByMonths.length > 0 && !this.numericByMonths.includes(month)) return [];
|
|
1224
|
-
const byMonthDay = this.opts.byMonthDay;
|
|
1225
|
-
const byDay = this.opts.byDay;
|
|
1226
|
-
const lastDay = this.daysInGregorianMonth(year, month);
|
|
1227
|
-
let byMonthDayHits = [];
|
|
1228
|
-
if (byMonthDay && byMonthDay.length > 0) {
|
|
1229
|
-
byMonthDayHits = byMonthDay.map((day) => day > 0 ? day : lastDay + day + 1).filter((day) => day >= 1 && day <= lastDay);
|
|
1230
|
-
byMonthDayHits = [...new Set(byMonthDayHits)].sort((a, b) => a - b);
|
|
1231
|
-
}
|
|
1232
|
-
if (!byDay && byMonthDay && byMonthDay.length > 0) return byMonthDayHits;
|
|
1233
|
-
if (!byDay) {
|
|
1234
|
-
const day = this.originalDtstart.day;
|
|
1235
|
-
return day >= 1 && day <= lastDay ? [day] : [];
|
|
1236
|
-
}
|
|
1237
|
-
const tokens = this.parsedByDayTokens;
|
|
1238
|
-
if (!tokens?.length) return [];
|
|
1239
|
-
const firstDayOfWeek = this.gregorianIsoDayOfWeek(year, month, 1);
|
|
1240
|
-
const lastDayOfWeek = addIsoDays(firstDayOfWeek, lastDay - 1);
|
|
1241
|
-
const byDayHits = /* @__PURE__ */ new Set();
|
|
1242
|
-
for (const { ord, isoDay } of tokens) if (ord === 0) {
|
|
1243
|
-
let day = 1 + (isoDay - firstDayOfWeek + 7) % 7;
|
|
1244
|
-
while (day <= lastDay) {
|
|
1245
|
-
byDayHits.add(day);
|
|
1246
|
-
day += 7;
|
|
1247
|
-
}
|
|
1248
|
-
} else {
|
|
1249
|
-
let day;
|
|
1250
|
-
if (ord > 0) day = 1 + (isoDay - firstDayOfWeek + 7) % 7 + 7 * (ord - 1);
|
|
1251
|
-
else day = lastDay - (lastDayOfWeek - isoDay + 7) % 7 + 7 * (ord + 1);
|
|
1252
|
-
if (day >= 1 && day <= lastDay) byDayHits.add(day);
|
|
1253
|
-
}
|
|
1254
|
-
let finalDays = [...byDayHits].sort((a, b) => a - b);
|
|
1255
|
-
if (byMonthDay && byMonthDay.length > 0) {
|
|
1256
|
-
if (byMonthDayHits.length === 0) return [];
|
|
1257
|
-
const byMonthDayHitSet = new Set(byMonthDayHits);
|
|
1258
|
-
finalDays = finalDays.filter((day) => byMonthDayHitSet.has(day));
|
|
1259
|
-
}
|
|
1260
|
-
return finalDays;
|
|
1261
|
-
}
|
|
1262
|
-
generateMonthlyOccurrenceEpochsUtc(year, month) {
|
|
1263
|
-
const days = this.generateMonthlyOccurrenceDaysUtc(year, month);
|
|
1264
|
-
if (days.length === 0) return [];
|
|
1265
|
-
const monthStartMs = Date.UTC(year, month - 1, 1, 0, 0, 0, 0);
|
|
1266
|
-
const timeSlotOffsets = this.timeSlotOffsetsMs ?? [0];
|
|
1267
|
-
if (this.opts.bySetPos && this.opts.bySetPos.length > 0) {
|
|
1268
|
-
if (timeSlotOffsets.length === 1) {
|
|
1269
|
-
const selectedDays = this.applyBySetPosToSortedList(days).sort((a, b) => a - b);
|
|
1270
|
-
const offset = timeSlotOffsets[0];
|
|
1271
|
-
return selectedDays.map((day) => monthStartMs + (day - 1) * MS_PER_DAY + offset);
|
|
1272
|
-
}
|
|
1273
|
-
const timestamps = [];
|
|
1274
|
-
for (const day of days) {
|
|
1275
|
-
const dayBase = monthStartMs + (day - 1) * MS_PER_DAY;
|
|
1276
|
-
for (const offset of timeSlotOffsets) timestamps.push(dayBase + offset);
|
|
1277
|
-
}
|
|
1278
|
-
return this.applyBySetPosToSortedList(timestamps).sort((a, b) => a - b);
|
|
1279
|
-
}
|
|
1280
|
-
const timestamps = [];
|
|
1281
|
-
for (const day of days) {
|
|
1282
|
-
const dayBase = monthStartMs + (day - 1) * MS_PER_DAY;
|
|
1283
|
-
for (const offset of timeSlotOffsets) timestamps.push(dayBase + offset);
|
|
1284
|
-
}
|
|
1285
|
-
return timestamps;
|
|
1286
|
-
}
|
|
1287
|
-
_allUtcMonthlyByDayOrMonthDay() {
|
|
1288
|
-
const dates = [];
|
|
1289
|
-
const startMilliseconds = this.originalDtstart.epochMilliseconds;
|
|
1290
|
-
const untilMilliseconds = this.opts.until?.epochMilliseconds;
|
|
1291
|
-
let iterationCount = 0;
|
|
1292
|
-
if (!this.addDtstartIfNeeded(dates)) return dates;
|
|
1293
|
-
let monthIndex = this.originalDtstart.year * 12 + (this.originalDtstart.month - 1);
|
|
1294
|
-
while (true) {
|
|
1295
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1296
|
-
const { year, month } = this.monthIndexToYearMonth(monthIndex);
|
|
1297
|
-
const occurrenceEpochs = this.generateMonthlyOccurrenceEpochsUtc(year, month);
|
|
1298
|
-
for (const epochMilliseconds of occurrenceEpochs) {
|
|
1299
|
-
if (epochMilliseconds < startMilliseconds) continue;
|
|
1300
|
-
if (untilMilliseconds !== void 0 && epochMilliseconds > untilMilliseconds) return dates;
|
|
1301
|
-
dates.push(this.utcZdtFromEpochMilliseconds(epochMilliseconds));
|
|
1302
|
-
if (this.shouldBreakForCountLimit(dates.length)) return dates;
|
|
1303
|
-
}
|
|
1304
|
-
monthIndex += this.opts.interval;
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
processOccurrences(occs, dates, start, iterator, extraFilters) {
|
|
1308
|
-
let shouldBreak = false;
|
|
1309
|
-
for (const occ of occs) {
|
|
1310
|
-
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
1311
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
1312
|
-
shouldBreak = true;
|
|
1313
|
-
break;
|
|
1314
|
-
}
|
|
1315
|
-
if (extraFilters && !extraFilters(occ)) continue;
|
|
1316
|
-
if (iterator && this.isExcluded(occ)) continue;
|
|
1317
|
-
if (iterator && !iterator(occ, dates.length)) {
|
|
1318
|
-
shouldBreak = true;
|
|
1319
|
-
break;
|
|
1320
|
-
}
|
|
1321
|
-
dates.push(occ);
|
|
1322
|
-
if (this.shouldBreakForCountLimit(dates.length)) {
|
|
1323
|
-
shouldBreak = true;
|
|
1324
|
-
break;
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
return { shouldBreak };
|
|
1328
|
-
}
|
|
1329
|
-
/**
|
|
1330
|
-
* Returns all occurrences of the rule.
|
|
1331
|
-
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
1332
|
-
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
1333
|
-
*/
|
|
1334
|
-
_allMonthlyByDayOrMonthDay(iterator) {
|
|
1335
|
-
const dates = [];
|
|
1336
|
-
let iterationCount = 0;
|
|
1337
|
-
const start = this.originalDtstart;
|
|
1338
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1339
|
-
let monthCursor = start.with({ day: 1 });
|
|
1340
|
-
while (true) {
|
|
1341
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1342
|
-
let occs = this.generateMonthlyOccurrencesOptimizedBySetPos(monthCursor);
|
|
1343
|
-
if (!occs) {
|
|
1344
|
-
occs = this.generateMonthlyOccurrences(monthCursor);
|
|
1345
|
-
occs = this.applyBySetPos(occs);
|
|
1346
|
-
}
|
|
1347
|
-
const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
|
|
1348
|
-
if (shouldBreak) break;
|
|
1349
|
-
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
1350
|
-
}
|
|
1351
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1352
|
-
}
|
|
1353
|
-
_allWeekly(iterator) {
|
|
1354
|
-
const dates = [];
|
|
1355
|
-
let iterationCount = 0;
|
|
1356
|
-
const start = this.originalDtstart;
|
|
1357
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1358
|
-
const dayMap = weekdayToIsoDay;
|
|
1359
|
-
const dows = this.opts.byDay ? [...this.allByDayIsoDays ?? []] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? [...Object.values(dayMap)] : [start.dayOfWeek];
|
|
1360
|
-
const firstOccurrence = dows.map((dw) => {
|
|
1361
|
-
const delta = (dw - start.dayOfWeek + 7) % 7;
|
|
1362
|
-
return start.add({ days: delta });
|
|
1363
|
-
}).reduce((a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
|
|
1364
|
-
const wkstDay = dayMap[extractWeekdayToken(this.opts.wkst || "MO") ?? "MO"] ?? 1;
|
|
1365
|
-
const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
|
|
1366
|
-
let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
|
|
1367
|
-
while (true) {
|
|
1368
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1369
|
-
let occs = dows.flatMap((dw) => {
|
|
1370
|
-
const delta = (dw - wkstDay + 7) % 7;
|
|
1371
|
-
const sameDate = weekCursor.add({ days: delta });
|
|
1372
|
-
return this.expandByTime(sameDate);
|
|
1373
|
-
}).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1374
|
-
occs = this.applyBySetPos(occs);
|
|
1375
|
-
const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator, (occ) => this.matchesByMonth(occ) && this.matchesByMonthDay(occ));
|
|
1376
|
-
if (shouldBreak) break;
|
|
1377
|
-
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
1378
|
-
}
|
|
1379
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1380
|
-
}
|
|
1381
|
-
_allMonthlyByMonth(iterator) {
|
|
1382
|
-
const dates = [];
|
|
1383
|
-
let iterationCount = 0;
|
|
1384
|
-
const start = this.originalDtstart;
|
|
1385
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1386
|
-
const months = this.opts.byMonth.filter((v) => typeof v === "number").sort((a, b) => a - b);
|
|
1387
|
-
let monthOffset = 0;
|
|
1388
|
-
let startMonthIndex = months.findIndex((m) => m >= start.month);
|
|
1389
|
-
if (startMonthIndex === -1) {
|
|
1390
|
-
startMonthIndex = 0;
|
|
1391
|
-
monthOffset = 1;
|
|
1392
|
-
}
|
|
1393
|
-
while (true) {
|
|
1394
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1395
|
-
const monthIndex = startMonthIndex + monthOffset;
|
|
1396
|
-
const targetMonth = months[monthIndex % months.length];
|
|
1397
|
-
const yearsToAdd = Math.floor(monthIndex / months.length);
|
|
1398
|
-
const candidate = start.with({
|
|
1399
|
-
year: start.year + yearsToAdd,
|
|
1400
|
-
month: targetMonth
|
|
1401
|
-
});
|
|
1402
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) break;
|
|
1403
|
-
if (Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
|
|
1404
|
-
if (iterator && this.isExcluded(candidate)) continue;
|
|
1405
|
-
if (iterator && !iterator(candidate, dates.length)) break;
|
|
1406
|
-
dates.push(candidate);
|
|
1407
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1408
|
-
}
|
|
1409
|
-
monthOffset++;
|
|
1410
|
-
}
|
|
1411
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1412
|
-
}
|
|
1413
|
-
_allYearlyByMonth(iterator) {
|
|
1414
|
-
const dates = [];
|
|
1415
|
-
let iterationCount = 0;
|
|
1416
|
-
const start = this.originalDtstart;
|
|
1417
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1418
|
-
const months = this.opts.byMonth.filter((v) => typeof v === "number").sort((a, b) => a - b);
|
|
1419
|
-
let yearOffset = 0;
|
|
1420
|
-
while (true) {
|
|
1421
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1422
|
-
const year = start.year + yearOffset * this.opts.interval;
|
|
1423
|
-
for (const month of months) {
|
|
1424
|
-
let occ = start.with({
|
|
1425
|
-
year,
|
|
1426
|
-
month
|
|
1427
|
-
});
|
|
1428
|
-
occ = this.applyTimeOverride(occ);
|
|
1429
|
-
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
1430
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1431
|
-
if (iterator && this.isExcluded(occ)) continue;
|
|
1432
|
-
if (iterator && !iterator(occ, dates.length)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1433
|
-
dates.push(occ);
|
|
1434
|
-
if (this.shouldBreakForCountLimit(dates.length)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1435
|
-
}
|
|
1436
|
-
yearOffset++;
|
|
1437
|
-
}
|
|
1438
|
-
}
|
|
1439
|
-
_allYearlyComplex(iterator) {
|
|
1440
|
-
const dates = [];
|
|
1441
|
-
let iterationCount = 0;
|
|
1442
|
-
const start = this.originalDtstart;
|
|
1443
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1444
|
-
let yearCursor = start.with({
|
|
1445
|
-
month: 1,
|
|
1446
|
-
day: 1
|
|
1447
|
-
});
|
|
1448
|
-
while (true) {
|
|
1449
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1450
|
-
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
1451
|
-
const uniqueOccs = [];
|
|
1452
|
-
if (occs.length > 0) {
|
|
1453
|
-
uniqueOccs.push(occs[0]);
|
|
1454
|
-
for (let i = 1; i < occs.length; i++) if (Temporal.ZonedDateTime.compare(occs[i], occs[i - 1]) !== 0) uniqueOccs.push(occs[i]);
|
|
1455
|
-
}
|
|
1456
|
-
const { shouldBreak } = this.processOccurrences(uniqueOccs, dates, start, iterator);
|
|
1457
|
-
if (shouldBreak) break;
|
|
1458
|
-
const interval = this.opts.freq === "WEEKLY" ? 1 : this.opts.interval;
|
|
1459
|
-
yearCursor = yearCursor.add({ years: interval });
|
|
1460
|
-
if (this.opts.freq === "WEEKLY" && this.opts.until && yearCursor.year > this.opts.until.year) break;
|
|
1461
|
-
}
|
|
1462
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1463
|
-
}
|
|
1464
|
-
_allMinutelySecondlyComplex(iterator) {
|
|
1465
|
-
const dates = [];
|
|
1466
|
-
let iterationCount = 0;
|
|
1467
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1468
|
-
let current = this.computeFirst();
|
|
1469
|
-
while (true) {
|
|
1470
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1471
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) break;
|
|
1472
|
-
if (this.matchesAll(current)) {
|
|
1473
|
-
if (iterator && this.isExcluded(current)) {
|
|
1474
|
-
current = this.nextCandidateSameDate(current);
|
|
1475
|
-
continue;
|
|
1476
|
-
}
|
|
1477
|
-
if (iterator && !iterator(current, dates.length)) break;
|
|
1478
|
-
dates.push(current);
|
|
1479
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1480
|
-
current = this.nextCandidateSameDate(current);
|
|
1481
|
-
} else current = this.findNextValidDate(current);
|
|
1482
|
-
}
|
|
1483
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1484
|
-
}
|
|
1485
|
-
_allMonthlyByWeekNo(iterator) {
|
|
1486
|
-
const dates = [];
|
|
1487
|
-
let iterationCount = 0;
|
|
1488
|
-
const start = this.originalDtstart;
|
|
1489
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1490
|
-
let current = start;
|
|
1491
|
-
const weekNos = [...this.opts.byWeekNo].sort((a, b) => a - b);
|
|
1492
|
-
const interval = this.opts.interval;
|
|
1493
|
-
let monthsAdvanced = 0;
|
|
1494
|
-
let lastYearProcessed = -1;
|
|
1495
|
-
outer_loop: while (true) {
|
|
1496
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1497
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1498
|
-
const year = current.year;
|
|
1499
|
-
if (year !== lastYearProcessed && current.month >= start.month) {
|
|
1500
|
-
lastYearProcessed = year;
|
|
1501
|
-
for (const weekNo of weekNos) {
|
|
1502
|
-
const occs = this.generateOccurrencesForWeekInYear(year, weekNo);
|
|
1503
|
-
for (const occ of occs) if (Temporal.ZonedDateTime.compare(occ, start) >= 0) {
|
|
1504
|
-
if (iterator && this.isExcluded(occ)) continue;
|
|
1505
|
-
if (iterator && !iterator(occ, dates.length)) break outer_loop;
|
|
1506
|
-
dates.push(occ);
|
|
1507
|
-
if (this.shouldBreakForCountLimit(dates.length)) break outer_loop;
|
|
1508
|
-
}
|
|
1509
|
-
}
|
|
1510
|
-
}
|
|
1511
|
-
monthsAdvanced += interval;
|
|
1512
|
-
current = start.add({ months: monthsAdvanced });
|
|
1513
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) break;
|
|
1514
|
-
}
|
|
1515
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1516
|
-
}
|
|
1517
|
-
_allMonthlyByYearDay(iterator) {
|
|
1518
|
-
const dates = [];
|
|
1519
|
-
let iterationCount = 0;
|
|
1520
|
-
const start = this.originalDtstart;
|
|
1521
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1522
|
-
let year = start.year;
|
|
1523
|
-
const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
|
|
1524
|
-
const interval = this.opts.interval;
|
|
1525
|
-
const startMonthAbs = start.year * 12 + start.month;
|
|
1526
|
-
outer_loop: while (true) {
|
|
1527
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1528
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1529
|
-
const yearStart = start.with({
|
|
1530
|
-
year,
|
|
1531
|
-
month: 1,
|
|
1532
|
-
day: 1
|
|
1533
|
-
});
|
|
1534
|
-
const lastDayOfYear = yearStart.with({
|
|
1535
|
-
month: 12,
|
|
1536
|
-
day: 31
|
|
1537
|
-
}).dayOfYear;
|
|
1538
|
-
for (const yd of yearDays) {
|
|
1539
|
-
const dayNum = yd > 0 ? yd : lastDayOfYear + yd + 1;
|
|
1540
|
-
if (dayNum <= 0 || dayNum > lastDayOfYear) continue;
|
|
1541
|
-
const baseOcc = yearStart.add({ days: dayNum - 1 });
|
|
1542
|
-
for (const occ of this.expandByTime(baseOcc)) {
|
|
1543
|
-
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
1544
|
-
if (dates.some((d) => Temporal.ZonedDateTime.compare(d, occ) === 0)) continue;
|
|
1545
|
-
if ((occ.year * 12 + occ.month - startMonthAbs) % interval !== 0) continue;
|
|
1546
|
-
if (!this.matchesByMonth(occ)) continue;
|
|
1547
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) break outer_loop;
|
|
1548
|
-
if (iterator && this.isExcluded(occ)) continue;
|
|
1549
|
-
if (iterator && !iterator(occ, dates.length)) break outer_loop;
|
|
1550
|
-
dates.push(occ);
|
|
1551
|
-
if (this.shouldBreakForCountLimit(dates.length)) break outer_loop;
|
|
1552
|
-
}
|
|
1553
|
-
}
|
|
1554
|
-
year++;
|
|
1555
|
-
if (this.opts.until && year > this.opts.until.year + 2) break;
|
|
1556
|
-
if (!this.opts.until && this.opts.count) {
|
|
1557
|
-
const yearsToScan = Math.ceil(this.opts.count / (this.opts.byYearDay.length || 1)) * interval + 5;
|
|
1558
|
-
if (year > start.year + yearsToScan) break;
|
|
1559
|
-
}
|
|
1560
|
-
}
|
|
1561
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1562
|
-
}
|
|
1563
|
-
_allDailyMinutelyHourlyWithBySetPos(iterator) {
|
|
1564
|
-
const dates = [];
|
|
1565
|
-
let iterationCount = 0;
|
|
1566
|
-
const start = this.originalDtstart;
|
|
1567
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1568
|
-
let cursor;
|
|
1569
|
-
let duration;
|
|
1570
|
-
switch (this.opts.freq) {
|
|
1571
|
-
case "MINUTELY":
|
|
1572
|
-
cursor = start.with({
|
|
1573
|
-
second: 0,
|
|
1574
|
-
microsecond: 0,
|
|
1575
|
-
nanosecond: 0
|
|
1576
|
-
});
|
|
1577
|
-
duration = { minutes: this.opts.interval };
|
|
1578
|
-
break;
|
|
1579
|
-
case "HOURLY":
|
|
1580
|
-
cursor = start.with({
|
|
1581
|
-
minute: 0,
|
|
1582
|
-
second: 0,
|
|
1583
|
-
microsecond: 0,
|
|
1584
|
-
nanosecond: 0
|
|
1585
|
-
});
|
|
1586
|
-
duration = { hours: this.opts.interval };
|
|
1587
|
-
break;
|
|
1588
|
-
case "DAILY":
|
|
1589
|
-
cursor = start.with({
|
|
1590
|
-
hour: 0,
|
|
1591
|
-
minute: 0,
|
|
1592
|
-
second: 0,
|
|
1593
|
-
microsecond: 0,
|
|
1594
|
-
nanosecond: 0
|
|
1595
|
-
});
|
|
1596
|
-
duration = { days: this.opts.interval };
|
|
1597
|
-
break;
|
|
1598
|
-
default: return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1599
|
-
}
|
|
1600
|
-
while (true) {
|
|
1601
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1602
|
-
let periodOccs = this.expandByTime(cursor);
|
|
1603
|
-
periodOccs = periodOccs.filter((occ) => this.matchesAll(occ));
|
|
1604
|
-
periodOccs = this.applyBySetPos(periodOccs);
|
|
1605
|
-
const { shouldBreak } = this.processOccurrences(periodOccs, dates, start, iterator);
|
|
1606
|
-
if (shouldBreak) break;
|
|
1607
|
-
cursor = cursor.add(duration);
|
|
1608
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(cursor, this.opts.until) > 0) break;
|
|
1609
|
-
}
|
|
1610
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1611
|
-
}
|
|
1612
|
-
_allFallback(iterator) {
|
|
1613
|
-
const dates = [];
|
|
1614
|
-
let iterationCount = 0;
|
|
1615
|
-
let current = this.computeFirst();
|
|
1616
|
-
if (this.includeDtstart && Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) if (iterator && this.isExcluded(this.originalDtstart)) {} else {
|
|
1617
|
-
if (iterator && !iterator(this.originalDtstart, dates.length)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1618
|
-
dates.push(this.originalDtstart);
|
|
1619
|
-
if (this.shouldBreakForCountLimit(dates.length)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1620
|
-
}
|
|
1621
|
-
while (true) {
|
|
1622
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1623
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) break;
|
|
1624
|
-
if (this.matchesAll(current)) if (iterator && this.isExcluded(current)) {} else {
|
|
1625
|
-
if (iterator && !iterator(current, dates.length)) break;
|
|
1626
|
-
dates.push(current);
|
|
1627
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1628
|
-
}
|
|
1629
|
-
current = this.nextCandidateSameDate(current);
|
|
1630
|
-
}
|
|
1631
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1632
|
-
}
|
|
1633
|
-
/**
|
|
1634
|
-
* Returns all occurrences of the rule.
|
|
1635
|
-
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
1636
|
-
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
1637
|
-
*/
|
|
1638
|
-
all(iterator) {
|
|
1639
|
-
return RRuleTemporal.toPublicDates(this.allInternal(RRuleTemporal.toInternalIterator(iterator)));
|
|
1640
|
-
}
|
|
1641
|
-
allInternal(iterator) {
|
|
1642
|
-
if (this.opts.rscale && [
|
|
1643
|
-
"CHINESE",
|
|
1644
|
-
"HEBREW",
|
|
1645
|
-
"INDIAN"
|
|
1646
|
-
].includes(this.opts.rscale)) {
|
|
1647
|
-
if ([
|
|
1648
|
-
"YEARLY",
|
|
1649
|
-
"MONTHLY",
|
|
1650
|
-
"WEEKLY"
|
|
1651
|
-
].includes(this.opts.freq) || !!this.opts.byYearDay || !!this.opts.byWeekNo || this.opts.byMonthDay && this.opts.byMonthDay.length > 0) return this._allRscaleNonGregorian(iterator);
|
|
1652
|
-
}
|
|
1653
|
-
if (this.opts.byWeekNo && this.opts.byYearDay) {
|
|
1654
|
-
const yearStart = this.originalDtstart.with({
|
|
1655
|
-
month: 1,
|
|
1656
|
-
day: 1,
|
|
1657
|
-
hour: 0,
|
|
1658
|
-
minute: 0,
|
|
1659
|
-
second: 0,
|
|
1660
|
-
millisecond: 0
|
|
1661
|
-
});
|
|
1662
|
-
const yearDays = this.opts.byYearDay.map((yd) => {
|
|
1663
|
-
const lastDayOfYear = yearStart.with({
|
|
1664
|
-
month: 12,
|
|
1665
|
-
day: 31
|
|
1666
|
-
}).dayOfYear;
|
|
1667
|
-
return yd > 0 ? yd : lastDayOfYear + yd + 1;
|
|
1668
|
-
});
|
|
1669
|
-
let possibleDate = false;
|
|
1670
|
-
for (const yd of yearDays) {
|
|
1671
|
-
const date = yearStart.add({ days: yd - 1 });
|
|
1672
|
-
if (this.matchesByWeekNo(date)) {
|
|
1673
|
-
possibleDate = true;
|
|
1674
|
-
break;
|
|
1675
|
-
}
|
|
1676
|
-
}
|
|
1677
|
-
if (!possibleDate) return [];
|
|
1678
|
-
}
|
|
1679
|
-
if (!this.opts.count && !this.opts.until && !iterator) throw new Error("all() requires iterator when no COUNT/UNTIL");
|
|
1680
|
-
const utcFastPathDates = this.allUtcFastPath(iterator);
|
|
1681
|
-
if (utcFastPathDates) return utcFastPathDates;
|
|
1682
|
-
if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay) && !this.opts.byWeekNo) return this._allMonthlyByDayOrMonthDay(iterator);
|
|
1683
|
-
if (this.opts.freq === "WEEKLY" && !(this.opts.byYearDay && this.opts.byYearDay.length > 0) && !(this.opts.byWeekNo && this.opts.byWeekNo.length > 0)) return this._allWeekly(iterator);
|
|
1684
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) return this._allMonthlyByMonth(iterator);
|
|
1685
|
-
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) return this._allYearlyByMonth(iterator);
|
|
1686
|
-
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay || this.opts.byYearDay || this.opts.byWeekNo) || this.opts.freq === "WEEKLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 || this.opts.freq === "WEEKLY" && this.opts.byWeekNo && this.opts.byWeekNo.length > 0) return this._allYearlyComplex(iterator);
|
|
1687
|
-
if ((this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") && (this.opts.byMonth || this.opts.byWeekNo || this.opts.byYearDay || this.opts.byMonthDay || this.opts.byDay)) return this._allMinutelySecondlyComplex(iterator);
|
|
1688
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byWeekNo && this.opts.byWeekNo.length > 0) return this._allMonthlyByWeekNo(iterator);
|
|
1689
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) return this._allMonthlyByYearDay(iterator);
|
|
1690
|
-
if (this.opts.rscale && this.opts.freq === "MONTHLY" && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byWeekNo && !this.opts.byYearDay) return this._allMonthlyRscaleSimple(iterator);
|
|
1691
|
-
if ((this.opts.freq === "MINUTELY" || this.opts.freq === "HOURLY" || this.opts.freq === "DAILY") && this.opts.bySetPos) return this._allDailyMinutelyHourlyWithBySetPos(iterator);
|
|
1692
|
-
return this._allFallback(iterator);
|
|
1693
|
-
}
|
|
1694
|
-
/**
|
|
1695
|
-
* RFC 7529: RSCALE present, simple monthly iteration with SKIP behavior.
|
|
1696
|
-
* Handles month-to-month stepping from DTSTART's year/month aiming for DTSTART's day-of-month.
|
|
1697
|
-
* Applies SKIP=OMIT (skip invalid months), BACKWARD (clamp to last day), FORWARD (first day of next month).
|
|
1698
|
-
*/
|
|
1699
|
-
_allMonthlyRscaleSimple(iterator) {
|
|
1700
|
-
const dates = [];
|
|
1701
|
-
let iterationCount = 0;
|
|
1702
|
-
const start = this.originalDtstart;
|
|
1703
|
-
const interval = this.opts.interval ?? 1;
|
|
1704
|
-
const targetDom = start.day;
|
|
1705
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1706
|
-
let cursor = start.with({ day: 1 });
|
|
1707
|
-
while (true) {
|
|
1708
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
1709
|
-
const lastDay = cursor.add({ months: 1 }).subtract({ days: 1 }).day;
|
|
1710
|
-
let occ = null;
|
|
1711
|
-
if (targetDom <= lastDay) occ = cursor.with({ day: targetDom });
|
|
1712
|
-
else {
|
|
1713
|
-
const skip = this.opts.skip || "OMIT";
|
|
1714
|
-
if (skip === "BACKWARD") occ = cursor.with({ day: lastDay });
|
|
1715
|
-
else if (skip === "FORWARD") occ = cursor.add({ months: 1 }).with({ day: 1 });
|
|
1716
|
-
else occ = null;
|
|
1717
|
-
}
|
|
1718
|
-
if (occ) {
|
|
1719
|
-
occ = occ.with({
|
|
1720
|
-
hour: start.hour,
|
|
1721
|
-
minute: start.minute,
|
|
1722
|
-
second: start.second
|
|
1723
|
-
});
|
|
1724
|
-
if (!(iterator && this.isExcluded(occ))) {
|
|
1725
|
-
if (Temporal.ZonedDateTime.compare(occ, start) >= 0) if (!iterator || iterator(occ, dates.length)) {
|
|
1726
|
-
dates.push(occ);
|
|
1727
|
-
if (this.shouldBreakForCountLimit(dates.length)) break;
|
|
1728
|
-
} else break;
|
|
1729
|
-
}
|
|
1730
|
-
}
|
|
1731
|
-
cursor = cursor.add({ months: interval });
|
|
1732
|
-
if (this.opts.until && Temporal.ZonedDateTime.compare(cursor, this.opts.until) > 0) break;
|
|
1733
|
-
}
|
|
1734
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
1735
|
-
}
|
|
1736
|
-
/**
|
|
1737
|
-
* Converts rDate entries to ZonedDateTime and merges with existing dates.
|
|
1738
|
-
* @param dates - Array of dates to merge with
|
|
1739
|
-
* @returns Merged and deduplicated array of dates
|
|
1740
|
-
*/
|
|
1741
|
-
mergeAndDeduplicateRDates(dates) {
|
|
1742
|
-
if (this.opts.rDate) dates.push(...this.opts.rDate);
|
|
1743
|
-
dates.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1744
|
-
const dedup = [];
|
|
1745
|
-
for (const d of dates) if (dedup.length === 0 || Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) dedup.push(d);
|
|
1746
|
-
return dedup;
|
|
1747
|
-
}
|
|
1748
|
-
/**
|
|
1749
|
-
* Checks if a date is in the exDate list.
|
|
1750
|
-
* @param date - Date to check
|
|
1751
|
-
* @returns True if the date is excluded
|
|
1752
|
-
*/
|
|
1753
|
-
isExcluded(date) {
|
|
1754
|
-
if (!this.opts.exDate || this.opts.exDate.length === 0) return false;
|
|
1755
|
-
return this.opts.exDate.some((exDate) => Temporal.ZonedDateTime.compare(date, exDate) === 0);
|
|
1756
|
-
}
|
|
1757
|
-
/**
|
|
1758
|
-
* Excludes exDate entries from the given array of dates.
|
|
1759
|
-
* @param dates - Array of dates to filter
|
|
1760
|
-
* @returns Filtered array with exDate entries removed
|
|
1761
|
-
*/
|
|
1762
|
-
excludeExDates(dates) {
|
|
1763
|
-
if (!this.opts.exDate || this.opts.exDate.length === 0) return dates;
|
|
1764
|
-
return dates.filter((date) => {
|
|
1765
|
-
return !this.isExcluded(date);
|
|
1766
|
-
});
|
|
1767
|
-
}
|
|
1768
|
-
/**
|
|
1769
|
-
* Applies count limit and merges rDates with the rule-generated dates.
|
|
1770
|
-
* @param dates - Array of dates generated by the rule
|
|
1771
|
-
* @param iterator - Optional iterator function
|
|
1772
|
-
* @returns Final array of dates after merging and applying count limit
|
|
1773
|
-
*/
|
|
1774
|
-
applyCountLimitAndMergeRDates(dates, iterator) {
|
|
1775
|
-
const merged = this.mergeAndDeduplicateRDates(dates);
|
|
1776
|
-
const excluded = this.excludeExDates(merged);
|
|
1777
|
-
const hasCountLimit = this.opts.count !== void 0;
|
|
1778
|
-
if (!hasCountLimit && !iterator) return excluded;
|
|
1779
|
-
let emitted = 0;
|
|
1780
|
-
const max = hasCountLimit ? this.opts.count : Infinity;
|
|
1781
|
-
const finalDates = [];
|
|
1782
|
-
for (const d of excluded) {
|
|
1783
|
-
if (emitted >= max) break;
|
|
1784
|
-
if (iterator && !iterator(d, emitted)) break;
|
|
1785
|
-
finalDates.push(d);
|
|
1786
|
-
emitted++;
|
|
1787
|
-
}
|
|
1788
|
-
return finalDates;
|
|
1789
|
-
}
|
|
1790
|
-
/**
|
|
1791
|
-
* Checks if the count limit should break the loop based on rDate presence.
|
|
1792
|
-
* @param matchCount - Current number of matches
|
|
1793
|
-
* @returns true if the loop should break
|
|
1794
|
-
*/
|
|
1795
|
-
shouldBreakForCountLimit(matchCount) {
|
|
1796
|
-
if (this.opts.count === void 0) return false;
|
|
1797
|
-
if (!this.opts.rDate) return matchCount >= this.opts.count;
|
|
1798
|
-
const rDateCount = this.opts.rDate.length;
|
|
1799
|
-
const targetRuleCount = Math.max(this.opts.count - rDateCount, 0);
|
|
1800
|
-
return matchCount >= targetRuleCount + Math.min(targetRuleCount, 10);
|
|
1801
|
-
}
|
|
1802
|
-
hasTimeOfDayBetween(startTime, endTime) {
|
|
1803
|
-
if (Temporal.PlainTime.compare(startTime, endTime) >= 0) return false;
|
|
1804
|
-
const base = this.originalDtstart;
|
|
1805
|
-
const hours = this.opts.byHour ?? [base.hour];
|
|
1806
|
-
const minutes = this.opts.byMinute ?? [base.minute];
|
|
1807
|
-
const seconds = this.opts.bySecond ?? [base.second];
|
|
1808
|
-
for (const hour of hours) for (const minute of minutes) for (const second of seconds) {
|
|
1809
|
-
const candidate = Temporal.PlainTime.from({
|
|
1810
|
-
hour,
|
|
1811
|
-
minute,
|
|
1812
|
-
second,
|
|
1813
|
-
millisecond: base.millisecond,
|
|
1814
|
-
microsecond: base.microsecond,
|
|
1815
|
-
nanosecond: base.nanosecond
|
|
1816
|
-
});
|
|
1817
|
-
if (Temporal.PlainTime.compare(candidate, startTime) >= 0 && Temporal.PlainTime.compare(candidate, endTime) < 0) return true;
|
|
1818
|
-
}
|
|
1819
|
-
return false;
|
|
1820
|
-
}
|
|
1821
|
-
/**
|
|
1822
|
-
* Returns all occurrences of the rule within a specified time window.
|
|
1823
|
-
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
1824
|
-
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
1825
|
-
* @param inc - Optional boolean flag to include the start and end dates in the results.
|
|
1826
|
-
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
1827
|
-
*/
|
|
1828
|
-
between(after, before, inc = false) {
|
|
1829
|
-
const startInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : normalizeZonedDateTime(after, "after").toInstant();
|
|
1830
|
-
const endInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : normalizeZonedDateTime(before, "before").toInstant();
|
|
1831
|
-
const startZdt = Temporal.Instant.from(startInst).toZonedDateTimeISO(this.tzid);
|
|
1832
|
-
const beforeZdt = Temporal.Instant.from(endInst).toZonedDateTimeISO(this.tzid);
|
|
1833
|
-
const tempOpts = { ...this.opts };
|
|
1834
|
-
if (!tempOpts.until || Temporal.ZonedDateTime.compare(beforeZdt, tempOpts.until) < 0) tempOpts.until = beforeZdt;
|
|
1835
|
-
if (tempOpts.count === void 0) {
|
|
1836
|
-
const interval = tempOpts.interval ?? 1;
|
|
1837
|
-
const aligned = startZdt.withPlainTime(this.originalDtstart.toPlainTime());
|
|
1838
|
-
let unit;
|
|
1839
|
-
switch (tempOpts.freq) {
|
|
1840
|
-
case "YEARLY":
|
|
1841
|
-
unit = "years";
|
|
1842
|
-
break;
|
|
1843
|
-
case "MONTHLY":
|
|
1844
|
-
unit = "months";
|
|
1845
|
-
break;
|
|
1846
|
-
case "WEEKLY":
|
|
1847
|
-
unit = "weeks";
|
|
1848
|
-
break;
|
|
1849
|
-
case "DAILY":
|
|
1850
|
-
unit = "days";
|
|
1851
|
-
break;
|
|
1852
|
-
case "HOURLY":
|
|
1853
|
-
unit = "hours";
|
|
1854
|
-
break;
|
|
1855
|
-
case "MINUTELY":
|
|
1856
|
-
unit = "minutes";
|
|
1857
|
-
break;
|
|
1858
|
-
default: unit = "seconds";
|
|
1859
|
-
}
|
|
1860
|
-
const dtstartNormalized = RRuleTemporal.normalizeToPolyfill(this.opts.dtstart);
|
|
1861
|
-
const startZdtNormalized = RRuleTemporal.normalizeToPolyfill(startZdt).withTimeZone(dtstartNormalized.timeZoneId);
|
|
1862
|
-
const alignedNormalized = RRuleTemporal.normalizeToPolyfill(aligned.withPlainTime(this.originalDtstart.toPlainTime())).withTimeZone(dtstartNormalized.timeZoneId);
|
|
1863
|
-
const diffAnchor = [
|
|
1864
|
-
"hours",
|
|
1865
|
-
"minutes",
|
|
1866
|
-
"seconds"
|
|
1867
|
-
].includes(unit) ? startZdtNormalized : alignedNormalized;
|
|
1868
|
-
const unitsBetween = dtstartNormalized.until(diffAnchor, { largestUnit: unit })[unit];
|
|
1869
|
-
let steps = Math.floor(unitsBetween / interval);
|
|
1870
|
-
const durationForJump = (jump) => {
|
|
1871
|
-
switch (unit) {
|
|
1872
|
-
case "years": return { years: jump };
|
|
1873
|
-
case "months": return { months: jump };
|
|
1874
|
-
case "weeks": return { weeks: jump };
|
|
1875
|
-
case "days": return { days: jump };
|
|
1876
|
-
case "hours": return { hours: jump };
|
|
1877
|
-
case "minutes": return { minutes: jump };
|
|
1878
|
-
default: return { seconds: jump };
|
|
1879
|
-
}
|
|
1880
|
-
};
|
|
1881
|
-
let candidate = RRuleTemporal.normalizeToPolyfill(this.opts.dtstart.add(durationForJump(steps * interval)));
|
|
1882
|
-
if (steps > 0 && [
|
|
1883
|
-
"years",
|
|
1884
|
-
"months",
|
|
1885
|
-
"weeks",
|
|
1886
|
-
"days"
|
|
1887
|
-
].includes(unit)) {
|
|
1888
|
-
if (candidate.toPlainDate().equals(startZdtNormalized.toPlainDate()) && Temporal.ZonedDateTime.compare(candidate, startZdtNormalized) > 0) {
|
|
1889
|
-
if (this.hasTimeOfDayBetween(startZdtNormalized.toPlainTime(), candidate.toPlainTime())) {
|
|
1890
|
-
steps -= 1;
|
|
1891
|
-
candidate = RRuleTemporal.normalizeToPolyfill(this.opts.dtstart.add(durationForJump(steps * interval)));
|
|
1892
|
-
}
|
|
1893
|
-
}
|
|
1894
|
-
}
|
|
1895
|
-
const dtstartForCompare = RRuleTemporal.normalizeToPolyfill(this.opts.dtstart);
|
|
1896
|
-
if (Temporal.ZonedDateTime.compare(candidate, dtstartForCompare) < 0) candidate = dtstartForCompare;
|
|
1897
|
-
tempOpts.dtstart = candidate;
|
|
1898
|
-
}
|
|
1899
|
-
const allDates = new RRuleTemporal(tempOpts).allInternal();
|
|
1900
|
-
return RRuleTemporal.toPublicDates(allDates.filter((date) => {
|
|
1901
|
-
const inst = date.toInstant();
|
|
1902
|
-
const afterStart = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
1903
|
-
const beforeEnd = inc ? Temporal.Instant.compare(inst, endInst) <= 0 : Temporal.Instant.compare(inst, endInst) < 0;
|
|
1904
|
-
return afterStart && beforeEnd;
|
|
1905
|
-
}));
|
|
1906
|
-
}
|
|
1907
|
-
/**
|
|
1908
|
-
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
1909
|
-
* This checks full date-time equality (including time and time zone).
|
|
1910
|
-
*/
|
|
1911
|
-
matches(date) {
|
|
1912
|
-
return this.between(date, date, true).length > 0;
|
|
1913
|
-
}
|
|
1914
|
-
/**
|
|
1915
|
-
* Convenience helper: true if any occurrence falls on the given calendar day
|
|
1916
|
-
* in the rule's time zone. This ignores time-of-day granularity.
|
|
1917
|
-
*/
|
|
1918
|
-
occursOn(date) {
|
|
1919
|
-
const startOfDay = Temporal.PlainDate.from(date.toString()).toZonedDateTime({
|
|
1920
|
-
timeZone: this.tzid,
|
|
1921
|
-
plainTime: Temporal.PlainTime.from("00:00")
|
|
1922
|
-
});
|
|
1923
|
-
const endOfDay = startOfDay.add({ days: 1 }).subtract({ nanoseconds: 1 });
|
|
1924
|
-
return this.between(startOfDay, endOfDay, true).length > 0;
|
|
1925
|
-
}
|
|
1926
|
-
/**
|
|
1927
|
-
* Returns the next occurrence of the rule after a specified date.
|
|
1928
|
-
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
1929
|
-
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
1930
|
-
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
1931
|
-
*/
|
|
1932
|
-
next(after = /* @__PURE__ */ new Date(), inc = false) {
|
|
1933
|
-
const afterInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : normalizeZonedDateTime(after, "after").toInstant();
|
|
1934
|
-
let result = null;
|
|
1935
|
-
this.allInternal((occ) => {
|
|
1936
|
-
const inst = occ.toInstant();
|
|
1937
|
-
if (inc ? Temporal.Instant.compare(inst, afterInst) >= 0 : Temporal.Instant.compare(inst, afterInst) > 0) {
|
|
1938
|
-
if (!result || Temporal.ZonedDateTime.compare(occ, result) < 0) result = occ;
|
|
1939
|
-
return false;
|
|
1940
|
-
}
|
|
1941
|
-
return true;
|
|
1942
|
-
});
|
|
1943
|
-
return RRuleTemporal.toPublicDate(result);
|
|
1944
|
-
}
|
|
1945
|
-
/**
|
|
1946
|
-
* Returns the previous occurrence of the rule before a specified date.
|
|
1947
|
-
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
1948
|
-
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
1949
|
-
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
1950
|
-
*/
|
|
1951
|
-
previous(before = /* @__PURE__ */ new Date(), inc = false) {
|
|
1952
|
-
const beforeInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : normalizeZonedDateTime(before, "before").toInstant();
|
|
1953
|
-
let prev = null;
|
|
1954
|
-
this.allInternal((occ) => {
|
|
1955
|
-
const inst = occ.toInstant();
|
|
1956
|
-
if (inc ? Temporal.Instant.compare(inst, beforeInst) > 0 : Temporal.Instant.compare(inst, beforeInst) >= 0) return false;
|
|
1957
|
-
prev = occ;
|
|
1958
|
-
return true;
|
|
1959
|
-
});
|
|
1960
|
-
return RRuleTemporal.toPublicDate(prev);
|
|
1961
|
-
}
|
|
1962
|
-
toString() {
|
|
1963
|
-
const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
|
|
1964
|
-
const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
|
|
1965
|
-
const rule = [];
|
|
1966
|
-
const { freq, interval, count, until, byHour, byMinute, bySecond, byDay, byMonth, byMonthDay, bySetPos, byWeekNo, byYearDay, wkst, rDate, exDate } = this.opts;
|
|
1967
|
-
if (this.opts.rscale) rule.push(`RSCALE=${this.opts.rscale}`);
|
|
1968
|
-
if (this.opts.rscale && this.opts.skip) rule.push(`SKIP=${this.opts.skip}`);
|
|
1969
|
-
rule.push(`FREQ=${freq}`);
|
|
1970
|
-
if (interval !== 1) rule.push(`INTERVAL=${interval}`);
|
|
1971
|
-
if (count !== void 0) rule.push(`COUNT=${count}`);
|
|
1972
|
-
if (until) rule.push(`UNTIL=${this.formatIcsDateTime(until)}`);
|
|
1973
|
-
if (byHour) rule.push(`BYHOUR=${byHour.join(",")}`);
|
|
1974
|
-
if (byMinute) rule.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
1975
|
-
if (bySecond) rule.push(`BYSECOND=${bySecond.join(",")}`);
|
|
1976
|
-
if (byDay) rule.push(`BYDAY=${byDay.join(",")}`);
|
|
1977
|
-
if (byMonth) rule.push(`BYMONTH=${byMonth.join(",")}`);
|
|
1978
|
-
if (byMonthDay) rule.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
|
|
1979
|
-
if (bySetPos) rule.push(`BYSETPOS=${bySetPos.join(",")}`);
|
|
1980
|
-
if (byWeekNo) rule.push(`BYWEEKNO=${byWeekNo.join(",")}`);
|
|
1981
|
-
if (byYearDay) rule.push(`BYYEARDAY=${byYearDay.join(",")}`);
|
|
1982
|
-
if (wkst) rule.push(`WKST=${wkst}`);
|
|
1983
|
-
const lines = [dtLine, `RRULE:${rule.join(";")}`];
|
|
1984
|
-
if (rDate) lines.push(`RDATE:${this.joinDates(rDate)}`);
|
|
1985
|
-
if (exDate) lines.push(`EXDATE:${this.joinDates(exDate)}`);
|
|
1986
|
-
return lines.join("\n");
|
|
1987
|
-
}
|
|
1988
|
-
formatIcsDateTime(date) {
|
|
1989
|
-
return date.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z";
|
|
1990
|
-
}
|
|
1991
|
-
joinDates(dates) {
|
|
1992
|
-
return dates.map((d) => this.formatIcsDateTime(d));
|
|
1993
|
-
}
|
|
1994
|
-
/**
|
|
1995
|
-
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
1996
|
-
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
1997
|
-
*/
|
|
1998
|
-
generateMonthlyOccurrences(sample) {
|
|
1999
|
-
const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
|
|
2000
|
-
if (!this.opts.byDay && !this.opts.byMonthDay) return this.expandByTime(sample);
|
|
2001
|
-
const finalDays = this.generateMonthlyOccurrenceDays(monthStart);
|
|
2002
|
-
if (finalDays.length === 0) return [];
|
|
2003
|
-
return finalDays.map((d) => monthStart.with({ day: d })).flatMap((z) => this.expandByTime(z));
|
|
2004
|
-
}
|
|
2005
|
-
/**
|
|
2006
|
-
* Given any date in a year, return all ZonedDateTimes in that year matching
|
|
2007
|
-
* the BYDAY/BYMONTHDAY/BYMONTH constraints. Months default to DTSTART's month
|
|
2008
|
-
* if BYMONTH is not specified.
|
|
2009
|
-
*/
|
|
2010
|
-
generateYearlyOccurrences(sample) {
|
|
2011
|
-
const months = this.opts.byMonth ? this.opts.byMonth.filter((v) => typeof v === "number").sort((a, b) => a - b) : this.opts.byMonthDay || this.opts.byDay ? [
|
|
2012
|
-
1,
|
|
2013
|
-
2,
|
|
2014
|
-
3,
|
|
2015
|
-
4,
|
|
2016
|
-
5,
|
|
2017
|
-
6,
|
|
2018
|
-
7,
|
|
2019
|
-
8,
|
|
2020
|
-
9,
|
|
2021
|
-
10,
|
|
2022
|
-
11,
|
|
2023
|
-
12
|
|
2024
|
-
] : [this.originalDtstart.month];
|
|
2025
|
-
let occs = [];
|
|
2026
|
-
if (this.opts.byDay && this.opts.byDay.some((t) => /^[+-]?\d/.test(t)) && !this.opts.byMonth) {
|
|
2027
|
-
const dayMap = weekdayToIsoDay;
|
|
2028
|
-
for (const tok of this.opts.byDay) {
|
|
2029
|
-
const parsed = parseByDayToken(tok);
|
|
2030
|
-
if (!parsed || parsed.ord === 0) continue;
|
|
2031
|
-
const ord = parsed.ord;
|
|
2032
|
-
const wd = dayMap[parsed.weekday];
|
|
2033
|
-
let dt;
|
|
2034
|
-
if (ord > 0) {
|
|
2035
|
-
const jan1 = sample.with({
|
|
2036
|
-
month: 1,
|
|
2037
|
-
day: 1
|
|
2038
|
-
});
|
|
2039
|
-
const delta = (wd - jan1.dayOfWeek + 7) % 7;
|
|
2040
|
-
dt = jan1.add({ days: delta + 7 * (ord - 1) });
|
|
2041
|
-
} else {
|
|
2042
|
-
const dec31 = sample.with({
|
|
2043
|
-
month: 12,
|
|
2044
|
-
day: 31
|
|
2045
|
-
});
|
|
2046
|
-
const delta = (dec31.dayOfWeek - wd + 7) % 7;
|
|
2047
|
-
dt = dec31.subtract({ days: delta + 7 * (-ord - 1) });
|
|
2048
|
-
}
|
|
2049
|
-
occs.push(...this.expandByTime(dt));
|
|
2050
|
-
}
|
|
2051
|
-
} else if (!this.opts.byYearDay && !this.opts.byWeekNo) {
|
|
2052
|
-
occs = [];
|
|
2053
|
-
for (const m of months) {
|
|
2054
|
-
const monthSample = sample.with({
|
|
2055
|
-
month: m,
|
|
2056
|
-
day: 1
|
|
2057
|
-
});
|
|
2058
|
-
const monthOccs = this.generateMonthlyOccurrences(monthSample);
|
|
2059
|
-
if (monthOccs.length === 0 && this.opts.rscale && this.opts.byMonthDay && this.opts.byMonthDay.length > 0) {
|
|
2060
|
-
const lastDay = monthSample.add({ months: 1 }).subtract({ days: 1 }).day;
|
|
2061
|
-
const target = this.opts.byMonthDay[0];
|
|
2062
|
-
const absTarget = target > 0 ? target : lastDay + target + 1;
|
|
2063
|
-
if (absTarget > lastDay || absTarget <= 0) {
|
|
2064
|
-
const skip = this.opts.skip || "OMIT";
|
|
2065
|
-
if (skip === "BACKWARD") occs.push(...this.expandByTime(monthSample.with({ day: lastDay })));
|
|
2066
|
-
else if (skip === "FORWARD") {
|
|
2067
|
-
const nextMonth = monthSample.add({ months: 1 }).with({ day: 1 });
|
|
2068
|
-
occs.push(...this.expandByTime(nextMonth));
|
|
2069
|
-
}
|
|
2070
|
-
}
|
|
2071
|
-
} else occs.push(...monthOccs);
|
|
2072
|
-
}
|
|
2073
|
-
}
|
|
2074
|
-
if (this.opts.byYearDay) {
|
|
2075
|
-
const last = sample.with({
|
|
2076
|
-
month: 12,
|
|
2077
|
-
day: 31
|
|
2078
|
-
}).dayOfYear;
|
|
2079
|
-
for (const d of this.opts.byYearDay) {
|
|
2080
|
-
const dayNum = d > 0 ? d : last + d + 1;
|
|
2081
|
-
if (dayNum <= 0 || dayNum > last) continue;
|
|
2082
|
-
const dt = this.opts.freq === "MINUTELY" ? sample.with({
|
|
2083
|
-
month: 1,
|
|
2084
|
-
day: 1,
|
|
2085
|
-
hour: 0,
|
|
2086
|
-
minute: 0,
|
|
2087
|
-
second: 0,
|
|
2088
|
-
millisecond: 0
|
|
2089
|
-
}).add({ days: dayNum - 1 }) : sample.with({
|
|
2090
|
-
month: 1,
|
|
2091
|
-
day: 1
|
|
2092
|
-
}).add({ days: dayNum - 1 });
|
|
2093
|
-
if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) occs.push(...this.expandByTime(dt));
|
|
2094
|
-
}
|
|
2095
|
-
}
|
|
2096
|
-
if (this.opts.byWeekNo) {
|
|
2097
|
-
const { lastWeek, firstWeekStart, tokens } = this.isoWeekByDay(sample);
|
|
2098
|
-
for (const weekNo of this.opts.byWeekNo) {
|
|
2099
|
-
if (weekNo > 0 && weekNo > lastWeek || weekNo < 0 && -weekNo > lastWeek) continue;
|
|
2100
|
-
const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
|
|
2101
|
-
const weekStart = firstWeekStart.add({ weeks: weekIndex });
|
|
2102
|
-
occs.push(...this.addByDay(tokens, weekStart));
|
|
2103
|
-
}
|
|
2104
|
-
}
|
|
2105
|
-
occs = occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2106
|
-
occs = this.applyBySetPos(occs);
|
|
2107
|
-
return occs;
|
|
2108
|
-
}
|
|
2109
|
-
addByDay(tokens, weekStart) {
|
|
2110
|
-
const dayMap = weekdayToIsoDay;
|
|
2111
|
-
const wkst = dayMap[this.opts.wkst || "MO"];
|
|
2112
|
-
const entries = [];
|
|
2113
|
-
for (const tok of tokens) {
|
|
2114
|
-
if (!tok) continue;
|
|
2115
|
-
const targetDow = dayMap[tok];
|
|
2116
|
-
const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
|
|
2117
|
-
if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) entries.push(...this.expandByTime(inst));
|
|
2118
|
-
}
|
|
2119
|
-
return entries;
|
|
2120
|
-
}
|
|
2121
|
-
/**
|
|
2122
|
-
* Helper to find the next valid value from a sorted array
|
|
2123
|
-
*/
|
|
2124
|
-
findNextValidValue(currentValue, validValues, compare) {
|
|
2125
|
-
return validValues.find((v) => compare(v, currentValue) > 0) || null;
|
|
2126
|
-
}
|
|
2127
|
-
/**
|
|
2128
|
-
* Efficiently find the next valid date for MINUTELY and SECONDLY frequency by jumping over
|
|
2129
|
-
* large gaps when BYXXX constraints don't match.
|
|
2130
|
-
*/
|
|
2131
|
-
findNextValidDate(current) {
|
|
2132
|
-
if (this.opts.byWeekNo && this.opts.byYearDay) {
|
|
2133
|
-
const yearStart = current.with({
|
|
2134
|
-
month: 1,
|
|
2135
|
-
day: 1,
|
|
2136
|
-
hour: 0,
|
|
2137
|
-
minute: 0,
|
|
2138
|
-
second: 0,
|
|
2139
|
-
millisecond: 0
|
|
2140
|
-
});
|
|
2141
|
-
const yearDays = this.opts.byYearDay.map((yd) => {
|
|
2142
|
-
const lastDayOfYear = yearStart.with({
|
|
2143
|
-
month: 12,
|
|
2144
|
-
day: 31
|
|
2145
|
-
}).dayOfYear;
|
|
2146
|
-
return yd > 0 ? yd : lastDayOfYear + yd + 1;
|
|
2147
|
-
});
|
|
2148
|
-
for (const yd of yearDays) {
|
|
2149
|
-
const date = yearStart.add({ days: yd - 1 });
|
|
2150
|
-
if (this.matchesByWeekNo(date)) break;
|
|
2151
|
-
}
|
|
2152
|
-
}
|
|
2153
|
-
if (this.opts.byMonth) {
|
|
2154
|
-
const numericMonths = this.opts.byMonth.filter((v) => typeof v === "number");
|
|
2155
|
-
if (numericMonths.length && !numericMonths.includes(current.month)) {
|
|
2156
|
-
const months = [...numericMonths].sort((a, b) => a - b);
|
|
2157
|
-
const nextMonth = this.findNextValidValue(current.month, months, (a, b) => a - b);
|
|
2158
|
-
if (nextMonth) current = current.with({
|
|
2159
|
-
month: nextMonth,
|
|
2160
|
-
day: 1,
|
|
2161
|
-
hour: 0,
|
|
2162
|
-
minute: 0,
|
|
2163
|
-
second: 0
|
|
2164
|
-
});
|
|
2165
|
-
else current = current.add({ years: 1 }).with({
|
|
2166
|
-
month: months[0],
|
|
2167
|
-
day: 1,
|
|
2168
|
-
hour: 0,
|
|
2169
|
-
minute: 0,
|
|
2170
|
-
second: 0
|
|
2171
|
-
});
|
|
2172
|
-
current = this.applyTimeOverride(current);
|
|
2173
|
-
return current;
|
|
2174
|
-
}
|
|
2175
|
-
}
|
|
2176
|
-
if (this.opts.byWeekNo && !this.matchesByWeekNo(current)) {
|
|
2177
|
-
current = current.add({ weeks: 1 }).with({
|
|
2178
|
-
hour: 0,
|
|
2179
|
-
minute: 0,
|
|
2180
|
-
second: 0
|
|
2181
|
-
});
|
|
2182
|
-
current = this.applyTimeOverride(current);
|
|
2183
|
-
return current;
|
|
2184
|
-
}
|
|
2185
|
-
if (this.opts.byYearDay && !this.matchesByYearDay(current)) {
|
|
2186
|
-
const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
|
|
2187
|
-
const currentYearDay = current.dayOfYear;
|
|
2188
|
-
const lastDayOfYear = current.with({
|
|
2189
|
-
month: 12,
|
|
2190
|
-
day: 31
|
|
2191
|
-
}).dayOfYear;
|
|
2192
|
-
let nextYearDay = yearDays.find((d) => {
|
|
2193
|
-
return (d > 0 ? d : lastDayOfYear + d + 1) > currentYearDay;
|
|
2194
|
-
});
|
|
2195
|
-
if (nextYearDay) {
|
|
2196
|
-
const dayNum = nextYearDay > 0 ? nextYearDay : lastDayOfYear + nextYearDay + 1;
|
|
2197
|
-
if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") current = current.with({
|
|
2198
|
-
month: 1,
|
|
2199
|
-
day: 1,
|
|
2200
|
-
hour: 0,
|
|
2201
|
-
minute: 0,
|
|
2202
|
-
second: 0,
|
|
2203
|
-
millisecond: 0
|
|
2204
|
-
}).add({ days: dayNum - 1 });
|
|
2205
|
-
else current = current.with({
|
|
2206
|
-
month: 1,
|
|
2207
|
-
day: 1
|
|
2208
|
-
}).add({ days: dayNum - 1 });
|
|
2209
|
-
} else {
|
|
2210
|
-
const nextYear = current.add({ years: 1 });
|
|
2211
|
-
const nextYearLastDay = nextYear.with({
|
|
2212
|
-
month: 12,
|
|
2213
|
-
day: 31
|
|
2214
|
-
}).dayOfYear;
|
|
2215
|
-
const firstYearDay = yearDays[0];
|
|
2216
|
-
if (firstYearDay !== void 0) {
|
|
2217
|
-
const dayNum = firstYearDay > 0 ? firstYearDay : nextYearLastDay + firstYearDay + 1;
|
|
2218
|
-
if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") current = nextYear.with({
|
|
2219
|
-
month: 1,
|
|
2220
|
-
day: 1,
|
|
2221
|
-
hour: 0,
|
|
2222
|
-
minute: 0,
|
|
2223
|
-
second: 0,
|
|
2224
|
-
millisecond: 0
|
|
2225
|
-
}).add({ days: dayNum - 1 });
|
|
2226
|
-
else current = nextYear.with({
|
|
2227
|
-
month: 1,
|
|
2228
|
-
day: 1
|
|
2229
|
-
}).add({ days: dayNum - 1 });
|
|
2230
|
-
}
|
|
2231
|
-
}
|
|
2232
|
-
current = this.applyTimeOverride(current);
|
|
2233
|
-
return current;
|
|
2234
|
-
}
|
|
2235
|
-
if (this.opts.byMonthDay && !this.matchesByMonthDay(current)) {
|
|
2236
|
-
const monthDays = [...this.opts.byMonthDay].sort((a, b) => a - b);
|
|
2237
|
-
const lastDayOfMonth = current.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
2238
|
-
const currentDay = current.day;
|
|
2239
|
-
const validDays = monthDays.map((d) => d > 0 ? d : lastDayOfMonth + d + 1).filter((d) => d > 0 && d <= lastDayOfMonth).sort((a, b) => a - b);
|
|
2240
|
-
const nextDay = this.findNextValidValue(currentDay, validDays, (a, b) => a - b);
|
|
2241
|
-
if (nextDay) current = current.with({
|
|
2242
|
-
day: nextDay,
|
|
2243
|
-
hour: 0,
|
|
2244
|
-
minute: 0,
|
|
2245
|
-
second: 0
|
|
2246
|
-
});
|
|
2247
|
-
else {
|
|
2248
|
-
const nextMonth = current.add({ months: 1 }).with({ day: 1 });
|
|
2249
|
-
const nextMonthLastDay = nextMonth.add({ months: 1 }).subtract({ days: 1 }).day;
|
|
2250
|
-
const firstMonthDay = monthDays[0];
|
|
2251
|
-
if (firstMonthDay !== void 0) {
|
|
2252
|
-
const dayNum = firstMonthDay > 0 ? firstMonthDay : nextMonthLastDay + firstMonthDay + 1;
|
|
2253
|
-
current = nextMonth.with({
|
|
2254
|
-
day: Math.max(1, Math.min(dayNum, nextMonthLastDay)),
|
|
2255
|
-
hour: 0,
|
|
2256
|
-
minute: 0,
|
|
2257
|
-
second: 0
|
|
2258
|
-
});
|
|
2259
|
-
} else current = current.add({ months: 1 }).with({
|
|
2260
|
-
day: 1,
|
|
2261
|
-
hour: 0,
|
|
2262
|
-
minute: 0,
|
|
2263
|
-
second: 0
|
|
2264
|
-
});
|
|
2265
|
-
}
|
|
2266
|
-
current = this.applyTimeOverride(current);
|
|
2267
|
-
return current;
|
|
2268
|
-
}
|
|
2269
|
-
if (this.opts.byDay && !this.matchesByDay(current)) {
|
|
2270
|
-
const targetDays = this.allByDayIsoDays;
|
|
2271
|
-
if (!targetDays?.length) return this.applyTimeOverride(current.add({ days: 1 }).with({
|
|
2272
|
-
hour: 0,
|
|
2273
|
-
minute: 0,
|
|
2274
|
-
second: 0
|
|
2275
|
-
}));
|
|
2276
|
-
const nextDayOfWeek = this.findNextValidValue(current.dayOfWeek, targetDays, (a, b) => a - b);
|
|
2277
|
-
if (nextDayOfWeek) {
|
|
2278
|
-
const delta = (nextDayOfWeek - current.dayOfWeek + 7) % 7;
|
|
2279
|
-
current = current.add({ days: delta }).with({
|
|
2280
|
-
hour: 0,
|
|
2281
|
-
minute: 0,
|
|
2282
|
-
second: 0
|
|
2283
|
-
});
|
|
2284
|
-
} else {
|
|
2285
|
-
const delta = (targetDays[0] - current.dayOfWeek + 7) % 7;
|
|
2286
|
-
current = current.add({ days: delta + 7 }).with({
|
|
2287
|
-
hour: 0,
|
|
2288
|
-
minute: 0,
|
|
2289
|
-
second: 0
|
|
2290
|
-
});
|
|
2291
|
-
}
|
|
2292
|
-
current = this.applyTimeOverride(current);
|
|
2293
|
-
return current;
|
|
2294
|
-
}
|
|
2295
|
-
switch (this.opts.freq) {
|
|
2296
|
-
case "SECONDLY":
|
|
2297
|
-
case "MINUTELY":
|
|
2298
|
-
current = current.add({ days: 1 }).with({
|
|
2299
|
-
hour: 0,
|
|
2300
|
-
minute: 0,
|
|
2301
|
-
second: 0
|
|
2302
|
-
});
|
|
2303
|
-
break;
|
|
2304
|
-
case "HOURLY":
|
|
2305
|
-
current = current.add({ days: 1 }).with({
|
|
2306
|
-
hour: 0,
|
|
2307
|
-
minute: 0,
|
|
2308
|
-
second: 0
|
|
2309
|
-
});
|
|
2310
|
-
break;
|
|
2311
|
-
case "DAILY":
|
|
2312
|
-
case "WEEKLY":
|
|
2313
|
-
current = current.add({ months: 1 }).with({
|
|
2314
|
-
day: 1,
|
|
2315
|
-
hour: 0,
|
|
2316
|
-
minute: 0,
|
|
2317
|
-
second: 0
|
|
2318
|
-
});
|
|
2319
|
-
break;
|
|
2320
|
-
case "MONTHLY":
|
|
2321
|
-
case "YEARLY":
|
|
2322
|
-
current = current.add({ years: 1 }).with({
|
|
2323
|
-
month: 1,
|
|
2324
|
-
day: 1,
|
|
2325
|
-
hour: 0,
|
|
2326
|
-
minute: 0,
|
|
2327
|
-
second: 0
|
|
2328
|
-
});
|
|
2329
|
-
break;
|
|
2330
|
-
}
|
|
2331
|
-
return this.applyTimeOverride(current);
|
|
2332
|
-
}
|
|
2333
|
-
applyBySetPos(list) {
|
|
2334
|
-
const { bySetPos } = this.opts;
|
|
2335
|
-
if (!bySetPos || !bySetPos.length) return list;
|
|
2336
|
-
const sorted = [...list].sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2337
|
-
return this.applyBySetPosToSortedList(sorted).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2338
|
-
}
|
|
2339
|
-
isoWeekByDay(sample) {
|
|
2340
|
-
const dayMap = weekdayToIsoDay;
|
|
2341
|
-
const wkst = dayMap[this.opts.wkst || "MO"];
|
|
2342
|
-
const jan1 = sample.with({
|
|
2343
|
-
month: 1,
|
|
2344
|
-
day: 1
|
|
2345
|
-
});
|
|
2346
|
-
const jan4 = sample.with({
|
|
2347
|
-
month: 1,
|
|
2348
|
-
day: 4
|
|
2349
|
-
});
|
|
2350
|
-
const delta = (jan4.dayOfWeek - wkst + 7) % 7;
|
|
2351
|
-
const firstWeekStart = jan4.subtract({ days: delta });
|
|
2352
|
-
const isLeapYear = jan1.inLeapYear;
|
|
2353
|
-
return {
|
|
2354
|
-
lastWeek: jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52,
|
|
2355
|
-
firstWeekStart,
|
|
2356
|
-
tokens: this.opts.byDay?.length ? this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]]
|
|
2357
|
-
};
|
|
2358
|
-
}
|
|
2359
|
-
/**
|
|
2360
|
-
* Generate occurrences for a specific week number in a given year
|
|
2361
|
-
*/
|
|
2362
|
-
generateOccurrencesForWeekInYear(year, weekNo) {
|
|
2363
|
-
const occs = [];
|
|
2364
|
-
const sample = this.originalDtstart.with({
|
|
2365
|
-
year,
|
|
2366
|
-
month: 1,
|
|
2367
|
-
day: 1
|
|
2368
|
-
});
|
|
2369
|
-
const { lastWeek, firstWeekStart, tokens } = this.isoWeekByDay(sample);
|
|
2370
|
-
if (weekNo > 0 && weekNo > lastWeek || weekNo < 0 && -weekNo > lastWeek) return occs;
|
|
2371
|
-
const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
|
|
2372
|
-
const weekStart = firstWeekStart.add({ weeks: weekIndex });
|
|
2373
|
-
occs.push(...this.addByDay(tokens, weekStart));
|
|
2374
|
-
return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2375
|
-
}
|
|
2376
|
-
getRscaleCalendarId() {
|
|
2377
|
-
return {
|
|
2378
|
-
GREGORIAN: "gregory",
|
|
2379
|
-
CHINESE: "chinese",
|
|
2380
|
-
HEBREW: "hebrew",
|
|
2381
|
-
INDIAN: "indian"
|
|
2382
|
-
}[this.opts.rscale?.toUpperCase() || ""] || null;
|
|
2383
|
-
}
|
|
2384
|
-
assertRscaleCalendarSupported(calId) {
|
|
2385
|
-
if (calId === "gregory" || calId === "iso8601") return;
|
|
2386
|
-
const cached = RRuleTemporal.rscaleCalendarSupport[calId];
|
|
2387
|
-
if (cached === true) return;
|
|
2388
|
-
if (cached === false) throw new Error(`RSCALE=${this.opts.rscale} is not supported by the current Temporal/Intl implementation`);
|
|
2389
|
-
let supported = true;
|
|
2390
|
-
try {
|
|
2391
|
-
const probe = Temporal.ZonedDateTime.from("2000-01-01T00:00:00+00:00[UTC]").withCalendar(calId);
|
|
2392
|
-
probe.year;
|
|
2393
|
-
probe.monthCode;
|
|
2394
|
-
probe.day;
|
|
2395
|
-
} catch {
|
|
2396
|
-
supported = false;
|
|
2397
|
-
}
|
|
2398
|
-
RRuleTemporal.rscaleCalendarSupport[calId] = supported;
|
|
2399
|
-
if (!supported) throw new Error(`RSCALE=${this.opts.rscale} is not supported by the current Temporal/Intl implementation`);
|
|
2400
|
-
}
|
|
2401
|
-
pad2(n) {
|
|
2402
|
-
return String(n).padStart(2, "0");
|
|
2403
|
-
}
|
|
2404
|
-
monthMatchesToken(monthCode, token) {
|
|
2405
|
-
if (typeof token === "number") return monthCode === `M${this.pad2(token)}`;
|
|
2406
|
-
if (/^\d+L$/i.test(token)) {
|
|
2407
|
-
const n = parseInt(token, 10);
|
|
2408
|
-
return monthCode === `M${this.pad2(n)}L`;
|
|
2409
|
-
}
|
|
2410
|
-
return false;
|
|
2411
|
-
}
|
|
2412
|
-
monthsOfYear(calId, year) {
|
|
2413
|
-
const out = [];
|
|
2414
|
-
for (let m = 1; m <= 20; m++) try {
|
|
2415
|
-
const d = Temporal.PlainDate.from({
|
|
2416
|
-
calendar: calId,
|
|
2417
|
-
year,
|
|
2418
|
-
month: m,
|
|
2419
|
-
day: 1
|
|
2420
|
-
});
|
|
2421
|
-
out.push(d);
|
|
2422
|
-
} catch {
|
|
2423
|
-
break;
|
|
2424
|
-
}
|
|
2425
|
-
return out;
|
|
2426
|
-
}
|
|
2427
|
-
startOfYear(calId, year) {
|
|
2428
|
-
return Temporal.PlainDate.from({
|
|
2429
|
-
calendar: calId,
|
|
2430
|
-
year,
|
|
2431
|
-
month: 1,
|
|
2432
|
-
day: 1
|
|
2433
|
-
});
|
|
2434
|
-
}
|
|
2435
|
-
endOfYear(calId, year) {
|
|
2436
|
-
return this.startOfYear(calId, year + 1).subtract({ days: 1 });
|
|
2437
|
-
}
|
|
2438
|
-
rscaleFirstWeekStart(calId, year, wkst) {
|
|
2439
|
-
const jan4 = Temporal.PlainDate.from({
|
|
2440
|
-
calendar: calId,
|
|
2441
|
-
year,
|
|
2442
|
-
month: 1,
|
|
2443
|
-
day: 4
|
|
2444
|
-
});
|
|
2445
|
-
const delta = (jan4.dayOfWeek - wkst + 7) % 7;
|
|
2446
|
-
return jan4.subtract({ days: delta });
|
|
2447
|
-
}
|
|
2448
|
-
rscaleLastWeekCount(calId, year, wkst) {
|
|
2449
|
-
const firstWeekStart = this.rscaleFirstWeekStart(calId, year, wkst);
|
|
2450
|
-
const diffDays = this.endOfYear(calId, year).since(firstWeekStart).days;
|
|
2451
|
-
return Math.floor(diffDays / 7) + 1;
|
|
2452
|
-
}
|
|
2453
|
-
lastDayOfMonth(pd) {
|
|
2454
|
-
return pd.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
2455
|
-
}
|
|
2456
|
-
buildZdtFromPlainDate(pd) {
|
|
2457
|
-
const t = this.originalDtstart;
|
|
2458
|
-
return Temporal.PlainDateTime.from({
|
|
2459
|
-
calendar: pd.calendarId,
|
|
2460
|
-
year: pd.year,
|
|
2461
|
-
month: pd.month,
|
|
2462
|
-
day: pd.day,
|
|
2463
|
-
hour: t.hour,
|
|
2464
|
-
minute: t.minute,
|
|
2465
|
-
second: t.second
|
|
2466
|
-
}).toZonedDateTime(this.tzid);
|
|
2467
|
-
}
|
|
2468
|
-
rscaleMatchesByYearDay(calId, pd) {
|
|
2469
|
-
const list = this.opts.byYearDay;
|
|
2470
|
-
if (!list || list.length === 0) return true;
|
|
2471
|
-
const last = this.endOfYear(calId, pd.year).dayOfYear;
|
|
2472
|
-
return list.some((d) => d > 0 ? pd.dayOfYear === d : pd.dayOfYear === last + d + 1);
|
|
2473
|
-
}
|
|
2474
|
-
rscaleMatchesByWeekNo(calId, pd) {
|
|
2475
|
-
const list = this.opts.byWeekNo;
|
|
2476
|
-
if (!list || list.length === 0) return true;
|
|
2477
|
-
const wkst = weekdayToIsoDay[this.opts.wkst || "MO"];
|
|
2478
|
-
const weekYear = pd.subtract({ days: (pd.dayOfWeek - wkst + 7) % 7 }).add({ days: (4 - wkst + 7) % 7 }).year;
|
|
2479
|
-
const firstStart = this.rscaleFirstWeekStart(calId, weekYear, wkst);
|
|
2480
|
-
const lastWeek = this.rscaleLastWeekCount(calId, weekYear, wkst);
|
|
2481
|
-
const idx = Math.floor(pd.since(firstStart).days / 7) + 1;
|
|
2482
|
-
return list.some((wn) => wn > 0 ? idx === wn : idx === lastWeek + wn + 1);
|
|
2483
|
-
}
|
|
2484
|
-
rscaleMatchesByMonth(calId, pd) {
|
|
2485
|
-
const tokens = this.opts.byMonth;
|
|
2486
|
-
if (!tokens || tokens.length === 0) return true;
|
|
2487
|
-
return tokens.some((tok) => this.monthMatchesToken(pd.monthCode, tok));
|
|
2488
|
-
}
|
|
2489
|
-
rscaleMatchesByMonthDay(pd) {
|
|
2490
|
-
const list = this.opts.byMonthDay;
|
|
2491
|
-
if (!list || list.length === 0) return true;
|
|
2492
|
-
const last = pd.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
2493
|
-
const value = pd.day;
|
|
2494
|
-
return list.some((d) => d > 0 ? value === d : value === last + d + 1);
|
|
2495
|
-
}
|
|
2496
|
-
rscaleMatchesByDayBasic(pd) {
|
|
2497
|
-
const byDay = this.opts.byDay;
|
|
2498
|
-
if (!byDay || byDay.length === 0) return true;
|
|
2499
|
-
const dayMap = weekdayToIsoDay;
|
|
2500
|
-
const tokens = byDay.map((tok) => extractWeekdayToken(tok)).filter((x) => x !== null);
|
|
2501
|
-
if (tokens.length === 0) return true;
|
|
2502
|
-
return tokens.some((wd) => dayMap[wd] === pd.dayOfWeek);
|
|
2503
|
-
}
|
|
2504
|
-
rscaleDateMatches(calId, pd) {
|
|
2505
|
-
return this.rscaleMatchesByMonth(calId, pd) && this.rscaleMatchesByYearDay(calId, pd) && this.rscaleMatchesByWeekNo(calId, pd) && this.rscaleMatchesByMonthDay(pd) && this.rscaleMatchesByDayBasic(pd);
|
|
2506
|
-
}
|
|
2507
|
-
applySkipForDay(calId, year, monthStart, targetDay) {
|
|
2508
|
-
const last = this.lastDayOfMonth(monthStart);
|
|
2509
|
-
const skip = this.opts.skip || "OMIT";
|
|
2510
|
-
if (targetDay >= 1 && targetDay <= last) return monthStart.with({ day: targetDay });
|
|
2511
|
-
if (skip === "BACKWARD") return monthStart.with({ day: last });
|
|
2512
|
-
if (skip === "FORWARD") return monthStart.add({ months: 1 }).with({ day: 1 });
|
|
2513
|
-
return null;
|
|
2514
|
-
}
|
|
2515
|
-
generateMonthlyOccurrencesRscale(calId, year, monthStart) {
|
|
2516
|
-
const occs = [];
|
|
2517
|
-
const byMonthDay = this.opts.byMonthDay;
|
|
2518
|
-
const byDay = this.opts.byDay;
|
|
2519
|
-
if (!byDay && !byMonthDay) {
|
|
2520
|
-
const targetDay = this.originalDtstart.withCalendar(calId).day;
|
|
2521
|
-
const pd = this.applySkipForDay(calId, year, monthStart, targetDay);
|
|
2522
|
-
if (pd) occs.push(this.buildZdtFromPlainDate(pd));
|
|
2523
|
-
return occs;
|
|
2524
|
-
}
|
|
2525
|
-
const addZ = (pd) => {
|
|
2526
|
-
occs.push(this.buildZdtFromPlainDate(pd));
|
|
2527
|
-
};
|
|
2528
|
-
const last = this.lastDayOfMonth(monthStart);
|
|
2529
|
-
const resolveDay = (d) => d > 0 ? d : last + d + 1;
|
|
2530
|
-
if (byMonthDay && byMonthDay.length > 0) for (const raw of byMonthDay) {
|
|
2531
|
-
const dayNum = resolveDay(raw);
|
|
2532
|
-
const pd = this.applySkipForDay(calId, year, monthStart, dayNum);
|
|
2533
|
-
if (pd) addZ(pd);
|
|
2534
|
-
}
|
|
2535
|
-
if (byDay && byDay.length > 0) {
|
|
2536
|
-
const dayMap = weekdayToIsoDay;
|
|
2537
|
-
const buckets = {};
|
|
2538
|
-
let cur = monthStart;
|
|
2539
|
-
while (cur.month === monthStart.month && cur.year === monthStart.year) {
|
|
2540
|
-
const wd = cur.dayOfWeek;
|
|
2541
|
-
(buckets[wd] ||= []).push(cur);
|
|
2542
|
-
cur = cur.add({ days: 1 });
|
|
2543
|
-
}
|
|
2544
|
-
for (const tok of byDay) {
|
|
2545
|
-
const parsed = parseByDayToken(tok);
|
|
2546
|
-
if (!parsed) continue;
|
|
2547
|
-
const ord = parsed.ord;
|
|
2548
|
-
const list = buckets[dayMap[parsed.weekday]] || [];
|
|
2549
|
-
if (list.length === 0) continue;
|
|
2550
|
-
if (ord === 0) for (const pd of list) addZ(pd);
|
|
2551
|
-
else {
|
|
2552
|
-
const pd = list[ord > 0 ? ord - 1 : list.length + ord];
|
|
2553
|
-
if (pd) addZ(pd);
|
|
2554
|
-
}
|
|
2555
|
-
}
|
|
2556
|
-
}
|
|
2557
|
-
return this.applyBySetPos(occs).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2558
|
-
}
|
|
2559
|
-
_allRscaleNonGregorian(iterator) {
|
|
2560
|
-
const calId = this.getRscaleCalendarId();
|
|
2561
|
-
if (!calId) return this._allFallback(iterator);
|
|
2562
|
-
this.assertRscaleCalendarSupported(calId);
|
|
2563
|
-
const dates = [];
|
|
2564
|
-
let iterationCount = 0;
|
|
2565
|
-
const start = this.originalDtstart;
|
|
2566
|
-
const seed = start.withCalendar(calId);
|
|
2567
|
-
const interval = this.opts.interval ?? 1;
|
|
2568
|
-
if (!this.addDtstartIfNeeded(dates, iterator)) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
2569
|
-
if (this.opts.freq === "YEARLY") {
|
|
2570
|
-
let yearOffset = 0;
|
|
2571
|
-
while (true) {
|
|
2572
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
2573
|
-
const tgtYear = seed.year + yearOffset * interval;
|
|
2574
|
-
let occs = [];
|
|
2575
|
-
const monthsTokens = this.opts.byMonth;
|
|
2576
|
-
const months = this.monthsOfYear(calId, tgtYear);
|
|
2577
|
-
const dayMap = weekdayToIsoDay;
|
|
2578
|
-
const wkst = dayMap[this.opts.wkst || "MO"];
|
|
2579
|
-
if (this.opts.byWeekNo && this.opts.byWeekNo.length > 0) {
|
|
2580
|
-
const firstStart = this.rscaleFirstWeekStart(calId, tgtYear, wkst);
|
|
2581
|
-
const lastWeek = this.rscaleLastWeekCount(calId, tgtYear, wkst);
|
|
2582
|
-
const tokens = this.opts.byDay?.length ? this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
|
|
2583
|
-
for (const wn of this.opts.byWeekNo) {
|
|
2584
|
-
let idx = wn > 0 ? wn - 1 : lastWeek + wn;
|
|
2585
|
-
if (idx < 0 || idx >= lastWeek) continue;
|
|
2586
|
-
const weekStart = firstStart.add({ weeks: idx });
|
|
2587
|
-
for (const tok of tokens) {
|
|
2588
|
-
const targetDow = dayMap[tok];
|
|
2589
|
-
const pd = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
|
|
2590
|
-
if (monthsTokens && monthsTokens.length > 0) {
|
|
2591
|
-
if (!monthsTokens.some((t) => this.monthMatchesToken(pd.monthCode, t))) continue;
|
|
2592
|
-
}
|
|
2593
|
-
if (this.opts.byYearDay && this.opts.byYearDay.length > 0) {
|
|
2594
|
-
const lastDay = this.endOfYear(calId, tgtYear).dayOfYear;
|
|
2595
|
-
if (!this.opts.byYearDay.some((d) => {
|
|
2596
|
-
const target = d > 0 ? d : lastDay + d + 1;
|
|
2597
|
-
return pd.dayOfYear === target;
|
|
2598
|
-
})) continue;
|
|
2599
|
-
}
|
|
2600
|
-
occs.push(this.buildZdtFromPlainDate(pd));
|
|
2601
|
-
}
|
|
2602
|
-
}
|
|
2603
|
-
} else if (this.opts.byYearDay && this.opts.byYearDay.length > 0) {
|
|
2604
|
-
const startOfYear = this.startOfYear(calId, tgtYear);
|
|
2605
|
-
const lastDay = this.endOfYear(calId, tgtYear).dayOfYear;
|
|
2606
|
-
for (const d of this.opts.byYearDay) {
|
|
2607
|
-
const target = d > 0 ? d : lastDay + d + 1;
|
|
2608
|
-
if (target < 1 || target > lastDay) continue;
|
|
2609
|
-
let pd = startOfYear.add({ days: target - 1 });
|
|
2610
|
-
if (monthsTokens && monthsTokens.length > 0) {
|
|
2611
|
-
if (!monthsTokens.some((t) => this.monthMatchesToken(pd.monthCode, t))) continue;
|
|
2612
|
-
}
|
|
2613
|
-
occs.push(this.buildZdtFromPlainDate(pd));
|
|
2614
|
-
}
|
|
2615
|
-
} else if (!monthsTokens || monthsTokens.length === 0) try {
|
|
2616
|
-
const pd = Temporal.PlainDate.from({
|
|
2617
|
-
calendar: calId,
|
|
2618
|
-
year: tgtYear,
|
|
2619
|
-
monthCode: seed.monthCode,
|
|
2620
|
-
day: seed.day
|
|
2621
|
-
});
|
|
2622
|
-
occs.push(this.buildZdtFromPlainDate(pd));
|
|
2623
|
-
} catch {
|
|
2624
|
-
const skip = this.opts.skip || "OMIT";
|
|
2625
|
-
if (skip === "FORWARD" || skip === "BACKWARD") {
|
|
2626
|
-
const mapped = seed.with({ year: tgtYear });
|
|
2627
|
-
const adjusted = skip === "BACKWARD" ? mapped.subtract({ days: 1 }) : mapped;
|
|
2628
|
-
occs.push(adjusted.withCalendar("iso8601"));
|
|
2629
|
-
}
|
|
2630
|
-
}
|
|
2631
|
-
else {
|
|
2632
|
-
const monthStarts = months.filter((m) => monthsTokens.some((tok) => this.monthMatchesToken(m.monthCode, tok)));
|
|
2633
|
-
for (const ms of monthStarts) occs.push(...this.generateMonthlyOccurrencesRscale(calId, tgtYear, ms));
|
|
2634
|
-
}
|
|
2635
|
-
if (occs.length > 0) {
|
|
2636
|
-
const sorted = occs.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2637
|
-
const { shouldBreak } = this.processOccurrences(sorted, dates, start, iterator);
|
|
2638
|
-
if (shouldBreak) break;
|
|
2639
|
-
}
|
|
2640
|
-
yearOffset++;
|
|
2641
|
-
if (this.opts.until && tgtYear > this.opts.until.withCalendar(calId).year) break;
|
|
2642
|
-
}
|
|
2643
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
2644
|
-
}
|
|
2645
|
-
if (this.opts.freq === "WEEKLY") {
|
|
2646
|
-
const dayMap = weekdayToIsoDay;
|
|
2647
|
-
const wkst = dayMap[this.opts.wkst || "MO"];
|
|
2648
|
-
const tokens = this.opts.byDay?.length ? this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
|
|
2649
|
-
let weekStart = seed.toPlainDate().subtract({ days: (seed.dayOfWeek - wkst + 7) % 7 });
|
|
2650
|
-
while (true) {
|
|
2651
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
2652
|
-
const occs = [];
|
|
2653
|
-
for (const tok of tokens) {
|
|
2654
|
-
const targetDow = dayMap[tok];
|
|
2655
|
-
const pd = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
|
|
2656
|
-
if (Temporal.ZonedDateTime.compare(this.buildZdtFromPlainDate(pd), this.originalDtstart) < 0) continue;
|
|
2657
|
-
if (this.opts.byWeekNo && this.opts.byWeekNo.length > 0) {
|
|
2658
|
-
const weekYear = weekStart.add({ days: (4 - wkst + 7) % 7 }).year;
|
|
2659
|
-
const firstStart = this.rscaleFirstWeekStart(calId, weekYear, wkst);
|
|
2660
|
-
const lastWeek = this.rscaleLastWeekCount(calId, weekYear, wkst);
|
|
2661
|
-
const idx = Math.floor(pd.since(firstStart).days / 7) + 1;
|
|
2662
|
-
if (!this.opts.byWeekNo.some((wn) => wn > 0 ? idx === wn : idx === lastWeek + wn + 1)) continue;
|
|
2663
|
-
}
|
|
2664
|
-
if (this.opts.byYearDay && this.opts.byYearDay.length > 0) {
|
|
2665
|
-
const last = this.endOfYear(calId, pd.year).dayOfYear;
|
|
2666
|
-
if (!this.opts.byYearDay.some((d) => d > 0 ? pd.dayOfYear === d : pd.dayOfYear === last + d + 1)) continue;
|
|
2667
|
-
}
|
|
2668
|
-
const monthsTokens = this.opts.byMonth;
|
|
2669
|
-
if (monthsTokens && monthsTokens.length > 0) {
|
|
2670
|
-
if (!monthsTokens.some((t) => this.monthMatchesToken(pd.monthCode, t))) continue;
|
|
2671
|
-
}
|
|
2672
|
-
occs.push(this.buildZdtFromPlainDate(pd));
|
|
2673
|
-
}
|
|
2674
|
-
if (occs.length) {
|
|
2675
|
-
const sorted = occs.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2676
|
-
const { shouldBreak } = this.processOccurrences(sorted, dates, start, iterator);
|
|
2677
|
-
if (shouldBreak) return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
2678
|
-
}
|
|
2679
|
-
weekStart = weekStart.add({ weeks: this.opts.interval ?? 1 });
|
|
2680
|
-
if (this.opts.until) {
|
|
2681
|
-
const z = this.buildZdtFromPlainDate(weekStart.add({ days: 6 }));
|
|
2682
|
-
if (Temporal.ZonedDateTime.compare(z, this.opts.until) > 0) break;
|
|
2683
|
-
}
|
|
2684
|
-
}
|
|
2685
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
2686
|
-
}
|
|
2687
|
-
if (this.opts.freq === "MONTHLY") {
|
|
2688
|
-
let cursor = seed.toPlainDate().with({ day: 1 });
|
|
2689
|
-
while (true) {
|
|
2690
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
2691
|
-
const year = cursor.year;
|
|
2692
|
-
const monthStart = cursor;
|
|
2693
|
-
let proceed = true;
|
|
2694
|
-
const monthsTokens = this.opts.byMonth;
|
|
2695
|
-
if (monthsTokens && monthsTokens.length > 0) proceed = monthsTokens.some((tok) => this.monthMatchesToken(monthStart.monthCode, tok));
|
|
2696
|
-
if (proceed) {
|
|
2697
|
-
const sorted = this.generateMonthlyOccurrencesRscale(calId, year, monthStart).flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
2698
|
-
const { shouldBreak } = this.processOccurrences(sorted, dates, start, iterator);
|
|
2699
|
-
if (shouldBreak) break;
|
|
2700
|
-
}
|
|
2701
|
-
cursor = cursor.add({ months: this.opts.interval ?? 1 });
|
|
2702
|
-
if (this.opts.until) {
|
|
2703
|
-
const z = this.buildZdtFromPlainDate(cursor);
|
|
2704
|
-
if (Temporal.ZonedDateTime.compare(z, this.opts.until) > 0) break;
|
|
2705
|
-
}
|
|
2706
|
-
}
|
|
2707
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
2708
|
-
}
|
|
2709
|
-
if (this.opts.freq === "DAILY") {
|
|
2710
|
-
let pd = seed.toPlainDate();
|
|
2711
|
-
while (true) {
|
|
2712
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
2713
|
-
if (this.rscaleDateMatches(calId, pd)) {
|
|
2714
|
-
const base = this.buildZdtFromPlainDate(pd);
|
|
2715
|
-
let occs = this.expandByTime(base);
|
|
2716
|
-
occs = this.applyBySetPos(occs);
|
|
2717
|
-
const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
|
|
2718
|
-
if (shouldBreak) break;
|
|
2719
|
-
}
|
|
2720
|
-
pd = pd.add({ days: this.opts.interval ?? 1 });
|
|
2721
|
-
if (this.opts.until) {
|
|
2722
|
-
const z = this.buildZdtFromPlainDate(pd);
|
|
2723
|
-
if (Temporal.ZonedDateTime.compare(z, this.opts.until) > 0) break;
|
|
2724
|
-
}
|
|
2725
|
-
}
|
|
2726
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
2727
|
-
}
|
|
2728
|
-
if (this.opts.freq === "HOURLY" || this.opts.freq === "MINUTELY") {
|
|
2729
|
-
this.opts.freq;
|
|
2730
|
-
const unitMs = this.opts.freq === "HOURLY" ? 36e5 : 6e4;
|
|
2731
|
-
const interval = this.opts.interval ?? 1;
|
|
2732
|
-
let pd = seed.toPlainDate();
|
|
2733
|
-
const startInstantMs = this.originalDtstart.toInstant().epochMilliseconds;
|
|
2734
|
-
while (true) {
|
|
2735
|
-
if (++iterationCount > this.maxIterations) throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
2736
|
-
if (this.rscaleDateMatches(calId, pd)) {
|
|
2737
|
-
const base = this.buildZdtFromPlainDate(pd);
|
|
2738
|
-
let occs = this.expandByTime(base);
|
|
2739
|
-
occs = occs.filter((occ) => {
|
|
2740
|
-
const delta = occ.toInstant().epochMilliseconds - startInstantMs;
|
|
2741
|
-
return Math.floor(delta / unitMs) % interval === 0;
|
|
2742
|
-
});
|
|
2743
|
-
const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
|
|
2744
|
-
if (shouldBreak) break;
|
|
2745
|
-
}
|
|
2746
|
-
pd = pd.add({ days: 1 });
|
|
2747
|
-
if (this.opts.until) {
|
|
2748
|
-
const z = this.buildZdtFromPlainDate(pd);
|
|
2749
|
-
if (Temporal.ZonedDateTime.compare(z, this.opts.until) > 0) break;
|
|
2750
|
-
}
|
|
2751
|
-
}
|
|
2752
|
-
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
2753
|
-
}
|
|
2754
|
-
return this._allFallback(iterator);
|
|
2755
|
-
}
|
|
2756
|
-
};
|
|
2757
|
-
//#endregion
|
|
1
|
+
import { i as weekdayToIsoDay, n as allowedFreq, r as allowedWeekdays, t as RRuleTemporal } from "./src-DEpYefSo.js";
|
|
2758
2
|
export { RRuleTemporal, allowedFreq, allowedWeekdays, weekdayToIsoDay };
|