Skip to content

fix: stop whole-day events reading as the day before - #100

Open
janbaer wants to merge 1 commit into
dominik1001:mainfrom
janbaer:fix/whole-day-dates-in-list-events
Open

janbaer wants to merge 1 commit into
dominik1001:mainfrom
janbaer:fix/whole-day-dates-in-list-events

Conversation

@janbaer

@janbaer janbaer commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

list-events returns the Date that ical.js builds from a VALUE=DATE. That
date carries no time and no zone, but the Date sits at local midnight, so
serialising it to JSON puts the event on the previous day in every zone east of
UTC:

DTSTART;VALUE=DATE:20260825  ->  "start": "2026-08-24T22:00:00.000Z"

Anything reading the date out of that string, a calling model included, is off
by one. I chased this as a create-event bug for a while, because a holiday
that was stored correctly came back looking like it had been created a day
early.

A whole-day event now reports wholeDay: true with start and end as plain
calendar dates. DTEND is exclusive, so end names the day before it: the last
day the event covers, which is the day create-event already takes as its own
end. The write schemas still want a full ISO datetime, and the returns text
says so. Timed events are untouched.

Before and after, for a holiday from 25 to 29 August:

- "start": "2026-08-24T22:00:00.000Z", "end": "2026-08-29T22:00:00.000Z"
+ "start": "2026-08-25", "end": "2026-08-29", "wholeDay": true

The dates are read off the local calendar fields rather than derived from the
difference between the two instants, so a daylight saving change inside the span
cannot add or lose a day.

Tests cover a multi-day span, a single day, a missing DTEND, a span across the
October clock change, and a timed event that must keep its instant. TZ is
pinned to Europe/Berlin inside that block, because in UTC the bug cannot occur
at all. The smoke harness now round-trips a three-day whole-day event and checks
both dates; its input stays in UTC so it does not depend on #91.

Related, all independent of each other: #91 is the same class of problem on the
create side, #95 on the update side, this one is the read side. It touches the
same map as #94, so if that lands first I will rebase.

janbaer added a commit to janbaer/caldav-mcp that referenced this pull request Aug 24, 2026
# Conflicts:
#	README.md
#	scripts/smoke.ts
#	src/tools/list-events.test.ts
#	src/tools/list-events.ts
dominik1001 added a commit that referenced this pull request Oct 23, 2026
test: whole-day regression guards from the PR #100 review

@dominik1001 dominik1001 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. The core fix works: list-events now reports whole-day events as plain dates with the last day included. A few things need to change before it can go in.

Rebase onto main. The branch has a merge conflict with main. main also now has src/tools/whole-day.test.ts (#118), which drives create-event, list-events and update-event through a real MCP client. Four of its tests are marked test.fails for known whole-day bugs. Three of them still fail with this PR applied, and the fourth (passing list-events dates straight back to update-event) now fails with Invalid ISO datetime at start. When a fix makes one of them pass, its .fails marker has to come off.

update-event still moves whole-day events a day earlier (src/tools/update-event.ts, not part of this diff). The local-midnight Date from ts-caldav is corrected only where list-events displays it. In update-event it still goes unchanged through ...existing into client.updateEvent, and ts-caldav writes whole-day dates as toISOString().split("T")[0]. So in Berlin, changing only the summary of an event on DTSTART;VALUE=DATE:20260825 stores it as 20260824, while list-events now shows the right date. The conversion should live in one place that both tools use (or be fixed upstream in ts-caldav), not in a list-events-only wholeDaySpan.

Inline comments cover the rest.

Comment thread src/tools/list-events.ts Outdated
},
returns:
"A list of events that fall within the given timeframe, each containing `uid`, `summary`, `start`, `end`, and optionally `description` and `location`",
"A list of events that fall within the given timeframe, each containing `uid`, `summary`, `start`, `end`, and optionally `description` and `location`. A whole-day event carries `wholeDay: true`, and its `start` and `end` are plain calendar dates (`YYYY-MM-DD`); `end` names the last day the event covers, not the exclusive DTEND, so it is the day create-event and update-event take as their own `end`. Those two require a full ISO 8601 datetime, so add a time and an offset before passing such a date back. Every other event gives `start` and `end` as ISO 8601 instants.",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guidance moves events a day earlier. "Add a time and an offset" doesn't work, because ts-caldav takes the whole-day date as the UTC day (toISOString().split("T")[0]). A caller in Berlin who turns 2026-08-25 into 2026-08-25T00:00:00+02:00 gets 2026-08-24T22:00:00Z, so DTSTART becomes 20260824. The guidance needs to say T00:00:00Z (or noon), or better, create-event/update-event should accept the plain date list-events returns. #118's create-event stores the day named in a date with an offset covers this.

