Skip to content

feat(parser): accept iso 8601 date times without seconds or a zone - #23

Merged
aurelleb merged 1 commit into
mainfrom
feat/15-iso-datetime
Nov 17, 2026
Merged

aurelleb merged 1 commit into
mainfrom
feat/15-iso-datetime

Conversation

@aurelleb

Copy link
Copy Markdown
Contributor

Fixes #15

What was wrong

Parser::parseRFC3339() only matched the exact shape YYYY-MM-DDThh:mm:ss followed by Z or ±hh[:mm]. Anything else fell through to parseYYYYMMDD, which kept the date and silently ignored the rest:

input before (UTC) after
2026-10-01T12:00Z (the issue's example) 2026-10-01 00:00:00 (time dropped) 2026-10-01 12:00:00 (Etc/UTC)
2026-10-01T12:00+02:00 error: Cannot add DateTime to DateTime same as T12:00:00+02:00
2026-10-01T12:00-05:30 8 months 1 week 5 days 18 hr 30 min same as T12:00:00-05:30
2026-10-01T12:00 2026-10-01 00:00:00 (time dropped) 2026-10-01 12:00:00, local
2026-10-01T12:00:00 error: Expected EOF, looks like there is nothing we can parse! 2026-10-01 12:00:00, local
2026-10-01T12:00:00.5Z 2026-10-01 00:00:00 (time dropped) 2026-10-01 12:00:00 (Etc/UTC)

The zone-less hh:mm:ss error came from parseRFC3339 advancing the lexer before checking for a zone, then returning nullopt with the tokens already consumed.

What changed (all inside parseRFC3339)

  • Seconds are optional. T12:00Z / T12:00+02:00 are read exactly like their :00 forms.
  • Fractional seconds are truncated (std::trunc before the existing range check). . is always a fraction delimiter in the lexer, whatever the locale, so this behaves the same everywhere.
  • No Z or offset means local time, read the same as 2026-10-01 12:00. A trailing zone name is picked up through the existing parseTimezone(), so 2026-10-01T12:00 tokyo matches 2026-10-01 12:00 tokyo. The Z and offset branches are unchanged.
  • The separator must be T (case-insensitive, so the already-working 2026-10-01t12:00:00Z keeps working). Before, any word was accepted. With the looser shape that would have pulled 2026-10-01 at 5:30 pm into this path and dropped the pm.
  • An offset must be a +/- glued to the time. Before, parseTimezoneOffset() was always tried, so 2026-10-01T12:00:00 + 1 hour read + 1 as UTC+1 (giving 13:00 (Etc/UTC+1) and dropping hour). It now gives 13:00 (Etc/UTC).

Behaviour changes outside the issue's forms

These come from the two checks above. I think they are all improvements, but please look:

  • 2026-10-01T12:00:00 + 1 hour: 13:00:00 (Etc/UTC+1) becomes 13:00:00 local.
  • json(2026-10-01T12:00:00) in a non-UTC zone was treated as UTC, because a glued ) reached parseTimezoneOffset(), which returns offset 0 for any operator. It is now local, like json(2026-10-01 12:00).
  • 2026-10-01 at 12:00:00Z (with at instead of T) no longer goes through the RFC path, so the Z is ignored as a trailing token and the time is local. That form used to work only because the separator wasn't checked.

Not handled (out of scope)

Week dates, ordinal dates, basic format (20261001T1200Z), hour-only times (T12Z), fractional minutes (T12:00.5Z), and sub-second precision (fractions are truncated, not kept).

Things that look off in the existing offset handling (not changed here)

  • Offset sign / meaning: 2026-01-18T14:30:00+02:00 gives 2026-01-18 16:30:00 (Etc/UTC+2). The wall-clock part is treated as UTC and then shown in UTC+2. Under RFC 3339, 14:30+02:00 is 14:30 local at +2 (12:30 UTC), so it should display 14:30:00 (Etc/UTC+2). The existing corpus asserts the current behaviour, so I left it alone and the new no-seconds cases follow it.
  • parseTimezoneOffset() returns an offset of 0 (UTC) for any operator token, not just +/-, without consuming it. parseRFC3339 now only calls it after a glued sign, but other callers (utc / gmt in parseTimezone) still behave that way.
  • 2026-10-01T12:00:00-2026-... (a glued minus followed by something that isn't a valid hour) consumes the - and yields offset 0. This is unchanged.

Tests

  • tests/corpus/datetime.corpus: added cases for each form above (no seconds with Z / +hh:mm / -hh:mm; no zone with and without seconds; trailing zone name; spaced + 1 hour with and without seconds; truncated fractional seconds).
  • tests/datetime.cpp: one new case under Europe/Moscow (UTC+3, no DST) checking that 2026-10-01T12:00[:30] is local time. The frozen corpus runs in UTC, where local and UTC can't be told apart.
  • make && ./build/numen-tests: all tests passed (15346 assertions in 94 test cases). The new cases failed before the fix.
  • make debug && ./build-debug/numen-tests (ASan + UBSan): all passed.
  • clang-format --dry-run --Werror is clean on the touched files.
  • Run locally on macOS (AppleClang, NUMEN_USE_DATE_TZ) only. Linux GCC/Clang, MSVC and Nix are left to CI.

🤖 Generated with Claude Code

parseRFC3339 only matched a full hh:mm:ss time followed by Z or an
offset. Anything shorter fell through to parseYYYYMMDD, which kept the
date and silently dropped the time ("2026-10-01T12:00Z" read as
midnight), and a zone-less hh:mm:ss consumed its tokens before bailing
out, leaving nothing to parse.

Seconds are now optional and fractional seconds are truncated. Without
Z or an offset the time is local, read the same as "2026-10-01 12:00",
including a trailing zone name. An offset has to be a +/- glued to the
time, so "...T12:00 + 1 hour" stays arithmetic, and the separator has to
be T so "2026-10-01 at 5:30 pm" keeps going through the regular date
path.

Fixes #15

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aurelleb
aurelleb marked this pull request as ready for review November 17, 2026 01:29
@aurelleb
aurelleb merged commit e9ff312 into main Nov 17, 2026
8 checks passed
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.

FR: Support full ISO dates

1 participant