Std.Time: parsing with `k`, `K` or `y`+`G` (BC) returns the wrong hour or year
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-27through elan, see Versions) - [ ] When reporting a perceived soundness bug: Does it affect
lake check --paranoid? (not a soundness issue)
Description
GenericFormat.parse resolves three pattern letters incorrectly when it builds the DateTime, so it returns a wrong value without any error:
k(clock-hour-of-day, 1-24): every hour comes back one too low, and24becomes 23 instead of 0.K(hour-of-am-pm, 0-11): every hour comes back one too high, so11 PMbecomes 12:xx (noon).ywithG= BC (year-of-era): the proleptic year is off by two (1 BCbecomes year -2 instead of 0).
The formatter already follows the documented meaning of these letters, so parsing the formatter's own output does not give back the original value.
The cause is in the private DateBuilder.build in src/Std/Time/Format/Basic.lean:
k:(Bounded.LE.sub · 1) <$> builder.k(L1021) subtracts 1 from every value instead of mapping only 24 to 0.K:marker.toAbsolute <$> ((Bounded.LE.add · 1) <$> builder.K)(L1014) shiftsKby one and then treats it as a 1-12 clock hour.- era:
convertYearAndEra … | .bce => -(year + 1)(L971-973); the year-of-era convention below needs1 - year.
https://github.com/leanprover/lean4/blob/38be6e2fb0a68a1660f6309c869cc630273e15be/src/Std/Time/Format/Basic.lean#L971-L1023
Context
The Modifier docs define k as clock-hour-of-day (1-24) and K as hour-of-am-pm (0-11), and say of y: "year 1 BC is formatted as 1 paired with a BC era designator". tests/elab/timeFormats.lean states "1 BC = year-of-era 1, 2 BC = year-of-era 2". #13845 aligned the patterns with Java/CLDR, where k = 24 means hour 0, hour = K + 12·PM, and the BCE year is 1 − year-of-era. The existing tests check the formatting side of k, K and G, not parsing.
Steps to Reproduce
import Std.Time
open Std.Time
def iso : GenericFormat .any := datespec("uuuu-MM-dd'T'HH:mm:ssXXX")
def check (pat input : String) : String :=
let f : GenericFormat .any := GenericFormat.spec! pat
match f.parse input with
| .ok d => s!"{pat} | {input.quote} -> {iso.format d}"
| .error e => s!"{pat} | {input.quote} -> error: {e}"
#eval check "uuuu-MM-dd kk:mm" "2024-01-01 13:00" -- expected T13:00
#eval check "uuuu-MM-dd kk:mm" "2024-01-01 24:00" -- expected T00:00
#eval check "uuuu-MM-dd KK:mm a" "2024-01-01 00:30 AM" -- expected T00:30
#eval check "uuuu-MM-dd KK:mm a" "2024-01-01 05:30 PM" -- expected T17:30
#eval check "y-MM-dd G" "1-03-01 BC" -- expected 0000-03-01
#eval check "y-MM-dd G" "2-03-01 BC" -- expected -0001-03-01
-- The formatter produces these strings itself:
def d := iso.parse! "2024-01-01T13:00:00Z"
#eval (GenericFormat.spec! "kk:mm" : GenericFormat .any).format d -- "13:00"
#eval (GenericFormat.spec! "KK:mm a" : GenericFormat .any).format d -- "01:00 PM"
#eval (GenericFormat.spec! "y-MM-dd G" : GenericFormat .any).format (iso.parse! "-0001-03-01T00:00:00Z") -- "2-03-01 BC"
Expected behavior: 13:00, 00:00, 00:30, 17:30, 0000-03-01 and -0001-03-01, so that parsing the formatter's output gives back the original value.
Actual behavior:
"uuuu-MM-dd kk:mm | \"2024-01-01 13:00\" -> 2024-01-01T12:00:00Z"
"uuuu-MM-dd kk:mm | \"2024-01-01 24:00\" -> 2024-01-01T23:00:00Z"
"uuuu-MM-dd KK:mm a | \"2024-01-01 00:30 AM\" -> 2024-01-01T01:30:00Z"
"uuuu-MM-dd KK:mm a | \"2024-01-01 05:30 PM\" -> 2024-01-01T18:30:00Z"
"y-MM-dd G | \"1-03-01 BC\" -> -0002-03-01T00:00:00Z"
"y-MM-dd G | \"2-03-01 BC\" -> -0003-03-01T00:00:00Z"
"13:00"
"01:00 PM"
"2-03-01 BC"
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: map k with if k = 24 then 0 else k; compute the K hour as K + (if marker = .pm then 12 else 0) instead of going through HourMarker.toAbsolute; use 1 - year for .bce in convertYearAndEra. HH, hh a and u are not affected.
This report was prepared with AI assistance (Claude Code, Claude Opus 5.5); the reproduction above was run on the toolchains listed.