Comment thread src/tools/list-events.ts Outdated
},
returns:
"A list of events that fall within the given timeframe, each containing `uid`, `summary`, `start`, `end`, and optionally `description` and `location`",
"A list of events that fall within the given timeframe, each containing `uid`, `summary`, `start`, `end`, and optionally `description` and `location`. A whole-day event carries `wholeDay: true`, and its `start` and `end` are plain calendar dates (`YYYY-MM-DD`); `end` names the last day the event covers, not the exclusive DTEND, so it is the day create-event and update-event take as their own `end`. Those two require a full ISO 8601 datetime, so add a time and an offset before passing such a date back. Every other event gives `start` and `end` as ISO 8601 instants.",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calling model never sees this text. returns isn't passed to registerTool. Only description and inputSchema reach the MCP client, and returns is read only by scripts/gen-tool-docs.ts for the README. The model gets "start":"2026-08-25","end":"2026-08-29","wholeDay":true with no explanation. It may read end as exclusive (as in iCal) and shorten the event by a day, or pass the bare date to update-event, which rejects it. The explanation of the output format belongs in description. #118's the list-events description the client sees explains whole-day dates checks this.

Comment thread src/tools/list-events.ts Outdated
lastDay.setDate(lastDay.getDate() - 1);
const start = localDate(event.start);
const end = localDate(lastDay);
return { start, end: end < start ? start : end, wholeDay: true };

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This compares strings, which only works for 4-digit years, and localDate doesn't pad getFullYear() (year 999 gives "999-…"). The clamp also quietly turns an event whose DTEND is before its DTSTART into a valid one-day event, which hides the bad data. Padding the year and comparing dates would avoid the first problem. I'd let the second one surface rather than clamp it.

Comment thread scripts/smoke.ts Outdated
),
) as Array<{ uid: string; start: string; end: string; wholeDay?: boolean }>;
const foundWholeDay = wholeDayListed.find((e) => e.uid === wholeDayUid);
if (!foundWholeDay)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this check or the one below throws, the delete-event call further down never runs, so the test event stays on the real calendar. That adds one more event with every failing run. Wrapping the section in try/finally, as the other smoke blocks on main do, fixes that.

Comment thread scripts/smoke.ts Outdated
// looked shifted. The dates must come back as plain dates, with `end` naming
// the last day. Input stays in UTC on purpose: preserving a non-UTC offset
// through create-event is a separate fix.
const wholeDayStart = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this works out the date with millisecond arithmetic right next to the new addDays helper. addDays(new Date().toISOString().slice(0, 10), 3) would keep all the smoke date maths on one path.

Comment thread src/tools/list-events.test.ts Outdated
// exactly the condition under test: in UTC the bug cannot appear at all, and
// the instants below are what a Berlin machine gets back for
// DTSTART;VALUE=DATE:20260825 and friends.
beforeAll(() => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main now pins TZ: "Europe/Berlin" for every test in vitest.config.ts, so after the rebase these hooks only repeat that setting. Worse, if the global zone ever changes to test another one, this block quietly switches back to Berlin for these tests only. Please remove beforeAll/afterAll and the comment along with them.

dominik1001 added a commit to robertlestak/caldav-mcp that referenced this pull request Oct 23, 2026
Drives create-event, list-events and update-event through an in-memory
MCP client against a fake calendar that reads and writes VALUE=DATE the
way ts-caldav 0.4.4 does. The four known bugs are marked test.fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
list-events returned the local-midnight Date ical.js builds from a
VALUE=DATE, which serialises to the previous day east of UTC. A whole-day
event now reports wholeDay: true with plain YYYY-MM-DD start and end; end
is the last covered day (inclusive), the same day create-event and
update-event take as their own end.

Both write tools now accept that plain date for a whole-day event, so the
dates round-trip unchanged. A plain date on a timed event is rejected
instead of silently meaning midnight UTC. The format is explained in the
list-events description, since returns never reaches the client.
@janbaer
janbaer force-pushed the fix/whole-day-dates-in-list-events branch from d3f27d9 to 46cc6a7 Compare November 14, 2026 09:36
@janbaer

janbaer commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, I agree with all points. I rebuilt the branch on current main, so the conflict is gone.

update-event was already fixed by #125, so this PR now covers only the read side. The conversion lives in whole-day-date.ts (wholeDaySpan), next to the other whole-day helpers.

The format is explained in the list-events description, which the client sees, and it no longer tells callers to add a time and an offset. create-event and update-event accept the plain YYYY-MM-DD that list-events returns when wholeDay is true, so the dates round-trip unchanged. A plain date on a timed event is rejected with an error instead of silently meaning midnight UTC. The year is padded, and an end before the start is no longer clamped.

Both remaining test.fails in whole-day.test.ts pass now, so I removed the markers. New unit tests cover the span (multi-day, single day, missing DTEND, the October clock change, year below 1000) and the plain-date rule. They rely on the TZ from vitest.config.ts. In the smoke script I only updated a stale comment, the whole-day block already uses try/finally.

npm test, check, knip, docs:check and build pass, as do npm run smoke and npm run smoke:agent against mailbox.org.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants