Skip to content

Commit

Permalink
Improve handling of negative durations
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed May 5, 2019
1 parent 38a8550 commit c1d430e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 8 additions & 5 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,14 @@ struct chrono_formatter {

explicit chrono_formatter(FormatContext& ctx, OutputIt o,
std::chrono::duration<Rep, Period> d)
: context(ctx),
out(o),
val(d.count()),
s(std::chrono::duration_cast<seconds>(d)),
ms(std::chrono::duration_cast<milliseconds>(d - s)) {}
: context(ctx), out(o), val(d.count()) {
if (d.count() < 0) {
d = -d;
*out++ = '-';
}
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)); }

Expand Down
4 changes: 4 additions & 0 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,8 @@ TEST(ChronoTest, LargeDuration) {
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

0 comments on commit c1d430e

Please sign in to comment.