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/index.cjs CHANGED
@@ -71,6 +71,22 @@ var allowedFreqSet = new Set(allowedFreq);
71
71
  var allowedWeekdaysSet = new Set(allowedWeekdays);
72
72
  var byDayTokenRegex = new RegExp(`^([+-]?\\d{1,2})?(${allowedWeekdays.join("|")})$`);
73
73
  var byDayWeekdaySuffixRegex = new RegExp(`(${allowedWeekdays.join("|")})$`);
74
+ var MS_PER_SECOND = 1e3;
75
+ var MS_PER_MINUTE = 60 * MS_PER_SECOND;
76
+ var MS_PER_HOUR = 60 * MS_PER_MINUTE;
77
+ var MS_PER_DAY = 24 * MS_PER_HOUR;
78
+ var MS_PER_WEEK = 7 * MS_PER_DAY;
79
+ var GREGORIAN_MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
80
+ var GREGORIAN_WEEKDAY_OFFSETS = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
81
+ var NS_PER_MILLISECOND = BigInt(1e6);
82
+ var NS_PER_SECOND = BigInt(1e9);
83
+ var NS_PER_MINUTE = BigInt(60) * NS_PER_SECOND;
84
+ var NS_PER_HOUR = BigInt(60) * NS_PER_MINUTE;
85
+ var NS_PER_DAY = BigInt(24) * NS_PER_HOUR;
86
+ var NS_PER_WEEK = BigInt(7) * NS_PER_DAY;
87
+ function addIsoDays(dayOfWeek, deltaDays) {
88
+ return (dayOfWeek - 1 + deltaDays % 7 + 7) % 7 + 1;
89
+ }
74
90
  function extractWeekdayToken(token) {
75
91
  const m = token.toUpperCase().match(byDayWeekdaySuffixRegex);
76
92
  const weekday = m == null ? void 0 : m[1];
@@ -297,7 +313,7 @@ var _RRuleTemporal = class _RRuleTemporal {
297
313
  return import_polyfill.Temporal.ZonedDateTime.from(zdt.toString());
298
314
  }
299
315
  constructor(params) {
300
- var _a, _b, _c, _d, _e, _f, _g, _h;
316
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
301
317
  let manual;
302
318
  if (isIcsOpts(params)) {
303
319
  const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, (_a = params.strict) != null ? _a : false);
@@ -338,6 +354,32 @@ var _RRuleTemporal = class _RRuleTemporal {
338
354
  this.opts = this.sanitizeOpts(manual);
339
355
  this.maxIterations = (_g = manual.maxIterations) != null ? _g : 1e4;
340
356
  this.includeDtstart = (_h = manual.includeDtstart) != null ? _h : false;
357
+ this.parsedByDayTokens = this.buildParsedByDayTokens(this.opts.byDay);
358
+ this.simpleByDayIsoDays = this.buildByDayIsoDays(this.parsedByDayTokens, false);
359
+ this.allByDayIsoDays = this.buildByDayIsoDays(this.parsedByDayTokens, true);
360
+ this.hasOrdinalByDay = (_j = (_i = this.parsedByDayTokens) == null ? void 0 : _i.some((token) => token.ord !== 0)) != null ? _j : false;
361
+ this.canUseEpochMillisecondsPrecisionFlag = this.originalDtstart.microsecond === 0 && this.originalDtstart.nanosecond === 0 && (!this.opts.until || this.opts.until.microsecond === 0 && this.opts.until.nanosecond === 0);
362
+ this.timeSlotOffsetsMs = this.buildTimeSlotOffsetsMs();
363
+ this.numericByMonths = (_k = this.opts.byMonth) == null ? void 0 : _k.filter((value) => typeof value === "number");
364
+ }
365
+ buildParsedByDayTokens(byDay) {
366
+ if (!(byDay == null ? void 0 : byDay.length)) return void 0;
367
+ const tokens = byDay.map((tok) => {
368
+ const parsed = parseByDayToken(tok);
369
+ if (!parsed) return null;
370
+ return {
371
+ ord: parsed.ord,
372
+ weekday: parsed.weekday,
373
+ isoDay: weekdayToIsoDay[parsed.weekday]
374
+ };
375
+ }).filter((token) => token !== null);
376
+ return tokens.length > 0 ? tokens : void 0;
377
+ }
378
+ buildByDayIsoDays(tokens, includeOrdinals) {
379
+ if (!(tokens == null ? void 0 : tokens.length)) return void 0;
380
+ const isoDays = tokens.filter((token) => includeOrdinals || token.ord === 0).map((token) => token.isoDay);
381
+ if (!isoDays.length) return void 0;
382
+ return [...new Set(isoDays)].sort((a, b) => a - b);
341
383
  }
342
384
  sanitizeNumericArray(arr, min, max, allowZero = false, sort = false) {
343
385
  if (!arr) return void 0;
@@ -459,9 +501,21 @@ var _RRuleTemporal = class _RRuleTemporal {
459
501
  */
460
502
  expandByTime(base) {
461
503
  var _a, _b, _c;
504
+ if (!this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond) {
505
+ return [base];
506
+ }
462
507
  const hours = (_a = this.opts.byHour) != null ? _a : [base.hour];
463
508
  const minutes = (_b = this.opts.byMinute) != null ? _b : [base.minute];
464
509
  const seconds = (_c = this.opts.bySecond) != null ? _c : [base.second];
510
+ if (hours.length === 1 && minutes.length === 1 && seconds.length === 1) {
511
+ const hour = hours[0];
512
+ const minute = minutes[0];
513
+ const second = seconds[0];
514
+ if (hour === base.hour && minute === base.minute && second === base.second) {
515
+ return [base];
516
+ }
517
+ return [base.with({ hour, minute, second })];
518
+ }
465
519
  const out = [];
466
520
  for (const h of hours) {
467
521
  for (const m of minutes) {
@@ -709,30 +763,26 @@ var _RRuleTemporal = class _RRuleTemporal {
709
763
  // --- NEW: constraint checks ---
710
764
  // 2) Replace your matchesByDay with this:
711
765
  matchesByDay(zdt) {
766
+ var _a, _b, _c;
712
767
  const { byDay, freq } = this.opts;
713
768
  if (!byDay) return true;
714
- const dayMap = weekdayToIsoDay;
715
- for (const token of byDay) {
716
- const parsed = parseByDayToken(token);
717
- if (!parsed) continue;
718
- const ord = parsed.ord;
719
- const wd = dayMap[parsed.weekday];
720
- if (freq === "DAILY") {
721
- if (zdt.dayOfWeek === wd) return true;
722
- continue;
723
- }
724
- if (ord === 0) {
725
- if (zdt.dayOfWeek === wd) return true;
769
+ if (!this.hasOrdinalByDay) {
770
+ return (_b = (_a = this.simpleByDayIsoDays) == null ? void 0 : _a.includes(zdt.dayOfWeek)) != null ? _b : false;
771
+ }
772
+ for (const token of (_c = this.parsedByDayTokens) != null ? _c : []) {
773
+ if (freq === "DAILY" && zdt.dayOfWeek === token.isoDay) return true;
774
+ if (token.ord === 0) {
775
+ if (zdt.dayOfWeek === token.isoDay) return true;
726
776
  continue;
727
777
  }
728
778
  const month = zdt.month;
729
779
  let dt = zdt.with({ day: 1 });
730
780
  const candidates = [];
731
781
  while (dt.month === month) {
732
- if (dt.dayOfWeek === wd) candidates.push(dt.day);
782
+ if (dt.dayOfWeek === token.isoDay) candidates.push(dt.day);
733
783
  dt = dt.add({ days: 1 });
734
784
  }
735
- const idx = ord > 0 ? ord - 1 : candidates.length + ord;
785
+ const idx = token.ord > 0 ? token.ord - 1 : candidates.length + token.ord;
736
786
  if (candidates[idx] === zdt.day) return true;
737
787
  }
738
788
  return false;
@@ -925,6 +975,485 @@ var _RRuleTemporal = class _RRuleTemporal {
925
975
  }
926
976
  return true;
927
977
  }
978
+ canUseUtcLinearFastPath(iterator) {
979
+ if (iterator || this.tzid !== "UTC" || this.opts.rscale || this.opts.rDate || this.opts.exDate) {
980
+ return false;
981
+ }
982
+ if (this.opts.byMonth || this.opts.byMonthDay || this.opts.byYearDay || this.opts.byWeekNo || this.opts.bySetPos) {
983
+ return false;
984
+ }
985
+ switch (this.opts.freq) {
986
+ case "DAILY":
987
+ return !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond && !this.hasOrdinalByDay;
988
+ case "HOURLY":
989
+ case "MINUTELY":
990
+ return !this.opts.byDay && !this.opts.byHour && !this.opts.byMinute && !this.opts.bySecond;
991
+ default:
992
+ return false;
993
+ }
994
+ }
995
+ canUseUtcWeeklyFastPath(iterator) {
996
+ 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;
997
+ }
998
+ canUseUtcMonthlyFastPath(iterator) {
999
+ 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);
1000
+ }
1001
+ utcZdtFromEpochNanoseconds(epochNanoseconds) {
1002
+ return import_polyfill.Temporal.Instant.fromEpochNanoseconds(epochNanoseconds).toZonedDateTimeISO("UTC");
1003
+ }
1004
+ utcZdtFromEpochMilliseconds(epochMilliseconds) {
1005
+ return import_polyfill.Temporal.Instant.fromEpochMilliseconds(epochMilliseconds).toZonedDateTimeISO("UTC");
1006
+ }
1007
+ canUseUtcEpochMillisecondsPrecision() {
1008
+ return this.canUseEpochMillisecondsPrecisionFlag;
1009
+ }
1010
+ buildTimeSlotOffsetsMs() {
1011
+ var _a, _b, _c;
1012
+ if (!this.canUseEpochMillisecondsPrecisionFlag) return void 0;
1013
+ const hours = (_a = this.opts.byHour) != null ? _a : [this.originalDtstart.hour];
1014
+ const minutes = (_b = this.opts.byMinute) != null ? _b : [this.originalDtstart.minute];
1015
+ const seconds = (_c = this.opts.bySecond) != null ? _c : [this.originalDtstart.second];
1016
+ const baseMilliseconds = this.originalDtstart.millisecond;
1017
+ const offsets = [];
1018
+ for (const hour of hours) {
1019
+ for (const minute of minutes) {
1020
+ for (const second of seconds) {
1021
+ offsets.push(((hour * 60 + minute) * 60 + second) * MS_PER_SECOND + baseMilliseconds);
1022
+ }
1023
+ }
1024
+ }
1025
+ return offsets;
1026
+ }
1027
+ findFirstMatchingDailyStep(startDayOfWeek, stepDays, allowedDays) {
1028
+ let dayOfWeek = startDayOfWeek;
1029
+ for (let steps = 0; steps < 7; steps++) {
1030
+ if (allowedDays.includes(dayOfWeek)) {
1031
+ return steps;
1032
+ }
1033
+ dayOfWeek = addIsoDays(dayOfWeek, stepDays);
1034
+ }
1035
+ return null;
1036
+ }
1037
+ allUtcFastPath(iterator) {
1038
+ if (this.canUseUtcLinearFastPath(iterator)) {
1039
+ switch (this.opts.freq) {
1040
+ case "DAILY":
1041
+ return this._allUtcDailySimple();
1042
+ case "HOURLY":
1043
+ return this._allUtcFixedStepSimple(NS_PER_HOUR * BigInt(this.opts.interval));
1044
+ case "MINUTELY":
1045
+ return this._allUtcFixedStepSimple(NS_PER_MINUTE * BigInt(this.opts.interval));
1046
+ }
1047
+ }
1048
+ if (this.canUseUtcMonthlyFastPath(iterator)) {
1049
+ return this._allUtcMonthlyByDayOrMonthDay();
1050
+ }
1051
+ if (this.canUseUtcWeeklyFastPath(iterator)) {
1052
+ return this._allUtcWeeklySimple();
1053
+ }
1054
+ return null;
1055
+ }
1056
+ _allUtcFixedStepSimple(stepNanoseconds) {
1057
+ var _a, _b;
1058
+ const dates = [];
1059
+ if (!this.addDtstartIfNeeded(dates)) {
1060
+ return dates;
1061
+ }
1062
+ let iterationCount = 0;
1063
+ if (this.canUseUtcEpochMillisecondsPrecision()) {
1064
+ let currentMilliseconds = this.originalDtstart.epochMilliseconds;
1065
+ const stepMilliseconds = Number(stepNanoseconds / NS_PER_MILLISECOND);
1066
+ const untilMilliseconds = (_a = this.opts.until) == null ? void 0 : _a.epochMilliseconds;
1067
+ while (true) {
1068
+ if (++iterationCount > this.maxIterations) {
1069
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1070
+ }
1071
+ if (untilMilliseconds !== void 0 && currentMilliseconds > untilMilliseconds) {
1072
+ break;
1073
+ }
1074
+ dates.push(this.utcZdtFromEpochMilliseconds(currentMilliseconds));
1075
+ if (this.shouldBreakForCountLimit(dates.length)) {
1076
+ break;
1077
+ }
1078
+ currentMilliseconds += stepMilliseconds;
1079
+ }
1080
+ return dates;
1081
+ }
1082
+ let currentNanoseconds = this.originalDtstart.epochNanoseconds;
1083
+ const untilNanoseconds = (_b = this.opts.until) == null ? void 0 : _b.epochNanoseconds;
1084
+ while (true) {
1085
+ if (++iterationCount > this.maxIterations) {
1086
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1087
+ }
1088
+ if (untilNanoseconds !== void 0 && currentNanoseconds > untilNanoseconds) {
1089
+ break;
1090
+ }
1091
+ dates.push(this.utcZdtFromEpochNanoseconds(currentNanoseconds));
1092
+ if (this.shouldBreakForCountLimit(dates.length)) {
1093
+ break;
1094
+ }
1095
+ currentNanoseconds += stepNanoseconds;
1096
+ }
1097
+ return dates;
1098
+ }
1099
+ _allUtcDailySimple() {
1100
+ var _a, _b;
1101
+ const dates = [];
1102
+ if (!this.addDtstartIfNeeded(dates)) {
1103
+ return dates;
1104
+ }
1105
+ const stepDays = this.opts.interval;
1106
+ const allowedDays = this.simpleByDayIsoDays;
1107
+ let iterationCount = 0;
1108
+ if (this.canUseUtcEpochMillisecondsPrecision()) {
1109
+ const stepMilliseconds = stepDays * MS_PER_DAY;
1110
+ const untilMilliseconds = (_a = this.opts.until) == null ? void 0 : _a.epochMilliseconds;
1111
+ let currentMilliseconds = this.originalDtstart.epochMilliseconds;
1112
+ let currentDayOfWeek2 = this.originalDtstart.dayOfWeek;
1113
+ if (allowedDays == null ? void 0 : allowedDays.length) {
1114
+ const firstMatchingStep = this.findFirstMatchingDailyStep(currentDayOfWeek2, stepDays, allowedDays);
1115
+ if (firstMatchingStep === null) {
1116
+ return dates;
1117
+ }
1118
+ currentMilliseconds += firstMatchingStep * stepMilliseconds;
1119
+ currentDayOfWeek2 = addIsoDays(currentDayOfWeek2, firstMatchingStep * stepDays);
1120
+ }
1121
+ while (true) {
1122
+ if (++iterationCount > this.maxIterations) {
1123
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1124
+ }
1125
+ if (untilMilliseconds !== void 0 && currentMilliseconds > untilMilliseconds) {
1126
+ break;
1127
+ }
1128
+ if (!allowedDays || allowedDays.includes(currentDayOfWeek2)) {
1129
+ dates.push(this.utcZdtFromEpochMilliseconds(currentMilliseconds));
1130
+ if (this.shouldBreakForCountLimit(dates.length)) {
1131
+ break;
1132
+ }
1133
+ }
1134
+ currentMilliseconds += stepMilliseconds;
1135
+ currentDayOfWeek2 = addIsoDays(currentDayOfWeek2, stepDays);
1136
+ }
1137
+ return dates;
1138
+ }
1139
+ const stepNanoseconds = BigInt(stepDays) * NS_PER_DAY;
1140
+ const untilNanoseconds = (_b = this.opts.until) == null ? void 0 : _b.epochNanoseconds;
1141
+ let currentNanoseconds = this.originalDtstart.epochNanoseconds;
1142
+ let currentDayOfWeek = this.originalDtstart.dayOfWeek;
1143
+ if (allowedDays == null ? void 0 : allowedDays.length) {
1144
+ const firstMatchingStep = this.findFirstMatchingDailyStep(currentDayOfWeek, stepDays, allowedDays);
1145
+ if (firstMatchingStep === null) {
1146
+ return dates;
1147
+ }
1148
+ currentNanoseconds += BigInt(firstMatchingStep) * stepNanoseconds;
1149
+ currentDayOfWeek = addIsoDays(currentDayOfWeek, firstMatchingStep * stepDays);
1150
+ }
1151
+ while (true) {
1152
+ if (++iterationCount > this.maxIterations) {
1153
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1154
+ }
1155
+ if (untilNanoseconds !== void 0 && currentNanoseconds > untilNanoseconds) {
1156
+ break;
1157
+ }
1158
+ if (!allowedDays || allowedDays.includes(currentDayOfWeek)) {
1159
+ dates.push(this.utcZdtFromEpochNanoseconds(currentNanoseconds));
1160
+ if (this.shouldBreakForCountLimit(dates.length)) {
1161
+ break;
1162
+ }
1163
+ }
1164
+ currentNanoseconds += stepNanoseconds;
1165
+ currentDayOfWeek = addIsoDays(currentDayOfWeek, stepDays);
1166
+ }
1167
+ return dates;
1168
+ }
1169
+ _allUtcWeeklySimple() {
1170
+ var _a, _b, _c, _d, _e;
1171
+ const dates = [];
1172
+ if (!this.addDtstartIfNeeded(dates)) {
1173
+ return dates;
1174
+ }
1175
+ const start = this.originalDtstart;
1176
+ const wkstToken = (_a = extractWeekdayToken(this.opts.wkst || "MO")) != null ? _a : "MO";
1177
+ const wkstDay = (_b = weekdayToIsoDay[wkstToken]) != null ? _b : 1;
1178
+ const targetDays = this.opts.byDay ? [...(_c = this.allByDayIsoDays) != null ? _c : []] : [start.dayOfWeek];
1179
+ const dayOffsets = targetDays.map((day) => (day - wkstDay + 7) % 7).sort((a, b) => a - b);
1180
+ const weekStartOffset = (start.dayOfWeek - wkstDay + 7) % 7;
1181
+ let iterationCount = 0;
1182
+ if (this.canUseUtcEpochMillisecondsPrecision()) {
1183
+ const startMilliseconds = start.epochMilliseconds;
1184
+ const untilMilliseconds = (_d = this.opts.until) == null ? void 0 : _d.epochMilliseconds;
1185
+ const weekStepMilliseconds = this.opts.interval * MS_PER_WEEK;
1186
+ let weekStartMilliseconds = startMilliseconds - weekStartOffset * MS_PER_DAY;
1187
+ while (true) {
1188
+ if (++iterationCount > this.maxIterations) {
1189
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1190
+ }
1191
+ for (const dayOffset of dayOffsets) {
1192
+ const occurrenceMilliseconds = weekStartMilliseconds + dayOffset * MS_PER_DAY;
1193
+ if (occurrenceMilliseconds < startMilliseconds) {
1194
+ continue;
1195
+ }
1196
+ if (untilMilliseconds !== void 0 && occurrenceMilliseconds > untilMilliseconds) {
1197
+ return dates;
1198
+ }
1199
+ dates.push(this.utcZdtFromEpochMilliseconds(occurrenceMilliseconds));
1200
+ if (this.shouldBreakForCountLimit(dates.length)) {
1201
+ return dates;
1202
+ }
1203
+ }
1204
+ weekStartMilliseconds += weekStepMilliseconds;
1205
+ }
1206
+ }
1207
+ const startNanoseconds = start.epochNanoseconds;
1208
+ const untilNanoseconds = (_e = this.opts.until) == null ? void 0 : _e.epochNanoseconds;
1209
+ const weekStepNanoseconds = BigInt(this.opts.interval) * NS_PER_WEEK;
1210
+ let weekStartNanoseconds = startNanoseconds - BigInt(weekStartOffset) * NS_PER_DAY;
1211
+ while (true) {
1212
+ if (++iterationCount > this.maxIterations) {
1213
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1214
+ }
1215
+ for (const dayOffset of dayOffsets) {
1216
+ const occurrenceNanoseconds = weekStartNanoseconds + BigInt(dayOffset) * NS_PER_DAY;
1217
+ if (occurrenceNanoseconds < startNanoseconds) {
1218
+ continue;
1219
+ }
1220
+ if (untilNanoseconds !== void 0 && occurrenceNanoseconds > untilNanoseconds) {
1221
+ return dates;
1222
+ }
1223
+ dates.push(this.utcZdtFromEpochNanoseconds(occurrenceNanoseconds));
1224
+ if (this.shouldBreakForCountLimit(dates.length)) {
1225
+ return dates;
1226
+ }
1227
+ }
1228
+ weekStartNanoseconds += weekStepNanoseconds;
1229
+ }
1230
+ }
1231
+ hasSingleExpandedTimeSlot() {
1232
+ var _a, _b, _c;
1233
+ if (this.timeSlotOffsetsMs) {
1234
+ return this.timeSlotOffsetsMs.length === 1;
1235
+ }
1236
+ const hours = (_a = this.opts.byHour) != null ? _a : [this.originalDtstart.hour];
1237
+ const minutes = (_b = this.opts.byMinute) != null ? _b : [this.originalDtstart.minute];
1238
+ const seconds = (_c = this.opts.bySecond) != null ? _c : [this.originalDtstart.second];
1239
+ return hours.length === 1 && minutes.length === 1 && seconds.length === 1;
1240
+ }
1241
+ buildMonthlyOccurrenceOnDay(monthStart, day) {
1242
+ const base = monthStart.day === day ? monthStart : monthStart.with({ day });
1243
+ return this.applyTimeOverride(base);
1244
+ }
1245
+ applyBySetPosToSortedList(list) {
1246
+ const { bySetPos } = this.opts;
1247
+ if (!bySetPos || !bySetPos.length || list.length === 0) return list;
1248
+ const out = [];
1249
+ const len = list.length;
1250
+ for (const pos of bySetPos) {
1251
+ const idx = pos > 0 ? pos - 1 : len + pos;
1252
+ if (idx >= 0 && idx < len) out.push(list[idx]);
1253
+ }
1254
+ return out;
1255
+ }
1256
+ generateMonthlyOccurrenceDays(sample) {
1257
+ const { byDay, byMonth, byMonthDay } = this.opts;
1258
+ const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
1259
+ if (byMonth && !byMonth.includes(sample.month)) return [];
1260
+ const lastDay = monthStart.add({ months: 1 }).subtract({ days: 1 }).day;
1261
+ let byMonthDayHits = [];
1262
+ if (byMonthDay && byMonthDay.length > 0) {
1263
+ byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
1264
+ byMonthDayHits = [...new Set(byMonthDayHits)].sort((a, b) => a - b);
1265
+ }
1266
+ if (!byDay && byMonthDay && byMonthDay.length > 0) {
1267
+ return byMonthDayHits;
1268
+ }
1269
+ if (!byDay) {
1270
+ return [sample.day];
1271
+ }
1272
+ const tokens = this.parsedByDayTokens;
1273
+ if (!(tokens == null ? void 0 : tokens.length)) return [];
1274
+ const firstDayOfWeek = monthStart.dayOfWeek;
1275
+ const lastDayOfWeek = (firstDayOfWeek - 1 + lastDay - 1) % 7 + 1;
1276
+ const byDayHits = /* @__PURE__ */ new Set();
1277
+ for (const { ord, isoDay } of tokens) {
1278
+ if (ord === 0) {
1279
+ let day = 1 + (isoDay - firstDayOfWeek + 7) % 7;
1280
+ while (day <= lastDay) {
1281
+ byDayHits.add(day);
1282
+ day += 7;
1283
+ }
1284
+ } else {
1285
+ let day;
1286
+ if (ord > 0) {
1287
+ day = 1 + (isoDay - firstDayOfWeek + 7) % 7 + 7 * (ord - 1);
1288
+ } else {
1289
+ const lastMatch = lastDay - (lastDayOfWeek - isoDay + 7) % 7;
1290
+ day = lastMatch + 7 * (ord + 1);
1291
+ }
1292
+ if (day >= 1 && day <= lastDay) {
1293
+ byDayHits.add(day);
1294
+ }
1295
+ }
1296
+ }
1297
+ let finalDays = [...byDayHits].sort((a, b) => a - b);
1298
+ if (byMonthDay && byMonthDay.length > 0) {
1299
+ if (byMonthDayHits.length === 0) {
1300
+ return [];
1301
+ }
1302
+ const byMonthDayHitSet = new Set(byMonthDayHits);
1303
+ finalDays = finalDays.filter((d) => byMonthDayHitSet.has(d));
1304
+ }
1305
+ return finalDays;
1306
+ }
1307
+ generateMonthlyOccurrencesOptimizedBySetPos(sample) {
1308
+ if (!this.opts.bySetPos || !this.hasSingleExpandedTimeSlot()) {
1309
+ return null;
1310
+ }
1311
+ const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
1312
+ const days = this.generateMonthlyOccurrenceDays(monthStart);
1313
+ if (days.length === 0) {
1314
+ return [];
1315
+ }
1316
+ const selectedDays = this.applyBySetPosToSortedList(days);
1317
+ if (selectedDays.length === 0) {
1318
+ return [];
1319
+ }
1320
+ return selectedDays.sort((a, b) => a - b).map((day) => this.buildMonthlyOccurrenceOnDay(monthStart, day));
1321
+ }
1322
+ isGregorianLeapYear(year) {
1323
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
1324
+ }
1325
+ daysInGregorianMonth(year, month) {
1326
+ if (month === 2 && this.isGregorianLeapYear(year)) {
1327
+ return 29;
1328
+ }
1329
+ return GREGORIAN_MONTH_LENGTHS[month - 1];
1330
+ }
1331
+ gregorianIsoDayOfWeek(year, month, day) {
1332
+ let adjustedYear = year;
1333
+ if (month < 3) adjustedYear -= 1;
1334
+ const sundayZero = (adjustedYear + Math.floor(adjustedYear / 4) - Math.floor(adjustedYear / 100) + Math.floor(adjustedYear / 400) + GREGORIAN_WEEKDAY_OFFSETS[month - 1] + day) % 7;
1335
+ return sundayZero === 0 ? 7 : sundayZero;
1336
+ }
1337
+ monthIndexToYearMonth(monthIndex) {
1338
+ const year = Math.floor(monthIndex / 12);
1339
+ return {
1340
+ year,
1341
+ month: monthIndex - year * 12 + 1
1342
+ };
1343
+ }
1344
+ generateMonthlyOccurrenceDaysUtc(year, month) {
1345
+ if (this.numericByMonths && this.numericByMonths.length > 0 && !this.numericByMonths.includes(month)) {
1346
+ return [];
1347
+ }
1348
+ const byMonthDay = this.opts.byMonthDay;
1349
+ const byDay = this.opts.byDay;
1350
+ const lastDay = this.daysInGregorianMonth(year, month);
1351
+ let byMonthDayHits = [];
1352
+ if (byMonthDay && byMonthDay.length > 0) {
1353
+ byMonthDayHits = byMonthDay.map((day) => day > 0 ? day : lastDay + day + 1).filter((day) => day >= 1 && day <= lastDay);
1354
+ byMonthDayHits = [...new Set(byMonthDayHits)].sort((a, b) => a - b);
1355
+ }
1356
+ if (!byDay && byMonthDay && byMonthDay.length > 0) {
1357
+ return byMonthDayHits;
1358
+ }
1359
+ if (!byDay) {
1360
+ const day = this.originalDtstart.day;
1361
+ return day >= 1 && day <= lastDay ? [day] : [];
1362
+ }
1363
+ const tokens = this.parsedByDayTokens;
1364
+ if (!(tokens == null ? void 0 : tokens.length)) return [];
1365
+ const firstDayOfWeek = this.gregorianIsoDayOfWeek(year, month, 1);
1366
+ const lastDayOfWeek = addIsoDays(firstDayOfWeek, lastDay - 1);
1367
+ const byDayHits = /* @__PURE__ */ new Set();
1368
+ for (const { ord, isoDay } of tokens) {
1369
+ if (ord === 0) {
1370
+ let day = 1 + (isoDay - firstDayOfWeek + 7) % 7;
1371
+ while (day <= lastDay) {
1372
+ byDayHits.add(day);
1373
+ day += 7;
1374
+ }
1375
+ } else {
1376
+ let day;
1377
+ if (ord > 0) {
1378
+ day = 1 + (isoDay - firstDayOfWeek + 7) % 7 + 7 * (ord - 1);
1379
+ } else {
1380
+ const lastMatch = lastDay - (lastDayOfWeek - isoDay + 7) % 7;
1381
+ day = lastMatch + 7 * (ord + 1);
1382
+ }
1383
+ if (day >= 1 && day <= lastDay) {
1384
+ byDayHits.add(day);
1385
+ }
1386
+ }
1387
+ }
1388
+ let finalDays = [...byDayHits].sort((a, b) => a - b);
1389
+ if (byMonthDay && byMonthDay.length > 0) {
1390
+ if (byMonthDayHits.length === 0) return [];
1391
+ const byMonthDayHitSet = new Set(byMonthDayHits);
1392
+ finalDays = finalDays.filter((day) => byMonthDayHitSet.has(day));
1393
+ }
1394
+ return finalDays;
1395
+ }
1396
+ generateMonthlyOccurrenceEpochsUtc(year, month) {
1397
+ var _a;
1398
+ const days = this.generateMonthlyOccurrenceDaysUtc(year, month);
1399
+ if (days.length === 0) return [];
1400
+ const monthStartMs = Date.UTC(year, month - 1, 1, 0, 0, 0, 0);
1401
+ const timeSlotOffsets = (_a = this.timeSlotOffsetsMs) != null ? _a : [0];
1402
+ if (this.opts.bySetPos && this.opts.bySetPos.length > 0) {
1403
+ if (timeSlotOffsets.length === 1) {
1404
+ const selectedDays = this.applyBySetPosToSortedList(days).sort((a, b) => a - b);
1405
+ const offset = timeSlotOffsets[0];
1406
+ return selectedDays.map((day) => monthStartMs + (day - 1) * MS_PER_DAY + offset);
1407
+ }
1408
+ const timestamps2 = [];
1409
+ for (const day of days) {
1410
+ const dayBase = monthStartMs + (day - 1) * MS_PER_DAY;
1411
+ for (const offset of timeSlotOffsets) {
1412
+ timestamps2.push(dayBase + offset);
1413
+ }
1414
+ }
1415
+ return this.applyBySetPosToSortedList(timestamps2).sort((a, b) => a - b);
1416
+ }
1417
+ const timestamps = [];
1418
+ for (const day of days) {
1419
+ const dayBase = monthStartMs + (day - 1) * MS_PER_DAY;
1420
+ for (const offset of timeSlotOffsets) {
1421
+ timestamps.push(dayBase + offset);
1422
+ }
1423
+ }
1424
+ return timestamps;
1425
+ }
1426
+ _allUtcMonthlyByDayOrMonthDay() {
1427
+ var _a;
1428
+ const dates = [];
1429
+ const startMilliseconds = this.originalDtstart.epochMilliseconds;
1430
+ const untilMilliseconds = (_a = this.opts.until) == null ? void 0 : _a.epochMilliseconds;
1431
+ let iterationCount = 0;
1432
+ if (!this.addDtstartIfNeeded(dates)) {
1433
+ return dates;
1434
+ }
1435
+ let monthIndex = this.originalDtstart.year * 12 + (this.originalDtstart.month - 1);
1436
+ while (true) {
1437
+ if (++iterationCount > this.maxIterations) {
1438
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1439
+ }
1440
+ const { year, month } = this.monthIndexToYearMonth(monthIndex);
1441
+ const occurrenceEpochs = this.generateMonthlyOccurrenceEpochsUtc(year, month);
1442
+ for (const epochMilliseconds of occurrenceEpochs) {
1443
+ if (epochMilliseconds < startMilliseconds) {
1444
+ continue;
1445
+ }
1446
+ if (untilMilliseconds !== void 0 && epochMilliseconds > untilMilliseconds) {
1447
+ return dates;
1448
+ }
1449
+ dates.push(this.utcZdtFromEpochMilliseconds(epochMilliseconds));
1450
+ if (this.shouldBreakForCountLimit(dates.length)) {
1451
+ return dates;
1452
+ }
1453
+ }
1454
+ monthIndex += this.opts.interval;
1455
+ }
1456
+ }
928
1457
  processOccurrences(occs, dates, start, iterator, extraFilters) {
929
1458
  let shouldBreak = false;
930
1459
  for (const occ of occs) {
@@ -968,8 +1497,11 @@ var _RRuleTemporal = class _RRuleTemporal {
968
1497
  if (++iterationCount > this.maxIterations) {
969
1498
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
970
1499
  }
971
- let occs = this.generateMonthlyOccurrences(monthCursor);
972
- occs = this.applyBySetPos(occs);
1500
+ let occs = this.generateMonthlyOccurrencesOptimizedBySetPos(monthCursor);
1501
+ if (!occs) {
1502
+ occs = this.generateMonthlyOccurrences(monthCursor);
1503
+ occs = this.applyBySetPos(occs);
1504
+ }
973
1505
  const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
974
1506
  if (shouldBreak) {
975
1507
  break;
@@ -979,7 +1511,7 @@ var _RRuleTemporal = class _RRuleTemporal {
979
1511
  return this.applyCountLimitAndMergeRDates(dates, iterator);
980
1512
  }
981
1513
  _allWeekly(iterator) {
982
- var _a, _b;
1514
+ var _a, _b, _c;
983
1515
  const dates = [];
984
1516
  let iterationCount = 0;
985
1517
  const start = this.originalDtstart;
@@ -987,15 +1519,14 @@ var _RRuleTemporal = class _RRuleTemporal {
987
1519
  return this.applyCountLimitAndMergeRDates(dates, iterator);
988
1520
  }
989
1521
  const dayMap = weekdayToIsoDay;
990
- 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]];
991
- const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
1522
+ const dows = this.opts.byDay ? [...(_a = this.allByDayIsoDays) != null ? _a : []] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? [...Object.values(dayMap)] : [start.dayOfWeek];
992
1523
  const firstWeekDates = dows.map((dw) => {
993
1524
  const delta = (dw - start.dayOfWeek + 7) % 7;
994
1525
  return start.add({ days: delta });
995
1526
  });
996
1527
  const firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
997
- const wkstToken = (_a = extractWeekdayToken(this.opts.wkst || "MO")) != null ? _a : "MO";
998
- const wkstDay = (_b = dayMap[wkstToken]) != null ? _b : 1;
1528
+ const wkstToken = (_b = extractWeekdayToken(this.opts.wkst || "MO")) != null ? _b : "MO";
1529
+ const wkstDay = (_c = dayMap[wkstToken]) != null ? _c : 1;
999
1530
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
1000
1531
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
1001
1532
  while (true) {
@@ -1394,6 +1925,10 @@ var _RRuleTemporal = class _RRuleTemporal {
1394
1925
  if (!this.opts.count && !this.opts.until && !iterator) {
1395
1926
  throw new Error("all() requires iterator when no COUNT/UNTIL");
1396
1927
  }
1928
+ const utcFastPathDates = this.allUtcFastPath(iterator);
1929
+ if (utcFastPathDates) {
1930
+ return utcFastPathDates;
1931
+ }
1397
1932
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay) && !this.opts.byWeekNo) {
1398
1933
  return this._allMonthlyByDayOrMonthDay(iterator);
1399
1934
  }
@@ -1797,58 +2332,14 @@ var _RRuleTemporal = class _RRuleTemporal {
1797
2332
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
1798
2333
  */
1799
2334
  generateMonthlyOccurrences(sample) {
1800
- var _a;
1801
- const { byDay, byMonth, byMonthDay } = this.opts;
1802
- if (byMonth && !byMonth.includes(sample.month)) return [];
1803
- const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
1804
- let byMonthDayHits = [];
1805
- if (byMonthDay && byMonthDay.length > 0) {
1806
- byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
1807
- }
1808
- if (!byDay && byMonthDay && byMonthDay.length > 0) {
1809
- if (byMonthDayHits.length === 0) {
1810
- return [];
1811
- }
1812
- const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
1813
- return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1814
- }
1815
- if (!byDay) {
2335
+ const monthStart = sample.day === 1 ? sample : sample.with({ day: 1 });
2336
+ if (!this.opts.byDay && !this.opts.byMonthDay) {
1816
2337
  return this.expandByTime(sample);
1817
2338
  }
1818
- const dayMap = weekdayToIsoDay;
1819
- const tokens = byDay.map((tok) => {
1820
- const parsed = parseByDayToken(tok);
1821
- if (!parsed) return null;
1822
- return { ord: parsed.ord, wd: dayMap[parsed.weekday] };
1823
- }).filter((x) => x !== null);
1824
- const buckets = {};
1825
- let cursor = sample.with({ day: 1 });
1826
- while (cursor.month === sample.month) {
1827
- const dow = cursor.dayOfWeek;
1828
- (buckets[dow] || (buckets[dow] = [])).push(cursor.day);
1829
- cursor = cursor.add({ days: 1 });
1830
- }
1831
- const byDayHits = [];
1832
- for (const { ord, wd } of tokens) {
1833
- const list = (_a = buckets[wd]) != null ? _a : [];
1834
- if (!list.length) continue;
1835
- if (ord === 0) {
1836
- for (const d of list) byDayHits.push(d);
1837
- } else {
1838
- const idx = ord > 0 ? ord - 1 : list.length + ord;
1839
- const dayN = list[idx];
1840
- if (dayN) byDayHits.push(dayN);
1841
- }
1842
- }
1843
- let finalDays = byDayHits;
1844
- if (byMonthDay && byMonthDay.length > 0) {
1845
- if (byMonthDayHits.length === 0) {
1846
- return [];
1847
- }
1848
- finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
1849
- }
1850
- const hits = finalDays.map((d) => sample.with({ day: d }));
1851
- return hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2339
+ const finalDays = this.generateMonthlyOccurrenceDays(monthStart);
2340
+ if (finalDays.length === 0) return [];
2341
+ const hits = finalDays.map((d) => monthStart.with({ day: d }));
2342
+ return hits.flatMap((z) => this.expandByTime(z));
1852
2343
  }
1853
2344
  /**
1854
2345
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -2044,9 +2535,11 @@ var _RRuleTemporal = class _RRuleTemporal {
2044
2535
  return current;
2045
2536
  }
2046
2537
  if (this.opts.byDay && !this.matchesByDay(current)) {
2047
- const dayMap = weekdayToIsoDay;
2048
- const targetDays = this.opts.byDay.map((tok) => extractWeekdayToken(tok)).filter((day) => day !== null).map((day) => dayMap[day]).filter(Boolean);
2049
- const nextDayOfWeek = this.findNextValidValue(current.dayOfWeek, targetDays.sort(), (a, b) => a - b);
2538
+ const targetDays = this.allByDayIsoDays;
2539
+ if (!(targetDays == null ? void 0 : targetDays.length)) {
2540
+ return this.applyTimeOverride(current.add({ days: 1 }).with({ hour: 0, minute: 0, second: 0 }));
2541
+ }
2542
+ const nextDayOfWeek = this.findNextValidValue(current.dayOfWeek, targetDays, (a, b) => a - b);
2050
2543
  if (nextDayOfWeek) {
2051
2544
  const delta = (nextDayOfWeek - current.dayOfWeek + 7) % 7;
2052
2545
  current = current.add({ days: delta }).with({ hour: 0, minute: 0, second: 0 });
@@ -2080,12 +2573,7 @@ var _RRuleTemporal = class _RRuleTemporal {
2080
2573
  const { bySetPos } = this.opts;
2081
2574
  if (!bySetPos || !bySetPos.length) return list;
2082
2575
  const sorted = [...list].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2083
- const out = [];
2084
- const len = sorted.length;
2085
- for (const pos of bySetPos) {
2086
- const idx = pos > 0 ? pos - 1 : len + pos;
2087
- if (idx >= 0 && idx < len) out.push(sorted[idx]);
2088
- }
2576
+ const out = this.applyBySetPosToSortedList(sorted);
2089
2577
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
2090
2578
  }
2091
2579
  isoWeekByDay(sample) {