Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -1667,10 +1667,8 @@ struct duration_formatter {
return true;
}
// must be +-inf
if (val > 0)
std::copy_n("inf", 3, out);
else
std::copy_n("-inf", 4, out);
write_sign();
std::copy_n("inf", 3, out);
return true;
}

Expand Down Expand Up @@ -1795,7 +1793,7 @@ struct duration_formatter {
auto buf = memory_buffer();
write_floating_seconds(buf, std::chrono::duration<rep, Period>(val),
precision);
if (negative) *out++ = '-';
write_sign();
if (buf.size() < 2 || buf[1] == '.')
out = detail::write_padding(out, pad);
out = copy<Char>(buf.begin(), buf.end(), out);
Expand Down
29 changes: 29 additions & 0 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,35 @@ TEST(chrono_test, special_durations) {
"01.234");
}

TEST(chrono_test, negative_floating_duration_sign) {
auto d = std::chrono::duration<double>(-61.25);
EXPECT_EQ(fmt::format("{:%S %M}", d), "-01.250000 01");
EXPECT_EQ(fmt::format("{:%S %S}", d), "-01.250000 01.250000");
EXPECT_EQ(fmt::format("{:%S %Q}", d), "-01.250000 61.25");
EXPECT_EQ(fmt::format("{:%Q %S}", d), "-61.25 01.250000");
EXPECT_EQ(fmt::format("{:%S %Q}", -d), "01.250000 61.25");
EXPECT_EQ(fmt::format("{:%S %Q}", std::chrono::duration<float>(-1)), "-01 1");
EXPECT_EQ(fmt::format("{:%S %Q}", std::chrono::duration<long double>(-1)),
"-01 1");
}

TEST(chrono_test, negative_infinite_duration_sign) {
auto inf = std::numeric_limits<double>::infinity();
auto d = std::chrono::duration<double>(-inf);
EXPECT_EQ(fmt::format("{:%Q}", d), "-inf");
EXPECT_EQ(fmt::format("{:%S}", d), "-inf");
EXPECT_EQ(fmt::format("{:%H}", d), "-inf");
EXPECT_EQ(fmt::format("{:%j}", d), "-inf");
EXPECT_EQ(fmt::format("{:%R}", d), "-inf:inf");
EXPECT_EQ(fmt::format("{:%T}", d), "-inf:inf:inf");
EXPECT_EQ(fmt::format("{:%S %Q}", d), "-inf inf");
EXPECT_EQ(fmt::format("{:>6%Q}", d), " -inf");
EXPECT_EQ(fmt::format("{:%Q %S}", -d), "inf inf");
EXPECT_EQ(fmt::format("{:%Q}", std::chrono::duration<float>(-inf)), "-inf");
EXPECT_EQ(fmt::format("{:%Q}", std::chrono::duration<long double>(-inf)),
"-inf");
}

TEST(chrono_test, unsigned_duration) {
EXPECT_EQ(fmt::format("{}", std::chrono::duration<unsigned>(42)), "42s");
}
Expand Down
Loading