rrule-temporal 1.4.6 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/totext.cjs CHANGED
@@ -55,6 +55,51 @@ module.exports = __toCommonJS(totext_exports);
55
55
 
56
56
  // src/index.ts
57
57
  var import_polyfill = require("@js-temporal/polyfill");
58
+ var allowedFreq = ["YEARLY", "MONTHLY", "WEEKLY", "DAILY", "HOURLY", "MINUTELY", "SECONDLY"];
59
+ var allowedWeekdays = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
60
+ var weekdayToIsoDay = {
61
+ MO: 1,
62
+ TU: 2,
63
+ WE: 3,
64
+ TH: 4,
65
+ FR: 5,
66
+ SA: 6,
67
+ SU: 7
68
+ };
69
+ var allowedFreqSet = new Set(allowedFreq);
70
+ var allowedWeekdaysSet = new Set(allowedWeekdays);
71
+ var byDayTokenRegex = new RegExp(`^([+-]?\\d{1,2})?(${allowedWeekdays.join("|")})$`);
72
+ var byDayWeekdaySuffixRegex = new RegExp(`(${allowedWeekdays.join("|")})$`);
73
+ var MS_PER_SECOND = 1e3;
74
+ var MS_PER_MINUTE = 60 * MS_PER_SECOND;
75
+ var MS_PER_HOUR = 60 * MS_PER_MINUTE;
76
+ var MS_PER_DAY = 24 * MS_PER_HOUR;
77
+ var MS_PER_WEEK = 7 * MS_PER_DAY;
78
+ var GREGORIAN_MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
79
+ var GREGORIAN_WEEKDAY_OFFSETS = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
80
+ var NS_PER_MILLISECOND = BigInt(1e6);
81
+ var NS_PER_SECOND = BigInt(1e9);
82
+ var NS_PER_MINUTE = BigInt(60) * NS_PER_SECOND;
83
+ var NS_PER_HOUR = BigInt(60) * NS_PER_MINUTE;
84
+ var NS_PER_DAY = BigInt(24) * NS_PER_HOUR;
85
+ var NS_PER_WEEK = BigInt(7) * NS_PER_DAY;
86
+ function addIsoDays(dayOfWeek, deltaDays) {
87
+ return (dayOfWeek - 1 + deltaDays % 7 + 7) % 7 + 1;
88
+ }
89
+ function extractWeekdayToken(token) {
90
+ const m = token.toUpperCase().match(byDayWeekdaySuffixRegex);
91
+ const weekday = m == null ? void 0 : m[1];
92
+ if (!weekday || !allowedWeekdaysSet.has(weekday)) return null;
93
+ return weekday;
94
+ }
95
+ function parseByDayToken(token) {
96
+ const m = token.toUpperCase().match(byDayTokenRegex);
97
+ if (!m) return null;
98
+ const ord = m[1] ? parseInt(m[1], 10) : 0;
99
+ const weekday = m[2];
100
+ if (!weekday || !allowedWeekdaysSet.has(weekday)) return null;
101
+ return { ord, weekday };
102
+ }
58
103
  function isIcsOpts(opts) {
59
104
  return typeof opts.rruleString === "string";
60
105
  }
@@ -228,7 +273,7 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
228
273
  opts.bySecond = parseNumberArray(val, true);
229
274
  break;
230
275
  case "BYDAY":
231
- opts.byDay = val.split(",");
276
+ opts.byDay = val.split(",").map((token) => token.toUpperCase());
232
277
  break;
233
278
  case "BYMONTH":
234
279
  opts.byMonth = parseByMonthArray(val);
@@ -246,7 +291,7 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
246
291
  opts.bySetPos = parseNumberArray(val);
247
292
  break;
248
293
  case "WKST":
249
- opts.wkst = val;
294
+ opts.wkst = val == null ? void 0 : val.toUpperCase();
250
295
  break;
251
296
  }
252
297
  }
@@ -267,7 +312,7 @@ var _RRuleTemporal = class _RRuleTemporal {
267
312
  return import_polyfill.Temporal.ZonedDateTime.from(zdt.toString());
268
313
  }
