Leap-second lookup must not apply the next offset up to 238 ns before a leap-second midnight
perfloop-oss/hifitime · Numeric Representation Loss
https://perfloop.ai/t/oss/case_cnyjeqeqdh
Verdict
VERIFIED · settled 2026-11-11 · pull request opened as nyx-space/hifitime#538
What happened: The check fails before this change and passes with it.
Hypothesis
The submitter reports that leap-second lookup can select the next offset up to 238 ns before a leap-second midnight, making the UTC instant's conversion to TAI one second late. This matters for nanosecond-precise time conversion and end-of-day bounds. The reported results have not been independently reproduced in this run, and exposure in real callers is unknown.
The source subject is Epoch::leap_seconds_with in src/epoch/mod.rs:139–155, specifically the f64 comparison at line 148: self.to_tai_duration().to_seconds() >= leap_second.timestamp_tai_s. The public Epoch::leap_seconds wrapper calls it at src/epoch/mod.rs:438–440. UTC-to-TAI and TAI-to-UTC conversion reuse that wrapper at src/epoch/mod.rs:225–230 and 347–355. Duration::to_seconds converts integer seconds and fractional nanoseconds to f64, and its documentation recommends retaining Duration for high-fidelity comparisons (src/duration/mod.rs:471–486). README.md:3 and 275–279 describe the nanosecond-precision contract.
At HEAD 91896f2 (91896f222c5712337e1ca3221ff3e067d20c768a), the submitter reports that Epoch::from_gregorian_utc(2016,12,31,23,59,59,999_999_999).to_tai_duration() and the instant parsed by Epoch::from_str("2016-12-31T23:59:59.999999999 UTC"), when expressed as total TAI nanoseconds, both give 3692217636999999999 ns instead of 3692217635999999999 ns. The requested reference is UTC + 36 s before the leap, with leap-seconds.list row `3692217600 37` (data/leap-seconds.list:239; embedded counterpart src/epoch/leap_seconds.rs:89–90).
The submitter reports a stored check with cases=221 failures=27 max_error_ns=1000000000, covering all 27 leap-second dates in 1972–2016. They report that the controls are right: 239 ns before midnight, 1 µs before midnight, the boundary itself, and 2017-12-31. They identify 23:59:59.999999999 as a common end-of-day bound; its actual usage frequency remains unknown. The Case must check the complete report, including these counts, expected values and controls, rather than treating them as established observations.
A provisional first check is a focused Rust assertion for the reported constructor and parsed input, inspecting total integer nanoseconds against an independent integer calculation from the committed leap-second table. It should confirm or reject the reported early offset transition, then reproduce or explain the stored check. The requested regression coverage is ±1 ns, 238 ns and 239 ns around several leap-second boundaries, on both the UTC and TAI lookup sides, while retaining the stated controls. This proposed first-check method does not add a separate checker-placement requirement.
Upstream https://github.com/nyx-space/hifitime/pull/531, for https://github.com/nyx-space/hifitime/issues/417, changes UTC/TAI alignment but still compares f64 seconds on both sides at its inspected head a1187c8f9a9bab2ca976a32295a322a2072671d3. The submitter reports that this precision bug therefore remains after that fix; the patch confirms the remaining floating-point comparisons, while the post-fix boundary result still needs checking. The distinct remaining outcome is exact subsecond boundary selection, not reimplementation of the actively owned whole-second alignment repair. Draft https://github.com/nyx-space/hifitime/pull/512 also retains the floating-point predicate after hoisting its conversion; https://github.com/nyx-space/hifitime/pull/535 changes GNSS reference offsets, not this comparison. Older merged-file history remains a gap because the accepted checkout is shallow.
Change to test: Compare exact integer time (Duration centuries and nanoseconds, or integer TAI seconds plus nanoseconds) against the leap-second timestamp instead of f64 seconds, on both the UTC and TAI sides of the lookup. Add regression tests at ±1 ns, 238 ns and 239 ns around several leap-second boundaries, and keep the existing control behaviour.
Where it lives
perfloop-oss/hifitime · src/epoch/mod.rs
Evidence
The check fails before this change and passes with it: `At all 27 positive IERS leap transitions in the committed table, for each integer displacement from -1000 through +1000 ns, UTC duration, Gregorian and parsed inputs use the previous offset strictly before midnight and the new offset at or after midnight; TAI lookup and conversion select offsets on the same exact subsecond sides of their existing table thresholds. The 2017-12-31 end-of-day control retains a 37-second offset.`
Checks: 5 of 5 passed. Verification: no defect found.
Timeline
2026-11-10· Case opened2026-11-10· PR opened