From 81b79283984e68597de2b80e3a5502bfc1ef2b22 Mon Sep 17 00:00:00 2001 From: Aurelle <72200344+aurelleb@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:26:46 +0100 Subject: [PATCH] feat(parser): accept iso 8601 date times without seconds or a zone 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 --- src/numen/parser.cpp | 35 ++++++++++++++++++++++++++--------- tests/corpus/datetime.corpus | 18 ++++++++++++++++++ tests/datetime.cpp | 8 ++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/numen/parser.cpp b/src/numen/parser.cpp index da7361c..772637e 100644 --- a/src/numen/parser.cpp +++ b/src/numen/parser.cpp @@ -589,16 +589,26 @@ std::optional Parser::parseTime(bool afterDate) { } std::optional Parser::parseRFC3339() { - // 2026-08-17T14:30:00Z + // 2026-08-17T14:30[:00[.5]][Z|+02:00] using L = Lexer; auto parsed = m_lexer.peakForward(); + L::Number, L::Operator, L::Number>(); if (!parsed) return std::nullopt; - auto &[year, s1, month, s2, day, tsep, hour, s3, min, s4, s] = *parsed; + auto &[year, s1, month, s2, day, tsep, hour, s3, min] = *parsed; - if (!(s1.op == "-" && s2.op == "-" && s3.op == ":" && s4.op == ":")) return std::nullopt; + if (!(s1.op == "-" && s2.op == "-" && s3.op == ":" && equalsIgnoreCase(tsep.data, "T"))) { + return std::nullopt; + } + + std::size_t length = std::tuple_size_v; + double secs = 0; + + if (auto tail = m_lexer.peakForward(length); tail && std::get<0>(*tail).op == ":") { + secs = std::get<1>(*tail).n.toDouble(); + length += 2; + } // the shape alone does not make it a timestamp: every component has to be // one a clock could read, or the cast below has nothing to land on @@ -607,8 +617,9 @@ std::optional Parser::parseRFC3339() { auto dayOfMonth = clockComponent(day.n.toDouble(), 31); auto hours = clockComponent(hour.n.toDouble(), 23); auto minutes = clockComponent(min.n.toDouble(), 59); - // :60 is a leap second, which is a reading RFC 3339 allows - auto seconds = clockComponent(s.n.toDouble(), 60); + // :60 is a leap second, which is a reading RFC 3339 allows. fractions of a + // second are dropped, a time only holds whole seconds + auto seconds = clockComponent(std::trunc(secs), 60); if (!yr || !mon || !dayOfMonth || !hours || !minutes || !seconds) return std::nullopt; if (*mon < 1 || *dayOfMonth < 1) return std::nullopt; @@ -624,18 +635,24 @@ std::optional Parser::parseRFC3339() { .seconds = std::chrono::seconds{*seconds}, }}; - m_lexer.advance(std::tuple_size_v); + m_lexer.advance(static_cast(length)); + + // an offset is a sign glued to the time: "14:30 + 1 hour" adds an hour, it is not UTC+1 + auto sign = m_lexer.peak(); + const bool hasOffset = sign && (sign->raw == "+" || sign->raw == "-") && m_lexer.isGluedLeft(*sign); if (auto zulu = m_lexer.peakAs(); zulu && zulu->data == "Z") { m_lexer.next(); ds.timezone = TimezoneOffset{.name = "UTC"}; return ds; - } else if (auto offset = parseTimezoneOffset()) { + } else if (auto offset = hasOffset ? parseTimezoneOffset() : std::nullopt) { ds.timezone = TimezoneOffset{.name = "UTC", .offset = *offset}; return ds; } - return std::nullopt; + // without a zone it is local time, read the same as "2026-08-17 14:30" + ds.timezone = parseTimezone(); + return ds; } std::optional Parser::parseTimezoneOffset() { diff --git a/tests/corpus/datetime.corpus b/tests/corpus/datetime.corpus index b40598d..d6cbe3b 100644 --- a/tests/corpus/datetime.corpus +++ b/tests/corpus/datetime.corpus @@ -78,6 +78,24 @@ Jan 2025 18 at 7 pm => 2025-01-18 19:00:00 (Etc/UTC) 2026-01-18T14:30:00+05 => 2026-01-18 19:30:00 (Etc/UTC+5) 2026-01-18T14:30:00+05: => 2026-01-18 19:30:00 (Etc/UTC+5) +# ISO 8601: seconds are optional and read as ":00" +2026-10-01T12:00Z => 2026-10-01 12:00:00 (Etc/UTC) +2026-10-01T12:00+02:00 => 2026-10-01 14:00:00 (Etc/UTC+2) +2026-10-01T12:00-05:30 => 2026-10-01 06:30:00 (Etc/UTC-5:30) + +# ISO 8601 without a zone is local time, the same as "2026-10-01 12:00" +2026-10-01T12:00 => 2026-10-01 12:00:00 (Etc/UTC) +2026-10-01T12:00:30 => 2026-10-01 12:00:30 (Etc/UTC) +2026-10-01T12:00 tokyo => 2026-10-01 03:00:00 (Etc/UTC) + +# an offset is glued to the time, a spaced sign is arithmetic +2026-10-01T12:00 + 1 hour => 2026-10-01 13:00:00 (Etc/UTC) +2026-10-01T12:00:00 + 1 hour => 2026-10-01 13:00:00 (Etc/UTC) + +# fractional seconds are truncated +2026-10-01T12:00:00.5Z => 2026-10-01 12:00:00 (Etc/UTC) +2026-10-01T12:00:59.999Z => 2026-10-01 12:00:59 (Etc/UTC) + # a component no clock could read means it is not a timestamp 2026-01-18T14:30:60Z => 2026-01-18 14:31:00 (Etc/UTC) 2026-01-18T80:30:00Z => 2026-01-18 00:00:00 (Etc/UTC) diff --git a/tests/datetime.cpp b/tests/datetime.cpp index 4fdfa50..e79e269 100644 --- a/tests/datetime.cpp +++ b/tests/datetime.cpp @@ -122,6 +122,14 @@ TEST_CASE("toRFC3339 discards subseconds") { CHECK(numen::DateTime{.time = t, .tz = test::zone("UTC")}.toRFC3339() == "2026-01-18T02:30:00Z"); } +TEST_CASE("ISO 8601 date times without a zone are local time") { + auto config = configAt({2026y, January, 18d}); + config.timezone = test::zone("Europe/Moscow"); // UTC+3, no DST + + test::assertExpr("2026-10-01T12:00", "2026-10-01 12:00:00 (Europe/Moscow)", config); + test::assertExpr("2026-10-01T12:00:30", "2026-10-01 12:00:30 (Europe/Moscow)", config); +} + TEST_CASE("json() renders date times as RFC 3339") { auto config = configAt({2026y, January, 18d}); test::assertExpr("json(now)", "2026-01-18T00:00:00Z", config);