rrule-temporal 0.2.7 → 0.3.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/dist/index.cjs CHANGED
@@ -140,6 +140,21 @@ function parseRRuleString(input, fallbackDtstart) {
140
140
  case "BYMONTHDAY":
141
141
  opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
142
142
  break;
143
+ case "BYSECOND":
144
+ opts.bySecond = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
145
+ break;
146
+ case "BYYEARDAY":
147
+ opts.byYearDay = val.split(",").map((n) => parseInt(n, 10));
148
+ break;
149
+ case "BYWEEKNO":
150
+ opts.byWeekNo = val.split(",").map((n) => parseInt(n, 10));
151
+ break;
152
+ case "BYSETPOS":
153
+ opts.bySetPos = val.split(",").map((n) => parseInt(n, 10));
154
+ break;
155
+ case "WKST":
156
+ opts.wkst = val;
157
+ break;
143
158
  }
144
159
  }
145
160
  return opts;
@@ -182,7 +197,53 @@ var RRuleTemporal = class {
182
197
  }
183
198
  if (!manual.freq) throw new Error("RRULE must include FREQ");
184
199
  manual.interval = manual.interval ?? 1;
185
- this.opts = manual;
200
+ if (manual.interval <= 0) {
201
+ throw new Error("Cannot create RRule: interval must be greater than 0");
202
+ }
203
+ if (manual.until && !(manual.until instanceof import_polyfill.Temporal.ZonedDateTime)) {
204
+ throw new Error("Manual until must be a ZonedDateTime");
205
+ }
206
+ this.opts = this.sanitizeOpts(manual);
207
+ }
208
+ sanitizeOpts(opts) {
209
+ const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
210
+ if (opts.byDay) {
211
+ opts.byDay = opts.byDay.filter((d) => validDay.test(d));
212
+ if (opts.byDay.length === 0) delete opts.byDay;
213
+ }
214
+ if (opts.byMonth) {
215
+ opts.byMonth = opts.byMonth.filter((n) => Number.isInteger(n) && n >= 1 && n <= 12);
216
+ if (opts.byMonth.length === 0) delete opts.byMonth;
217
+ }
218
+ if (opts.byMonthDay) {
219
+ opts.byMonthDay = opts.byMonthDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -31 && n <= 31);
220
+ if (opts.byMonthDay.length === 0) delete opts.byMonthDay;
221
+ }
222
+ if (opts.byYearDay) {
223
+ opts.byYearDay = opts.byYearDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -366 && n <= 366);
224
+ if (opts.byYearDay.length === 0) delete opts.byYearDay;
225
+ }
226
+ if (opts.byWeekNo) {
227
+ opts.byWeekNo = opts.byWeekNo.filter((n) => Number.isInteger(n) && n !== 0 && n >= -53 && n <= 53);
228
+ if (opts.byWeekNo.length === 0) delete opts.byWeekNo;
229
+ }
230
+ if (opts.byHour) {
231
+ opts.byHour = opts.byHour.filter((n) => Number.isInteger(n) && n >= 0 && n <= 23).sort((a, b) => a - b);
232
+ if (opts.byHour.length === 0) delete opts.byHour;
233
+ }
234
+ if (opts.byMinute) {
235
+ opts.byMinute = opts.byMinute.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
236
+ if (opts.byMinute.length === 0) delete opts.byMinute;
237
+ }
238
+ if (opts.bySecond) {
239
+ opts.bySecond = opts.bySecond.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
240
+ if (opts.bySecond.length === 0) delete opts.bySecond;
241
+ }
242
+ if (opts.bySetPos) {
243
+ opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
244
+ if (opts.bySetPos.length === 0) delete opts.bySetPos;
245
+ }
246
+ return opts;
186
247
  }
187
248
  rawAdvance(zdt) {
188
249
  const { freq, interval } = this.opts;
@@ -205,38 +266,64 @@ var RRuleTemporal = class {
205
266
  throw new Error(`Unsupported FREQ: ${freq}`);
206
267
  }
207
268
  }
208
- /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE combinations,
209
- * keeping chronological order. If BYHOUR/BYMINUTE are not present the
210
- * original date is returned unchanged.
269
+ /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE × BYSECOND
270
+ * combinations, keeping chronological order. If the options are not
271
+ * present the original date is returned unchanged.
211
272
  */
