aboutsummaryrefslogtreecommitdiff
path: root/modules/calendar-sync-recurrence.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-07-20 23:37:31 -0500
committerCraig Jennings <c@cjennings.net>2026-07-20 23:37:31 -0500
commitf131fa4fb16949ba66d4153fde8b9c95fc407f84 (patch)
treebe639a223d4be6d0c99cad6c0787eeed21e54c38 /modules/calendar-sync-recurrence.el
parentc3afc208b538c0a1de0890514e9d362c840c2930 (diff)
downloaddotemacs-f131fa4fb16949ba66d4153fde8b9c95fc407f84.tar.gz
dotemacs-f131fa4fb16949ba66d4153fde8b9c95fc407f84.zip
fix(calendar): skip short months for plain monthly-on-the-31st rules
The stepper kept day-of-month verbatim, so Jan 31 stepped to Feb 31 and encode-time normalized it into a phantom Mar 3 occurrence. Plain monthly rules now advance to the next month that has the day, per RFC 5545.
Diffstat (limited to 'modules/calendar-sync-recurrence.el')
-rw-r--r--modules/calendar-sync-recurrence.el20
1 files changed, 18 insertions, 2 deletions
diff --git a/modules/calendar-sync-recurrence.el b/modules/calendar-sync-recurrence.el
index a3bd4dc4..9ef12ce5 100644
--- a/modules/calendar-sync-recurrence.el
+++ b/modules/calendar-sync-recurrence.el
@@ -498,11 +498,27 @@ DTSTART's month, landing each occurrence on the day its BYDAY rule selects
BASE-EVENT is the event plist, RRULE is parsed rrule, RANGE is date range.
A rule with BYDAY (nth weekday, e.g. 2WE, -1TU, or SU with BYSETPOS)
expands via `calendar-sync--expand-monthly-byday'; a plain rule steps
-DTSTART's day-of-month."
+DTSTART's day-of-month, skipping months without that day."
(if (plist-get rrule :byday)
(calendar-sync--expand-monthly-byday base-event rrule range)
(calendar-sync--expand-simple-recurrence
- base-event rrule range #'calendar-sync--add-months)))
+ base-event rrule range #'calendar-sync--next-monthly-date)))
+
+(defun calendar-sync--next-monthly-date (date interval)
+ "Step DATE forward INTERVAL months, skipping months without DATE's day.
+A plain FREQ=MONTHLY on the 31st must skip short months (RFC 5545):
+`calendar-sync--add-months' keeps day-of-month verbatim, so Jan 31 would
+step to Feb 31, which encode-time normalizes into a phantom Mar 3
+occurrence. Bounded so a pathological input can't loop forever."
+ (require 'time-date)
+ (let ((next (calendar-sync--add-months date interval))
+ (day (nth 2 date))
+ (guard 0))
+ (while (and (< guard 100)
+ (> day (date-days-in-month (nth 0 next) (nth 1 next))))
+ (setq guard (1+ guard))
+ (setq next (calendar-sync--add-months next interval)))
+ next))
(defun calendar-sync--expand-yearly-byday (base-event rrule range)
"Expand a yearly nth-weekday event (e.g. BYMONTH=3;BYDAY=2SU).