From 4ef7820b60efd6b3c7fadc19d7e37a56a7f5e127 Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Mon, 30 Nov 2026 10:25:38 -0600 Subject: [PATCH] fix(calendar): preserve repeated-hour instants in ICS exports (#2782) --- deeptutor/services/calendar/ics.py | 11 +++++++---- tests/services/test_calendar_ics.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/deeptutor/services/calendar/ics.py b/deeptutor/services/calendar/ics.py index e34a57588..525e535c1 100644 --- a/deeptutor/services/calendar/ics.py +++ b/deeptutor/services/calendar/ics.py @@ -1,7 +1,8 @@ """Render stored calendar events as RFC 5545 iCalendar text. Timed events use local wall time with ``TZID`` and a ``VTIMEZONE`` per used -zone. Offsets come from ``zoneinfo`` transitions in a window covering the +zone. A non-recurring event that uses the second occurrence of a repeated +wall time is written in UTC to preserve the intended instant. Offsets come from ``zoneinfo`` transitions in a window covering the exported events (padded by a year; spans over 40 years are rejected). UTC timed events use the ``Z`` form and skip ``VTIMEZONE``. All-day events use ``VALUE=DATE`` with the store's exclusive end date. @@ -165,10 +166,12 @@ def _dtstart_dtend(event: CalendarEvent) -> list[str]: _prop("DTSTART", _ics_date(timing.dtstart), VALUE="DATE"), _prop("DTEND", _ics_date(timing.dtend), VALUE="DATE"), ] - if event.tzid == "UTC": + # RFC 5545 local times in a repeated hour always select the first + # occurrence. UTC preserves both endpoints without changing a repeat rule. + if event.tzid == "UTC" or (not event.rrule and (timing.start_fold or timing.end_fold)): return [ - _prop("DTSTART", f"{_ics_datetime(timing.dtstart)}Z"), - _prop("DTEND", f"{_ics_datetime(timing.dtend)}Z"), + _prop("DTSTART", _stamp(event.start)), + _prop("DTEND", _stamp(event.end)), ] return [ _prop("DTSTART", _ics_datetime(timing.dtstart), TZID=event.tzid), diff --git a/tests/services/test_calendar_ics.py b/tests/services/test_calendar_ics.py index a33769f09..d679d9473 100644 --- a/tests/services/test_calendar_ics.py +++ b/tests/services/test_calendar_ics.py @@ -277,3 +277,19 @@ def test_all_day_events_do_not_widen_the_timezone_window(store): onsets = [line.split(":", 1)[1] for line in zone_lines if line.startswith("DTSTART:")] assert min(onsets) >= "2025" assert "20260329T020000" in onsets + + +@pytest.mark.parametrize( + ("start", "end", "ics_start", "ics_end"), + [ + ("2026-11-01T06:30:00Z", "2026-11-01T07:00:00Z", "20261101T063000Z", "20261101T070000Z"), + ("2026-11-01T05:30:00Z", "2026-11-01T06:30:00Z", "20261101T053000Z", "20261101T063000Z"), + ], +) +def test_single_event_export_preserves_the_second_occurrence_of_a_wall_time( + store, start, end, ics_start, ics_end +): + create(store, tzid="America/New_York", start=start, end=end) + block = vevents(ics(store))[0] + assert block["DTSTART"] == [ics_start] + assert block["DTEND"] == [ics_end]