Skip to content

fix: keep whole-day dates stable across an update - #95

Closed
janbaer wants to merge 5 commits into
langmail:mainfrom
janbaer:fix/whole-day-roundtrip-on-update
Closed

janbaer wants to merge 5 commits into
langmail:mainfrom
janbaer:fix/whole-day-roundtrip-on-update

Conversation

@janbaer

@janbaer janbaer commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Updating only the location of a whole-day event moves it: DTSTART comes back a day early while DTEND stays where it was.

Two mismatches combine when the caller passes no dates and the stored ones are carried over from the existing event:

  • A VALUE=DATE arrives as local midnight, but the date that gets stored is derived with toISOString(), which is a day earlier in any zone east of UTC.
  • The reader reports DTEND exclusively while the writer takes it inclusively and adds the day back itself.

So the end survives by accident while the start does not. This reshapes the carried values into the form the writer expects, which makes an untouched date stay untouched.

It also closes a second, pre-existing bug that the title does not cover. West of UTC the toISOString() truncation loses nothing, so end was carried through with no inclusive/exclusive correction at all and every update pushed DTEND one day later — a drift that compounds on repeated edits.

Dates the caller supplies explicitly are deliberately left alone: that path is what #91 addresses, and I did not want the two to overlap.

Verified against mailbox.org (Open-Xchange): a whole-day event created for 10 to 12 March keeps both boundaries to the millisecond when only its location is changed. Three tests added; reverting the fix while keeping them fails under both TZ=UTC and TZ=Europe/Berlin.

One caveat worth knowing: CI runs under TZ=UTC, where new Date(2026, 7, 18) and Date.UTC(2026, 7, 18) are the same instant, so the DTSTART half of the bug cannot be reproduced there. The regression test fails on CI only through the DTEND half. Pinning a non-UTC TZ for the test run would close that gap, but that is your call, not something I wanted to change unasked.

Updating only the location of a whole-day event moved it: DTSTART came back a
day early while DTEND stayed where it was.

Two mismatches combine when the caller passes no dates and the stored ones are
carried over. A VALUE=DATE arrives as local midnight, but the date that gets
stored is derived with toISOString(), which is a day earlier in any zone east
of UTC. On top of that the reader reports DTEND exclusively while the writer
takes it inclusively and adds the day back itself, so the end survives by
accident while the start does not.

Reshape the carried values into the form the writer expects, so an untouched
date stays untouched. Dates the caller supplies explicitly are left alone;
that path is what langmail#91 addresses.
@janbaer
janbaer force-pushed the fix/whole-day-roundtrip-on-update branch from 6668715 to a01b1bc Compare October 12, 2026 07:28

@dominik1001 dominik1001 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for tracking this down. The carry-over fix works end to end: against Radicale, a whole-day event now keeps its stored DTSTART/DTEND when only its location is updated. However, three points need changes before this can be merged.

  1. Converting a timed event to whole-day regressed. Calling update-event with wholeDay: true and no dates now produces an event with DTEND equal to DTSTART. #116 added a unit test and a smoke check for this case, and both fail on this branch, which is why CI is red. Details are in the inline comment.
  2. Explicit whole-day dates still use the old conversion. A given start or end is still read with new Date(x) and stored with toISOString(), while the carried-over dates are now anchored at UTC. East of UTC, an explicit end therefore lands a day short, and a single update can mix both conventions. See the inline comment.
  3. recurrenceRule.until still moves. It is a stored whole-day date as well, so an UNTIL=20301230 written by another client is re-written a day earlier east of UTC. The one-day shift can be handled here in the same way as start/end. The full fix needs ts-caldav, however: it writes UNTIL in extended format (2030-12-30) and cannot read its own output back (KlautNet/ts-caldav#29). For that reason this point is optional for this PR.

Failing tests for points 2 and 3 are on the branch test/whole-day-drift. It merges cleanly into this branch, and npm test plus npm run smoke then show what is left. Note that vitest and the CI smoke step now run in Europe/Berlin (#116), because these bugs only appear east of UTC.

Comment thread src/tools/update-event.ts
throw new Error(`Event not found: ${uid}`);
}

const staysWholeDay = wholeDay ?? existing.wholeDay;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

staysWholeDay is also true when a timed event is being converted, i.e. wholeDay: true on an event whose existing.wholeDay is false. The carry then treats the timed end as an exclusive date and subtracts a day. For example, 10:00–11:00 on 2026-08-18 becomes DTSTART;VALUE=DATE:20260818 and DTEND;VALUE=DATE:20260818, while before this change the same call gave a one-day event. Gating the carry on existing.wholeDay should be sufficient.

Comment thread src/tools/update-event.ts
...(staysWholeDay && carryWholeDayDates(existing, start, end)),
...(summary !== undefined && { summary }),
...(start !== undefined && { start: new Date(start) }),
...(end !== undefined && { end: new Date(end) }),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These two lines still convert an explicit date with new Date(x), which the writer then formats with toISOString(). For a caller in Berlin, end: "2026-08-25T00:00:00+02:00" becomes 2026-08-24T22:00Z, so the event ends on the 24th instead of the 25th. Combined with a carried-over start, one update now uses two different conventions. The calendar day named in the string is what the caller means; the test reads an explicit whole-day end as the day it names on test/whole-day-drift covers this. (create-event has the same problem, but that can be a follow-up.)

dominik1001 pushed a commit that referenced this pull request Oct 23, 2026
ts-caldav writes a whole-day VALUE=DATE as the UTC day of the Date it is
given and takes `end` as inclusive, while ical.js reads a VALUE=DATE back
as local midnight with DTEND exclusive. East of UTC that shifts dates a
day earlier in two places:

- An explicit start/end/until such as 2026-09-30T00:00:00+09:00 was
  stored as 2026-09-29 (create-event and update-event).
- An update that changes only another field (e.g. location) carried the
  stored dates back as is, moving DTSTART a day earlier.

Add src/tools/whole-day-date.ts with toWholeDayDate() for ISO input and
storedStartToWholeDayDate()/storedEndToWholeDayDate() for carried-over
dates, and use them in create-event and update-event. Timed events are
unchanged, as is converting a timed event to whole-day without dates.

Supersedes #91 and #95.

Co-authored-by: Jan Baer <jan@janbaer.de>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CjZppzN6ZPGrd6Bvxxe4Uh

Copy link
Copy Markdown
Collaborator

Thanks a lot for this, @janbaer! Your carry-over fix is now part of #125, which just merged. That PR combined it with #91's fix for explicitly passed dates. Your asStoredDate/carryWholeDayDates approach is now carryOverWholeDayDates() in src/tools/whole-day-date.ts. It also covers RRULE UNTIL, EXDATE, RDATE and RECURRENCE-ID, and your tests were ported too. You're credited as co-author on the commit.

Closing this one as superseded by #125.


Generated by Claude Code

@janbaer
janbaer deleted the fix/whole-day-roundtrip-on-update branch November 14, 2026 11:45
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