rrule-temporal 0.2.6 → 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,39 +266,84 @@ 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 { byHour, byMinute } = this.opts;
288
+ const { freq, interval = 1, byHour, byMinute, bySecond } = this.opts;
289
+ if (freq === "HOURLY" && byHour && byHour.length === 1) {
290
+ return this.applyTimeOverride(zdt.add({ days: interval }));
291
+ }
292
+ if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
293
+ return this.applyTimeOverride(zdt.add({ hours: interval }));
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
+ }
309
+ if (freq === "SECONDLY" && byMinute && byMinute.length === 1) {
310
+ const next = zdt.add({ seconds: interval });
311
+ if (next.minute === byMinute[0]) return next;
312
+ return this.applyTimeOverride(zdt.add({ hours: interval })).with({ second: 0 });
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
+ }
320
+ if (byMinute && byMinute.length > 1) {
321
+ const idx = byMinute.indexOf(zdt.minute);
322
+ if (idx !== -1 && idx < byMinute.length - 1) {
323
+ return zdt.with({
324
+ minute: byMinute[idx + 1],
325
+ second: bySecond ? bySecond[0] : zdt.second
326
+ });
327
+ }
328
+ }
225
329
  if (byHour && byHour.length > 1) {
226
330
  const idx = byHour.indexOf(zdt.hour);
227
331
  if (idx !== -1 && idx < byHour.length - 1) {
228
332
  return zdt.with({
229
333
  hour: byHour[idx + 1],
230
- minute: byMinute ? byMinute[0] : zdt.minute
334
+ minute: byMinute ? byMinute[0] : zdt.minute,
335
+ second: bySecond ? bySecond[0] : zdt.second
231
336
  });
232
337
  }
233
338
  }
234
339
  return this.applyTimeOverride(this.rawAdvance(zdt));
235
340
  }
236
341
  applyTimeOverride(zdt) {
237
- const { byHour, byMinute } = this.opts;
342
+ const { byHour, byMinute, bySecond } = this.opts;
238
343
  let dt = zdt;
239
344
  if (byHour) dt = dt.with({ hour: byHour[0] });
240
345
  if (byMinute) dt = dt.with({ minute: byMinute[0] });
346
+ if (bySecond) dt = dt.with({ second: bySecond[0] });
241
347
  return dt;
242
348
  }
243
349
  computeFirst() {
@@ -267,8 +373,8 @@ var RRuleTemporal = class {
267
373
  zdt = zdt.add({ days: Math.min(...deltas) });
268
374
  }
269
375
  }
270
- const { byHour, byMinute } = this.opts;
271
- if (byHour || byMinute) {
376
+ const { byHour, byMinute, bySecond } = this.opts;
377
+ if (byHour || byMinute || bySecond) {
272
378
  zdt = this.applyTimeOverride(zdt);
273
379
  if (import_polyfill.Temporal.Instant.compare(
274
380
  zdt.toInstant(),
@@ -295,7 +401,7 @@ var RRuleTemporal = class {
295
401
  const targetDow = dayMap[tok];
296
402
  const delta = (targetDow - sample.dayOfWeek + 7) % 7;
297
403
  const sameDate = sample.add({ days: delta });
298
- return this.expandByHourMinute(sameDate);
404
+ return this.expandByTime(sameDate);
299
405
  });
300
406
  return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
301
407
  }
@@ -314,7 +420,7 @@ var RRuleTemporal = class {
314
420
  SU: 7
315
421
  };
316
422
  for (const token of byDay) {
317
- 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)$/);
318
424
  if (!m) continue;
319
425
  const ord = m[1] ? parseInt(m[1], 10) : 0;
320
426
  const weekday = m[2];
@@ -350,7 +456,28 @@ var RRuleTemporal = class {
350
456
  );
351
457
  }
352
458
  matchesAll(zdt) {
353
- 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);
354
481
  }
355
482
  options() {
356
483
  return this.opts;
@@ -380,18 +507,18 @@ var RRuleTemporal = class {
380
507
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
381
508
  break outer_month;
382
509
  }
383
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
384
- break outer_month;
385
- }
386
510
  if (iterator && !iterator(occ, matchCount2)) {
387
511
  break outer_month;
388
512
  }
389
513
  dates.push(occ);
390
514
  matchCount2++;
