Skip to content

Fix fractional seconds for sys_time with custom periods - #5034

Open
vera-rubin wants to merge 2 commits into
fmtlib:mainfrom
vera-rubin:fix/timestamp-fractions
Open

vera-rubin wants to merge 2 commits into
fmtlib:mainfrom
vera-rubin:fix/timestamp-fractions

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

A sys_time whose period is not a divisor of one second loses its fractional seconds:

using tick = std::chrono::duration<int, std::ratio<3, 2>>;
fmt::format("{:%S}", fmt::sys_time<tick>(tick(1)));   // "01.0", expected "01.5"
fmt::format("{:%S}", fmt::sys_time<tick>(tick(-1)));  // "59.0", expected "58.5"

The remainder epoch - duration_cast<seconds>(epoch) is half a second here, but it is cast back to Duration (3/2 s ticks), which truncates it to zero. This keeps it in the common duration type, as the local_time formatter already does since 4ded41f. utc_time goes through the same code.

The new test fails on main and passes with the change. The full test suite passes (Release, C++20), and chrono-test also passes when built as C++11 and C++17.

@vera-rubin
vera-rubin requested a review from vitaut as a code owner November 19, 2026 13:41
@vitaut vitaut closed this Nov 27, 2026
@vitaut vitaut reopened this Nov 27, 2026
@vitaut

vitaut commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

For some reason, GitHub Actions hasn't been triggered. Closing and reopening the PR as a workaround.

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase to fix CI fuzz signal.

Keep the subsecond remainder of a sys_time in the common duration type
instead of casting it back to Duration, which truncates it when one
second is not a whole number of Duration ticks, e.g. a 3/2 s period.
@vera-rubin
vera-rubin force-pushed the fix/timestamp-fractions branch from 3b0ed16 to 2387a5c Compare November 28, 2026 12:36
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Rebased on main, which has the CIFuzz update; the change itself is the same single commit.

@vitaut vitaut closed this Nov 28, 2026
@vitaut vitaut reopened this Nov 28, 2026
Comment thread include/fmt/chrono.h Outdated
Comment on lines +2238 to +2239
auto second =
detail::duration_cast<decltype(subsecs)>(std::chrono::seconds(1));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explicit duration cast seems unnecessary.

Drop the explicit duration cast: std::chrono::seconds converts implicitly
to the common duration type of the subsecond remainder.
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Removed the explicit cast, thanks: std::chrono::seconds(1) converts implicitly to the common duration type, as in the local_time formatter.

@vitaut

vitaut commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

For some reason, GitHub Actions doesn't get triggered by your pushes, but closing and reopening the PR does trigger CI. There might be an account-specific restriction on GitHub's side. Could you contact GitHub Support to investigate?

@vera-rubin

Copy link
Copy Markdown
Contributor Author

Thanks for re-running it. I see the same thing on my PRs in other repos since about Nov 23: my own pushes and new PRs create no Actions runs at all, while a maintainer's close/reopen does. So it looks account-side, and I've opened a ticket with GitHub Support.

@vera-rubin vera-rubin closed this Nov 30, 2026
@vera-rubin vera-rubin reopened this Nov 30, 2026
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.

2 participants