mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-25 06:21:00 +00:00
Improve handling of negative durations
This commit is contained in:
parent
38a85502ed
commit
c1d430e61a
@ -430,11 +430,14 @@ struct chrono_formatter {
|
|||||||
|
|
||||||
explicit chrono_formatter(FormatContext& ctx, OutputIt o,
|
explicit chrono_formatter(FormatContext& ctx, OutputIt o,
|
||||||
std::chrono::duration<Rep, Period> d)
|
std::chrono::duration<Rep, Period> d)
|
||||||
: context(ctx),
|
: context(ctx), out(o), val(d.count()) {
|
||||||
out(o),
|
if (d.count() < 0) {
|
||||||
val(d.count()),
|
d = -d;
|
||||||
s(std::chrono::duration_cast<seconds>(d)),
|
*out++ = '-';
|
||||||
ms(std::chrono::duration_cast<milliseconds>(d - s)) {}
|
}
|
||||||
|
s = std::chrono::duration_cast<seconds>(d);
|
||||||
|
ms = std::chrono::duration_cast<milliseconds>(d - s);
|
||||||
|
}
|
||||||
|
|
||||||
int hour() const { return to_int(mod((s.count() / 3600), 24)); }
|
int hour() const { return to_int(mod((s.count() / 3600), 24)); }
|
||||||
|
|
||||||
|
@ -310,4 +310,8 @@ TEST(ChronoTest, LargeDuration) {
|
|||||||
EXPECT_EQ("40", fmt::format("{:%S}", std::chrono::duration<double>(1e20)));
|
EXPECT_EQ("40", fmt::format("{:%S}", std::chrono::duration<double>(1e20)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(ChronoTest, NegativeDuration) {
|
||||||
|
EXPECT_EQ("-00:01", fmt::format("{:%M:%S}", std::chrono::duration<double>(-1)));
|
||||||
|
}
|
||||||
|
|
||||||
#endif // FMT_STATIC_THOUSANDS_SEPARATOR
|
#endif // FMT_STATIC_THOUSANDS_SEPARATOR
|
||||||
|
Loading…
Reference in New Issue
Block a user