-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 1 commit
ac6dd9f
10591ac
e2c2704
3bf4aba
e41100c
7a33f5e
4b9c9b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just |
||
auto today = std::chrono::high_resolution_clock::duration(std::chrono::milliseconds(seconds * 1000)); | ||
auto subseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch - today); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just do There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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('.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to check locale an insert the preferred decimal separator? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
buf.append(std::to_string(subseconds)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not add leading zeroes. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
|
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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>>
.