-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 all commits
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 |
---|---|---|
|
@@ -405,13 +405,33 @@ inline size_t strftime(wchar_t* str, size_t count, const wchar_t* format, | |
|
||
FMT_END_DETAIL_NAMESPACE | ||
|
||
template <typename Char, typename Duration> | ||
struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>, | ||
template <typename Char, typename Rep, typename Period> | ||
struct formatter<std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<Rep, Period>>, | ||
Char> : formatter<std::tm, Char> { | ||
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}); | ||
subseconds = std::chrono::seconds{1} + 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. nit: |
||
} else { | ||
time = localtime(val); | ||
} | ||
|
||
if (subseconds.count() > 0) { | ||
auto width = std::to_string(Period::den).size() - 1; | ||
std::basic_stringstream<Char> os; | ||
os.fill('0'); | ||
os.width(width); | ||
os << subseconds.count(); | ||
auto formatted_ss = os.str(); | ||
Comment on lines
+427
to
+432
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. Please use auto formatted_ss format("{:0{}}", subseconds.count(), width);
return formatter<std::tm, Char>::format(time, ctx, bytes(formatted_ss)); |
||
return formatter<std::tm, Char>::format(time, ctx, formatted_ss); | ||
} | ||
return formatter<std::tm, Char>::format(time, ctx); | ||
} | ||
}; | ||
|
@@ -428,14 +448,21 @@ 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, const std::basic_string<Char> 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.size() > 0); | ||
if (writeSubseconds) | ||
// should be std::use_facet<std::numpunct<Char>>(locale).decimal_point(); | ||
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,8 +476,15 @@ template <typename Char> struct formatter<std::tm, Char> { | |
const size_t MIN_GROWTH = 10; | ||
buf.reserve(buf.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH)); | ||
} | ||
|
||
auto buf_end = buf.end() - 1; | ||
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(subseconds); | ||
buf_end = buf.end(); | ||
} | ||
|
||
// Remove the extra space. | ||
return std::copy(buf.begin(), buf.end() - 1, ctx.out()); | ||
return std::copy(buf.begin(), buf_end, ctx.out()); | ||
} | ||
|
||
basic_string_view<Char> specs; | ||
|
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>>
.