mirror of
https://github.com/fmtlib/fmt.git
synced 2026-11-09 14:50:24 +08:00
Fix 12-hour formatting of floating-point durations (#4988)
This commit is contained in:
parent
8362a1be15
commit
92d2c541e3
@ -1682,7 +1682,7 @@ struct duration_formatter {
|
||||
|
||||
auto hour12() const -> Rep {
|
||||
Rep hour = static_cast<Rep>(mod((s.count() / 3600), 12));
|
||||
return hour <= 0 ? 12 : hour;
|
||||
return hour < 1 ? 12 : hour;
|
||||
}
|
||||
|
||||
auto minute() const -> Rep {
|
||||
|
||||
@ -545,6 +545,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<double>;
|
||||
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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user