rrule-temporal 0.1.6 → 0.2.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/README.md CHANGED
@@ -1,10 +1,13 @@
1
1
  # rrule-temporal
2
2
 
3
- Recurrence rule (RFC 5545) processing using Temporal PlainDate/PlainDateTime.
3
+ Recurrence rule (RFC 5545) processing built on the Temporal API.
4
+ The library accepts the familiar `RRULE` format and returns
5
+ `Temporal.ZonedDateTime` instances for easy time‑zone aware scheduling.
4
6
 
5
- This was created to advance the rrule library to use Temporal, and to provide a more modern API, as the original rrule library is [not maintained anymore](https://github.com/jkbrzt/rrule/issues/615). Maintainers suggested to use Temporal instead of Date:
7
+ See the [demo site](https://ggaabe.github.io/rrule-temporal/) for an interactive playground.
6
8
 
7
- https://github.com/jkbrzt/rrule/issues/450#issuecomment-1055853095
9
+ > This was created to advance the rrule library to use Temporal, and to provide a more modern API, as the original rrule library is [not maintained anymore](https://github.com/jkbrzt/rrule/issues/615). Maintainers suggested to use Temporal instead of Date:
10
+ >https://github.com/jkbrzt/rrule/issues/450#issuecomment-1055853095
8
11
 
9
12
  ## Installation
10
13
 
@@ -12,192 +15,106 @@ https://github.com/jkbrzt/rrule/issues/450#issuecomment-1055853095
12
15
  npm install rrule-temporal
13
16
  ```
14
17
 
15
- ## Usage
18
+ ## Quick start
16
19
 
17
- ## 1. Parsing an ICS snippet
18
-
19
- Parse a full DTSTART/RRULE snippet and reproduce it or enumerate all occurrences:
20
-
21
- ```typescript
22
- // Daily at 17:00 America/Chicago, 5 total occurrences
23
- const ics = `
24
- DTSTART;TZID=America/Chicago:20250320T170000
25
- RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
26
- `.trim();
27
-
28
- const rule = new RRuleTemporal({ rruleString: ics });
29
-
30
- // toString() outputs the original DTSTART and RRULE line:
31
- console.log(rule.toString());
32
- /*
33
- DTSTART;TZID=America/Chicago:20250320T170000
34
- RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
35
- */
36
-
37
- // all() returns an array of Temporal.ZonedDateTime:
38
- const dates = rule.all();
39
- dates.forEach(dt => console.log(dt.toString()));
40
- // [
41
- // "2025-03-20T17:00:00[America/Chicago]",
42
- // "2025-03-21T17:00:00[America/Chicago]",
43
- // // … total of 5 days
44
- // ]
45
- ```
46
-
47
- ## 2. Enumerate occurrences in a time window
20
+ Parse an ICS snippet and enumerate the occurrences:
48
21
 
49
22
  ```typescript
23
+ import { RRuleTemporal } from "rrule-temporal";
50
24
 
51
- const ics = `DTSTART;TZID=America/Chicago:20250401T000000
52
- RRULE:FREQ=DAILY;BYHOUR=0;BYMINUTE=0;UNTIL=20250405T000000Z`.trim();
53
- const rule = new RRuleTemporal({ rruleString: ics });
54
- const start = new Date(Date.UTC(2025, 3, 2, 0, 0)); // Apr 2 00:00 UTC
55
- // after:
56
- const end = new Date(
57
- // 2025-04-04 00:00 America/Chicago → 05:00 UTC
58
- Date.UTC(2025, 3, 4, 5, 0, 0)
59
- );
60
-
61
- test("between returns occurrences in window inclusive/exclusive", () => {
62
- const arrExc = rule.between(start, end, false);
63
- expect(arrExc.map((d) => d.day)).toEqual([2, 3]);
25
+ const rule = new RRuleTemporal({
26
+ rruleString: `DTSTART;TZID=UTC:20250101T090000\nRRULE:FREQ=DAILY;COUNT=3`
27
+ });
64
28
 
65
- const arrInc = rule.between(start, end, true);
66
- expect(arrInc.map((d) => d.day)).toEqual([2, 3, 4]);
67
- });
68
- ```
29
+ rule.all().forEach(dt => console.log(dt.toString()));
30
+ // 2025-01-01T09:00:00[UTC]
31
+ // 2025-01-02T09:00:00[UTC]
32
+ // 2025-01-03T09:00:00[UTC]
33
+ ```
69
34
 
70
- ## 3. Manual options
35
+ ## Creating a rule with options
71
36
 
72
- Build a rule from explicit options, overriding start time and interval:
37
+ Instead of a full ICS string you can supply the recurrence parameters directly:
73
38
 
74
39
  ```typescript
75
- // Every 2 days at 09:15 America/Chicago, 3 occurrences, starting 2025-04-20 08:30 CT
76
- const dtstart = Temporal.ZonedDateTime.from({
77
- year: 2025, month: 4, day: 20,
78
- hour: 8, minute: 30,
79
- timeZone: "America/Chicago"
80
- });
40
+ import { Temporal } from "@js-temporal/polyfill";
81
41
 
82
42
  const rule = new RRuleTemporal({
83
- freq: "DAILY",
43
+ freq: "DAILY",
84
44
  interval: 2,
85
- count: 3,
86
- byHour: [9],
45
+ count: 3,
46
+ byHour: [9],
87
47
  byMinute: [15],
88
- dtstart,
89
- tzid: "America/Chicago",
48
+ tzid: "America/Chicago",
49
+ dtstart: Temporal.ZonedDateTime.from({
50
+ year: 2025, month: 4, day: 20,
51
+ hour: 8, minute: 30,
52
+ timeZone: "America/Chicago"
53
+ })
90
54
  });
91
55
 
92
- rule.all().forEach((dt, i) => {
93
- console.log(i, dt.toString());
94
- });
95
- // 0 "2025-04-20T09:15:00[America/Chicago]"
96
- // 1 "2025-04-22T09:15:00[America/Chicago]"
97
- // 2 "2025-04-24T09:15:00[America/Chicago]"
56
+ rule.all().forEach(dt => console.log(dt.toString()));
98
57
  ```
99
58
 
100
- ## 4. Windowed queries with between()
59
+ ## Querying occurrences
101
60
 
102
- Fetch occurrences in a given time window, with inclusive/exclusive end:
103
- ```typescript
104
- // Daily at 00:00 CT until 2025-04-05T00:00Z inclusive
105
- const ics2 = `
106
- DTSTART;TZID=America/Chicago:20250401T000000
107
- RRULE:FREQ=DAILY;BYHOUR=0;BYMINUTE=0;UNTIL=20250405T000000Z
108
- `.trim();
109
-
110
- const rule2 = new RRuleTemporal({ rruleString: ics2 });
61
+ Use the provided methods to enumerate or search for occurrences:
111
62
 
112
- // Define UTC window: from 2025-04-02 00:00 UTC through 2025-04-04 05:00 UTC
63
+ ```typescript
64
+ // Get all events within a window
113
65
  const start = new Date(Date.UTC(2025, 3, 2, 0, 0));
114
- const end = new Date(Date.UTC(2025, 3, 4, 5, 0));
66
+ const end = new Date(Date.UTC(2025, 3, 4, 5, 0));
67
+ const hits = rule.between(start, end, true);
115
68
 
116
- console.log(rule2.between(start, end, false).map(d => d.toString()));
117
- // [
118
- // "2025-04-02T00:00:00[America/Chicago]",
119
- // "2025-04-03T00:00:00[America/Chicago]"
120
- // ]
121
-
122
- console.log(rule2.between(start, end, true).map(d => d.toString()));
123
- // adds "2025-04-04T00:00:00[America/Chicago]"
69
+ // Next and previous occurrences
70
+ const next = rule.next();
71
+ const prev = rule.previous(new Date("2025-05-01T00:00Z"));
124
72
  ```
125
73
 
126
- ## 5. Next/previous occurrence
127
-
128
- Find the next or previous occurrence relative to any date:
74
+ ## Converting back to text
129
75
 
130
76
  ```typescript
131
- // Monthly at 12:00 America/Chicago, 12 total
132
- const ics3 = `
133
- DTSTART;TZID=America/Chicago:20250101T120000
134
- RRULE:FREQ=MONTHLY;BYHOUR=12;BYMINUTE=0;COUNT=12
135
- `.trim();
136
-
137
- const rule3 = new RRuleTemporal({ rruleString: ics3 });
138
-
139
- // Next occurrence after the current time in Central Time
140
- // Where hour and minute conditions are met
141
- const firstOccurrence = rule.next(
142
- Temporal.Now.zonedDateTimeISO("America/Chicago")
143
- );
144
-
145
- // Next after 2025-03-15T00:00:00Z
146
- const nxt = rule3.next(new Date("2025-03-15T00:00:00Z"));
147
- console.log(nxt?.toString()); // "2025-04-01T12:00:00[UTC]"
148
-
149
- // Previous on or before 2025-06-05T00:00:00Z
150
- const prev = rule3.previous(new Date("2025-06-05T00:00:00Z"), true);
151
- console.log(prev?.toString()); // "2025-06-01T12:00:00[UTC]"
77
+ rule.toString(); // DTSTART and RRULE lines
78
+ rule.toText(); // human readable description
152
79
  ```
153
80
 
154
- ## 6. toString()
81
+ ## API
155
82
 
156
- Convert the rule back to an ICS string:
83
+ | Method | Description |
84
+ | ------ | ----------- |
85
+ | `new RRuleTemporal(opts)` | Create a rule from an ICS snippet or manual options. |
86
+ | `all(iterator?)` | Return every occurrence. When the rule has no end the optional iterator is required. |
87
+ | `between(after, before, inclusive?)` | Occurrences within a time range. |
88
+ | `next(after?, inclusive?)` | Next occurrence after a given date. |
89
+ | `previous(before?, inclusive?)` | Previous occurrence before a date. |
90
+ | `toString()` | Convert the rule back into `DTSTART` and `RRULE` lines. |
91
+ | `toText(formatter?)` | English description of the rule. |
92
+ | `options()` | Return the normalized options object. |
157
93
 
158
- ```typescript
159
- console.log(rule.toString());
160
- // DTSTART;TZID=America/Chicago:20250320T170000
161
- // RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
162
- ```
94
+ ## Further examples
163
95
 
164
- ## 7. BYDAY/BYMONTH examples:
96
+ Enumerating weekdays within a month or rotating through months can be achieved
97
+ with the more advanced RFC 5545 fields:
165
98
 
166
99
  ```typescript
167
- // 2nd & 4th Fridays each month at 00:00 CT, first 6 occurrences
168
- const ics4 = `
169
- DTSTART;TZID=America/Chicago:20250325T000000
170
- RRULE:FREQ=MONTHLY;BYDAY=2FR,4FR;BYHOUR=0;BYMINUTE=0;COUNT=6
171
- `.trim();
172
-
173
- const rule4 = new RRuleTemporal({ rruleString: ics4 });
174
- rule4.all().forEach(dt => console.log(dt.toString()));
175
- // [
176
- // "2025-04-11T00:00:00[America/Chicago]",
177
- // "2025-04-25T00:00:00[America/Chicago]",
178
- // "2025-05-09T00:00:00[America/Chicago]",
179
- // …
180
- ]
181
-
182
- // Yearly rotated through Jan, Jun, Dec at 09:00 UTC, 4 occurrences
183
- const dtstart5 = Temporal.ZonedDateTime.from({
100
+ // 2nd & 4th Fridays each month at midnight CT, first 6 occurrences
101
+ const ruleA = new RRuleTemporal({
102
+ rruleString: `DTSTART;TZID=America/Chicago:20250325T000000\nRRULE:FREQ=MONTHLY;BYDAY=2FR,4FR;BYHOUR=0;BYMINUTE=0;COUNT=6`
103
+ });
104
+ ruleA.all().forEach(dt => console.log(dt.toString()));
105
+
106
+ // Rotate yearly through Jan, Jun and Dec at 09:00 UTC
107
+ const dtstart = Temporal.ZonedDateTime.from({
184
108
  year: 2025, month: 1, day: 10, hour: 9, minute: 0, timeZone: "UTC"
185
109
  });
186
- const rule5 = new RRuleTemporal({
187
- freq: "YEARLY",
110
+ const ruleB = new RRuleTemporal({
111
+ freq: "YEARLY",
188
112
  interval: 1,
189
- count: 4,
190
- byMonth: [1, 6, 12],
191
- byHour: [9],
113
+ count: 4,
114
+ byMonth: [1, 6, 12],
115
+ byHour: [9],
192
116
  byMinute: [0],
193
- dtstart: dtstart5
117
+ dtstart
194
118
  });
195
- rule5.all().forEach(dt => console.log(dt.toString()));
196
- // [
197
- // "2025-01-10T09:00:00[UTC]",
198
- // "2026-06-10T09:00:00[UTC]",
199
- // "2027-12-10T09:00:00[UTC]",
200
- // "2028-01-10T09:00:00[UTC]"
201
- // ]
119
+ ruleB.all().forEach(dt => console.log(dt.toString()));
202
120
  ```
203
-
package/dist/index.cjs CHANGED
@@ -24,6 +24,60 @@ __export(index_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(index_exports);
26
26
  var import_polyfill = require("@js-temporal/polyfill");
27
+ var WEEKDAY_NAMES = [
28
+ "Monday",
29
+ "Tuesday",
30
+ "Wednesday",
31
+ "Thursday",
32
+ "Friday",
33
+ "Saturday",
34
+ "Sunday"
35
+ ];
36
+ var MONTH_NAMES = [
37
+ "January",
38
+ "February",
39
+ "March",
40
+ "April",
41
+ "May",
42
+ "June",
43
+ "July",
44
+ "August",
45
+ "September",
46
+ "October",
47
+ "November",
48
+ "December"
49
+ ];
50
+ var defaultDateFormatter = (year, month, day) => `${month} ${day}, ${year}`;
51
+ function ordinal(n) {
52
+ const abs = Math.abs(n);
53
+ const suffix = abs % 10 === 1 && abs % 100 !== 11 ? "st" : abs % 10 === 2 && abs % 100 !== 12 ? "nd" : abs % 10 === 3 && abs % 100 !== 13 ? "rd" : "th";
54
+ return n < 0 ? `last` : `${abs}${suffix}`;
55
+ }
56
+ function list(arr, mapFn = (x) => `${x}`, final = "and") {
57
+ const mapped = arr.map(mapFn);
58
+ if (mapped.length === 1) return mapped[0];
59
+ return mapped.slice(0, -1).join(", ") + ` ${final} ` + mapped[mapped.length - 1];
60
+ }
61
+ function formatByDayToken(tok) {
62
+ if (typeof tok === "number") return tok.toString();
63
+ const m = tok.match(/^([+-]?\d+)?(MO|TU|WE|TH|FR|SA|SU)$/);
64
+ if (!m) return tok;
65
+ const ord = m[1] ? parseInt(m[1], 10) : 0;
66
+ const weekdayMap = {
67
+ MO: 0,
68
+ TU: 1,
69
+ WE: 2,
70
+ TH: 3,
71
+ FR: 4,
72
+ SA: 5,
73
+ SU: 6
74
+ };
75
+ const idx = weekdayMap[m[2]];
76
+ const name = WEEKDAY_NAMES[idx];
77
+ if (ord === 0) return name;
78
+ if (ord === -1) return `last ${name}`;
79
+ return `${ordinal(ord)} ${name}`;
80
+ }
27
81
  function parseRRuleString(input, fallbackDtstart) {
28
82
  let dtstart;
29
83
  let tzid = "UTC";
@@ -83,10 +137,30 @@ function parseRRuleString(input, fallbackDtstart) {
83
137
  case "BYMONTH":
84
138
  opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
85
139
  break;
140
+ case "BYMONTHDAY":
141
+ opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
142
+ break;
86
143
  }
87
144
  }
88
145
  return opts;
89
146
  }
147
+ function formatTime(hour, minute = 0) {
148
+ const hr12 = (hour + 11) % 12 + 1;
149
+ const ampm = hour < 12 ? "AM" : "PM";
150
+ if (minute) {
151
+ return `${hr12}:${String(minute).padStart(2, "0")} ${ampm}`;
152
+ }
153
+ return `${hr12} ${ampm}`;
154
+ }
155
+ function tzAbbreviation(zdt) {
156
+ const parts = new Intl.DateTimeFormat("en-US", {
157
+ timeZone: zdt.timeZoneId,
158
+ timeZoneName: "short",
159
+ hour: "numeric"
160
+ }).formatToParts(new Date(zdt.epochMilliseconds));
161
+ const tzPart = parts.find((p) => p.type === "timeZoneName");
162
+ return tzPart?.value || zdt.timeZoneId;
163
+ }
90
164
  var RRuleTemporal = class {
91
165
  tzid;
92
166
  originalDtstart;
@@ -239,7 +313,6 @@ var RRuleTemporal = class {
239
313
  if (zdt.dayOfWeek === wd) return true;
240
314
  continue;
241
315
  }
242
- const year = zdt.year;
243
316
  const month = zdt.month;
244
317
  let dt = zdt.with({ day: 1 });
245
318
  const candidates = [];
@@ -257,8 +330,16 @@ var RRuleTemporal = class {
257
330
  if (!byMonth) return true;
258
331
  return byMonth.includes(zdt.month);
259
332
  }
333
+ matchesByMonthDay(zdt) {
334
+ const { byMonthDay } = this.opts;
335
+ if (!byMonthDay) return true;
336
+ const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
337
+ return byMonthDay.some(
338
+ (d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1
339
+ );
340
+ }
260
341
  matchesAll(zdt) {
261
- return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
342
+ return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt);
262
343
  }
263
344
  options() {
264
345
  return this.opts;
@@ -273,7 +354,7 @@ var RRuleTemporal = class {
273
354
  throw new Error("all() requires iterator when no COUNT/UNTIL");
274
355
  }
275
356
  const dates = [];
276
- if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
357
+ if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
277
358
  const start = this.originalDtstart;
278
359
  let monthCursor = start.with({ day: 1 });
279
360
  let matchCount2 = 0;
@@ -347,7 +428,7 @@ var RRuleTemporal = class {
347
428
  }
348
429
  return dates;
349
430
  }
350
- if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
431
+ if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
351
432
  const start = this.originalDtstart;
352
433
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
353
434
  let i = 0;
@@ -374,6 +455,30 @@ var RRuleTemporal = class {
374
455
  }
375
456
  return dates;
376
457
  }
458
+ if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
459
+ const start = this.originalDtstart;
460
+ let yearCursor = start.with({ month: 1, day: 1 });
461
+ let matchCount2 = 0;
462
+ outer_year: while (true) {
463
+ const occs = this.generateYearlyOccurrences(yearCursor);
464
+ for (const occ of occs) {
465
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
466
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
467
+ break outer_year;
468
+ }
469
+ if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
470
+ break outer_year;
471
+ }
472
+ if (iterator && !iterator(occ, matchCount2)) {
473
+ break outer_year;
474
+ }
475
+ dates.push(occ);
476
+ matchCount2++;
477
+ }
478
+ yearCursor = yearCursor.add({ years: this.opts.interval });
479
+ }
480
+ return dates;
481
+ }
377
482
  let current = this.computeFirst();
378
483
  let matchCount = 0;
379
484
  while (true) {
@@ -405,7 +510,7 @@ var RRuleTemporal = class {
405
510
  const startInst = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
406
511
  const endInst = before instanceof Date ? import_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
407
512
  const results = [];
408
- if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
513
+ if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
409
514
  let monthCursor = this.computeFirst().with({ day: 1 });
410
515
  outer: while (true) {
411
516
  const occs = this.generateMonthlyOccurrences(monthCursor);
@@ -422,7 +527,7 @@ var RRuleTemporal = class {
422
527
  }
423
528
  return results;
424
529
  }
425
- if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
530
+ if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
426
531
  const start = this.originalDtstart;
427
532
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
428
533
  let i = 0;
@@ -442,6 +547,24 @@ var RRuleTemporal = class {
442
547
  }
443
548
  return results;
444
549
  }
550
+ if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
551
+ const start = this.originalDtstart;
552
+ let yearCursor = start.with({ month: 1, day: 1 });
553
+ outer_year: while (true) {
554
+ const occs = this.generateYearlyOccurrences(yearCursor);
555
+ for (const occ of occs) {
556
+ const inst = occ.toInstant();
557
+ if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
558
+ break outer_year;
559
+ }
560
+ if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0) {
561
+ results.push(occ);
562
+ }
563
+ }
564
+ yearCursor = yearCursor.add({ years: this.opts.interval });
565
+ }
566
+ return results;
567
+ }
445
568
  if (this.opts.freq === "WEEKLY") {
446
569
  const startInst2 = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
447
570
  const endInst2 = before instanceof Date ? import_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
@@ -532,24 +655,123 @@ var RRuleTemporal = class {
532
655
  const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
533
656
  const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
534
657
  const parts = [];
535
- const { freq, interval, count, byHour, byMinute, byDay, byMonth } = this.opts;
658
+ const {
659
+ freq,
660
+ interval,
661
+ count,
662
+ until,
663
+ byHour,
664
+ byMinute,
665
+ byDay,
666
+ byMonth,
667
+ byMonthDay
668
+ } = this.opts;
536
669
  parts.push(`FREQ=${freq}`);
537
670
  if (interval !== 1) parts.push(`INTERVAL=${interval}`);
538
671
  if (count !== void 0) parts.push(`COUNT=${count}`);
672
+ if (until) {
673
+ const u = until.toInstant().toString().replace(/[-:]/g, "");
674
+ parts.push(`UNTIL=${u.slice(0, 15)}Z`);
675
+ }
539
676
  if (byHour) parts.push(`BYHOUR=${byHour.join(",")}`);
540
677
  if (byMinute) parts.push(`BYMINUTE=${byMinute.join(",")}`);
541
678
  if (byDay) parts.push(`BYDAY=${byDay.join(",")}`);
542
679
  if (byMonth) parts.push(`BYMONTH=${byMonth.join(",")}`);
680
+ if (byMonthDay) parts.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
543
681
  return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
544
682
  }
683
+ /**
684
+ * Convert this rule to a human readable English description.
685
+ */
686
+ toText(formatter = defaultDateFormatter) {
687
+ const {
688
+ freq,
689
+ interval = 1,
690
+ count,
691
+ until,
692
+ byDay,
693
+ byHour,
694
+ byMinute,
695
+ byMonth,
696
+ byMonthDay
697
+ } = this.opts;
698
+ const parts = ["every"];
699
+ const base = {
700
+ YEARLY: "year",
701
+ MONTHLY: "month",
702
+ WEEKLY: "week",
703
+ DAILY: "day",
704
+ HOURLY: "hour",
705
+ MINUTELY: "minute",
706
+ SECONDLY: "second"
707
+ }[freq];
708
+ const daysNormalized = byDay?.map((d) => d.toUpperCase());
709
+ const isWeekdays = daysNormalized && daysNormalized.length === 5 && ["MO", "TU", "WE", "TH", "FR"].every((d) => daysNormalized.includes(d));
710
+ const isEveryday = daysNormalized && daysNormalized.length === 7 && ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].every(
711
+ (d) => daysNormalized.includes(d)
712
+ );
713
+ if (freq === "WEEKLY" && interval === 1 && isWeekdays) {
714
+ parts.push("weekday");
715
+ } else if (freq === "WEEKLY" && interval === 1 && isEveryday) {
716
+ parts.push("day");
717
+ } else {
718
+ if (interval !== 1) {
719
+ parts.push(interval.toString(), base + "s");
720
+ } else {
721
+ parts.push(base);
722
+ }
723
+ }
724
+ if (freq === "WEEKLY" && byDay && !isWeekdays && !isEveryday) {
725
+ parts.push("on", list(byDay, formatByDayToken));
726
+ } else if (byDay && freq !== "WEEKLY") {
727
+ parts.push("on", list(byDay, formatByDayToken));
728
+ }
729
+ if (byMonth) {
730
+ parts.push(
731
+ "in",
732
+ list(byMonth, (m) => MONTH_NAMES[m - 1])
733
+ );
734
+ }
735
+ if (byMonthDay) {
736
+ parts.push(
737
+ "on the",
738
+ list(byMonthDay, (d) => ordinal(d)),
739
+ "day of the month"
740
+ );
741
+ }
742
+ if (byHour) {
743
+ const minutes = byMinute ?? [0];
744
+ const times = byHour.flatMap((h) => minutes.map((m) => formatTime(h, m)));
745
+ parts.push("at", list(times));
746
+ parts.push(tzAbbreviation(this.originalDtstart));
747
+ }
748
+ if (until) {
749
+ parts.push(
750
+ "until",
751
+ formatter(until.year, MONTH_NAMES[until.month - 1], until.day)
752
+ );
753
+ } else if (count !== void 0) {
754
+ parts.push("for", count.toString(), count === 1 ? "time" : "times");
755
+ }
756
+ return parts.join(" ");
757
+ }
545
758
  /**
546
759
  * Given any date in a month, return all the ZonedDateTimes in that month
547
760
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
548
761
  */
549
762
  generateMonthlyOccurrences(sample) {
550
- const { byDay, byMonth, tzid } = this.opts;
763
+ const { byDay, byMonth, byMonthDay } = this.opts;
551
764
  if (byMonth && !byMonth.includes(sample.month)) return [];
552
- if (!byDay || byDay.length === 0) {
765
+ const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
766
+ let byMonthDayHits = [];
767
+ if (byMonthDay && byMonthDay.length > 0) {
768
+ byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
769
+ }
770
+ if (!byDay && byMonthDayHits.length) {
771
+ const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
772
+ return dates.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
773
+ }
774
+ if (!byDay) {
553
775
  return this.expandByHourMinute(sample);
554
776
  }
555
777
  const dayMap = {
@@ -573,20 +795,38 @@ var RRuleTemporal = class {
573
795
  (buckets[dow] ||= []).push(cursor.day);
574
796
  cursor = cursor.add({ days: 1 });
575
797
  }
576
- const hits = [];
798
+ const byDayHits = [];
577
799
  for (const { ord, wd } of tokens) {
578
- const list = buckets[wd] ?? [];
579
- if (!list.length) continue;
800
+ const list2 = buckets[wd] ?? [];
801
+ if (!list2.length) continue;
580
802
  if (ord === 0) {
581
- for (const d of list) hits.push(sample.with({ day: d }));
803
+ for (const d of list2) byDayHits.push(d);
582
804
  } else {
583
- const idx = ord > 0 ? ord - 1 : list.length + ord;
584
- const dayN = list[idx];
585
- if (dayN) hits.push(sample.with({ day: dayN }));
805
+ const idx = ord > 0 ? ord - 1 : list2.length + ord;
806
+ const dayN = list2[idx];
807
+ if (dayN) byDayHits.push(dayN);
586
808
  }
587
809
  }
810
+ let finalDays = byDayHits;
811
+ if (byMonthDayHits.length) {
812
+ finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
813
+ }
814
+ const hits = finalDays.map((d) => sample.with({ day: d }));
588
815
  return hits.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
589
816
  }
817
+ /**
818
+ * Given any date in a year, return all ZonedDateTimes in that year matching
819
+ * the BYDAY/BYMONTHDAY/BYMONTH constraints. Months default to DTSTART's month
820
+ * if BYMONTH is not specified.
821
+ */
822
+ generateYearlyOccurrences(sample) {
823
+ const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
824
+ const occs = months.flatMap((m) => {
825
+ const monthSample = sample.with({ month: m, day: 1 });
826
+ return this.generateMonthlyOccurrences(monthSample);
827
+ });
828
+ return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
829
+ }
590
830
  };
591
831
  // Annotate the CommonJS export names for ESM import in node:
592
832
  0 && (module.exports = {