212
- expandByHourMinute(base) {
273
+ expandByTime(base) {
213
274
  const hours = this.opts.byHour ?? [base.hour];
214
275
  const minutes = this.opts.byMinute ?? [base.minute];
276
+ const seconds = this.opts.bySecond ?? [base.second];
215
277
  const out = [];
216
278
  for (const h of hours) {
217
279
  for (const m of minutes) {
218
- out.push(base.with({ hour: h, minute: m }));
280
+ for (const s of seconds) {
281
+ out.push(base.with({ hour: h, minute: m, second: s }));
282
+ }
219
283
  }
220
284
  }
221
285
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
222
286
  }
223
287
  nextCandidateSameDate(zdt) {
224
- const { freq, interval = 1, byHour, byMinute } = this.opts;
288
+ const { freq, interval = 1, byHour, byMinute, bySecond } = this.opts;
225
289
  if (freq === "HOURLY" && byHour && byHour.length === 1) {
226
290
  return this.applyTimeOverride(zdt.add({ days: interval }));
227
291
  }
228
292
  if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
229
293
  return this.applyTimeOverride(zdt.add({ hours: interval }));
230
294
  }
295
+ if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute && interval > 1) {
296
+ const next = zdt.add({ minutes: interval });
297
+ if (byHour.includes(next.hour)) {
298
+ return next;
299
+ }
300
+ const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
301
+ if (nextHour && nextHour > zdt.hour) {
302
+ return zdt.with({ hour: nextHour, minute: 0 });
303
+ }
304
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
305
+ }
306
+ if (freq === "SECONDLY" && bySecond && bySecond.length === 1) {
307
+ return this.applyTimeOverride(zdt.add({ minutes: interval })).with({ second: bySecond[0] });
308
+ }
231
309
  if (freq === "SECONDLY" && byMinute && byMinute.length === 1) {
232
310
  const next = zdt.add({ seconds: interval });
233
311
  if (next.minute === byMinute[0]) return next;
234
312
  return this.applyTimeOverride(zdt.add({ hours: interval })).with({ second: 0 });
235
313
  }
314
+ if (bySecond && bySecond.length > 1) {
315
+ const idx = bySecond.indexOf(zdt.second);
316
+ if (idx !== -1 && idx < bySecond.length - 1) {
317
+ return zdt.with({ second: bySecond[idx + 1] });
318
+ }
319
+ }
236
320
  if (byMinute && byMinute.length > 1) {
237
321
  const idx = byMinute.indexOf(zdt.minute);
238
322
  if (idx !== -1 && idx < byMinute.length - 1) {
239
- return zdt.with({ minute: byMinute[idx + 1] });
323
+ return zdt.with({
324
+ minute: byMinute[idx + 1],
325
+ second: bySecond ? bySecond[0] : zdt.second
326
+ });
240
327
  }
241
328
  }
242
329
  if (byHour && byHour.length > 1) {
@@ -244,17 +331,19 @@ var RRuleTemporal = class {
244
331
  if (idx !== -1 && idx < byHour.length - 1) {
245
332
  return zdt.with({
246
333
  hour: byHour[idx + 1],
247
- minute: byMinute ? byMinute[0] : zdt.minute
334
+ minute: byMinute ? byMinute[0] : zdt.minute,
335
+ second: bySecond ? bySecond[0] : zdt.second
248
336
  });
249
337
  }
250
338
  }
251
339
  return this.applyTimeOverride(this.rawAdvance(zdt));
252
340
  }
253
341
  applyTimeOverride(zdt) {
254
- const { byHour, byMinute } = this.opts;
342
+ const { byHour, byMinute, bySecond } = this.opts;
255
343
  let dt = zdt;
256
344
  if (byHour) dt = dt.with({ hour: byHour[0] });
257
345
  if (byMinute) dt = dt.with({ minute: byMinute[0] });
346
+ if (bySecond) dt = dt.with({ second: bySecond[0] });
258
347
  return dt;
259
348
  }
260
349
  computeFirst() {
@@ -284,8 +373,8 @@ var RRuleTemporal = class {
284
373
  zdt = zdt.add({ days: Math.min(...deltas) });
285
374
  }
286
375
  }
