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.js CHANGED
@@ -116,6 +116,21 @@ function parseRRuleString(input, fallbackDtstart) {
116
116
  case "BYMONTHDAY":
117
117
  opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
118
118
  break;
119
+ case "BYSECOND":
120
+ opts.bySecond = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
121
+ break;
122
+ case "BYYEARDAY":
123
+ opts.byYearDay = val.split(",").map((n) => parseInt(n, 10));
124
+ break;
125
+ case "BYWEEKNO":
126
+ opts.byWeekNo = val.split(",").map((n) => parseInt(n, 10));
127
+ break;
128
+ case "BYSETPOS":
129
+ opts.bySetPos = val.split(",").map((n) => parseInt(n, 10));
130
+ break;
131
+ case "WKST":
132
+ opts.wkst = val;
133
+ break;
119
134
  }
120
135
  }
121
136
  return opts;
@@ -158,7 +173,53 @@ var RRuleTemporal = class {
158
173
  }
159
174
  if (!manual.freq) throw new Error("RRULE must include FREQ");
160
175
  manual.interval = manual.interval ?? 1;
161
- this.opts = manual;
176
+ if (manual.interval <= 0) {
177
+ throw new Error("Cannot create RRule: interval must be greater than 0");
178
+ }
179
+ if (manual.until && !(manual.until instanceof Temporal.ZonedDateTime)) {
180
+ throw new Error("Manual until must be a ZonedDateTime");
181
+ }
182
+ this.opts = this.sanitizeOpts(manual);
183
+ }
184
+ sanitizeOpts(opts) {
185
+ const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
186
+ if (opts.byDay) {
187
+ opts.byDay = opts.byDay.filter((d) => validDay.test(d));
188
+ if (opts.byDay.length === 0) delete opts.byDay;
189
+ }
190
+ if (opts.byMonth) {
191
+ opts.byMonth = opts.byMonth.filter((n) => Number.isInteger(n) && n >= 1 && n <= 12);
192
+ if (opts.byMonth.length === 0) delete opts.byMonth;
193
+ }
194
+ if (opts.byMonthDay) {
195
+ opts.byMonthDay = opts.byMonthDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -31 && n <= 31);
196
+ if (opts.byMonthDay.length === 0) delete opts.byMonthDay;
197
+ }
198
+ if (opts.byYearDay) {
199
+ opts.byYearDay = opts.byYearDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -366 && n <= 366);
200
+ if (opts.byYearDay.length === 0) delete opts.byYearDay;
201
+ }
202
+ if (opts.byWeekNo) {
203
+ opts.byWeekNo = opts.byWeekNo.filter((n) => Number.isInteger(n) && n !== 0 && n >= -53 && n <= 53);
204
+ if (opts.byWeekNo.length === 0) delete opts.byWeekNo;
205
+ }
206
+ if (opts.byHour) {
207
+ opts.byHour = opts.byHour.filter((n) => Number.isInteger(n) && n >= 0 && n <= 23).sort((a, b) => a - b);
208
+ if (opts.byHour.length === 0) delete opts.byHour;
209
+ }
210
+ if (opts.byMinute) {
211
+ opts.byMinute = opts.byMinute.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
212
+ if (opts.byMinute.length === 0) delete opts.byMinute;
213
+ }
214
+ if (opts.bySecond) {
215
+ opts.bySecond = opts.bySecond.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
216
+ if (opts.bySecond.length === 0) delete opts.bySecond;
217
+ }
218
+ if (opts.bySetPos) {
219
+ opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
220
+ if (opts.bySetPos.length === 0) delete opts.bySetPos;
221
+ }
222
+ return opts;
162
223
  }
163
224
  rawAdvance(zdt) {
164
225
  const { freq, interval } = this.opts;
@@ -181,38 +242,64 @@ var RRuleTemporal = class {
181
242
  throw new Error(`Unsupported FREQ: ${freq}`);
182
243
  }
183
244
  }
184
- /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE combinations,
185
- * keeping chronological order. If BYHOUR/BYMINUTE are not present the
186
- * original date is returned unchanged.
245
+ /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE × BYSECOND
246
+ * combinations, keeping chronological order. If the options are not
247
+ * present the original date is returned unchanged.
187
248
  */
