Skip to content

Commit

Permalink
Do not compute localtime twice
Browse files Browse the repository at this point in the history
  • Loading branch information
brcha committed May 18, 2021
1 parent 7a33f5e commit 4b9c9b9
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,16 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock, std::chrono:
template <typename FormatContext>
auto format(std::chrono::time_point<std::chrono::system_clock> val,
FormatContext& ctx) -> decltype(ctx.out()) {
std::tm time = localtime(val);
std::tm time;
auto epoch = val.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch);
auto subseconds = std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(epoch - seconds);

if (subseconds < subseconds.zero()) {
time = localtime(val - std::chrono::seconds{1});

This comment has been minimized.

Copy link
@ecorm

ecorm May 18, 2021

Should pass std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>{seconds - std::chrono::seconds{1}} instead to localtime so that you're not relying on its rounding behavior.

subseconds = std::chrono::seconds{1} + subseconds;
} else {
time = localtime(val);

This comment has been minimized.

Copy link
@ecorm

ecorm May 18, 2021

Should pass std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>{seconds} instead to localtime so that you're not relying on its rounding behavior.

}

if (subseconds.count() > 0) {
Expand Down

0 comments on commit 4b9c9b9

Please sign in to comment.