From 61bfea60baa4b1c90034e845f95a04e900000e83 Mon Sep 17 00:00:00 2001 From: vera-rubin <322284620+vera-rubin@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:13:46 +0000 Subject: [PATCH] Fix 12-hour formatting of floating-point durations --- include/fmt/chrono.h | 2 +- test/chrono-test.cc | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 19571164..8c4e88cb 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1681,7 +1681,7 @@ struct duration_formatter { auto hour12() const -> Rep { Rep hour = static_cast(mod((s.count() / 3600), 12)); - return hour <= 0 ? 12 : hour; + return hour < 1 ? 12 : hour; } auto minute() const -> Rep { diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 818ec42f..a3218737 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -499,6 +499,15 @@ TEST(chrono_test, format_specs) { EXPECT_EQ(fmt::format("{:%q}", std::chrono::seconds(12345)), "s"); } +TEST(chrono_test, format_12_hour_fp) { + using seconds = std::chrono::duration; + EXPECT_EQ(fmt::format("{:%I:%M:%S %p}", seconds(1)), "12:00:01 AM"); + EXPECT_EQ(fmt::format("{:%I:%M:%S %p}", seconds(43201)), "12:00:01 PM"); + EXPECT_EQ(fmt::format("{:%I}", seconds(3599)), "12"); + EXPECT_EQ(fmt::format("{:%I}", seconds(3600)), "01"); + EXPECT_EQ(fmt::format("{:%I}", seconds(86401)), "12"); +} + TEST(chrono_test, invalid_specs) { auto sec = std::chrono::seconds(0); EXPECT_THROW_MSG((void)fmt::format(runtime("{:%a}"), sec), fmt::format_error,