188
- expandByHourMinute(base) {
249
+ expandByTime(base) {
189
250
  const hours = this.opts.byHour ?? [base.hour];
190
251
  const minutes = this.opts.byMinute ?? [base.minute];
252
+ const seconds = this.opts.bySecond ?? [base.second];
191
253
  const out = [];
192
254
  for (const h of hours) {
193
255
  for (const m of minutes) {
194
- out.push(base.with({ hour: h, minute: m }));
256
+ for (const s of seconds) {
257
+ out.push(base.with({ hour: h, minute: m, second: s }));
258
+ }
195
259
  }
196
260
  }
197
261
  return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
198
262
  }
199
263
  nextCandidateSameDate(zdt) {
200
- const { freq, interval = 1, byHour, byMinute } = this.opts;
264
+ const { freq, interval = 1, byHour, byMinute, bySecond } = this.opts;
201
265
  if (freq === "HOURLY" && byHour && byHour.length === 1) {
202
266
  return this.applyTimeOverride(zdt.add({ days: interval }));
203
267
  }
204
268
  if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
205
269
  return this.applyTimeOverride(zdt.add({ hours: interval }));
206
270
  }
271
+ if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute && interval > 1) {
272
+ const next = zdt.add({ minutes: interval });
273
+ if (byHour.includes(next.hour)) {
274
+ return next;
275
+ }
276
+ const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
277
+ if (nextHour && nextHour > zdt.hour) {
278
+ return zdt.with({ hour: nextHour, minute: 0 });
279
+ }
280
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
281
+ }
282
+ if (freq === "SECONDLY" && bySecond && bySecond.length === 1) {
283
+ return this.applyTimeOverride(zdt.add({ minutes: interval })).with({ second: bySecond[0] });
284
+ }
207
285
  if (freq === "SECONDLY" && byMinute && byMinute.length === 1) {
208
286
  const next = zdt.add({ seconds: interval });
209
287
  if (next.minute === byMinute[0]) return next;
210
288
  return this.applyTimeOverride(zdt.add({ hours: interval })).with({ second: 0 });
211
289
  }
290
+ if (bySecond && bySecond.length > 1) {
291
+ const idx = bySecond.indexOf(zdt.second);
292
+ if (idx !== -1 && idx < bySecond.length - 1) {
293
+ return zdt.with({ second: bySecond[idx + 1] });
294
+ }
295
+ }
212
296
  if (byMinute && byMinute.length > 1) {
213
297
  const idx = byMinute.indexOf(zdt.minute);
214
298
  if (idx !== -1 && idx < byMinute.length - 1) {
215
- return zdt.with({ minute: byMinute[idx + 1] });
299
+ return zdt.with({
300
+ minute: byMinute[idx + 1],
301
+ second: bySecond ? bySecond[0] : zdt.second
302
+ });
216
303
  }
217
304
  }
218
305
  if (byHour && byHour.length > 1) {
@@ -220,17 +307,19 @@ var RRuleTemporal = class {
220
307
  if (idx !== -1 && idx < byHour.length - 1) {
221
308
  return zdt.with({
222
309
  hour: byHour[idx + 1],
223
- minute: byMinute ? byMinute[0] : zdt.minute
310
+ minute: byMinute ? byMinute[0] : zdt.minute,
311
+ second: bySecond ? bySecond[0] : zdt.second
224
312
  });
225
313
  }
226
314
  }
227
315
  return this.applyTimeOverride(this.rawAdvance(zdt));
228
316
  }
229
317
  applyTimeOverride(zdt) {
230
- const { byHour, byMinute } = this.opts;
318
+ const { byHour, byMinute, bySecond } = this.opts;
231
319
  let dt = zdt;
232
320
  if (byHour) dt = dt.with({ hour: byHour[0] });
233
321
  if (byMinute) dt = dt.with({ minute: byMinute[0] });
322
+ if (bySecond) dt = dt.with({ second: bySecond[0] });
234
323
  return dt;
235
324
  }
236
325
  computeFirst() {
@@ -260,8 +349,8 @@ var RRuleTemporal = class {
260
349
  zdt = zdt.add({ days: Math.min(...deltas) });
261
350
  }
262
351
  }
