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 UTC exclusions at their local time
Claude750d865bc894
diff --git a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts b/apps/client/src/widgets/collections/calendar/event_builder.spec.tsindex 35770f3745..18ead04688 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.spec.ts@@ -8,6 +8,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";import { buildNote, buildNotes } from "../../../test/easy-froca.js";import { buildEvent, buildEvents } from "./event_builder.js";import { LOCALE_MAPPINGS } from "./index.js";+import { serializeRecurrence } from "./recurrence.js";import { isValidDuration, parseDurationSeconds } from "./utils.js";describe("Building events", () => {@@ -420,6 +421,48 @@ describe("Recurrence", () => {]);expect(occurrences.every(({ end }) => end.endsWith(" 13:00"))).toBe(true);});++ it("skips the dates a UTC EXDATE names, keeping the rest at local time", async () => {+ const noteIds = buildNotes([+ {+ title: "Weekly",+ "#startDate": "2026-10-04",+ "#startTime": "11:00",+ "#endTime": "13:00",+ "#recurrence": [+ "RRULE:FREQ=WEEKLY;BYDAY=SU",+ "EXDATE:20261011T110000Z",+ "EXDATE:20261025T110000Z"+ ].join("\n")+ }+ ]);+ const events = await buildEvents(noteIds);++ expect(renderOccurrences(events, "2026-10-01").map(({ start }) => start)).toEqual([+ "2026-10-04 11:00", "2026-10-18 11:00", "2026-11-01 11:00", "2026-11-08 11:00"+ ]);+ });++ it.each([+ [ "the recurrence editor writes", untilRule("2026-11-29") ],+ [ "is stored in UTC", "RRULE:FREQ=WEEKLY;BYDAY=SU;UNTIL=20261129T235959Z" ]+ ])("ends the series on the day an UNTIL that %s names", async (_, recurrence) => {+ const noteIds = buildNotes([+ {+ title: "Weekly",+ "#startDate": "2026-10-04",+ "#startTime": "11:00",+ "#endTime": "13:00",+ "#recurrence": recurrence+ }+ ]);+ const events = await buildEvents(noteIds);++ expect(renderOccurrences(events, "2026-11-15").map(({ start }) => start)).toEqual([+ "2026-11-01 11:00", "2026-11-08 11:00", "2026-11-15 11:00", "2026-11-22 11:00",+ "2026-11-29 11:00"+ ]);+ });});describe("in a time zone behind UTC", () => {@@ -440,6 +483,27 @@ describe("Recurrence", () => {"2026-11-01 00:00", "2026-11-08 00:00"]);});++ it.each([+ [ "the recurrence editor writes", untilRule("2026-11-29") ],+ [ "is stored in UTC", "RRULE:FREQ=WEEKLY;BYDAY=SU;UNTIL=20261129T235959Z" ]+ ])("keeps a late occurrence on the day an UNTIL that %s names", async (_, recurrence) => {+ const noteIds = buildNotes([+ {+ title: "Weekly",+ "#startDate": "2026-10-04",+ "#startTime": "21:00",+ "#endTime": "22:00",+ "#recurrence": recurrence+ }+ ]);+ const events = await buildEvents(noteIds);++ expect(renderOccurrences(events, "2026-11-15").map(({ start }) => start)).toEqual([+ "2026-11-01 21:00", "2026-11-08 21:00", "2026-11-15 21:00", "2026-11-22 21:00",+ "2026-11-29 21:00"+ ]);+ });});});@@ -473,10 +537,27 @@ function useTimeZone(timeZone: string) {process.env.TZ = timeZone;});afterAll(() => {- process.env.TZ = originalTimeZone;+ if (originalTimeZone === undefined) {+ delete process.env.TZ;+ } else {+ process.env.TZ = originalTimeZone;+ }});}+/** A weekly Sunday rule ending on `date`, as the recurrence editor writes it. */+function untilRule(date: string) {+ return serializeRecurrence({+ kind: "simple",+ rule: {+ frequency: "WEEKLY",+ interval: 1,+ weekdays: [ "SU" ],+ ends: { type: "until", date }+ }+ }) ?? "";+}+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 7e704dd45e..36daf032e9 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.ts@@ -157,7 +157,7 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, e// 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 dtStart = dayjs(startDate).format(allDay ? "YYYYMMDD" : "YYYYMMDD[T]HHmmss");- const rruleString = `DTSTART:${dtStart}\n${toLocalUntil(recurrence)}`;+ const rruleString = `DTSTART:${dtStart}\n${toLocalDates(recurrence)}`;// Validate rrule stringlet rruleValid = true;@@ -183,12 +183,14 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, e}/**- * 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.+ * Rewrites the UTC dates of `UNTIL` and `EXDATE` (`…T235959Z`) as local time, like the DTSTART+ * that `buildEvent()` writes. FullCalendar's rrule plugin reads the whole rule as UTC when any of+ * them 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");+function toLocalDates(recurrence: string) {+ return recurrence+ .replace(/\b(UNTIL=\d{8}T\d{6})Z\b/gi, "$1")+ .replace(/^EXDATE:.*$/gim, (line) => line.replace(/(\d{8}T\d{6})Z\b/gi, "$1"));}/**