287
- const { byHour, byMinute } = this.opts;
288
- if (byHour || byMinute) {
376
+ const { byHour, byMinute, bySecond } = this.opts;
377
+ if (byHour || byMinute || bySecond) {
289
378
  zdt = this.applyTimeOverride(zdt);
290
379
  if (import_polyfill.Temporal.Instant.compare(
291
380
  zdt.toInstant(),
@@ -312,7 +401,7 @@ var RRuleTemporal = class {
312
401
  const targetDow = dayMap[tok];
313
402
  const delta = (targetDow - sample.dayOfWeek + 7) % 7;
314
403
  const sameDate = sample.add({ days: delta });
315
- return this.expandByHourMinute(sameDate);
404
+ return this.expandByTime(sameDate);
316
405
  });
317
406
  return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
318
407
  }
@@ -331,7 +420,7 @@ var RRuleTemporal = class {
331
420
  SU: 7
332
421
  };
333
422
  for (const token of byDay) {
334
- const m = token.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
423
+ const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
335
424
  if (!m) continue;
336
425
  const ord = m[1] ? parseInt(m[1], 10) : 0;
337
426
  const weekday = m[2];
@@ -367,7 +456,28 @@ var RRuleTemporal = class {
367
456
  );
368
457
  }
369
458
  matchesAll(zdt) {
370
- return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt);
459
+ return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt);
460
+ }
461
+ matchesByYearDay(zdt) {
462
+ const { byYearDay } = this.opts;
463
+ if (!byYearDay) return true;
464
+ const dayOfYear = zdt.dayOfYear;
465
+ const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
466
+ return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
467
+ }
468
+ matchesByWeekNo(zdt) {
469
+ const { byWeekNo, wkst } = this.opts;
470
+ if (!byWeekNo) return true;
471
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
472
+ const startDow = dayMap[wkst ?? "MO"];
473
+ const jan1 = zdt.with({ month: 1, day: 1 });
474
+ const delta = (jan1.dayOfWeek - startDow + 7) % 7;
475
+ const firstWeekStart = jan1.subtract({ days: delta });
476
+ const diffDays = zdt.toPlainDate().since(firstWeekStart.toPlainDate()).days;
477
+ const week = Math.floor(diffDays / 7) + 1;
478
+ const lastWeekDiff = zdt.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
479
+ const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
480
+ return byWeekNo.some((n) => n > 0 ? week === n : week === lastWeek + n + 1);
371
481
  }
372
482
  options() {
373
483
  return this.opts;
@@ -397,18 +507,18 @@ var RRuleTemporal = class {
397
507
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
398
508
  break outer_month;
399
509
  }
400
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
401
- break outer_month;
402
- }
403
510
  if (iterator && !iterator(occ, matchCount2)) {
404
511
  break outer_month;
405
512
  }
406
513
  dates.push(occ);
407
514
  matchCount2++;
515
+ if (this.shouldBreakForCountLimit(matchCount2)) {
516
+ break outer_month;
517
+ }
408
518
  }
409
519
  monthCursor = monthCursor.add({ months: this.opts.interval });
410
520
  }
411
- return dates;
521
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
412
522
  }
413
523
  if (this.opts.freq === "WEEKLY") {
414
524
  const start = this.originalDtstart;
@@ -427,61 +537,65 @@ var RRuleTemporal = class {
427
537
  const delta = (dw - start.dayOfWeek + 7) % 7;
428
538
  return start.add({ days: delta });
429
539
  });
430
- let weekCursor = firstWeekDates.reduce(
540
+ let firstOccurrence = firstWeekDates.reduce(
431
541
  (a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
432
542
  );
543
+ const wkstDay = dayMap[this.opts.wkst || "MO"] ?? 1;
544
+ const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
545
+ let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
433
546
  let matchCount2 = 0;
434
547
  outer_week: while (true) {
435
- const baseDow = weekCursor.dayOfWeek;
436
548
  const occs = dows.flatMap((dw) => {
437
- const delta = dw - baseDow;
549
+ const delta = (dw - wkstDay + 7) % 7;
438
550
  const sameDate = weekCursor.add({ days: delta });
439
- return this.expandByHourMinute(sameDate);
551
+ return this.expandByTime(sameDate);
440
552
  }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
441
553
  for (const occ of occs) {
442
554
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
443
555
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
444
556
  break outer_week;
445
557
  }
446
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
447
- break outer_week;
448
- }
449
558
  if (iterator && !iterator(occ, matchCount2)) {
450
559
  break outer_week;
451
560
  }
452
561
  dates.push(occ);
453
562
  matchCount2++;
563
+ if (this.shouldBreakForCountLimit(matchCount2)) {
564
+ break outer_week;
565
+ }
454
566
  }
455
567
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
456
568
  }
457
- return dates;
569
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
458
570
  }