263
- const { byHour, byMinute } = this.opts;
264
- if (byHour || byMinute) {
352
+ const { byHour, byMinute, bySecond } = this.opts;
353
+ if (byHour || byMinute || bySecond) {
265
354
  zdt = this.applyTimeOverride(zdt);
266
355
  if (Temporal.Instant.compare(
267
356
  zdt.toInstant(),
@@ -288,7 +377,7 @@ var RRuleTemporal = class {
288
377
  const targetDow = dayMap[tok];
289
378
  const delta = (targetDow - sample.dayOfWeek + 7) % 7;
290
379
  const sameDate = sample.add({ days: delta });
291
- return this.expandByHourMinute(sameDate);
380
+ return this.expandByTime(sameDate);
292
381
  });
293
382
  return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
294
383
  }
@@ -307,7 +396,7 @@ var RRuleTemporal = class {
307
396
  SU: 7
308
397
  };
309
398
  for (const token of byDay) {
310
- const m = token.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
399
+ const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
311
400
  if (!m) continue;
312
401
  const ord = m[1] ? parseInt(m[1], 10) : 0;
313
402
  const weekday = m[2];
@@ -343,7 +432,28 @@ var RRuleTemporal = class {
343
432
  );
344
433
  }
345
434
  matchesAll(zdt) {
346
- return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt);
435
+ return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt);
436
+ }
437
+ matchesByYearDay(zdt) {
438
+ const { byYearDay } = this.opts;
439
+ if (!byYearDay) return true;
440
+ const dayOfYear = zdt.dayOfYear;
441
+ const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
442
+ return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
443
+ }
444
+ matchesByWeekNo(zdt) {
445
+ const { byWeekNo, wkst } = this.opts;
446
+ if (!byWeekNo) return true;
447
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
448
+ const startDow = dayMap[wkst ?? "MO"];
449
+ const jan1 = zdt.with({ month: 1, day: 1 });
450
+ const delta = (jan1.dayOfWeek - startDow + 7) % 7;
451
+ const firstWeekStart = jan1.subtract({ days: delta });
452
+ const diffDays = zdt.toPlainDate().since(firstWeekStart.toPlainDate()).days;
453
+ const week = Math.floor(diffDays / 7) + 1;
454
+ const lastWeekDiff = zdt.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
455
+ const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
456
+ return byWeekNo.some((n) => n > 0 ? week === n : week === lastWeek + n + 1);
347
457
  }
348
458
  options() {
349
459
  return this.opts;
@@ -373,18 +483,18 @@ var RRuleTemporal = class {
373
483
  if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
374
484
  break outer_month;
375
485
  }
376
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
377
- break outer_month;
378
- }
379
486
  if (iterator && !iterator(occ, matchCount2)) {
380
487
  break outer_month;
381
488
  }
382
489
  dates.push(occ);
383
490
  matchCount2++;
491
+ if (this.shouldBreakForCountLimit(matchCount2)) {
492
+ break outer_month;
493
+ }
384
494
  }
385
495
  monthCursor = monthCursor.add({ months: this.opts.interval });
386
496
  }
387
- return dates;
497
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
388
498
  }
389
499
  if (this.opts.freq === "WEEKLY") {
390
500
  const start = this.originalDtstart;
@@ -403,61 +513,65 @@ var RRuleTemporal = class {
403
513
  const delta = (dw - start.dayOfWeek + 7) % 7;
404
514
  return start.add({ days: delta });
405
515
  });
406
- let weekCursor = firstWeekDates.reduce(
516
+ let firstOccurrence = firstWeekDates.reduce(
407
517
  (a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
408
518
  );
519
+ const wkstDay = dayMap[this.opts.wkst || "MO"] ?? 1;
520
+ const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
521
+ let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
409
522
  let matchCount2 = 0;
410
523
  outer_week: while (true) {
411
- const baseDow = weekCursor.dayOfWeek;
412
524
  const occs = dows.flatMap((dw) => {
413
- const delta = dw - baseDow;
525
+ const delta = (dw - wkstDay + 7) % 7;
414
526
  const sameDate = weekCursor.add({ days: delta });
415
- return this.expandByHourMinute(sameDate);
527
+ return this.expandByTime(sameDate);
416
528
  }).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
417
529
  for (const occ of occs) {
418
530
  if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
419
531
  if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
420
532
  break outer_week;
421
533
  }
422
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
423
- break outer_week;
424
- }
425
534
  if (iterator && !iterator(occ, matchCount2)) {
426
535
  break outer_week;
427
536
  }
428
537
  dates.push(occ);
429
538
  matchCount2++;
539
+ if (this.shouldBreakForCountLimit(matchCount2)) {
540
+ break outer_week;
541
+ }
430
542
  }
431
543
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
432
544
  }
