Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for subsecond resolution for time_point #2292

Closed
wants to merge 7 commits into from
21 changes: 18 additions & 3 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,11 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
auto format(std::chrono::time_point<std::chrono::system_clock> val,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a template accepting any std::chrono::time_point<std::chrono::system_clock, Period>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or more precisely, the type of the first parameter should be std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<Rep, Period>>.

FormatContext& ctx) -> decltype(ctx.out()) {
std::tm time = localtime(val);
return formatter<std::tm, Char>::format(time, ctx);
auto epoch = val.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::milliseconds>(epoch).count() / 1000;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just duration_cast<std::chrono::seconds>

auto today = std::chrono::high_resolution_clock::duration(std::chrono::milliseconds(seconds * 1000));
auto subseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch - today);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just do subseconds = epoch - seconds.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Times preceding the epoch (1970-01-01) need to be considered so that subseconds is never negative.

return formatter<std::tm, Char>::format(time, ctx, subseconds.count());
}
};

Expand All @@ -428,14 +432,20 @@ template <typename Char> struct formatter<std::tm, Char> {
}

template <typename FormatContext>
auto format(const std::tm& tm, FormatContext& ctx) const
auto format(const std::tm& tm, FormatContext& ctx, int subseconds) const
-> decltype(ctx.out()) {
basic_memory_buffer<Char> tm_format;
tm_format.append(specs.begin(), specs.end());
// By appending an extra space we can distinguish an empty result that
// indicates insufficient buffer size from a guaranteed non-empty result
// https://github.com/fmtlib/fmt/issues/2238
tm_format.push_back(' ');
auto hasS = std::find(tm_format.begin(), tm_format.end(), 'S') != tm_format.end();
auto hasT = std::find(tm_format.begin(), tm_format.end(), 'T') != tm_format.end();
auto writeSubseconds = (hasS || hasT) && (subseconds != 0);
if (writeSubseconds)
tm_format.push_back('.');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to check locale an insert the preferred decimal separator?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should check, but I don't know how to get the locale in the chrono formatter. I'll try to grep for anything related to locale.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locale can be addressed separately, the default is locale-independent anyway.

else
tm_format.push_back(' ');
tm_format.push_back('\0');
basic_memory_buffer<Char> buf;
size_t start = buf.size();
Expand All @@ -449,6 +459,11 @@ template <typename Char> struct formatter<std::tm, Char> {
const size_t MIN_GROWTH = 10;
buf.reserve(buf.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
}

if (writeSubseconds) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writeSubseconds -> write_subseconds (naming convention).

buf.append(std::to_string(subseconds));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not add leading zeroes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comments. I did notice I've overlooked some of those, but I was out. Should be fixed now.

}

// Remove the extra space.
return std::copy(buf.begin(), buf.end() - 1, ctx.out());
}
Expand Down