515
+ if (this.shouldBreakForCountLimit(matchCount2)) {
516
+ break outer_month;
517
+ }
391
518
  }
392
519
  monthCursor = monthCursor.add({ months: this.opts.interval });
393
520
  }
394
- return dates;
521
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
395
522
  }
396
523
  if (this.opts.freq === "WEEKLY") {
397
524
  const start = this.originalDtstart;
@@ -410,61 +537,65 @@ var RRuleTemporal = class {
410
537
  const delta = (dw - start.dayOfWeek + 7) % 7;
411
538
  return start.add({ days: delta });
412
539
  });
413
- let weekCursor = firstWeekDates.reduce(
540
+ let firstOccurrence = firstWeekDates.reduce(
414
541
  (a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
415
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 });
416
546
  let matchCount2 = 0;
417
547
  outer_week: while (true) {
418
- const baseDow = weekCursor.dayOfWeek;
419
548
  const occs = dows.flatMap((dw) => {
420
- const delta = dw - baseDow;
549
+ const delta = (dw - wkstDay + 7) % 7;
421
550
  const sameDate = weekCursor.add({ days: delta });
422
- return this.expandByHourMinute(sameDate);
551
+ return this.expandByTime(sameDate);
423
552
  }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
424
553
  for (const occ of occs) {
425
554
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
426
555
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
427
556
  break outer_week;
428
557
  }
429
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
430
- break outer_week;
431
- }
432
558
  if (iterator && !iterator(occ, matchCount2)) {
433
559
  break outer_week;
434
560
  }
435
561
  dates.push(occ);
436
562
  matchCount2++;
563
+ if (this.shouldBreakForCountLimit(matchCount2)) {
564
+ break outer_week;
565
+ }
437
566
  }
438
567
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
439
568
  }
440
- return dates;
569
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
441
570
  }
442
571
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
443
572
  const start = this.originalDtstart;
444
573
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
445
- let i = 0;
574
+ let yearOffset = 0;
575
+ let matchCount2 = 0;
446
576
  while (true) {
447
- const year = start.year + i * this.opts.interval;
448
- const month = months[i % months.length];
449
- let occ = start.with({ year, month });
450
- occ = this.applyTimeOverride(occ);
451
- if (i === 0 && import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) {
452
- i++;
453
- continue;
454
- }
455
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
456
- break;
457
- }
458
- if (this.opts.count !== void 0 && i >= this.opts.count) {
459
- break;
460
- }
461
- if (iterator && !iterator(occ, i)) {
462
- 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
+ }
463
595
  }
464
- dates.push(occ);
465
- i++;
596
+ yearOffset++;
466
597
  }
467
- return dates;
598
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
468
599
  }
469
600
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
470
601
  const start = this.originalDtstart;
@@ -477,18 +608,42 @@ var RRuleTemporal = class {
477
608
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
478
609
  break outer_year;
479
610
  }
480
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
611
+ if (iterator && !iterator(occ, matchCount2)) {
481
612
  break outer_year;
482
613
  }
483
- if (iterator && !iterator(occ, matchCount2)) {
614
+ dates.push(occ);
615
+ matchCount2++;
616
+ if (this.shouldBreakForCountLimit(matchCount2)) {
484
617
  break outer_year;
485
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
+ }
486
638
  dates.push(occ);
487
639
  matchCount2++;
640
+ if (this.shouldBreakForCountLimit(matchCount2)) {
641
+ break outer_year2;
642
+ }
488
643
  }
489
644
  yearCursor = yearCursor.add({ years: this.opts.interval });
490
645
  }
491
- return dates;
646
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
492
647
  }
493
648
  let current = this.computeFirst();
494
649
  let matchCount = 0;
@@ -497,18 +652,71 @@ var RRuleTemporal = class {
497
652
  break;
498
653
  }
499
654
  if (this.matchesAll(current)) {
500
- if (this.opts.count !== void 0 && matchCount >= this.opts.count) {
501
- break;
502
- }
503
655
  if (iterator && !iterator(current, matchCount)) {
504
656
  break;
505
657
  }
506
658
  dates.push(current);
507
659
  matchCount++;
660
+ if (this.shouldBreakForCountLimit(matchCount)) {
661
+ break;
662
+ }
508
663
  }