433
- return dates;
545
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
434
546
  }
435
547
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
436
548
  const start = this.originalDtstart;
437
549
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
438
- let i = 0;
550
+ let yearOffset = 0;
551
+ let matchCount2 = 0;
439
552
  while (true) {
440
- const year = start.year + i * this.opts.interval;
441
- const month = months[i % months.length];
442
- let occ = start.with({ year, month });
443
- occ = this.applyTimeOverride(occ);
444
- if (i === 0 && Temporal.ZonedDateTime.compare(occ, start) < 0) {
445
- i++;
446
- continue;
447
- }
448
- if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
449
- break;
450
- }
451
- if (this.opts.count !== void 0 && i >= this.opts.count) {
452
- break;
453
- }
454
- if (iterator && !iterator(occ, i)) {
455
- break;
553
+ const year = start.year + yearOffset * this.opts.interval;
554
+ for (const month of months) {
555
+ let occ = start.with({ year, month });
556
+ occ = this.applyTimeOverride(occ);
557
+ if (Temporal.ZonedDateTime.compare(occ, start) < 0) {
558
+ continue;
559
+ }
560
+ if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
561
+ return dates;
562
+ }
563
+ if (iterator && !iterator(occ, matchCount2)) {
564
+ return dates;
565
+ }
566
+ dates.push(occ);
567
+ matchCount2++;
568
+ if (this.shouldBreakForCountLimit(matchCount2)) {
569
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
570
+ }
456
571
  }
457
- dates.push(occ);
458
- i++;
572
+ yearOffset++;
459
573
  }
460
- return dates;
574
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
461
575
  }
462
576
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
463
577
  const start = this.originalDtstart;
@@ -470,18 +584,42 @@ var RRuleTemporal = class {
470
584
  if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
471
585
  break outer_year;
472
586
  }
473
- if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
587
+ if (iterator && !iterator(occ, matchCount2)) {
474
588
  break outer_year;
475
589
  }
476
- if (iterator && !iterator(occ, matchCount2)) {
590
+ dates.push(occ);
591
+ matchCount2++;
592
+ if (this.shouldBreakForCountLimit(matchCount2)) {
477
593
  break outer_year;
478
594
  }
595
+ }
596
+ yearCursor = yearCursor.add({ years: this.opts.interval });
597
+ }
598
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
599
+ }
600
+ if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
601
+ const start = this.originalDtstart;
602
+ let yearCursor = start.with({ month: 1, day: 1 });
603
+ let matchCount2 = 0;
604
+ outer_year2: while (true) {
605
+ const occs = this.generateYearlyOccurrences(yearCursor);
606
+ for (const occ of occs) {
607
+ if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
608
+ if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
609
+ break outer_year2;
610
+ }
611
+ if (iterator && !iterator(occ, matchCount2)) {
612
+ break outer_year2;
613
+ }
479
614
  dates.push(occ);
480
615
  matchCount2++;
616
+ if (this.shouldBreakForCountLimit(matchCount2)) {
617
+ break outer_year2;
618
+ }
481
619
  }
482
620
  yearCursor = yearCursor.add({ years: this.opts.interval });
483
621
  }
484
- return dates;
622
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
485
623
  }
486
624
  let current = this.computeFirst();
487
625
  let matchCount = 0;
@@ -490,18 +628,71 @@ var RRuleTemporal = class {
490
628
  break;
491
629
  }
492
630
  if (this.matchesAll(current)) {
493
- if (this.opts.count !== void 0 && matchCount >= this.opts.count) {
494
- break;
495
- }
496
631
  if (iterator && !iterator(current, matchCount)) {
497
632
  break;
498
633
  }
499
634
  dates.push(current);
500
635
  matchCount++;
636
+ if (this.shouldBreakForCountLimit(matchCount)) {
637
+ break;
638
+ }
501
639
  }
502
640
  current = this.nextCandidateSameDate(current);
503
641
  }
