Added support for subsecond resolution for time_point and %T or %S formats
This commit is contained in:
parent
0dd91e20d5
commit
ac6dd9f6de
@ -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,
|
||||
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;
|
||||
auto today = std::chrono::high_resolution_clock::duration(std::chrono::milliseconds(seconds * 1000));
|
||||
auto subseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch - today);
|
||||
return formatter<std::tm, Char>::format(time, ctx, subseconds.count());
|
||||
}
|
||||
};
|
||||
|
||||
@ -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('.');
|
||||
else
|
||||
tm_format.push_back(' ');
|
||||
tm_format.push_back('\0');
|
||||
basic_memory_buffer<Char> buf;
|
||||
size_t start = buf.size();
|
||||
@ -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) {
|
||||
buf.append(std::to_string(subseconds));
|
||||
}
|
||||
|
||||
// Remove the extra space.
|
||||
return std::copy(buf.begin(), buf.end() - 1, ctx.out());
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user