509
664
  current = this.nextCandidateSameDate(current);
510
665
  }
511
- 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;
512
720
  }
513
721
  /**
514
722
  * Returns all occurrences of the rule within a specified time window.
@@ -530,33 +738,36 @@ var RRuleTemporal = class {
530
738
  if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
531
739
  break outer;
532
740
  }
533
- 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) {
534
743
  results.push(occ);
535
744
  }
536
745
  }
537
746
  monthCursor = monthCursor.add({ months: this.opts.interval });
538
747
  }
539
- return results;
748
+ return this.mergeRDates(results, startInst, endInst, inc);
540
749
  }
541
750
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
542
751
  const start = this.originalDtstart;
543
752
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
544
- let i = 0;
753
+ let yearOffset = 0;
545
754
  outer: while (true) {
546
- const year = start.year + i * this.opts.interval;
547
- const month = months[i % months.length];
548
- let occ = start.with({ year, month });
549
- occ = this.applyTimeOverride(occ);
550
- const inst = occ.toInstant();
551
- if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
552
- break;
553
- }
554
- if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0) {
555
- 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
+ }
556
767
  }
557
- i++;
768
+ yearOffset++;
558
769
  }
559
- return results;
770
+ return this.mergeRDates(results, startInst, endInst, inc);
560
771
  }
561
772
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
562
773
  const start = this.originalDtstart;
@@ -568,13 +779,33 @@ var RRuleTemporal = class {
568
779
  if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
569
780
  break outer_year;
570
781
  }
571
- 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) {
572
784
  results.push(occ);
573
785
  }
574
786
  }
575
787
  yearCursor = yearCursor.add({ years: this.opts.interval });
576
788
  }
577
- 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);
578
809
  }
579
810
  if (this.opts.freq === "WEEKLY") {
580
811
  const startInst2 = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
@@ -588,13 +819,14 @@ var RRuleTemporal = class {
588
819
  if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst2) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst2) >= 0) {
589
820
  break outer;
590
821
  }
591
- 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) {
592
824
  results2.push(occ);
593
825
  }
594
826
  }
595
827
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
596
828
  }
597
- return results2;
829
+ return this.mergeRDates(results2, startInst2, endInst2, inc);
598
830
  }
599
831
  let current = this.computeFirst();
600
832
  while (true) {
@@ -604,12 +836,13 @@ var RRuleTemporal = class {
604
836
  } else {
605
837
  if (import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) break;
606
838
  }
607
- 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)) {
608
841
  results.push(current);
609
842
  }
610
843
  current = this.nextCandidateSameDate(current);
611
844
  }
612
- return results;
845
+ return this.mergeRDates(results, startInst, endInst, inc);
613
846
  }
614
847
  /**
615
848
  * Returns the next occurrence of the rule after a specified date.
@@ -767,10 +1000,10 @@ var RRuleTemporal = class {
767
1000
  }
768
1001
  if (!byDay && byMonthDayHits.length) {
769
1002
  const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
770
- 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));
771
1004
  }
772
1005
  if (!byDay) {
773
- return this.expandByHourMinute(sample);
1006
+ return this.expandByTime(sample);
774
1007
  }
775
1008
  const dayMap = {
776
1009
  MO: 1,
@@ -782,7 +1015,7 @@ var RRuleTemporal = class {
782
1015
  SU: 7
783
1016
  };
784
1017
  const tokens = byDay.map((tok) => {
785
- 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)$/);
786
1019
  if (!m) return null;
787
1020
  return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
788
1021
  }).filter((x) => x !== null);
@@ -810,7 +1043,9 @@ var RRuleTemporal = class {
810
1043
  finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
811
1044
  }
812
1045
  const hits = finalDays.map((d) => sample.with({ day: d }));
813
- 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;
814
1049
  }
815
1050
  /**
816
1051
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -819,11 +1054,117 @@ var RRuleTemporal = class {
819
1054
  */
820
1055
  generateYearlyOccurrences(sample) {
821
1056
  const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
822
- const occs = months.flatMap((m) => {
823
- const monthSample = sample.with({ month: m, day: 1 });
824
- return this.generateMonthlyOccurrences(monthSample);
825
- });
826
- 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;
827
1168
  }
828
1169
  };
829
1170
  // Annotate the CommonJS export names for ESM import in node: