lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

Std.Time: `O`/`OOOO` (at UTC), `xxxxx` and `Z`-`ZZZ` (with seconds) cannot parse their own formatted output

Open hiroki-abe-58 opened this issue 1 month ago • 0 comments

Prerequisites

  • [x] Check that your issue is not already filed: https://github.com/leanprover/lean4/issues
  • [x] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • [x] Test your test case against the latest nightly release (here: nightly-2026-10-27 through elan, see Versions)
  • [ ] When reporting a perceived soundness bug: Does it affect lake check --paranoid? (not a soundness issue)

Description

For several zone-offset patterns, GenericFormat.parse rejects strings that GenericFormat.format produces with the same pattern:

  • O and OOOO format a zero offset as GMT (pinned in tests/elab/timeFormats.lean), but the parser (pstring "GMT" *> parseOffset …, src/Std/Time/Format/Basic.lean L835-838) requires a sign after GMT. ZZZZ produces the same GMT and parses it, because it makes the offset optional (L864-867).
  • xxxxx formats +05:30 when the seconds are zero (pinned in the tests as "+09:00"), but the parser uses parseOffset .yes .yes true (L858-859) and requires seconds. XXXXX produces the same string and parses it (.yes .optional true, L846).
  • Z/ZZ/ZZZ are documented as "output ±HHMMss (no colon); parse ±HHMM[ss]" (Format/Modifier.lean L669), but the parser uses parseOffset .yes .no false (L862-863) and rejects the seconds that the formatter writes.

https://github.com/leanprover/lean4/blob/38be6e2fb0a68a1660f6309c869cc630273e15be/src/Std/Time/Format/Basic.lean#L835-L870

Context

tests/elab/timeParse.lean has -- OOOO round-trips checks for O and OOOO, but only with non-zero offsets. In CLDR, GMT is the zero-offset form of O/OOOO, and the seconds of xxxxx are optional. A program that formats timestamps with O/OOOO and parses them back fails for every UTC timestamp. (#9025 discusses ZonedDateTime parsing usability in general; this report is only about patterns that cannot read their own output.)

Steps to Reproduce

import Std.Time
open Std.Time

def iso : GenericFormat .any := datespec("uuuu-MM-dd'T'HH:mm:ssXXXXX")
def utc := iso.parse! "2024-01-01T13:00:00Z"
def ist := iso.parse! "2024-01-01T13:00:00+05:30"
def lmt := iso.parse! "2024-01-01T13:00:00+01:30:15"

def roundTrip (pat : String) (d : DateTime) : String :=
  let f : GenericFormat .any := GenericFormat.spec! pat
  let s := f.format d
  match f.parse s with
  | .ok d' => s!"{pat}: {s.quote} -> ok {iso.format d'}"
  | .error e => s!"{pat}: {s.quote} -> error: {e}"

#eval roundTrip "uuuu-MM-dd HH:mm O" utc
#eval roundTrip "uuuu-MM-dd HH:mm OOOO" utc
#eval roundTrip "uuuu-MM-dd HH:mm ZZZZ" utc   -- same output, accepted
#eval roundTrip "uuuu-MM-dd HH:mm xxxxx" ist
#eval roundTrip "uuuu-MM-dd HH:mm XXXXX" ist  -- same output, accepted
#eval roundTrip "uuuu-MM-dd HH:mm ZZZ" lmt

Expected behavior: every line ends in ok … with the original offset.

Actual behavior:

"uuuu-MM-dd HH:mm O: \"2024-01-01 13:00 GMT\" -> error: offset 20: unexpected end of input"
"uuuu-MM-dd HH:mm OOOO: \"2024-01-01 13:00 GMT\" -> error: offset 20: unexpected end of input"
"uuuu-MM-dd HH:mm ZZZZ: \"2024-01-01 13:00 GMT\" -> ok 2024-01-01T13:00:00Z"
"uuuu-MM-dd HH:mm xxxxx: \"2024-01-01 13:00 +05:30\" -> error: offset 23: unexpected end of input"
"uuuu-MM-dd HH:mm XXXXX: \"2024-01-01 13:00 +05:30\" -> ok 2024-01-01T13:00:00+05:30"
"uuuu-MM-dd HH:mm ZZZ: \"2024-01-01 13:00 +013015\" -> error: offset 22: expected end of input"

Versions

Identical output on:

  • Lean (version 4.37.0-nightly-2026-10-27, x86_64-unknown-linux-gnu, commit 67bc4fe8130f9b5e1a073c73ed5cda09de6c63fe, Release)
  • Lean (version 4.36.0-rc1, x86_64-unknown-linux-gnu, commit c9afcddf514a6772bd4d649f7cfc04eff0b9f15e, Release)

Ubuntu 24.04 on WSL2. src/Std/Time/Format/Basic.lean is unchanged on master (38be6e2fb0).

Additional Information

Possible fix: after GMT, make the offset optional for O/OOOO, as ZZZZ already does; use parseOffset .yes .optional true for xxxxx and parseOffset .yes .optional false for Z-ZZZ. Adding UTC cases to the existing O/OOOO round-trip tests would cover the first one. (Related, separate report: #16038.)

This report was prepared with AI assistance (Claude Code, Claude Opus 5.5); the reproduction above was run on the toolchains listed.

hiroki-abe-58 avatar Oct 27 '26 22:10 hiroki-abe-58