rrule-temporal 1.4.7 → 1.5.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/totext.cjs CHANGED
@@ -70,6 +70,22 @@ var allowedFreqSet = new Set(allowedFreq);
70
70
  var allowedWeekdaysSet = new Set(allowedWeekdays);
71
71
  var byDayTokenRegex = new RegExp(`^([+-]?\\d{1,2})?(${allowedWeekdays.join("|")})$`);
72
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
+ }
73
89
  function extractWeekdayToken(token) {
74
90
  const m = token.toUpperCase().match(byDayWeekdaySuffixRegex);
75
91
  const weekday = m == null ? void 0 : m[1];
@@ -296,7 +312,7 @@ var _RRuleTemporal = class _RRuleTemporal {
296
312
  return import_polyfill.Temporal.ZonedDateTime.from(zdt.toString());
297
313
  }
298
314
  constructor(params) {
299
- var _a, _b, _c, _d, _e, _f, _g, _h;
315
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
300
316
  let manual;
301
317
  if (isIcsOpts(params)) {
302
318
  const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, (_a = params.strict) != null ? _a : false);
@@ -337,6 +353,32 @@ var _RRuleTemporal = class _RRuleTemporal {
337
353
  this.opts = this.sanitizeOpts(manual);
338
354
  this.maxIterations = (_g = manual.maxIterations) != null ? _g : 1e4;
339
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);
340
382
  }
341
383
  sanitizeNumericArray(arr, min, max, allowZero = false, sort = false) {
342
384
  if (!arr) return void 0;
@@ -458,9 +500,21 @@ var _RRuleTemporal = class _RRuleTemporal {
458
500
  */
459
501
  expandByTime(base) {
460
502
  var _a, _b, _c;
503
+ if (!this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond) {
504
+ return [base];
505
+ }
461
506
  const hours = (_a = this.opts.byHour) != null ? _a : [base.hour];
462
507
  const minutes = (_b = this.opts.byMinute) != null ? _b : [base.minute];
463
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
+ }
464
518
  const out = [];
465
519
  for (const h of hours) {
466
520
  for (const m of minutes) {
@@ -708,30 +762,26 @@ var _RRuleTemporal = class _RRuleTemporal {
708
762
  // --- NEW: constraint checks ---
709
763
  // 2) Replace your matchesByDay with this:
710
764
  matchesByDay(zdt) {
765
+ var _a, _b, _c;
711
766
  const { byDay, freq } = this.opts;
712
767
  if (!byDay) return true;
713
- const dayMap = weekdayToIsoDay;
714
- for (const token of byDay) {
715
- const parsed = parseByDayToken(token);
716
- if (!parsed) continue;
717
- const ord = parsed.ord;
718
- const wd = dayMap[parsed.weekday];
719
- if (freq === "DAILY") {
720
- if (zdt.dayOfWeek === wd) return true;
721
- continue;
722
- }
723
- if (ord === 0) {
724
- 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;
725
775
  continue;
726
776
  }
727
777
  const month = zdt.month;
728
778
  let dt = zdt.with({ day: 1 });
729
779
  const candidates = [];
730
780
  while (dt.month === month) {
731
- if (dt.dayOfWeek === wd) candidates.push(dt.day);
781
+ if (dt.dayOfWeek === token.isoDay) candidates.push(dt.day);
732
782
  dt = dt.add({ days: 1 });
733
783
  }
734
- const idx = ord > 0 ? ord - 1 : candidates.length + ord;
784
+ const idx = token.ord > 0 ? token.ord - 1 : candidates.length + token.ord;
735
785
  if (candidates[idx] === zdt.day) return true;
736
786
  }
737
787
  return false;
@@ -924,6 +974,485 @@ var _RRuleTemporal = class _RRuleTemporal {
924
974
  }
925
975
  return true;
926
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
+ }
927
1456
  processOccurrences(occs, dates, start, iterator, extraFilters) {
928
1457
  let shouldBreak = false;
929
1458
  for (const occ of occs) {
@@ -967,8 +1496,11 @@ var _RRuleTemporal = class _RRuleTemporal {
967
1496
  if (++iterationCount > this.maxIterations) {
968
1497
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
969
1498
  }
970
- let occs = this.generateMonthlyOccurrences(monthCursor);
971
- occs = this.applyBySetPos(occs);
1499
+ let occs = this.generateMonthlyOccurrencesOptimizedBySetPos(monthCursor);
1500
+ if (!occs) {
1501
+ occs = this.generateMonthlyOccurrences(monthCursor);
1502
+ occs = this.applyBySetPos(occs);
1503
+ }
972
1504
  const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
973
1505
  if (shouldBreak) {
974
1506
  break;
@@ -978,7 +1510,7 @@ var _RRuleTemporal = class _RRuleTemporal {
978
1510
  return this.applyCountLimitAndMergeRDates(dates, iterator);
979
1511
  }
980
1512
  _allWeekly(iterator) {
981
- var _a, _b;
1513
+ var _a, _b, _c;
982
1514
  const dates = [];
983
1515
  let iterationCount = 0;
984
1516
  const start = this.originalDtstart;
@@ -986,15 +1518,14 @@ var _RRuleTemporal = class _RRuleTemporal {
986
1518
  return this.applyCountLimitAndMergeRDates(dates, iterator);
987
1519
  }
988
1520
  const dayMap = weekdayToIsoDay;
989
- 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]];
990
- const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
1521
+ const dows = this.opts.byDay ? [...(_a = this.allByDayIsoDays) != null ? _a : []] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? [...Object.values(dayMap)] : [start.dayOfWeek];
991
1522
  const firstWeekDates = dows.map((dw) => {
992
1523
  const delta = (dw - start.dayOfWeek + 7) % 7;
993
1524
  return start.add({ days: delta });
994
1525
  });
995
1526
  const firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
996
- const wkstToken = (_a = extractWeekdayToken(this.opts.wkst || "MO")) != null ? _a : "MO";
997
- const wkstDay = (_b = dayMap[wkstToken]) != null ? _b : 1;
1527
+ const wkstToken = (_b = extractWeekdayToken(this.opts.wkst || "MO")) != null ? _b : "MO";
1528
+ const wkstDay = (_c = dayMap[wkstToken]) != null ? _c : 1;
998
1529
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
999
1530
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
1000
1531
  while (true) {
@@ -1393,6 +1924,10 @@ var _RRuleTemporal = class _RRuleTemporal {
1393
1924
  if (!this.opts.count && !this.opts.until && !iterator) {
1394
1925
  throw new Error("all() requires iterator when no COUNT/UNTIL");
1395
1926
  }
1927
+ const utcFastPathDates = this.allUtcFastPath(iterator);
1928
+ if (utcFastPathDates) {
1929
+ return utcFastPathDates;
1930
+ }
1396
1931
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay) && !this.opts.byWeekNo) {
1397
1932
  return this._allMonthlyByDayOrMonthDay(iterator);
1398
1933
  }
@@ -1796,58 +2331,14 @@ var _RRuleTemporal = class _RRuleTemporal {
1796
2331
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
1797
2332
  */
1798
2333
  generateMonthlyOccurrences(sample) {
1799
- var _a;
1800
- const { byDay, byMonth, byMonthDay } = this.opts;
1801
- if (byMonth && !byMonth.includes(sample.month)) return [];
1802
- const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
1803
- let byMonthDayHits = [];
1804
- if (byMonthDay && byMonthDay.length > 0) {
1805
- byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
1806
- }
1807
- if (!byDay && byMonthDay && byMonthDay.length > 0) {
1808
- if (byMonthDayHits.length === 0) {
1809
- return [];
1810
- }
1811
- const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
1812
- return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1813
- }
1814
- if (!byDay) {
2334
+ const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
2335
+ if (!this.opts.byDay && !this.opts.byMonthDay) {
1815
2336
  return this.expandByTime(sample);
1816
2337
  }
1817
- const dayMap = weekdayToIsoDay;
1818
- const tokens = byDay.map((tok) => {
1819
- const parsed = parseByDayToken(tok);
1820
- if (!parsed) return null;
1821
- return { ord: parsed.ord, wd: dayMap[parsed.weekday] };
1822
- }).filter((x) => x !== null);
1823
- const buckets = {};
1824
- let cursor = sample.with({ day: 1 });
1825
- while (cursor.month === sample.month) {
1826
- const dow = cursor.dayOfWeek;
1827
- (buckets[dow] || (buckets[dow] = [])).push(cursor.day);
1828
- cursor = cursor.add({ days: 1 });
1829
- }
1830
- const byDayHits = [];
1831
- for (const { ord, wd } of tokens) {
1832
- const list2 = (_a = buckets[wd]) != null ? _a : [];
1833
- if (!list2.length) continue;
1834
- if (ord === 0) {
1835
- for (const d of list2) byDayHits.push(d);
1836
- } else {
1837
- const idx = ord > 0 ? ord - 1 : list2.length + ord;
1838
- const dayN = list2[idx];
1839
- if (dayN) byDayHits.push(dayN);
1840
- }
1841
- }
1842
- let finalDays = byDayHits;
1843
- if (byMonthDay && byMonthDay.length > 0) {
1844
- if (byMonthDayHits.length === 0) {
1845
- return [];
1846
- }
1847
- finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
1848
- }
1849
- const hits = finalDays.map((d) => sample.with({ day: d }));
1850
- 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));
1851
2342
  }
1852
2343
  /**
1853
2344
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -2043,9 +2534,11 @@ var _RRuleTemporal = class _RRuleTemporal {
2043
2534
  return current;
2044
2535
  }
2045
2536
  if (this.opts.byDay && !this.matchesByDay(current)) {
2046
- const dayMap = weekdayToIsoDay;
2047
- const targetDays = this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null).map((day) => dayMap[day]).filter(Boolean);
2048
- 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);
2049
2542
  if (nextDayOfWeek) {
2050
2543
  const delta = (nextDayOfWeek - current.dayOfWeek + 7) % 7;
2051
2544
  current = current.add({ days: delta }).with({ hour: 0, minute: 0, second: 0 });
@@ -2079,12 +2572,7 @@ var _RRuleTemporal = class _RRuleTemporal {
2079
2572
  const { bySetPos } = this.opts;
2080
2573
  if (!bySetPos || !bySetPos.length) return list2;
2081
2574
  const sorted = [...list2].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2082
- const out = [];
2083
- const len = sorted.length;
2084
- for (const pos of bySetPos) {
2085
- const idx = pos > 0 ? pos - 1 : len + pos;
2086
- if (idx >= 0 && idx < len) out.push(sorted[idx]);
2087
- }
2575
+ const out = this.applyBySetPosToSortedList(sorted);
2088
2576
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2089
2577
  }
2090
2578
  isoWeekByDay(sample) {