Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(calendar): keep recurring events with an end date at their local time
Claude45184c896ede
diff --git a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts b/apps/client/src/widgets/collections/calendar/event_builder.spec.tsindex 2ae7559dc9..35770f3745 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.spec.ts@@ -327,7 +327,7 @@ describe("Recurrence", () => {expect(events).toHaveLength(2);// Said outright rather than left to FullCalendar to guess: the rrule plugin reads the whole- // of the rule for a time, and a `UNTIL=…T235959Z` — which the recurrence editor writes —+ // of the rule for a time, and a `UNTIL=…T235959` — which the recurrence editor writes —// makes it guess a timed event however the DTSTART is written.expect(events[0].allDay).toBe(true);expect(events[1].allDay).toBe(false);@@ -396,15 +396,7 @@ describe("Recurrence", () => {});describe("in a time zone ahead of UTC", () => {- const originalTimeZone = process.env.TZ;-- beforeAll(() => {- process.env.TZ = "Europe/Bucharest";- });-- afterAll(() => {- process.env.TZ = originalTimeZone;- });+ useTimeZone("Europe/Bucharest");it.each([[ "without an end date", "RRULE:FREQ=WEEKLY;BYDAY=SU" ],@@ -429,6 +421,26 @@ describe("Recurrence", () => {expect(occurrences.every(({ end }) => end.endsWith(" 13:00"))).toBe(true);});});++ describe("in a time zone behind UTC", () => {+ useTimeZone("America/New_York");++ it.each([+ [ "without an end date", "RRULE:FREQ=WEEKLY;BYDAY=SU" ],+ [ "with an end date", "RRULE:FREQ=WEEKLY;BYDAY=SU;UNTIL=20261130T235959Z" ]+ ])("shows a whole-day occurrence on its own day %s", async (_, recurrence) => {+ const noteIds = buildNotes([+ { title: "Weekly", "#startDate": "2026-10-04", "#recurrence": recurrence }+ ]);+ const events = await buildEvents(noteIds);++ const occurrences = renderOccurrences(events, "2026-10-01");+ expect(occurrences.map(({ start }) => start)).toEqual([+ "2026-10-04 00:00", "2026-10-11 00:00", "2026-10-18 00:00", "2026-10-25 00:00",+ "2026-11-01 00:00", "2026-11-08 00:00"+ ]);+ });+ });});/**@@ -454,6 +466,17 @@ function renderOccurrences(events: EventInput[], date: string) {return occurrences;}+/** Runs the specs of the enclosing `describe` with the local time zone set to `timeZone`. */+function useTimeZone(timeZone: string) {+ const originalTimeZone = process.env.TZ;+ beforeAll(() => {+ process.env.TZ = timeZone;+ });+ afterAll(() => {+ process.env.TZ = originalTimeZone;+ });+}+function formatLocal(date: Date | null) {if (!date) return "";const pad = (value: number) => String(value).padStart(2, "0");diff --git a/apps/client/src/widgets/collections/calendar/event_builder.ts b/apps/client/src/widgets/collections/calendar/event_builder.tsindex cec3ef745d..7e704dd45e 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.ts@@ -109,7 +109,7 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, e// An event with no start time takes the whole day, which is how the editor writes one too (see// EventDatesEditor). When the event repeats this has to be said outright rather than left to// FullCalendar's rrule plugin to infer from the rule: the plugin reads the whole of the rule- // looking for a time, so an `UNTIL=…T235959Z` — which the recurrence editor writes for an end+ // looking for a time, so an `UNTIL=…T235959` — which the recurrence editor writes for an end// date (see recurrence.ts) — makes it take the event for a timed one whatever its DTSTART says.const allDay = !startTime;@@ -156,7 +156,8 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, e// invented for hours the event does not have. `DTSTART;VALUE=DATE:` — the way iCalendar// spells the same thing — is not an option: the rrule library drops a DTSTART written// that way without a word and starts the series from today instead.- const rruleString = `DTSTART:${dayjs(startDate).format(allDay ? "YYYYMMDD" : "YYYYMMDD[T]HHmmss")}\n${recurrence}`;+ const dtStart = dayjs(startDate).format(allDay ? "YYYYMMDD" : "YYYYMMDD[T]HHmmss");+ const rruleString = `DTSTART:${dtStart}\n${toLocalUntil(recurrence)}`;// Validate rrule stringlet rruleValid = true;@@ -181,6 +182,15 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, ereturn events;}+/**+ * Rewrites a UTC `UNTIL` (`UNTIL=20261130T235959Z`) as local time, like the DTSTART that+ * `buildEvent()` writes. FullCalendar's rrule plugin reads the whole rule as UTC when any of its+ * dates carries a zone, which moves every occurrence by the local UTC offset.+ */+function toLocalUntil(recurrence: string) {+ return recurrence.replace(/\b(UNTIL=\d{8}T\d{6})Z\b/gi, "$1");+}+/*** How long one occurrence of a recurring event lasts, as the duration object FullCalendar builds* its own from rather than as an `HH:mm` string. A string of hours and minutes has nowhere to putdiff --git a/apps/client/src/widgets/collections/calendar/recurrence.spec.ts b/apps/client/src/widgets/collections/calendar/recurrence.spec.tsindex 9f18c1f97d..9010755f14 100644--- a/apps/client/src/widgets/collections/calendar/recurrence.spec.ts+++ b/apps/client/src/widgets/collections/calendar/recurrence.spec.ts@@ -93,7 +93,7 @@ describe("serializeRecurrence", () => {expect(serialize({ frequency: "WEEKLY", weekdays: [ "FR", "MO" ] })).toBe("RRULE:FREQ=WEEKLY;BYDAY=MO,FR");expect(serialize({ frequency: "DAILY", ends: { type: "until", date: "2026-06-05" } }))- .toBe("RRULE:FREQ=DAILY;UNTIL=20260605T235959Z");+ .toBe("RRULE:FREQ=DAILY;UNTIL=20260605T235959");});it("holds an 'until' whose date has not been picked yet as never ending", () => {diff --git a/apps/client/src/widgets/collections/calendar/recurrence.ts b/apps/client/src/widgets/collections/calendar/recurrence.tsindex 15413b7020..b4c18c74d5 100644--- a/apps/client/src/widgets/collections/calendar/recurrence.ts+++ b/apps/client/src/widgets/collections/calendar/recurrence.ts@@ -94,10 +94,10 @@ function serializeSimpleRule({ frequency, interval, weekdays, ends }: SimpleRecu}if (ends.type === "until" && ends.date) {- // The end of the picked day rather than its midnight, so "until June 5th" keeps June 5th:- // the builder's DTSTART is written floating (see event_builder.ts), which the rrule library- // reads as UTC, and a bare date would cut off before the day's own occurrence.- parts.push(`UNTIL=${ends.date.replaceAll("-", "")}T235959Z`);+ // The end of the picked day rather than its midnight, so "until June 5th" keeps June 5th.+ // Local time, like the DTSTART that `buildEvent()` writes: RFC 5545 requires the two to+ // match, and FullCalendar reads the whole rule as UTC when UNTIL is in UTC.+ parts.push(`UNTIL=${ends.date.replaceAll("-", "")}T235959`);} else if (ends.type === "count") {parts.push(`COUNT=${ends.count}`);}