Skip to content

[core] Fix FormatTimeSys rendering the same timestamp inconsistently (#3225)#3342

Open
steps-re wants to merge 1 commit into
Haivision:masterfrom
steps-re:fix/formattimesys-3225
Open

[core] Fix FormatTimeSys rendering the same timestamp inconsistently (#3225)#3342
steps-re wants to merge 1 commit into
Haivision:masterfrom
steps-re:fix/formattimesys-3225

Conversation

@steps-re

@steps-re steps-re commented Jul 7, 2026

Copy link
Copy Markdown

Fixes #3225.

Bug

FormatTimeSys() renders a steady_clock timestamp as wall-clock time, but it combined whole-second wall time from ::time() with the sub-second part of steady_clock::now(). Those clocks have unrelated sub-second phases (steady counts from an arbitrary epoch), so near a second boundary the same timestamp rendered one second apart between calls — the ±1s discrepancy in the issue. Per-call re-sampling of two clocks non-atomically also adds ~1µs jitter.

Fix

Map the timestamp through a single steady↔wall reference pair, captured once at load time, in one consistent microsecond domain. A given timestamp now always formats to exactly the same string. The arithmetic is factored into a pure, testable overload FormatTimeSys(target_us, steady_now_us, wall_now_us). This touches only the [SYST] logging/trace formatter — not the transport path.

Tradeoff (for maintainer review)

This caches the steady↔wall offset for stability. If the wall clock is NTP-stepped mid-run, [SYST] times drift by the step amount but stay internally consistent. The alternative — live re-tracking with only the flicker removed — is also reasonable; happy to switch to that if you prefer. Flagging since it's your issue and touches a utility.

Verification

  • New Sync.FormatTimeSysStable sweeps the reference across misaligned steady/wall second boundaries and asserts a fixed timestamp renders identically — fails on the old arithmetic, passes with the fix.
  • Full unit suite: 271/271 pass. --gtest_repeat=200 on Sync.FormatTime* → 200/200 (also removes a latent flake in the existing back-to-back-equality test).
  • Built with -DENABLE_UNITTESTS=ON, no new warnings. DCO sign-off included.

FormatTimeSys mapped a steady-clock timestamp onto wall-clock time by
combining whole-second wall time from ::time() with the sub-second part
of steady_clock::now(). The two clocks have unrelated sub-second phases
(steady counts from an arbitrary epoch such as boot), so near a second
boundary the very same timestamp could render one second apart between
calls (issue Haivision#3225).

Map the timestamp using a single steady<->wall reference pair captured
once at load time, in a consistent microsecond domain. This removes the
+/-1 s flicker and also the ~1 us jitter that per-call re-sampling would
cause, so a given timestamp always formats to exactly the same string.

The mapping arithmetic is factored into a pure overload
FormatTimeSys(target_us, steady_now_us, wall_now_us) so it can be unit
tested deterministically. Adds a regression test (Sync.FormatTimeSysStable)
that sweeps the "now" reference across steady/wall second boundaries with
misaligned phases and asserts the output is stable.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Mike German <mike@stepsventures.com>
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.

[BUG] FormatTimeSys happens to work incorrectly.

1 participant