504
- return dates;
642
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
643
+ }
644
+ /**
645
+ * Converts rDate entries to ZonedDateTime and merges with existing dates.
646
+ * @param dates - Array of dates to merge with
647
+ * @returns Merged and deduplicated array of dates
648
+ */
649
+ mergeAndDeduplicateRDates(dates) {
650
+ if (!this.opts.rDate) return dates;
651
+ const extras = this.opts.rDate.map(
652
+ (d) => d instanceof Temporal.ZonedDateTime ? d : Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
653
+ );
654
+ dates.push(...extras);
655
+ dates.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
656
+ const dedup = [];
657
+ for (const d of dates) {
658
+ if (dedup.length === 0 || Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
659
+ dedup.push(d);
660
+ }
661
+ }
662
+ return dedup;
663
+ }
664
+ /**
665
+ * Applies count limit and merges rDates with the rule-generated dates.
666
+ * @param dates - Array of dates generated by the rule
667
+ * @param iterator - Optional iterator function
668
+ * @returns Final array of dates after merging and applying count limit
669
+ */
670
+ applyCountLimitAndMergeRDates(dates, iterator) {
671
+ const merged = this.mergeAndDeduplicateRDates(dates);
672
+ if (this.opts.count !== void 0) {
673
+ let finalCount = 0;
674
+ const finalDates = [];
675
+ for (const d of merged) {
676
+ if (finalCount >= this.opts.count) break;
677
+ if (iterator && !iterator(d, finalCount)) break;
678
+ finalDates.push(d);
679
+ finalCount++;
680
+ }
681
+ return finalDates;
682
+ }
683
+ return merged;
684
+ }
685
+ /**
686
+ * Checks if the count limit should break the loop based on rDate presence.
687
+ * @param matchCount - Current number of matches
688
+ * @returns true if the loop should break
689
+ */
690
+ shouldBreakForCountLimit(matchCount) {
691
+ if (this.opts.count === void 0) return false;
692
+ if (!this.opts.rDate) {
693
+ return matchCount >= this.opts.count;
694
+ }
695
+ return matchCount >= this.opts.count * 2;
505
696
  }
