Skip to content

Commit

Permalink
Handle subsecond time before epoch start correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
brcha committed May 18, 2021
1 parent e41100c commit 7a33f5e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,12 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock, std::chrono:
std::tm time = localtime(val);

This comment has been minimized.

Copy link
@ecorm

ecorm May 18, 2021

This is computed needlessly when the given time precedes the epoch (it is recomputed again on line 420).

auto epoch = val.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch);
// auto subseconds = abs(std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(epoch - seconds));
auto subseconds = std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(epoch - seconds);

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

This comment has been minimized.

Copy link
@ecorm

ecorm May 18, 2021

This does indeed seem to effectively perform the std::chrono::floor operation. Testing will confirm.


if (subseconds.count() > 0) {
auto width = std::to_string(Period::den).size() - 1;
Expand Down

0 comments on commit 7a33f5e

Please sign in to comment.