459
571
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
460
572
  const start = this.originalDtstart;
461
573
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
462
- let i = 0;
574
+ let yearOffset = 0;
575
+ let matchCount2 = 0;
463
576
  while (true) {
464
- const year = start.year + i * this.opts.interval;
465
- const month = months[i % months.length];
466
- let occ = start.with({ year, month });
467
- occ = this.applyTimeOverride(occ);
468
- if (i === 0 && import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) {
469
- i++;
470
- continue;
471
- }
472
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
473
- break;
474
- }
475
- if (this.opts.count !== void 0 && i >= this.opts.count) {
476
- break;
477
- }
478
- if (iterator && !iterator(occ, i)) {
479
- break;
577
+ const year = start.year + yearOffset * this.opts.interval;
578
+ for (const month of months) {
579
+ let occ = start.with({ year, month });
580
+ occ = this.applyTimeOverride(occ);
581
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) {
582
+ continue;
583
+ }
584
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
585
+ return dates;
586
+ }
587
+ if (iterator && !iterator(occ, matchCount2)) {
588
+ return dates;
589
+ }
590
+ dates.push(occ);
591
+ matchCount2++;
592
+ if (this.shouldBreakForCountLimit(matchCount2)) {
593
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
594
+ }
480
595
  }
481
- dates.push(occ);
482
- i++;
596
+ yearOffset++;
483
597
  }
484
- return dates;
598
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
485
599
  }
486
600
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
487
601
  const start = this.originalDtstart;
@@ -494,18 +608,42 @@ var RRuleTemporal = class {
494
608
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
495
609
  break outer_year;
496
610
  }
497
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
611
+ if (iterator && !iterator(occ, matchCount2)) {
498
612
  break outer_year;
499
613
  }
500
- if (iterator && !iterator(occ, matchCount2)) {
614
+ dates.push(occ);
615
+ matchCount2++;
616
+ if (this.shouldBreakForCountLimit(matchCount2)) {
501
617
  break outer_year;
502
618
  }
619
+ }
620
+ yearCursor = yearCursor.add({ years: this.opts.interval });
621
+ }
622
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
623
+ }
624
+ if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
625
+ const start = this.originalDtstart;
626
+ let yearCursor = start.with({ month: 1, day: 1 });
627
+ let matchCount2 = 0;
628
+ outer_year2: while (true) {
629
+ const occs = this.generateYearlyOccurrences(yearCursor);
630
+ for (const occ of occs) {
631
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
632
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
633
+ break outer_year2;
634
+ }
635
+ if (iterator && !iterator(occ, matchCount2)) {
636
+ break outer_year2;
637
+ }
503
638
  dates.push(occ);
504
639
  matchCount2++;
640
+ if (this.shouldBreakForCountLimit(matchCount2)) {
641
+ break outer_year2;
642
+ }
505
643
  }
506
644
  yearCursor = yearCursor.add({ years: this.opts.interval });
507
645
  }
508
- return dates;
646
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
509
647
  }
510
648
  let current = this.computeFirst();
511
649
  let matchCount = 0;
@@ -514,18 +652,71 @@ var RRuleTemporal = class {
514
652
  break;
515
653
  }
516
654
  if (this.matchesAll(current)) {
517
- if (this.opts.count !== void 0 && matchCount >= this.opts.count) {
518
- break;
519
- }
520
655
  if (iterator && !iterator(current, matchCount)) {
521
656
  break;
522
657
  }
523
658
  dates.push(current);
524
659
  matchCount++;
660
+ if (this.shouldBreakForCountLimit(matchCount)) {
661
+ break;
662
+ }
525
663
  }
526
664
  current = this.nextCandidateSameDate(current);
527
665
  }
528
- return dates;
666
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
667
+ }
668
+ /**
669
+ * Converts rDate entries to ZonedDateTime and merges with existing dates.
670
+ * @param dates - Array of dates to merge with
671
+ * @returns Merged and deduplicated array of dates
672
+ */
673
+ mergeAndDeduplicateRDates(dates) {
674
+ if (!this.opts.rDate) return dates;
675
+ const extras = this.opts.rDate.map(
676
+ (d) => d instanceof import_polyfill.Temporal.ZonedDateTime ? d : import_polyfill.Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
677
+ );
678
+ dates.push(...extras);
679
+ dates.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
680
+ const dedup = [];
681
+ for (const d of dates) {
682
+ if (dedup.length === 0 || import_polyfill.Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
683
+ dedup.push(d);
684
+ }
685
+ }
686
+ return dedup;
687
+ }
688
+ /**
689
+ * Applies count limit and merges rDates with the rule-generated dates.
690
+ * @param dates - Array of dates generated by the rule
691
+ * @param iterator - Optional iterator function
692
+ * @returns Final array of dates after merging and applying count limit
693
+ */
694
+ applyCountLimitAndMergeRDates(dates, iterator) {
695
+ const merged = this.mergeAndDeduplicateRDates(dates);
696
+ if (this.opts.count !== void 0) {
697
+ let finalCount = 0;
698
+ const finalDates = [];
699
+ for (const d of merged) {
700
+ if (finalCount >= this.opts.count) break;
701
+ if (iterator && !iterator(d, finalCount)) break;
702
+ finalDates.push(d);
703
+ finalCount++;
704
+ }
705
+ return finalDates;
706
+ }
707
+ return merged;
708
+ }
709
+ /**
710
+ * Checks if the count limit should break the loop based on rDate presence.
711
+ * @param matchCount - Current number of matches
712
+ * @returns true if the loop should break
713
+ */
714
+ shouldBreakForCountLimit(matchCount) {
715
+ if (this.opts.count === void 0) return false;
716
+ if (!this.opts.rDate) {
717
+ return matchCount >= this.opts.count;
718
+ }
719
+ return matchCount >= this.opts.count * 2;
529
720
  }