269
314
  constructor(params) {
270
- var _a, _b, _c, _d, _e, _f, _g, _h;
315
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
271
316
  let manual;
272
317
  if (isIcsOpts(params)) {
273
318
  const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, (_a = params.strict) != null ? _a : false);
@@ -308,6 +353,32 @@ var _RRuleTemporal = class _RRuleTemporal {
308
353
  this.opts = this.sanitizeOpts(manual);
309
354
  this.maxIterations = (_g = manual.maxIterations) != null ? _g : 1e4;
310
355
  this.includeDtstart = (_h = manual.includeDtstart) != null ? _h : false;
356
+ this.parsedByDayTokens = this.buildParsedByDayTokens(this.opts.byDay);
357
+ this.simpleByDayIsoDays = this.buildByDayIsoDays(this.parsedByDayTokens, false);
358
+ this.allByDayIsoDays = this.buildByDayIsoDays(this.parsedByDayTokens, true);
359
+ this.hasOrdinalByDay = (_j = (_i = this.parsedByDayTokens) == null ? void 0 : _i.some((token) => token.ord !== 0)) != null ? _j : false;
360
+ this.canUseEpochMillisecondsPrecisionFlag = this.originalDtstart.microsecond === 0 && this.originalDtstart.nanosecond === 0 && (!this.opts.until || this.opts.until.microsecond === 0 && this.opts.until.nanosecond === 0);
361
+ this.timeSlotOffsetsMs = this.buildTimeSlotOffsetsMs();
362
+ this.numericByMonths = (_k = this.opts.byMonth) == null ? void 0 : _k.filter((value) => typeof value === "number");
363
+ }
364
+ buildParsedByDayTokens(byDay) {
365
+ if (!(byDay == null ? void 0 : byDay.length)) return void 0;
366
+ const tokens = byDay.map((tok) => {
367
+ const parsed = parseByDayToken(tok);
368
+ if (!parsed) return null;
369
+ return {
370
+ ord: parsed.ord,
371
+ weekday: parsed.weekday,
372
+ isoDay: weekdayToIsoDay[parsed.weekday]
373
+ };
374
+ }).filter((token) => token !== null);
375
+ return tokens.length > 0 ? tokens : void 0;
376
+ }
377
+ buildByDayIsoDays(tokens, includeOrdinals) {
378
+ if (!(tokens == null ? void 0 : tokens.length)) return void 0;
379
+ const isoDays = tokens.filter((token) => includeOrdinals || token.ord === 0).map((token) => token.isoDay);
380
+ if (!isoDays.length) return void 0;
381
+ return [...new Set(isoDays)].sort((a, b) => a - b);
311
382
  }
312
383
  sanitizeNumericArray(arr, min, max, allowZero = false, sort = false) {
313
384
  if (!arr) return void 0;
@@ -316,22 +387,20 @@ var _RRuleTemporal = class _RRuleTemporal {
316
387
  return sort ? sanitized.sort((a, b) => a - b) : sanitized;
317
388
  }
318
389
  sanitizeByDay(byDay) {
319
- const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
320
- const days = (byDay != null ? byDay : []).filter((day) => day && typeof day === "string");
390
+ const days = (byDay != null ? byDay : []).filter((day) => Boolean(day) && typeof day === "string");
391
+ const normalized = [];
321
392
  for (const day of days) {
322
- const match = day.match(validDay);
323
- if (!match) {
393
+ const token = day.toUpperCase();
394
+ const parsed = parseByDayToken(token);
395
+ if (!parsed) {
324
396
  throw new Error(`Invalid BYDAY value: ${day}`);
325
397
  }
326
- const ord = match[1];
327
- if (ord) {
328
- const ordInt = parseInt(ord, 10);
329
- if (ordInt === 0) {
330
- throw new Error(`Invalid BYDAY value: ${day}`);
331
- }
398
+ if (parsed.ord === 0 && /^[+-]?\d/.test(token)) {
399
+ throw new Error(`Invalid BYDAY value: ${day}`);
332
400
  }
401
+ normalized.push(token);
333
402
  }
334
- return days.length > 0 ? days : void 0;
403
+ return normalized.length > 0 ? normalized : void 0;
335
404
  }
336
405
  enforceStrictRfc(opts) {
337
406
  var _a;
@@ -362,7 +431,17 @@ var _RRuleTemporal = class _RRuleTemporal {
362
431
  }
363
432
  sanitizeOpts(opts) {
364
433
  var _a;
434
+ if (!allowedFreqSet.has(opts.freq)) {
435
+ throw new Error(`Invalid FREQ value: ${opts.freq}`);
436
+ }
365
437
  opts.byDay = this.sanitizeByDay(opts.byDay);
438
+ if (opts.wkst) {
439
+ const wkst = opts.wkst.toUpperCase();
440
+ if (!allowedWeekdaysSet.has(wkst)) {
441
+ throw new Error(`Invalid WKST value: ${opts.wkst}`);
442
+ }
443
+ opts.wkst = wkst;
444
+ }
366
445
  if (opts.byMonth) {
367
446
  const numeric = opts.byMonth.filter((v) => typeof v === "number");
368
447
  const stringy = opts.byMonth.filter((v) => typeof v === "string");
@@ -421,9 +500,21 @@ var _RRuleTemporal = class _RRuleTemporal {
421
500
  */
422
501
  expandByTime(base) {
423
502
  var _a, _b, _c;
503
+ if (!this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond) {
504
+ return [base];
505
+ }
424
506
  const hours = (_a = this.opts.byHour) != null ? _a : [base.hour];
425
507
  const minutes = (_b = this.opts.byMinute) != null ? _b : [base.minute];
426
508
  const seconds = (_c = this.opts.bySecond) != null ? _c : [base.second];
509
+ if (hours.length === 1 && minutes.length === 1 && seconds.length === 1) {
510
+ const hour = hours[0];
511
+ const minute = minutes[0];
512
+ const second = seconds[0];
513
+ if (hour === base.hour && minute === base.minute && second === base.second) {
514
+ return [base];
515
+ }
516
+ return [base.with({ hour, minute, second })];
517
+ }
427
518
  const out = [];
428
519
  for (const h of hours) {
429
520
  for (const m of minutes) {
@@ -577,11 +668,8 @@ var _RRuleTemporal = class _RRuleTemporal {
577
668
  weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: lastWeek + targetWeek });
578
669
  }
579
670
  if ((_b = this.opts.byDay) == null ? void 0 : _b.length) {
580
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
581
- const targetDays = this.opts.byDay.map((tok) => {
582
- var _a2;
583
- return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
584
- }).filter(Boolean).map((day) => dayMap[day]).filter(Boolean);
671
+ const dayMap = weekdayToIsoDay;
672
+ const targetDays = this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null).map((day) => dayMap[day]).filter(Boolean);
585
673
  if (targetDays.length) {
586
674
  const candidates = targetDays.map((dayOfWeek) => {
587
675
  const delta = (dayOfWeek - weekStart.dayOfWeek + 7) % 7;
@@ -604,7 +692,7 @@ var _RRuleTemporal = class _RRuleTemporal {
604
692
  }
605
693
  }
606
694
  if (((_c = this.opts.byDay) == null ? void 0 : _c.length) && !this.opts.byWeekNo) {
607
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
695
+ const dayMap = weekdayToIsoDay;
608
696
  const hasOrdinalTokens = this.opts.byDay.some((tok) => /^[+-]?\d/.test(tok));
609
697
  if (hasOrdinalTokens && this.opts.byMonth && (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY")) {
610
698
  const months = this.opts.byMonth.filter((v) => typeof v === "number").sort((a, b) => a - b);
@@ -630,14 +718,11 @@ var _RRuleTemporal = class _RRuleTemporal {
630
718
  }
631
719
  } else {
632
720
  let deltas;
633
- if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
634
- deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
721
+ const weekdayTokens = this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((tok) => tok !== null);
722
+ if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && weekdayTokens.length === this.opts.byDay.length) {
723
+ deltas = weekdayTokens.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
635
724
  } else {
636
- deltas = this.opts.byDay.map((tok) => {
637
- var _a2;
638
- const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
639
- return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
640
- }).filter((d) => d !== null);
725
+ deltas = weekdayTokens.map((wdTok) => (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7);
641
726
  }
642
727
  if (deltas.length) {
643
728
  zdt = zdt.add({ days: Math.min(...deltas) });
@@ -677,32 +762,26 @@ var _RRuleTemporal = class _RRuleTemporal {
677
762
  // --- NEW: constraint checks ---
678
763
  // 2) Replace your matchesByDay with this:
679
764
  matchesByDay(zdt) {
765
+ var _a, _b, _c;
680
766
  const { byDay, freq } = this.opts;
681
767
  if (!byDay) return true;
682
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
683
- for (const token of byDay) {
684
- const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
685
- if (!m) continue;
686
- const ord = m[1] ? parseInt(m[1], 10) : 0;
687
- const weekday = m[2];
688
- if (!weekday) continue;
689
- const wd = dayMap[weekday];
690
- if (freq === "DAILY") {
691
- if (zdt.dayOfWeek === wd) return true;
692
- continue;
693
- }
694
- if (ord === 0) {
695
- if (zdt.dayOfWeek === wd) return true;
768
+ if (!this.hasOrdinalByDay) {
769
+ return (_b = (_a = this.simpleByDayIsoDays) == null ? void 0 : _a.includes(zdt.dayOfWeek)) != null ? _b : false;
770
+ }
771
+ for (const token of (_c = this.parsedByDayTokens) != null ? _c : []) {
772
+ if (freq === "DAILY" && zdt.dayOfWeek === token.isoDay) return true;
773
+ if (token.ord === 0) {
774
+ if (zdt.dayOfWeek === token.isoDay) return true;
696
775
  continue;
697
776
  }
698
777
  const month = zdt.month;
699
778
  let dt = zdt.with({ day: 1 });
700
779
  const candidates = [];
701
780
  while (dt.month === month) {
702
- if (dt.dayOfWeek === wd) candidates.push(dt.day);
781
+ if (dt.dayOfWeek === token.isoDay) candidates.push(dt.day);
703
782
  dt = dt.add({ days: 1 });
704
783
  }
705
- const idx = ord > 0 ? ord - 1 : candidates.length + ord;
784
+ const idx = token.ord > 0 ? token.ord - 1 : candidates.length + token.ord;
706
785
  if (candidates[idx] === zdt.day) return true;
707
786
  }
708
787
  return false;
@@ -895,6 +974,485 @@ var _RRuleTemporal = class _RRuleTemporal {
895
974
  }
896
975
  return true;
897
976
  }
977
+ canUseUtcLinearFastPath(iterator) {
978
+ if (iterator || this.tzid !== "UTC" || this.opts.rscale || this.opts.rDate || this.opts.exDate) {
979
+ return false;
980
+ }
981
+ if (this.opts.byMonth || this.opts.byMonthDay || this.opts.byYearDay || this.opts.byWeekNo || this.opts.bySetPos) {
982
+ return false;
983
+ }
984
+ switch (this.opts.freq) {
985
+ case "DAILY":
986
+ return !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond && !this.hasOrdinalByDay;
987
+ case "HOURLY":
988
+ case "MINUTELY":
989
+ return !this.opts.byDay && !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond;
990
+ default:
991
+ return false;
992
+ }
993
+ }
994
+ canUseUtcWeeklyFastPath(iterator) {
995
+ return !iterator && this.tzid === "UTC" && this.opts.freq === "WEEKLY" && !this.opts.rscale && !this.opts.rDate && !this.opts.exDate && !this.opts.byMonth && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo && !this.opts.bySetPos && !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond && !this.hasOrdinalByDay;
996
+ }
997
+ canUseUtcMonthlyFastPath(iterator) {
998
+ return !iterator && this.tzid === "UTC" && this.opts.freq === "MONTHLY" && !this.opts.rscale && !this.opts.rDate && !this.opts.exDate && !this.opts.byYearDay && !this.opts.byWeekNo && this.canUseEpochMillisecondsPrecisionFlag && !!(this.opts.byDay || this.opts.byMonthDay);
999
+ }
1000
+ utcZdtFromEpochNanoseconds(epochNanoseconds) {
1001
+ return import_polyfill.Temporal.Instant.fromEpochNanoseconds(epochNanoseconds).toZonedDateTimeISO("UTC");
1002
+ }
1003
+ utcZdtFromEpochMilliseconds(epochMilliseconds) {
1004
+ return import_polyfill.Temporal.Instant.fromEpochMilliseconds(epochMilliseconds).toZonedDateTimeISO("UTC");
1005
+ }
1006
+ canUseUtcEpochMillisecondsPrecision() {
1007
+ return this.canUseEpochMillisecondsPrecisionFlag;
1008
+ }
1009
+ buildTimeSlotOffsetsMs() {
1010
+ var _a, _b, _c;
1011
+ if (!this.canUseEpochMillisecondsPrecisionFlag) return void 0;
1012
+ const hours = (_a = this.opts.byHour) != null ? _a : [this.originalDtstart.hour];
1013
+ const minutes = (_b = this.opts.byMinute) != null ? _b : [this.originalDtstart.minute];
1014
+ const seconds = (_c = this.opts.bySecond) != null ? _c : [this.originalDtstart.second];
1015
+ const baseMilliseconds = this.originalDtstart.millisecond;
1016
+ const offsets = [];
1017
+ for (const hour of hours) {
1018
+ for (const minute of minutes) {
1019
+ for (const second of seconds) {
1020
+ offsets.push(((hour * 60 + minute) * 60 + second) * MS_PER_SECOND + baseMilliseconds);
1021
+ }
1022
+ }
1023
+ }
1024
+ return offsets;
1025
+ }
1026
+ findFirstMatchingDailyStep(startDayOfWeek, stepDays, allowedDays) {
1027
+ let dayOfWeek = startDayOfWeek;
1028
+ for (let steps = 0; steps < 7; steps++) {
1029
+ if (allowedDays.includes(dayOfWeek)) {
1030
+ return steps;
1031
+ }
1032
+ dayOfWeek = addIsoDays(dayOfWeek, stepDays);
1033
+ }
1034
+ return null;
1035
+ }
1036
+ allUtcFastPath(iterator) {
1037
+ if (this.canUseUtcLinearFastPath(iterator)) {
1038
+ switch (this.opts.freq) {
1039
+ case "DAILY":
1040
+ return this._allUtcDailySimple();
1041
+ case "HOURLY":
1042
+ return this._allUtcFixedStepSimple(NS_PER_HOUR * BigInt(this.opts.interval));
1043
+ case "MINUTELY":
1044
+ return this._allUtcFixedStepSimple(NS_PER_MINUTE * BigInt(this.opts.interval));
1045
+ }
1046
+ }
1047
+ if (this.canUseUtcMonthlyFastPath(iterator)) {
1048
+ return this._allUtcMonthlyByDayOrMonthDay();
1049
+ }
1050
+ if (this.canUseUtcWeeklyFastPath(iterator)) {
1051
+ return this._allUtcWeeklySimple();
1052
+ }
1053
+ return null;
1054
+ }
1055
+ _allUtcFixedStepSimple(stepNanoseconds) {
1056
+ var _a, _b;
1057
+ const dates = [];
1058
+ if (!this.addDtstartIfNeeded(dates)) {
1059
+ return dates;
1060
+ }
1061
+ let iterationCount = 0;
1062
+ if (this.canUseUtcEpochMillisecondsPrecision()) {
1063
+ let currentMilliseconds = this.originalDtstart.epochMilliseconds;
1064
+ const stepMilliseconds = Number(stepNanoseconds / NS_PER_MILLISECOND);
1065
+ const untilMilliseconds = (_a = this.opts.until) == null ? void 0 : _a.epochMilliseconds;
1066
+ while (true) {
1067
+ if (++iterationCount > this.maxIterations) {
1068
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1069
+ }
1070
+ if (untilMilliseconds !== void 0 && currentMilliseconds > untilMilliseconds) {
1071
+ break;
1072
+ }
1073
+ dates.push(this.utcZdtFromEpochMilliseconds(currentMilliseconds));
1074
+ if (this.shouldBreakForCountLimit(dates.length)) {
1075
+ break;
1076
+ }
1077
+ currentMilliseconds += stepMilliseconds;
1078
+ }
1079
+ return dates;
1080
+ }
1081
+ let currentNanoseconds = this.originalDtstart.epochNanoseconds;
1082
+ const untilNanoseconds = (_b = this.opts.until) == null ? void 0 : _b.epochNanoseconds;
1083
+ while (true) {
1084
+ if (++iterationCount > this.maxIterations) {
1085
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1086
+ }
1087
+ if (untilNanoseconds !== void 0 && currentNanoseconds > untilNanoseconds) {
1088
+ break;
1089
+ }
1090
+ dates.push(this.utcZdtFromEpochNanoseconds(currentNanoseconds));
1091
+ if (this.shouldBreakForCountLimit(dates.length)) {
1092
+ break;
1093
+ }
1094
+ currentNanoseconds += stepNanoseconds;
1095
+ }
1096
+ return dates;
1097
+ }
1098
+ _allUtcDailySimple() {
1099
+ var _a, _b;
1100
+ const dates = [];
1101
+ if (!this.addDtstartIfNeeded(dates)) {
1102
+ return dates;
1103
+ }
1104
+ const stepDays = this.opts.interval;
1105
+ const allowedDays = this.simpleByDayIsoDays;
1106
+ let iterationCount = 0;
1107
+ if (this.canUseUtcEpochMillisecondsPrecision()) {
1108
+ const stepMilliseconds = stepDays * MS_PER_DAY;
1109
+ const untilMilliseconds = (_a = this.opts.until) == null ? void 0 : _a.epochMilliseconds;
1110
+ let currentMilliseconds = this.originalDtstart.epochMilliseconds;
1111
+ let currentDayOfWeek2 = this.originalDtstart.dayOfWeek;
1112
+ if (allowedDays == null ? void 0 : allowedDays.length) {
1113
+ const firstMatchingStep = this.findFirstMatchingDailyStep(currentDayOfWeek2, stepDays, allowedDays);
1114
+ if (firstMatchingStep === null) {
1115
+ return dates;
1116
+ }
1117
+ currentMilliseconds += firstMatchingStep * stepMilliseconds;
1118
+ currentDayOfWeek2 = addIsoDays(currentDayOfWeek2, firstMatchingStep * stepDays);
1119
+ }
1120
+ while (true) {
1121
+ if (++iterationCount > this.maxIterations) {
1122
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1123
+ }
1124
+ if (untilMilliseconds !== void 0 && currentMilliseconds > untilMilliseconds) {
1125
+ break;
1126
+ }
1127
+ if (!allowedDays || allowedDays.includes(currentDayOfWeek2)) {
1128
+ dates.push(this.utcZdtFromEpochMilliseconds(currentMilliseconds));
1129
+ if (this.shouldBreakForCountLimit(dates.length)) {
1130
+ break;
1131
+ }
1132
+ }
1133
+ currentMilliseconds += stepMilliseconds;
1134
+ currentDayOfWeek2 = addIsoDays(currentDayOfWeek2, stepDays);
1135
+ }
1136
+ return dates;
1137
+ }
1138
+ const stepNanoseconds = BigInt(stepDays) * NS_PER_DAY;
1139
+ const untilNanoseconds = (_b = this.opts.until) == null ? void 0 : _b.epochNanoseconds;
1140
+ let currentNanoseconds = this.originalDtstart.epochNanoseconds;
1141
+ let currentDayOfWeek = this.originalDtstart.dayOfWeek;
1142
+ if (allowedDays == null ? void 0 : allowedDays.length) {
1143
+ const firstMatchingStep = this.findFirstMatchingDailyStep(currentDayOfWeek, stepDays, allowedDays);
1144
+ if (firstMatchingStep === null) {
1145
+ return dates;
1146
+ }
1147
+ currentNanoseconds += BigInt(firstMatchingStep) * stepNanoseconds;
1148
+ currentDayOfWeek = addIsoDays(currentDayOfWeek, firstMatchingStep * stepDays);
1149
+ }
1150
+ while (true) {
1151
+ if (++iterationCount > this.maxIterations) {
1152
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1153
+ }
1154
+ if (untilNanoseconds !== void 0 && currentNanoseconds > untilNanoseconds) {
1155
+ break;
1156
+ }
1157
+ if (!allowedDays || allowedDays.includes(currentDayOfWeek)) {
1158
+ dates.push(this.utcZdtFromEpochNanoseconds(currentNanoseconds));
1159
+ if (this.shouldBreakForCountLimit(dates.length)) {
1160
+ break;
1161
+ }
1162
+ }
1163
+ currentNanoseconds += stepNanoseconds;
1164
+ currentDayOfWeek = addIsoDays(currentDayOfWeek, stepDays);
1165
+ }
1166
+ return dates;
1167
+ }
1168
+ _allUtcWeeklySimple() {
1169
+ var _a, _b, _c, _d, _e;
1170
+ const dates = [];
1171
+ if (!this.addDtstartIfNeeded(dates)) {
1172
+ return dates;
1173
+ }
1174
+ const start = this.originalDtstart;
1175
+ const wkstToken = (_a = extractWeekdayToken(this.opts.wkst || "MO")) != null ? _a : "MO";
1176
+ const wkstDay = (_b = weekdayToIsoDay[wkstToken]) != null ? _b : 1;
1177
+ const targetDays = this.opts.byDay ? [...(_c = this.allByDayIsoDays) != null ? _c : []] : [start.dayOfWeek];
1178
+ const dayOffsets = targetDays.map((day) => (day - wkstDay + 7) % 7).sort((a, b) => a - b);
1179
+ const weekStartOffset = (start.dayOfWeek - wkstDay + 7) % 7;
1180
+ let iterationCount = 0;
1181
+ if (this.canUseUtcEpochMillisecondsPrecision()) {
1182
+ const startMilliseconds = start.epochMilliseconds;
1183
+ const untilMilliseconds = (_d = this.opts.until) == null ? void 0 : _d.epochMilliseconds;
1184
+ const weekStepMilliseconds = this.opts.interval * MS_PER_WEEK;
1185
+ let weekStartMilliseconds = startMilliseconds - weekStartOffset * MS_PER_DAY;
1186
+ while (true) {
1187
+ if (++iterationCount > this.maxIterations) {
1188
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1189
+ }
1190
+ for (const dayOffset of dayOffsets) {
1191
+ const occurrenceMilliseconds = weekStartMilliseconds + dayOffset * MS_PER_DAY;
1192
+ if (occurrenceMilliseconds < startMilliseconds) {
1193
+ continue;
1194
+ }
1195
+ if (untilMilliseconds !== void 0 && occurrenceMilliseconds > untilMilliseconds) {
1196
+ return dates;
1197
+ }
1198
+ dates.push(this.utcZdtFromEpochMilliseconds(occurrenceMilliseconds));
1199
+ if (this.shouldBreakForCountLimit(dates.length)) {
1200
+ return dates;
1201
+ }
1202
+ }
1203
+ weekStartMilliseconds += weekStepMilliseconds;
1204
+ }
1205
+ }
1206
+ const startNanoseconds = start.epochNanoseconds;
1207
+ const untilNanoseconds = (_e = this.opts.until) == null ? void 0 : _e.epochNanoseconds;
1208
+ const weekStepNanoseconds = BigInt(this.opts.interval) * NS_PER_WEEK;
1209
+ let weekStartNanoseconds = startNanoseconds - BigInt(weekStartOffset) * NS_PER_DAY;
1210
+ while (true) {
1211
+ if (++iterationCount > this.maxIterations) {
1212
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1213
+ }
1214
+ for (const dayOffset of dayOffsets) {
1215
+ const occurrenceNanoseconds = weekStartNanoseconds + BigInt(dayOffset) * NS_PER_DAY;
1216
+ if (occurrenceNanoseconds < startNanoseconds) {
1217
+ continue;
1218
+ }
1219
+ if (untilNanoseconds !== void 0 && occurrenceNanoseconds > untilNanoseconds) {
1220
+ return dates;
1221
+ }
1222
+ dates.push(this.utcZdtFromEpochNanoseconds(occurrenceNanoseconds));
1223
+ if (this.shouldBreakForCountLimit(dates.length)) {
1224
+ return dates;
1225
+ }
1226
+ }
1227
+ weekStartNanoseconds += weekStepNanoseconds;
1228
+ }
1229
+ }
1230
+ hasSingleExpandedTimeSlot() {
1231
+ var _a, _b, _c;
1232
+ if (this.timeSlotOffsetsMs) {
1233
+ return this.timeSlotOffsetsMs.length === 1;
1234
+ }
1235
+ const hours = (_a = this.opts.byHour) != null ? _a : [this.originalDtstart.hour];
1236
+ const minutes = (_b = this.opts.byMinute) != null ? _b : [this.originalDtstart.minute];
1237
+ const seconds = (_c = this.opts.bySecond) != null ? _c : [this.originalDtstart.second];
1238
+ return hours.length === 1 && minutes.length === 1 && seconds.length === 1;
1239
+ }
1240
+ buildMonthlyOccurrenceOnDay(monthStart, day) {
1241
+ const base = monthStart.day === day ? monthStart : monthStart.with({ day });
1242
+ return this.applyTimeOverride(base);
1243
+ }
1244
+ applyBySetPosToSortedList(list2) {
1245
+ const { bySetPos } = this.opts;
1246
+ if (!bySetPos || !bySetPos.length || list2.length === 0) return list2;
1247
+ const out = [];
1248
+ const len = list2.length;
1249
+ for (const pos of bySetPos) {
1250
+ const idx = pos > 0 ? pos - 1 : len + pos;
1251
+ if (idx >= 0 && idx < len) out.push(list2[idx]);
1252
+ }
1253
+ return out;
1254
+ }
1255
+ generateMonthlyOccurrenceDays(sample) {
1256
+ const { byDay, byMonth, byMonthDay } = this.opts;
1257
+ const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
1258
+ if (byMonth && !byMonth.includes(sample.month)) return [];
1259
+ const lastDay = monthStart.add({ months: 1 }).subtract({ days: 1 }).day;
1260
+ let byMonthDayHits = [];
1261
+ if (byMonthDay && byMonthDay.length > 0) {
1262
+ byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
1263
+ byMonthDayHits = [...new Set(byMonthDayHits)].sort((a, b) => a - b);
1264
+ }
1265
+ if (!byDay && byMonthDay && byMonthDay.length > 0) {
1266
+ return byMonthDayHits;
1267
+ }
1268
+ if (!byDay) {
1269
+ return [sample.day];
1270
+ }
1271
+ const tokens = this.parsedByDayTokens;
1272
+ if (!(tokens == null ? void 0 : tokens.length)) return [];
1273
+ const firstDayOfWeek = monthStart.dayOfWeek;
1274
+ const lastDayOfWeek = (firstDayOfWeek - 1 + lastDay - 1) % 7 + 1;
1275
+ const byDayHits = /* @__PURE__ */ new Set();
1276
+ for (const { ord, isoDay } of tokens) {
1277
+ if (ord === 0) {
1278
+ let day = 1 + (isoDay - firstDayOfWeek + 7) % 7;
1279
+ while (day <= lastDay) {
1280
+ byDayHits.add(day);
1281
+ day += 7;
1282
+ }
1283
+ } else {
1284
+ let day;
1285
+ if (ord > 0) {
1286
+ day = 1 + (isoDay - firstDayOfWeek + 7) % 7 + 7 * (ord - 1);
1287
+ } else {
1288
+ const lastMatch = lastDay - (lastDayOfWeek - isoDay + 7) % 7;
1289
+ day = lastMatch + 7 * (ord + 1);
1290
+ }
1291
+ if (day >= 1 && day <= lastDay) {
1292
+ byDayHits.add(day);
1293
+ }
1294
+ }
1295
+ }
1296
+ let finalDays = [...byDayHits].sort((a, b) => a - b);
1297
+ if (byMonthDay && byMonthDay.length > 0) {
1298
+ if (byMonthDayHits.length === 0) {
1299
+ return [];
1300
+ }
1301
+ const byMonthDayHitSet = new Set(byMonthDayHits);
1302
+ finalDays = finalDays.filter((d) => byMonthDayHitSet.has(d));
1303
+ }
1304
+ return finalDays;
1305
+ }
1306
+ generateMonthlyOccurrencesOptimizedBySetPos(sample) {
1307
+ if (!this.opts.bySetPos || !this.hasSingleExpandedTimeSlot()) {
1308
+ return null;
1309
+ }
1310
+ const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
1311
+ const days = this.generateMonthlyOccurrenceDays(monthStart);
1312
+ if (days.length === 0) {
1313
+ return [];
1314
+ }
1315
+ const selectedDays = this.applyBySetPosToSortedList(days);
1316
+ if (selectedDays.length === 0) {
1317
+ return [];
1318
+ }
1319
+ return selectedDays.sort((a, b) => a - b).map((day) => this.buildMonthlyOccurrenceOnDay(monthStart, day));
1320
+ }
1321
+ isGregorianLeapYear(year) {
1322
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
1323
+ }
1324
+ daysInGregorianMonth(year, month) {
1325
+ if (month === 2 && this.isGregorianLeapYear(year)) {
1326
+ return 29;
1327
+ }
1328
+ return GREGORIAN_MONTH_LENGTHS[month - 1];
1329
+ }
1330
+ gregorianIsoDayOfWeek(year, month, day) {
1331
+ let adjustedYear = year;
1332
+ if (month < 3) adjustedYear -= 1;
1333
+ const sundayZero = (adjustedYear + Math.floor(adjustedYear / 4) - Math.floor(adjustedYear / 100) + Math.floor(adjustedYear / 400) + GREGORIAN_WEEKDAY_OFFSETS[month - 1] + day) % 7;
1334
+ return sundayZero === 0 ? 7 : sundayZero;
1335
+ }
1336
+ monthIndexToYearMonth(monthIndex) {
1337
+ const year = Math.floor(monthIndex / 12);
1338
+ return {
1339
+ year,
1340
+ month: monthIndex - year * 12 + 1
1341
+ };
1342
+ }
1343
+ generateMonthlyOccurrenceDaysUtc(year, month) {
1344
+ if (this.numericByMonths && this.numericByMonths.length > 0 && !this.numericByMonths.includes(month)) {
1345
+ return [];
1346
+ }
1347
+ const byMonthDay = this.opts.byMonthDay;
1348
+ const byDay = this.opts.byDay;
1349
+ const lastDay = this.daysInGregorianMonth(year, month);
1350
+ let byMonthDayHits = [];
1351
+ if (byMonthDay && byMonthDay.length > 0) {
1352
+ byMonthDayHits = byMonthDay.map((day) => day > 0 ? day : lastDay + day + 1).filter((day) => day >= 1 && day <= lastDay);
1353
+ byMonthDayHits = [...new Set(byMonthDayHits)].sort((a, b) => a - b);
1354
+ }
1355
+ if (!byDay && byMonthDay && byMonthDay.length > 0) {
1356
+ return byMonthDayHits;
1357
+ }
1358
+ if (!byDay) {
1359
+ const day = this.originalDtstart.day;
1360
+ return day >= 1 && day <= lastDay ? [day] : [];
1361
+ }
1362
+ const tokens = this.parsedByDayTokens;
1363
+ if (!(tokens == null ? void 0 : tokens.length)) return [];
1364
+ const firstDayOfWeek = this.gregorianIsoDayOfWeek(year, month, 1);
1365
+ const lastDayOfWeek = addIsoDays(firstDayOfWeek, lastDay - 1);
1366
+ const byDayHits = /* @__PURE__ */ new Set();
1367
+ for (const { ord, isoDay } of tokens) {
1368
+ if (ord === 0) {
1369
+ let day = 1 + (isoDay - firstDayOfWeek + 7) % 7;
1370
+ while (day <= lastDay) {
1371
+ byDayHits.add(day);
1372
+ day += 7;
1373
+ }
1374
+ } else {
1375
+ let day;
1376
+ if (ord > 0) {
1377
+ day = 1 + (isoDay - firstDayOfWeek + 7) % 7 + 7 * (ord - 1);
1378
+ } else {
1379
+ const lastMatch = lastDay - (lastDayOfWeek - isoDay + 7) % 7;
1380
+ day = lastMatch + 7 * (ord + 1);
1381
+ }
1382
+ if (day >= 1 && day <= lastDay) {
1383
+ byDayHits.add(day);
1384
+ }
1385
+ }
1386
+ }
1387
+ let finalDays = [...byDayHits].sort((a, b) => a - b);
1388
+ if (byMonthDay && byMonthDay.length > 0) {
1389
+ if (byMonthDayHits.length === 0) return [];
1390
+ const byMonthDayHitSet = new Set(byMonthDayHits);
1391
+ finalDays = finalDays.filter((day) => byMonthDayHitSet.has(day));
1392
+ }
1393
+ return finalDays;
1394
+ }
1395
+ generateMonthlyOccurrenceEpochsUtc(year, month) {
1396
+ var _a;
1397
+ const days = this.generateMonthlyOccurrenceDaysUtc(year, month);
1398
+ if (days.length === 0) return [];
1399
+ const monthStartMs = Date.UTC(year, month - 1, 1, 0, 0, 0, 0);
1400
+ const timeSlotOffsets = (_a = this.timeSlotOffsetsMs) != null ? _a : [0];
1401
+ if (this.opts.bySetPos && this.opts.bySetPos.length > 0) {
1402
+ if (timeSlotOffsets.length === 1) {
1403
+ const selectedDays = this.applyBySetPosToSortedList(days).sort((a, b) => a - b);
1404
+ const offset = timeSlotOffsets[0];
1405
+ return selectedDays.map((day) => monthStartMs + (day - 1) * MS_PER_DAY + offset);
1406
+ }
1407
+ const timestamps2 = [];
1408
+ for (const day of days) {
1409
+ const dayBase = monthStartMs + (day - 1) * MS_PER_DAY;
1410
+ for (const offset of timeSlotOffsets) {
1411
+ timestamps2.push(dayBase + offset);
1412
+ }
1413
+ }
1414
+ return this.applyBySetPosToSortedList(timestamps2).sort((a, b) => a - b);
1415
+ }
1416
+ const timestamps = [];
1417
+ for (const day of days) {
1418
+ const dayBase = monthStartMs + (day - 1) * MS_PER_DAY;
1419
+ for (const offset of timeSlotOffsets) {
1420
+ timestamps.push(dayBase + offset);
1421
+ }
1422
+ }
1423
+ return timestamps;
1424
+ }
1425
+ _allUtcMonthlyByDayOrMonthDay() {
1426
+ var _a;
1427
+ const dates = [];
1428
+ const startMilliseconds = this.originalDtstart.epochMilliseconds;
1429
+ const untilMilliseconds = (_a = this.opts.until) == null ? void 0 : _a.epochMilliseconds;
1430
+ let iterationCount = 0;
1431
+ if (!this.addDtstartIfNeeded(dates)) {
1432
+ return dates;
1433
+ }
1434
+ let monthIndex = this.originalDtstart.year * 12 + (this.originalDtstart.month - 1);
1435
+ while (true) {
1436
+ if (++iterationCount > this.maxIterations) {
1437
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1438
+ }
1439
+ const { year, month } = this.monthIndexToYearMonth(monthIndex);
1440
+ const occurrenceEpochs = this.generateMonthlyOccurrenceEpochsUtc(year, month);
1441
+ for (const epochMilliseconds of occurrenceEpochs) {
1442
+ if (epochMilliseconds < startMilliseconds) {
1443
+ continue;
1444
+ }
1445
+ if (untilMilliseconds !== void 0 && epochMilliseconds > untilMilliseconds) {
1446
+ return dates;
1447
+ }
1448
+ dates.push(this.utcZdtFromEpochMilliseconds(epochMilliseconds));
1449
+ if (this.shouldBreakForCountLimit(dates.length)) {
1450
+ return dates;
1451
+ }
1452
+ }
1453
+ monthIndex += this.opts.interval;
1454
+ }
1455
+ }
898
1456
  processOccurrences(occs, dates, start, iterator, extraFilters) {
899
1457
  let shouldBreak = false;
900
1458
  for (const occ of occs) {
@@ -938,11 +1496,10 @@ var _RRuleTemporal = class _RRuleTemporal {
938
1496
  if (++iterationCount > this.maxIterations) {
939
1497
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
940
1498
  }
941
- let occs = this.generateMonthlyOccurrences(monthCursor);
942
- occs = this.applyBySetPos(occs);
943
- if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0) && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) === 0)) {
944
- monthCursor = monthCursor.add({ months: this.opts.interval });
945
- continue;
1499
+ let occs = this.generateMonthlyOccurrencesOptimizedBySetPos(monthCursor);
1500
+ if (!occs) {
1501
+ occs = this.generateMonthlyOccurrences(monthCursor);
1502
+ occs = this.applyBySetPos(occs);
946
1503
  }
947
1504
  const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
948
1505
  if (shouldBreak) {
@@ -953,22 +1510,22 @@ var _RRuleTemporal = class _RRuleTemporal {
953
1510
  return this.applyCountLimitAndMergeRDates(dates, iterator);
954
1511
  }
955
1512
  _allWeekly(iterator) {
956
- var _a;
1513
+ var _a, _b, _c;
957
1514
  const dates = [];
958
1515
  let iterationCount = 0;
959
1516
  const start = this.originalDtstart;
960
1517
  if (!this.addDtstartIfNeeded(dates, iterator)) {
961
1518
  return this.applyCountLimitAndMergeRDates(dates, iterator);
962
1519
  }
963
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
964
- const tokens = this.opts.byDay ? [...this.opts.byDay] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? Object.keys(dayMap) : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
965
- const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
1520
+ const dayMap = weekdayToIsoDay;
1521
+ const dows = this.opts.byDay ? [...(_a = this.allByDayIsoDays) != null ? _a : []] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? [...Object.values(dayMap)] : [start.dayOfWeek];
966
1522
  const firstWeekDates = dows.map((dw) => {
967
1523
  const delta = (dw - start.dayOfWeek + 7) % 7;
968
1524
  return start.add({ days: delta });
969
1525
  });
970
1526
  const firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
971
- const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
1527
+ const wkstToken = (_b = extractWeekdayToken(this.opts.wkst || "MO")) != null ? _b : "MO";
1528
+ const wkstDay = (_c = dayMap[wkstToken]) != null ? _c : 1;
972
1529
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
973
1530
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
974
1531
  while (true) {
@@ -1367,6 +1924,10 @@ var _RRuleTemporal = class _RRuleTemporal {
1367
1924
  if (!this.opts.count && !this.opts.until && !iterator) {
1368
1925
  throw new Error("all() requires iterator when no COUNT/UNTIL");
1369
1926
  }
1927
+ const utcFastPathDates = this.allUtcFastPath(iterator);
1928
+ if (utcFastPathDates) {
1929
+ return utcFastPathDates;
1930
+ }
1370
1931
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay) && !this.opts.byWeekNo) {
1371
1932
  return this._allMonthlyByDayOrMonthDay(iterator);
1372
1933
  }
@@ -1770,58 +2331,14 @@ var _RRuleTemporal = class _RRuleTemporal {
1770
2331
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
1771
2332
  */
1772
2333
  generateMonthlyOccurrences(sample) {
1773
- var _a;
1774
- const { byDay, byMonth, byMonthDay } = this.opts;
1775
- if (byMonth && !byMonth.includes(sample.month)) return [];
1776
- const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
1777
- let byMonthDayHits = [];
1778
- if (byMonthDay && byMonthDay.length > 0) {
1779
- byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
1780
- }
1781
- if (!byDay && byMonthDay && byMonthDay.length > 0) {
1782
- if (byMonthDayHits.length === 0) {
1783
- return [];
1784
- }
1785
- const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
1786
- return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1787
- }
1788
- if (!byDay) {
2334
+ const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
2335
+ if (!this.opts.byDay && !this.opts.byMonthDay) {
1789
2336
  return this.expandByTime(sample);
1790
2337
  }
1791
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1792
- const tokens = byDay.map((tok) => {
1793
- const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
1794
- if (!m) return null;
1795
- return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
1796
- }).filter((x) => x !== null);
1797
- const buckets = {};
1798
- let cursor = sample.with({ day: 1 });
1799
- while (cursor.month === sample.month) {
1800
- const dow = cursor.dayOfWeek;
1801
- (buckets[dow] || (buckets[dow] = [])).push(cursor.day);
1802
- cursor = cursor.add({ days: 1 });
1803
- }
1804
- const byDayHits = [];
1805
- for (const { ord, wd } of tokens) {
1806
- const list2 = (_a = buckets[wd]) != null ? _a : [];
1807
- if (!list2.length) continue;
1808
- if (ord === 0) {
1809
- for (const d of list2) byDayHits.push(d);
1810
- } else {
1811
- const idx = ord > 0 ? ord - 1 : list2.length + ord;
1812
- const dayN = list2[idx];
1813
- if (dayN) byDayHits.push(dayN);
1814
- }
1815
- }
1816
- let finalDays = byDayHits;
1817
- if (byMonthDay && byMonthDay.length > 0) {
1818
- if (byMonthDayHits.length === 0) {
1819
- return [];
1820
- }
1821
- finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
1822
- }
1823
- const hits = finalDays.map((d) => sample.with({ day: d }));
1824
- return hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2338
+ const finalDays = this.generateMonthlyOccurrenceDays(monthStart);
2339
+ if (finalDays.length === 0) return [];
2340
+ const hits = finalDays.map((d) => monthStart.with({ day: d }));
2341
+ return hits.flatMap((z) => this.expandByTime(z));
1825
2342
  }
1826
2343
  /**
1827
2344
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -1833,12 +2350,12 @@ var _RRuleTemporal = class _RRuleTemporal {
1833
2350
  let occs = [];
1834
2351
  const hasOrdinalByDay = this.opts.byDay && this.opts.byDay.some((t) => /^[+-]?\d/.test(t));
1835
2352
  if (hasOrdinalByDay && !this.opts.byMonth) {
1836
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2353
+ const dayMap = weekdayToIsoDay;
1837
2354
  for (const tok of this.opts.byDay) {
1838
- const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
1839
- if (!m) continue;
1840
- const ord = parseInt(m[1], 10);
1841
- const wd = dayMap[m[2]];
2355
+ const parsed = parseByDayToken(tok);
2356
+ if (!parsed || parsed.ord === 0) continue;
2357
+ const ord = parsed.ord;
2358
+ const wd = dayMap[parsed.weekday];
1842
2359
  let dt;
1843
2360
  if (ord > 0) {
1844
2361
  const jan1 = sample.with({ month: 1, day: 1 });
@@ -1902,7 +2419,7 @@ var _RRuleTemporal = class _RRuleTemporal {
1902
2419
  return occs;
1903
2420
  }
1904
2421
  addByDay(tokens, weekStart) {
1905
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2422
+ const dayMap = weekdayToIsoDay;
1906
2423
  const wkst = dayMap[this.opts.wkst || "MO"];
1907
2424
  const entries = [];
1908
2425
  for (const tok of tokens) {
@@ -2017,12 +2534,11 @@ var _RRuleTemporal = class _RRuleTemporal {
2017
2534
  return current;
2018
2535
  }
2019
2536
  if (this.opts.byDay && !this.matchesByDay(current)) {
2020
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2021
- const targetDays = this.opts.byDay.map((tok) => {
2022
- var _a;
2023
- return (_a = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a[1];
2024
- }).filter(Boolean).map((day) => dayMap[day]).filter(Boolean);
2025
- const nextDayOfWeek = this.findNextValidValue(current.dayOfWeek, targetDays.sort(), (a, b) => a - b);
2537
+ const targetDays = this.allByDayIsoDays;
2538
+ if (!(targetDays == null ? void 0 : targetDays.length)) {
2539
+ return this.applyTimeOverride(current.add({ days: 1 }).with({ hour: 0, minute: 0, second: 0 }));
2540
+ }
2541
+ const nextDayOfWeek = this.findNextValidValue(current.dayOfWeek, targetDays, (a, b) => a - b);
2026
2542
  if (nextDayOfWeek) {
2027
2543
  const delta = (nextDayOfWeek - current.dayOfWeek + 7) % 7;
2028
2544
  current = current.add({ days: delta }).with({ hour: 0, minute: 0, second: 0 });
@@ -2056,17 +2572,12 @@ var _RRuleTemporal = class _RRuleTemporal {
2056
2572
  const { bySetPos } = this.opts;
2057
2573
  if (!bySetPos || !bySetPos.length) return list2;
2058
2574
  const sorted = [...list2].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2059
- const out = [];
2060
- const len = sorted.length;
2061
- for (const pos of bySetPos) {
2062
- const idx = pos > 0 ? pos - 1 : len + pos;
2063
- if (idx >= 0 && idx < len) out.push(sorted[idx]);
2064
- }
2575
+ const out = this.applyBySetPosToSortedList(sorted);
2065
2576
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2066
2577
  }
2067
2578
  isoWeekByDay(sample) {
2068
2579
  var _a;
2069
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2580
+ const dayMap = weekdayToIsoDay;
2070
2581
  const wkst = dayMap[this.opts.wkst || "MO"];
2071
2582
  const jan1 = sample.with({ month: 1, day: 1 });
2072
2583
  const jan4 = sample.with({ month: 1, day: 4 });
@@ -2074,10 +2585,7 @@ var _RRuleTemporal = class _RRuleTemporal {
2074
2585
  const firstWeekStart = jan4.subtract({ days: delta });
2075
2586
  const isLeapYear = jan1.inLeapYear;
2076
2587
  const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
2077
- const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
2078
- var _a2;
2079
- return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
2080
- }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
2588
+ const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
2081
2589
  return { lastWeek, firstWeekStart, tokens };
2082
2590
  }
2083
2591
  /**
@@ -2195,7 +2703,7 @@ var _RRuleTemporal = class _RRuleTemporal {
2195
2703
  rscaleMatchesByWeekNo(calId, pd) {
2196
2704
  const list2 = this.opts.byWeekNo;
2197
2705
  if (!list2 || list2.length === 0) return true;
2198
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2706
+ const dayMap = weekdayToIsoDay;
2199
2707
  const wkst = dayMap[this.opts.wkst || "MO"];
2200
2708
  const weekStart = pd.subtract({ days: (pd.dayOfWeek - wkst + 7) % 7 });
2201
2709
  const thursday = weekStart.add({ days: (4 - wkst + 7) % 7 });
@@ -2220,11 +2728,8 @@ var _RRuleTemporal = class _RRuleTemporal {
2220
2728
  rscaleMatchesByDayBasic(pd) {
2221
2729
  const byDay = this.opts.byDay;
2222
2730
  if (!byDay || byDay.length === 0) return true;
2223
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2224
- const tokens = byDay.map((tok) => {
2225
- var _a;
2226
- return (_a = tok.match(/^(?:[+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a[1];
2227
- }).filter((x) => !!x);
2731
+ const dayMap = weekdayToIsoDay;
2732
+ const tokens = byDay.map((tok) => extractWeekdayToken(tok)).filter((x) => x !== null);
2228
2733
  if (tokens.length === 0) return true;
2229
2734
  return tokens.some((wd) => dayMap[wd] === pd.dayOfWeek);
2230
2735
  }
@@ -2269,7 +2774,7 @@ var _RRuleTemporal = class _RRuleTemporal {
2269
2774
  }
2270
2775
  }
2271
2776
  if (byDay && byDay.length > 0) {
2272
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2777
+ const dayMap = weekdayToIsoDay;
2273
2778
  const buckets = {};
2274
2779
  let cur = monthStart;
2275
2780
  while (cur.month === monthStart.month && cur.year === monthStart.year) {
@@ -2278,10 +2783,10 @@ var _RRuleTemporal = class _RRuleTemporal {
2278
2783
  cur = cur.add({ days: 1 });
2279
2784
  }
2280
2785
  for (const tok of byDay) {
2281
- const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
2282
- if (!m) continue;
2283
- const ord = m[1] ? parseInt(m[1], 10) : 0;
2284
- const wd = dayMap[m[2]];
2786
+ const parsed = parseByDayToken(tok);
2787
+ if (!parsed) continue;
2788
+ const ord = parsed.ord;
2789
+ const wd = dayMap[parsed.weekday];
2285
2790
  const list2 = buckets[wd] || [];
2286
2791
  if (list2.length === 0) continue;
2287
2792
  if (ord === 0) {
@@ -2318,15 +2823,12 @@ var _RRuleTemporal = class _RRuleTemporal {
2318
2823
  let occs = [];
2319
2824
  const monthsTokens = this.opts.byMonth;
2320
2825
  const months = this.monthsOfYear(calId, tgtYear);
2321
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2826
+ const dayMap = weekdayToIsoDay;
2322
2827
  const wkst = dayMap[this.opts.wkst || "MO"];
2323
2828
  if (this.opts.byWeekNo && this.opts.byWeekNo.length > 0) {
2324
2829
  const firstStart = this.rscaleFirstWeekStart(calId, tgtYear, wkst);
2325
2830
  const lastWeek = this.rscaleLastWeekCount(calId, tgtYear, wkst);
2326
- const tokens = ((_b = this.opts.byDay) == null ? void 0 : _b.length) ? this.opts.byDay.map((tok) => {
2327
- var _a2;
2328
- return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
2329
- }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
2831
+ const tokens = ((_b = this.opts.byDay) == null ? void 0 : _b.length) ? this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
2330
2832
  for (const wn of this.opts.byWeekNo) {
2331
2833
  let idx = wn > 0 ? wn - 1 : lastWeek + wn;
2332
2834
  if (idx < 0 || idx >= lastWeek) continue;
@@ -2395,12 +2897,9 @@ var _RRuleTemporal = class _RRuleTemporal {
2395
2897
  return this.applyCountLimitAndMergeRDates(dates, iterator);
2396
2898
  }
2397
2899
  if (this.opts.freq === "WEEKLY") {
2398
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
2900
+ const dayMap = weekdayToIsoDay;
2399
2901
  const wkst = dayMap[this.opts.wkst || "MO"];
2400
- const tokens = ((_c = this.opts.byDay) == null ? void 0 : _c.length) ? this.opts.byDay.map((tok) => {
2401
- var _a2;
2402
- return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
2403
- }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
2902
+ const tokens = ((_c = this.opts.byDay) == null ? void 0 : _c.length) ? this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
2404
2903
  let weekStart = seed.toPlainDate().subtract({ days: (seed.dayOfWeek - wkst + 7) % 7 });
2405
2904
  while (true) {
2406
2905
  if (++iterationCount > this.maxIterations) {
@@ -3058,6 +3557,11 @@ var LOCALES = {};
3058
3557
  for (const l of active) {
3059
3558
  if (ALL_LOCALES[l]) LOCALES[l] = ALL_LOCALES[l];
3060
3559
  }
3560
+ var workweekWeekdays = allowedWeekdays.slice(0, 5);
3561
+ var byDayTokenRegex2 = new RegExp(`^([+-]?\\d+)?(${allowedWeekdays.join("|")})$`);
3562
+ var weekdayIndexByToken = Object.fromEntries(
3563
+ allowedWeekdays.map((weekday, idx) => [weekday, idx])
3564
+ );
3061
3565
  function defaultOrdinal(n) {
3062
3566
  const abs = Math.abs(n);
3063
3567
  const suffix = abs % 10 === 1 && abs % 100 !== 11 ? "st" : abs % 10 === 2 && abs % 100 !== 12 ? "nd" : abs % 10 === 3 && abs % 100 !== 13 ? "rd" : "th";
@@ -3073,19 +3577,11 @@ function list(arr, mapFn = (x) => `${x}`, final) {
3073
3577
  }
3074
3578
  function formatByDayToken(tok, locale) {
3075
3579
  if (typeof tok === "number") return tok.toString();
3076
- const m = tok.match(/^([+-]?\d+)?(MO|TU|WE|TH|FR|SA|SU)$/);
3580
+ const m = tok.toUpperCase().match(byDayTokenRegex2);
3077
3581
  if (!m) return tok;
3078
3582
  const ord = m[1] ? parseInt(m[1], 10) : 0;
3079
- const weekdayMap = {
3080
- MO: 0,
3081
- TU: 1,
3082
- WE: 2,
3083
- TH: 3,
3084
- FR: 4,
3085
- SA: 5,
3086
- SU: 6
3087
- };
3088
- const idx = weekdayMap[m[2]];
3583
+ const weekday = m[2];
3584
+ const idx = weekdayIndexByToken[weekday];
3089
3585
  const name = locale.weekdayNames[idx];
3090
3586
  if (ord === 0) return name;
3091
3587
  if (ord === -1) return `${locale.words.last} ${name}`;
@@ -3105,8 +3601,7 @@ function formatTime(hour, minute = 0, second = 0) {
3105
3601
  return `${hr12} ${ampm}`;
3106
3602
  }
3107
3603
  function weekdayTokenFromZdt(zdt) {
3108
- const tokens = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
3109
- return tokens[zdt.dayOfWeek - 1];
3604
+ return allowedWeekdays[zdt.dayOfWeek - 1];
3110
3605
  }
3111
3606
  function tzAbbreviation(zdt) {
3112
3607
  const parts = new Intl.DateTimeFormat("en-US", {
@@ -3169,8 +3664,8 @@ function toText(input, locale, options = {}) {
3169
3664
  }[freq];
3170
3665
  const base = data.units[baseKey];
3171
3666
  const daysNormalized = textByDay == null ? void 0 : textByDay.map((d) => d.toUpperCase());
3172
- const isWeekdays = daysNormalized && daysNormalized.length === 5 && ["MO", "TU", "WE", "TH", "FR"].every((d) => daysNormalized.includes(d));
3173
- const isEveryday = daysNormalized && daysNormalized.length === 7 && ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].every((d) => daysNormalized.includes(d));
3667
+ const isWeekdays = daysNormalized && daysNormalized.length === workweekWeekdays.length && workweekWeekdays.every((d) => daysNormalized.includes(d));
3668
+ const isEveryday = daysNormalized && daysNormalized.length === allowedWeekdays.length && allowedWeekdays.every((d) => daysNormalized.includes(d));
3174
3669
  if (freq === "WEEKLY" && interval === 1 && isWeekdays) {
3175
3670
  parts.push(data.words.weekday);
3176
3671
  } else if (freq === "WEEKLY" && interval === 1 && isEveryday) {