Skip to content

feat: write date-times in a named zone (TZID + VTIMEZONE) - #27

Merged
PhilflowIO merged 18 commits into
mainfrom
feat/named-zone
Oct 8, 2026
Merged

PhilflowIO merged 18 commits into
mainfrom
feat/named-zone

Conversation

@PhilflowIO

@PhilflowIO PhilflowIO commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Problem

The library could keep a TZID that was already there, but couldn't add one. A new value given with Z or an offset was written as UTC, and one without a zone as floating. A recurring meeting created that way (for example by dav-mcp's create_event) was a UTC series, so its local time moved by an hour at every DST change. RFC 5545 3.6.5 also requires a VTIMEZONE for every TZID, and the library had no way to produce one.

Change

  • zone: '<IANA name>' option. Every date-time the call writes is a wall-clock time in that zone and gets TZID=<zone>:
    • A Z or offset value becomes its wall-clock time in the zone. An instant in the second pass of the repeated hour has no wall-clock time there and throws DST_AMBIGUOUS.
    • A zoneless value is read as wall clock in the zone (RFC 5545 3.3.5 for gaps and folds).
    • Dates stay dates.
    • UTC-only properties and vCard values are read in the zone and written as UTC.
  • Values that follow DTSTART (DTEND, DUE, EXDATE, RDATE, RECURRENCE-ID, UNTIL) are written in the zone when DTSTART is in the call. Without DTSTART in the call they are only written in the zone when DTSTART is already in it (or the component has no DTSTART). Otherwise they throw ZONE_MISMATCH with remedy same-call.
  • Moving an existing series into a zone uses the existing series move, onto the new zone's wall clock. Each occurrence, EXDATE, RDATE, UNTIL and override keeps its place in the series. The old instants are not kept: keeping local time is what changes. The move's existing refusals still apply.
  • Precedence: zone decides. floatingTime (either value) and absoluteTime: 'as-given' contradict it, so giving them with zone throws INVALID_FLOATING_TIME or INVALID_ABSOLUTE_TIME. keep-zone agrees with zone and may be given.
  • Zone names:
    • An unknown name, an abbreviation (CEST) or an offset (+01:00) throws UNKNOWN_TZID.
    • Case is corrected (europe/berlin is written Europe/Berlin), and aliases are kept as given (Asia/Kolkata).
    • A zone that only the object's own VTIMEZONE defines is accepted.
  • Ends move with DTSTART. When DTSTART is written in a zone and the call doesn't write DTEND (or a todo's DUE), the existing end is moved into the zone and keeps its duration: measured on the wall clock if start and end shared a zone, as elapsed time otherwise (also across a DST change). Under zone, an end before its start throws the new code END_BEFORE_START.
  • zone: 'UTC' (also Etc/UTC, GMT, Zulu) writes Z, with no TZID and no VTIMEZONE.
  • VTIMEZONE generation, from Intl:
    • Coverage starts on 1 January of the year before the earliest value. The first observance only states the offset at that start, because ical.js reads times before a VTIMEZONE's first observance as offset 0.

    • Yearly rules become RRULE pairs, ended by UNTIL where the rule stopped. Other changes are listed by RDATE, with DTSTART repeated among them because ical.js ignores DTSTART in an observance that has RDATEs.

    • Final state last, for Outlook. Outlook and Exchange read the zone's current rule from the latest STANDARD and the latest DAYLIGHT, each picked on its own (MS-OXCICAL 2.1.3.1.1.19.2, note <61>). Both describe the zone's final state:

      • the current rule, as an open DAYLIGHT/STANDARD pair;
      • or, for a zone without DST now, a STANDARD and a DAYLIGHT with the same offsets at the final DTSTART (Exchange's convention, note <65>).

      An abolished DST rule, like Sao Paulo's, Moscow's or Tehran's, is never the latest of its kind.

    • TZNAME is the abbreviation from Intl (CET, EST, IST); without one, the offset (-03).

    • Rules crossing a month end are recognised. Egypt's DST ends on the Friday after the last Thursday of October, which can be 1 November; this becomes an open BYYEARDAY=-67..-61;BYDAY=FR rule.

    • Ramadan-based zones. Only Casablanca, El Aaiun, Gaza and Hebron are listed by date, up to 2087, where the tz database's own list ends. The median VTIMEZONE for a 2026 value is 9 lines.

    • Checks. The output is read back and compared with the zone's scanned changes before use, and the same input always gives the same text.

    • Scan length. A value's last year doesn't extend the scan: once the zone has settled, its final state covers every later year. Rule runs are built in linear time, so UNTIL=99991231 takes 70 ms (it was 81 s).

    • Memory. The scan cache keeps one change list per zone, extended by scanning only missing years: about 1 MB for all 418 zones (it was over 900 MB).

    • Existing VTIMEZONE. One already in the object is reused. If a write in the zone puts a value before its first observance: a VTIMEZONE this library generated (byte-identical to what it would generate) is regenerated for the wider range; any other is refused with UNSUPPORTED_VTIMEZONE (rewrite-object).

    • Local mean time. A value on local mean time, whose offset is in seconds, throws UNSUPPORTED_VTIMEZONE.

  • New exports for consumers (for dav-mcp #110/#78/#117):
    • generateVtimezone(tzid, { from, to? }) returns the VTIMEZONE as text.
    • resolveZone(tzid, source?) / resolvePropertyZone(property) convert UTC → wall time exactly (ical.js#847 does not) and wall → UTC per RFC 5545 §3.3.5. They read the object's VTIMEZONE, with IANA as fallback, from text or from a component of any ical.js copy.
    • expandOccurrences(object, { budget, until, from?, limit? }) (overrides end at their own DTEND, else their own DURATION, else the master's length from their own start), with createRecurrenceBudget() charges every ical.js candidate to a budget that can be shared across calls, using the same hook as the series checks. When the budget runs out it fails closed with complete: false, stoppedBy: 'budget'.
  • ianaZone no longer accepts a UTC offset (+01:00) as an IANA zone. Newer Intl versions accept it, but it is not a TZID.
  • README: new section "Writing in a named zone", updated error table, and the "Known Limitations → Timezone handling" entry replaced.

Tests

  • Generated VTIMEZONEs vs Intl. Each generated VTIMEZONE is compared with Intl, read once with the library's own reader and once with ical.js. Covered: 17 zones, for values from 1971 and from 2026, plus Cairo and Recife's one-week DST in 2000. The final-state check covers 13 zones, among them Sao Paulo, Moscow, Mexico City, Istanbul, Tehran, Amman and Cairo.
  • All zones. VTIMEZONE_ALL_ZONES=1 covers all 418 zones from 1971 and from 2026: 0 mismatches, and every final state correct. VTIMEZONE_DEEP=1 passes.
  • New behaviour: the option end to end, ends moving with DTSTART, END_BEFORE_START, the UTC zone, alias spelling, and regeneration vs. refusal of an existing VTIMEZONE (2020 and 1950 moves).
  • Exports: each export, including the three never-ending rules from the review, which stop within the budget.
  • Memory, with the review's mem.cjs and --expose-gc: all 418 zones retain 1 MB, Cairo 0.46 MB. 20 distinct Berlin UNTIL years take 103 ms and retain 0.64 MB.
  • Full suite: 485 passed under TZ=UTC, America/New_York and Asia/Kolkata (on the compute host).
  • Packed tarball: the smoke test passes, including the zone write and the helpers in both the ESM and the CommonJS build.
  • Live on Baïkal (sabre 4.7.0), with the final build:
    • A weekly Berlin series was created on a skeleton with updateFields + zone, PUT (201), and read back with GET.
    • Every line, including the 23-line VTIMEZONE, was stored as written.
    • ical.js expanded the stored object to 09:00 Berlin every week (07:00Z before 25 Oct, 08:00Z after).
    • The object was deleted, and a scan of all calendars found nothing left.
    • Server-side expand was not checked: this server answers every expand request with a 500, also for a plain UTC event.

Breaking changes

None for callers that don't pass zone: the default path is byte-identical to 0.6.0 over 60,000 calls (re-verified in review).

Two TypeScript-only effects:

  • UpdateFieldsErrorCode gains END_BEFORE_START. An exhaustive switch over the codes (with a never check) stops compiling until it handles the new code.
  • UPDATE_FIELDS_ERROR_CODES has one more entry.

One edge case: a TZID spelled as a UTC offset (+01:00) used to resolve through Intl on Node versions that accept it, and now throws UNKNOWN_TZID on every Node version.

Closes #4

🤖 Generated with Claude Code

PhilflowIO and others added 18 commits October 8, 2026 13:29
RFC 5545 3.6.5 requires a VTIMEZONE for every TZID; to write a named
zone the library has to produce one. It is built from Intl's offsets
since 1970: yearly rules as RRULE runs (the current one open, so an
unbounded series is covered), other changes by date, and read back
against Intl before use. ical.js reads times before the first
observance as offset 0 and ignores DTSTART in an observance with
RDATEs, so the form anchors the start and repeats DTSTART. A UTC
offset name ("+01:00"), which newer Intl accepts, is no TZID.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The library could keep a TZID but not introduce one: a new value with
Z or an offset was written as UTC, a zoneless one as floating, so a new
recurring meeting drifted an hour in local time at each DST change.
With zone, date-times are written as wall clock in that IANA zone with
its TZID, and a VTIMEZONE is added when the VCALENDAR has none.

Values that follow DTSTART are refused in another zone than DTSTART's
unless DTSTART is in the call; moving a series into the zone is the
existing series move, on the new zone's wall clock. floatingTime and
absoluteTime "as-given" contradict zone and are refused with it.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Each generated VTIMEZONE is read with the library's reader and with
ical.js and compared with Intl, for 16 zones chosen for southern DST,
half- and quarter-hour offsets, rule changes and irregular rules; the
option is checked end to end, including series expanded by ical.js
across both 2026 DST changes. VTIMEZONE_DEEP and VTIMEZONE_ALL_ZONES
enable the dense and the every-zone sweep. The smoke test checks both
builds on the consumer's ical.js.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The README still listed named zones as unsupported and pointed to
other libraries for time zones. It now describes the option, its
precedence over floatingTime and absoluteTime, what moving a series
into a zone means, the VTIMEZONE form and its limits, and the codes
the option can raise.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The every-zone sweep found DST periods of one week that a weekly
sample skips: Boa Vista, Recife and Noronha in October 2000, Gaza
and Hebron in October 2040. Their generated VTIMEZONE missed those
changes. Sampling daily finds them; the scan is cached per zone, so
it costs about 200 ms once per zone and process.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Recife's one week of DST in 2000 and Gaza's in 2040 are now in the
default zones, and every comparison starts where the VTIMEZONE does,
so a zone that left local mean time after 1970 (Monrovia) is checked
from then on instead of failing before it.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The zone option and the generated VTIMEZONE are new API; in 0.x that
is a minor bump. A TZID spelled as a UTC offset ("+01:00") is no
longer resolved through Intl.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Review of #27 found four problems in the generated VTIMEZONE:
- the scan cache kept every sample, keyed by value years: 36 MB
  for Cairo, over 900 MB for all zones. It now keeps one change list
  per zone, grown only by the years not yet scanned (1 MB for all);
- Outlook takes the observances with the latest DTSTART as the rule
  (MS-OXCICAL 2.1.3.1.1.19.2), and those were abolished rules (Sao
  Paulo, Moscow, Tehran, ...). The final state now always comes
  last, as an open rule pair or the last change on its own;
- Egypt's rule ending on the Friday after the last Thursday of
  October, sometimes 1 November, is now an open BYYEARDAY rule;
- coverage starts the year before the earliest value instead of
  1970, with TZNAME. An own VTIMEZONE is regenerated when a value
  moves before it; another one is refused there.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Writing DTSTART in a zone left DTEND in the old zone: a Berlin
10:00-11:00 meeting moved to 10:00 New York ended at 09:00Z, before
its 14:00Z start, and the ZONE_MISMATCH remedy told callers to do
exactly that. An end the call does not write now moves along,
keeping its duration; an end before the start is refused with the
new code END_BEFORE_START. zone "UTC" (and its aliases) is written
with Z and no VTIMEZONE, and a lower-case alias is written as the
zone it links to instead of in lower case.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
dav-mcp needs what updateFields uses internally: generateVtimezone
for objects it builds itself, zone conversion that is right near DST
changes (ical.js#847) with RFC 5545 3.3.5 gap and fold reading, and
a recurrence expansion that cannot loop. expandOccurrences charges
every candidate ical.js tests to a budget a caller can share across
calls, through the same hook as the series checks, and reports an
exhausted budget instead of running on.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Final state last for zones that abolished or changed DST, Cairo's
rule, TZNAME, cache size, regeneration and refusal of an existing
VTIMEZONE, ends moved with DTSTART, UTC zone, alias spelling, and
each export, including the three rules that never end (DAILY on
30 February, SECONDLY and MINUTELY since 1950/1960). The smoke test
checks the helpers in both builds.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The VTIMEZONE form (final state last, coverage, TZNAME, Ramadan
zones), how an existing VTIMEZONE is treated, ends that move with
DTSTART, zone UTC and alias spelling, ical.js' different reading of
gap and fold times, a line for LLM tool callers, the new error code,
and a section on the three helpers.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Node 18's ICU names Asia/Kathmandu NPT, later ones have no
abbreviation for it; the fallback to the offset is checked on a zone
no ICU names.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
An UNTIL in 9999 made the scan run to 10001 and the rule runs grow
quadratically: 81 s for a weekly Berlin series. A value's last year
no longer extends the scan, since the zone's settled rule or fixed
offset covers every later year, and runs keep the candidate rules
and narrow them per year. Now 70 ms. For Outlook, which reads the
latest STANDARD and DAYLIGHT each on its own, a zone without DST
now ends in both kinds at the same DTSTART.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
expandOccurrences gave an override without DTEND the master's end at
the original slot, before its own start, and ignored its DURATION.
A moved end measured in elapsed time was added on the wall clock, an
hour off across a DST change. resolveZone and generateVtimezone kept
a lower-case name instead of spelling it as updateFields does.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Guards the 9999 UNTIL and a value in year 1 under 200 ms, checks
the latest STANDARD and DAYLIGHT separately for Outlook from four
start years, override ends, an elapsed end across a DST change, and
the spelling of zone names in the helpers.

Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Refs #4

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@PhilflowIO

Copy link
Copy Markdown
Owner Author

Review summary: two reviewers (A correctness vs own RFC evaluator + Intl for all zones; B interop/consumer incl. live Baïkal), then two re-verify rounds. Fixed along the way: unbounded scan cache (903 MB → ~2 MB for all zones), Outlook/Exchange per-kind latest-observance rule (final state as latest STANDARD and DAYLIGHT), coverage from earliest year with regeneration of library-generated VTIMEZONEs and refusal for foreign ones, DTEND/DUE follow a zone change, far-future UNTIL (81 s → 72 ms), override ends in expandOccurrences, UTC aliases → Z. Final sweep: 0 mismatches over 1,688 VTIMEZONE texts with two readers; default path byte-identical to 0.6.0 over 60,000 calls. New exports for consumers: generateVtimezone, resolveZone/resolvePropertyZone, expandOccurrences + createRecurrenceBudget. Not verified: rendering in real Outlook. CI green on Node 18–26.

@PhilflowIO
PhilflowIO merged commit 0430f51 into main Oct 8, 2026
5 checks passed
@PhilflowIO
PhilflowIO deleted the feat/named-zone branch October 8, 2026 13:10
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.

Write date-times in a named time zone (TZID + VTIMEZONE)

1 participant