530
721
  /**
531
722
  * Returns all occurrences of the rule within a specified time window.
@@ -547,33 +738,36 @@ var RRuleTemporal = class {
547
738
  if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
548
739
  break outer;
549
740
  }
550
- if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0) {
741
+ const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
742
+ if (startOk) {
551
743
  results.push(occ);
552
744
  }
553
745
  }
554
746
  monthCursor = monthCursor.add({ months: this.opts.interval });
555
747
  }
556
- return results;
748
+ return this.mergeRDates(results, startInst, endInst, inc);
557
749
  }
558
750
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
559
751
  const start = this.originalDtstart;
560
752
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
561
- let i = 0;
753
+ let yearOffset = 0;
562
754
  outer: while (true) {
563
- const year = start.year + i * this.opts.interval;
564
- const month = months[i % months.length];
565
- let occ = start.with({ year, month });
566
- occ = this.applyTimeOverride(occ);
567
- const inst = occ.toInstant();
568
- if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
569
- break;
570
- }
571
- if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0) {
572
- results.push(occ);
755
+ const year = start.year + yearOffset * this.opts.interval;
756
+ for (const month of months) {
757
+ let occ = start.with({ year, month });
758
+ occ = this.applyTimeOverride(occ);
759
+ const inst = occ.toInstant();
760
+ if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
761
+ break outer;
762
+ }
763
+ const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
764
+ if (startOk) {
765
+ results.push(occ);
766
+ }
573
767
  }
574
- i++;
768
+ yearOffset++;
575
769
  }
576
- return results;
770
+ return this.mergeRDates(results, startInst, endInst, inc);
577
771
  }
578
772
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
579
773
  const start = this.originalDtstart;
@@ -585,13 +779,33 @@ var RRuleTemporal = class {
585
779
  if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
586
780
  break outer_year;
587
781
  }
588
- if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0) {
782
+ const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
783
+ if (startOk) {
589
784
  results.push(occ);
590
785
  }
591
786
  }
592
787
  yearCursor = yearCursor.add({ years: this.opts.interval });
593
788
  }
594
- return results;
789
+ return this.mergeRDates(results, startInst, endInst, inc);
790
+ }
791
+ if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
792
+ const start = this.originalDtstart;
793
+ let yearCursor = start.with({ month: 1, day: 1 });
794
+ outer_year2: while (true) {
795
+ const occs = this.generateYearlyOccurrences(yearCursor);
796
+ for (const occ of occs) {
797
+ const inst = occ.toInstant();
798
+ if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
799
+ break outer_year2;
800
+ }
801
+ const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
802
+ if (startOk) {
803
+ results.push(occ);
804
+ }
805
+ }
806
+ yearCursor = yearCursor.add({ years: this.opts.interval });
807
+ }
808
+ return this.mergeRDates(results, startInst, endInst, inc);
595
809
  }
596
810
  if (this.opts.freq === "WEEKLY") {
597
811
  const startInst2 = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
@@ -605,13 +819,14 @@ var RRuleTemporal = class {
605
819
  if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst2) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst2) >= 0) {
606
820
  break outer;
607
821
  }
608
- if (import_polyfill.Temporal.Instant.compare(inst, startInst2) >= 0) {
822
+ const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst2) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst2) > 0;
823
+ if (startOk) {
609
824
  results2.push(occ);
610
825
  }
611
826
  }
612
827
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
613
828
  }
614
- return results2;
829
+ return this.mergeRDates(results2, startInst2, endInst2, inc);
615
830
  }
616
831
  let current = this.computeFirst();
617
832
  while (true) {
@@ -621,12 +836,13 @@ var RRuleTemporal = class {
621
836
  } else {
622
837
  if (import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) break;
623
838
  }
624
- if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 && this.matchesAll(current)) {
839
+ const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
840
+ if (startOk && this.matchesAll(current)) {
625
841
  results.push(current);
626
842
  }
627
843
  current = this.nextCandidateSameDate(current);
628
844
  }
629
- return results;
845
+ return this.mergeRDates(results, startInst, endInst, inc);
630
846
  }
631
847
  /**
632
848
  * Returns the next occurrence of the rule after a specified date.
@@ -784,10 +1000,10 @@ var RRuleTemporal = class {
784
1000
  }
785
1001
  if (!byDay && byMonthDayHits.length) {
786
1002
  const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
787
- return dates.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1003
+ return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
788
1004
  }
789
1005
  if (!byDay) {
790
- return this.expandByHourMinute(sample);
1006
+ return this.expandByTime(sample);
791
1007
  }
792
1008
  const dayMap = {
793
1009
  MO: 1,
@@ -799,7 +1015,7 @@ var RRuleTemporal = class {
799
1015
  SU: 7
800
1016
  };
801
1017
  const tokens = byDay.map((tok) => {
802
- const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
1018
+ const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
803
1019
  if (!m) return null;
804
1020
  return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
805
1021
  }).filter((x) => x !== null);
@@ -827,7 +1043,9 @@ var RRuleTemporal = class {
827
1043
  finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
828
1044
  }
829
1045
  const hits = finalDays.map((d) => sample.with({ day: d }));
830
- return hits.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1046
+ let expanded = hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1047
+ expanded = this.applyBySetPos(expanded);
1048
+ return expanded;
831
1049
  }
832
1050
  /**
833
1051
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -836,11 +1054,117 @@ var RRuleTemporal = class {
836
1054
  */
837
1055
  generateYearlyOccurrences(sample) {
838
1056
  const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
839
- const occs = months.flatMap((m) => {
840
- const monthSample = sample.with({ month: m, day: 1 });
841
- return this.generateMonthlyOccurrences(monthSample);
842
- });
843
- return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1057
+ let occs = [];
1058
+ if (this.opts.byDay && this.opts.byDay.some((t) => /\d{2}/.test(t))) {
1059
+ const dayMap = {
1060
+ MO: 1,
1061
+ TU: 2,
1062
+ WE: 3,
1063
+ TH: 4,
1064
+ FR: 5,
1065
+ SA: 6,
1066
+ SU: 7
1067
+ };
1068
+ for (const tok of this.opts.byDay) {
1069
+ const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
1070
+ if (!m) continue;
1071
+ const ord = parseInt(m[1], 10);
1072
+ const wd = dayMap[m[2]];
1073
+ let dt;
1074
+ if (ord > 0) {
1075
+ const jan1 = sample.with({ month: 1, day: 1 });
1076
+ const delta = (wd - jan1.dayOfWeek + 7) % 7;
1077
+ dt = jan1.add({ days: delta + 7 * (ord - 1) });
1078
+ } else {
1079
+ const dec31 = sample.with({ month: 12, day: 31 });
1080
+ const delta = (dec31.dayOfWeek - wd + 7) % 7;
1081
+ dt = dec31.subtract({ days: delta + 7 * (-ord - 1) });
1082
+ }
1083
+ if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1084
+ occs.push(...this.expandByTime(dt));
1085
+ }
1086
+ }
1087
+ } else if (!this.opts.byYearDay && !this.opts.byWeekNo) {
1088
+ occs = months.flatMap((m) => {
1089
+ const monthSample = sample.with({ month: m, day: 1 });
1090
+ return this.generateMonthlyOccurrences(monthSample);
1091
+ });
1092
+ }
1093
+ if (this.opts.byYearDay) {
1094
+ const last = sample.with({ month: 12, day: 31 }).dayOfYear;
1095
+ for (const d of this.opts.byYearDay) {
1096
+ const dayNum = d > 0 ? d : last + d + 1;
1097
+ const dt = sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1098
+ if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1099
+ occs.push(...this.expandByTime(dt));
1100
+ }
1101
+ }
1102
+ }
1103
+ if (this.opts.byWeekNo) {
1104
+ const dayMap = {
1105
+ MO: 1,
1106
+ TU: 2,
1107
+ WE: 3,
1108
+ TH: 4,
1109
+ FR: 5,
1110
+ SA: 6,
1111
+ SU: 7
1112
+ };
1113
+ const wkst = dayMap[this.opts.wkst || "MO"];
1114
+ const jan1 = sample.with({ month: 1, day: 1 });
1115
+ const delta = (jan1.dayOfWeek - wkst + 7) % 7;
1116
+ const firstWeekStart = jan1.subtract({ days: delta });
1117
+ const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
1118
+ const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
1119
+ const tokens = this.opts.byDay?.length ? this.opts.byDay.map((tok) => tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)?.[1]) : [this.opts.wkst || "MO"];
1120
+ for (const weekNo of this.opts.byWeekNo) {
1121
+ const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1122
+ const weekStart = firstWeekStart.add({ weeks: weekIndex });
1123
+ for (const tok of tokens) {
1124
+ if (!tok) continue;
1125
+ const targetDow = dayMap[tok];
1126
+ const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
1127
+ if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
1128
+ occs.push(...this.expandByTime(inst));
1129
+ }
1130
+ }
1131
+ }
1132
+ }
1133
+ occs = occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1134
+ occs = this.applyBySetPos(occs);
1135
+ return occs;
1136
+ }
1137
+ applyBySetPos(list2) {
1138
+ const { bySetPos } = this.opts;
1139
+ if (!bySetPos || !bySetPos.length) return list2;
1140
+ const sorted = [...list2].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1141
+ const out = [];
1142
+ const len = sorted.length;
1143
+ for (const pos of bySetPos) {
1144
+ const idx = pos > 0 ? pos - 1 : len + pos;
1145
+ if (idx >= 0 && idx < len) out.push(sorted[idx]);
1146
+ }
1147
+ return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1148
+ }
1149
+ mergeRDates(list2, startInst, endInst, inc) {
1150
+ if (!this.opts.rDate) return list2;
1151
+ const extras = this.opts.rDate.map(
1152
+ (d) => d instanceof import_polyfill.Temporal.ZonedDateTime ? d : import_polyfill.Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
1153
+ );
1154
+ for (const z of extras) {
1155
+ const inst = z.toInstant();
1156
+ const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
1157
+ const endOk = inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) <= 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) < 0;
1158
+ if (startOk && endOk) list2.push(z);
1159
+ }
1160
+ list2.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1161
+ const dedup = [];
1162
+ for (const d of list2) {
1163
+ if (dedup.length === 0 || import_polyfill.Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
1164
+ dedup.push(d);
1165
+ }
1166
+ }
1167
+ return dedup;
844
1168
  }
845
1169
  };
846
1170
  // Annotate the CommonJS export names for ESM import in node: