Conversation
|
Could you copy the summary (updating it if needed) from #3122? |
| return; | ||
| } | ||
|
|
||
| FMT_ASSERT(std::is_floating_point<typename Duration::rep>::value, ""); |
There was a problem hiding this comment.
Let's add an alias for typename Duration::rep and use it here and in several places below.
| } | ||
|
|
||
| template <typename Char, typename OutputIt, typename Duration> | ||
| void write_fractional_seconds(OutputIt& out, Duration d, int precision) { |
There was a problem hiding this comment.
Let's merge this overload with the one above to reduce code duplication.
| : std::chrono::duration_cast<subsecond_precision>(fractional) | ||
| .count(); | ||
| uint32_or_64_or_128_t<long long> n = | ||
| to_unsigned(to_nonnegative_int(subseconds, max_value<long long>())); |
There was a problem hiding this comment.
Can subseconds be negative?
There was a problem hiding this comment.
It's nonnegative because fractional is nonnegative:
const auto fractional = detail::abs(d) - std::chrono::duration_cast<std::chrono::seconds>(d);But subseconds can be signed or unsigned and to_unsigned raises error if argument is unsigned, so i'll make static_cast.
| template <typename Duration> | ||
| void write_floating_seconds(memory_buffer& buf, Duration duration) { | ||
| FMT_ASSERT(std::is_floating_point<typename Duration::rep>::value, ""); | ||
| using Rep = typename Duration::rep; |
There was a problem hiding this comment.
nit: Rep -> rep to follow naming conventions
| void write_floating_seconds(memory_buffer& buf, Duration duration, | ||
| int precision) { |
There was a problem hiding this comment.
Similarly, let's merge the two write_floating_seconds overloads.
| bool is_floating_point = false; | ||
| bool has_precision = false; |
There was a problem hiding this comment.
We only need one flag for the condition has_precision && !is_floating_point, something like has_precision_integral.
|
|
||
| const auto fractional = | ||
| detail::abs(d) - std::chrono::duration_cast<std::chrono::seconds>(d); | ||
| const auto& subseconds = |
| if (precision >= 0) { | ||
| write_fractional_seconds<char_type>( | ||
| out, std::chrono::duration<rep, Period>(val), precision); | ||
| } else { | ||
| write_fractional_seconds<char_type>( | ||
| out, std::chrono::duration<rep, Period>(val)); | ||
| } |
There was a problem hiding this comment.
Now that write_fractional_seconds overloads have been merged, the if statement is no longer needed. You can just call
write_fractional_seconds<char_type>(
out, std::chrono::duration<rep, Period>(val), precision);
|
Thanks! |
Continuation of #3122
Add precision modifier for seconds in chrono format and test for it.
It's became important for custom datetime types that store date, time and subseconds. I want to use chrono format for those types, but I have no way to control subsecond precision.
Use case:
So precision can be applied not only for %Q spec, but for %S too.