506
697
  /**
507
698
  * Returns all occurrences of the rule within a specified time window.
@@ -523,33 +714,36 @@ var RRuleTemporal = class {
523
714
  if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
524
715
  break outer;
525
716
  }
526
- if (Temporal.Instant.compare(inst, startInst) >= 0) {
717
+ const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
718
+ if (startOk) {
527
719
  results.push(occ);
528
720
  }
529
721
  }
530
722
  monthCursor = monthCursor.add({ months: this.opts.interval });
531
723
  }
532
- return results;
724
+ return this.mergeRDates(results, startInst, endInst, inc);
533
725
  }
534
726
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
535
727
  const start = this.originalDtstart;
536
728
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
537
- let i = 0;
729
+ let yearOffset = 0;
538
730
  outer: while (true) {
539
- const year = start.year + i * this.opts.interval;
540
- const month = months[i % months.length];
541
- let occ = start.with({ year, month });
542
- occ = this.applyTimeOverride(occ);
543
- const inst = occ.toInstant();
544
- if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
545
- break;
546
- }
547
- if (Temporal.Instant.compare(inst, startInst) >= 0) {
548
- results.push(occ);
731
+ const year = start.year + yearOffset * this.opts.interval;
732
+ for (const month of months) {
733
+ let occ = start.with({ year, month });
734
+ occ = this.applyTimeOverride(occ);
735
+ const inst = occ.toInstant();
736
+ if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
737
+ break outer;
738
+ }
739
+ const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
740
+ if (startOk) {
741
+ results.push(occ);
742
+ }
549
743
  }
550
- i++;
744
+ yearOffset++;
551
745
  }
552
- return results;
746
+ return this.mergeRDates(results, startInst, endInst, inc);
553
747
  }
554
748
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
555
749
  const start = this.originalDtstart;
@@ -561,13 +755,33 @@ var RRuleTemporal = class {
561
755
  if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
562
756
  break outer_year;
563
757
  }
564
- if (Temporal.Instant.compare(inst, startInst) >= 0) {
758
+ const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
759
+ if (startOk) {
565
760
  results.push(occ);
566
761
  }
567
762
  }
568
763
  yearCursor = yearCursor.add({ years: this.opts.interval });
569
764
  }
570
- return results;
765
+ return this.mergeRDates(results, startInst, endInst, inc);
766
+ }
767
+ if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
768
+ const start = this.originalDtstart;
769
+ let yearCursor = start.with({ month: 1, day: 1 });
770
+ outer_year2: while (true) {
771
+ const occs = this.generateYearlyOccurrences(yearCursor);
772
+ for (const occ of occs) {
773
+ const inst = occ.toInstant();
774
+ if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
775
+ break outer_year2;
776
+ }
777
+ const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
778
+ if (startOk) {
779
+ results.push(occ);
780
+ }
781
+ }
782
+ yearCursor = yearCursor.add({ years: this.opts.interval });
783
+ }
784
+ return this.mergeRDates(results, startInst, endInst, inc);
571
785
  }
572
786
  if (this.opts.freq === "WEEKLY") {
573
787
  const startInst2 = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
@@ -581,13 +795,14 @@ var RRuleTemporal = class {
581
795
  if (inc ? Temporal.Instant.compare(inst, endInst2) > 0 : Temporal.Instant.compare(inst, endInst2) >= 0) {
582
796
  break outer;
583
797
  }
584
- if (Temporal.Instant.compare(inst, startInst2) >= 0) {
798
+ const startOk = inc ? Temporal.Instant.compare(inst, startInst2) >= 0 : Temporal.Instant.compare(inst, startInst2) > 0;
799
+ if (startOk) {
585
800
  results2.push(occ);
586
801
  }
587
802
  }
588
803
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
589
804
  }
590
- return results2;
805
+ return this.mergeRDates(results2, startInst2, endInst2, inc);
591
806
  }
592
807
  let current = this.computeFirst();
593
808
  while (true) {
@@ -597,12 +812,13 @@ var RRuleTemporal = class {
597
812
  } else {
598
813
  if (Temporal.Instant.compare(inst, endInst) >= 0) break;
599
814
  }
600
- if (Temporal.Instant.compare(inst, startInst) >= 0 && this.matchesAll(current)) {
815
+ const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
816
+ if (startOk && this.matchesAll(current)) {
601
817
  results.push(current);
602
818
  }
603
819
  current = this.nextCandidateSameDate(current);
604
820
  }
605
- return results;
821
+ return this.mergeRDates(results, startInst, endInst, inc);
606
822
  }
607
823
  /**
608
824
  * Returns the next occurrence of the rule after a specified date.
@@ -760,10 +976,10 @@ var RRuleTemporal = class {
760
976
  }
761
977
  if (!byDay && byMonthDayHits.length) {
762
978
  const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
763
- return dates.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
979
+ return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
764
980
  }
765
981
  if (!byDay) {
766
- return this.expandByHourMinute(sample);
982
+ return this.expandByTime(sample);
767
983
  }
768
984
  const dayMap = {
769
985
  MO: 1,
@@ -775,7 +991,7 @@ var RRuleTemporal = class {
775
991
  SU: 7
776
992
  };
777
993
  const tokens = byDay.map((tok) => {
778
- const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
994
+ const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
779
995
  if (!m) return null;
780
996
  return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
781
997
  }).filter((x) => x !== null);
@@ -803,7 +1019,9 @@ var RRuleTemporal = class {
803
1019
  finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
804
1020
  }
805
1021
  const hits = finalDays.map((d) => sample.with({ day: d }));
806
- return hits.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
1022
+ let expanded = hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
1023
+ expanded = this.applyBySetPos(expanded);
1024
+ return expanded;
807
1025
  }
808
1026
  /**
809
1027
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -812,11 +1030,117 @@ var RRuleTemporal = class {
812
1030
  */
