Skip to content

fix: properly format fractional seconds for timestamps prior to POSIX epoch (fixes #4975) - #4976

Merged
vitaut merged 1 commit into
fmtlib:mainfrom
ReturnKartikey:fix-4975-negative-time-subseconds
Oct 18, 2026
Merged

vitaut merged 1 commit into
fmtlib:mainfrom
ReturnKartikey:fix-4975-negative-time-subseconds

Conversation

@ReturnKartikey

Copy link
Copy Markdown
Contributor

Fixes #4975

Summary of Changes

When formatting chrono time points prior to the POSIX epoch (1970-01-01) with subsecond specifiers (e.g., :%S), formatter<local_time<Duration>, Char>::format in include/fmt/chrono.h passed negative subsecond durations to write_fractional_seconds. Because write_fractional_seconds casts duration counts to uint64_t, this caused an unsigned integer underflow and produced corrupted outputs like 02.18446744073709550739 instead of 01.123.

This patch normalizes negative subsecond durations by adjusting the whole seconds count and shifting fractional units into the positive range (subsecs += second) before calling write_fractional_seconds.

Verification

  • Added regression tests in test/chrono-test.cc covering negative millisecond, microsecond, and nanosecond formatting (e.g. epoch - 250ms -> 59.750, -14398877ms -> 01.123).
  • Verified 100% test pass rate across chrono-test (34/34 tests) and full ctest suite (22/22 tests).

@ReturnKartikey
ReturnKartikey requested a review from vitaut as a code owner October 7, 2026 20:26
@madscientist

Copy link
Copy Markdown

Any thoughts on this patch @vitaut ? Cheers!

@vitaut
vitaut merged commit 6ba294f into fmtlib:main Oct 18, 2026
47 checks passed
@vitaut

vitaut commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix!

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.

fmt fails to properly print time values prior to the POSIX epoch

3 participants