813
1031
  generateYearlyOccurrences(sample) {
814
1032
  const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
815
- const occs = months.flatMap((m) => {
816
- const monthSample = sample.with({ month: m, day: 1 });
817
- return this.generateMonthlyOccurrences(monthSample);
818
- });
819
- return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
1033
+ let occs = [];
1034
+ if (this.opts.byDay && this.opts.byDay.some((t) => /\d{2}/.test(t))) {
1035
+ const dayMap = {
1036
+ MO: 1,
1037
+ TU: 2,
1038
+ WE: 3,
1039
+ TH: 4,
1040
+ FR: 5,
1041
+ SA: 6,
1042
+ SU: 7
1043
+ };
1044
+ for (const tok of this.opts.byDay) {
1045
+ const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
1046
+ if (!m) continue;
1047
+ const ord = parseInt(m[1], 10);
1048
+ const wd = dayMap[m[2]];
1049
+ let dt;
1050
+ if (ord > 0) {
1051
+ const jan1 = sample.with({ month: 1, day: 1 });
1052
+ const delta = (wd - jan1.dayOfWeek + 7) % 7;
1053
+ dt = jan1.add({ days: delta + 7 * (ord - 1) });
1054
+ } else {
1055
+ const dec31 = sample.with({ month: 12, day: 31 });
1056
+ const delta = (dec31.dayOfWeek - wd + 7) % 7;
1057
+ dt = dec31.subtract({ days: delta + 7 * (-ord - 1) });
1058
+ }
1059
+ if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1060
+ occs.push(...this.expandByTime(dt));
1061
+ }
1062
+ }
1063
+ } else if (!this.opts.byYearDay && !this.opts.byWeekNo) {
1064
+ occs = months.flatMap((m) => {
1065
+ const monthSample = sample.with({ month: m, day: 1 });
1066
+ return this.generateMonthlyOccurrences(monthSample);
1067
+ });
1068
+ }
1069
+ if (this.opts.byYearDay) {
1070
+ const last = sample.with({ month: 12, day: 31 }).dayOfYear;
1071
+ for (const d of this.opts.byYearDay) {
1072
+ const dayNum = d > 0 ? d : last + d + 1;
1073
+ const dt = sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1074
+ if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1075
+ occs.push(...this.expandByTime(dt));
1076
+ }
1077
+ }
1078
+ }
1079
+ if (this.opts.byWeekNo) {
1080
+ const dayMap = {
1081
+ MO: 1,
1082
+ TU: 2,
1083
+ WE: 3,
1084
+ TH: 4,
1085
+ FR: 5,
1086
+ SA: 6,
1087
+ SU: 7
1088
+ };
1089
+ const wkst = dayMap[this.opts.wkst || "MO"];
1090
+ const jan1 = sample.with({ month: 1, day: 1 });
1091
+ const delta = (jan1.dayOfWeek - wkst + 7) % 7;
1092
+ const firstWeekStart = jan1.subtract({ days: delta });
1093
+ const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
1094
+ const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
1095
+ 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"];
1096
+ for (const weekNo of this.opts.byWeekNo) {
1097
+ const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1098
+ const weekStart = firstWeekStart.add({ weeks: weekIndex });
1099
+ for (const tok of tokens) {
1100
+ if (!tok) continue;
1101
+ const targetDow = dayMap[tok];
1102
+ const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
1103
+ if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
1104
+ occs.push(...this.expandByTime(inst));
1105
+ }
1106
+ }
1107
+ }
1108
+ }
1109
+ occs = occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
1110
+ occs = this.applyBySetPos(occs);
1111
+ return occs;
1112
+ }
1113
+ applyBySetPos(list2) {
1114
+ const { bySetPos } = this.opts;
1115
+ if (!bySetPos || !bySetPos.length) return list2;
1116
+ const sorted = [...list2].sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
1117
+ const out = [];
1118
+ const len = sorted.length;
1119
+ for (const pos of bySetPos) {
1120
+ const idx = pos > 0 ? pos - 1 : len + pos;
1121
+ if (idx >= 0 && idx < len) out.push(sorted[idx]);
1122
+ }
1123
+ return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
1124
+ }
1125
+ mergeRDates(list2, startInst, endInst, inc) {
1126
+ if (!this.opts.rDate) return list2;
1127
+ const extras = this.opts.rDate.map(
1128
+ (d) => d instanceof Temporal.ZonedDateTime ? d : Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
1129
+ );
1130
+ for (const z of extras) {
1131
+ const inst = z.toInstant();
1132
+ const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
1133
+ const endOk = inc ? Temporal.Instant.compare(inst, endInst) <= 0 : Temporal.Instant.compare(inst, endInst) < 0;
1134
+ if (startOk && endOk) list2.push(z);
1135
+ }
1136
+ list2.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
1137
+ const dedup = [];
1138
+ for (const d of list2) {
1139
+ if (dedup.length === 0 || Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
1140
+ dedup.push(d);
1141
+ }
1142
+ }
1143
+ return dedup;
820
1144
  }
821
1145
  };
822
1146
  export {