Skip to content

Commit

Permalink
<chrono>: Fix hh_mm_ss subsecond formatting for floats (#1866)
Browse files Browse the repository at this point in the history
* <chrono>: Fix hh_mm_ss subsecond formatting for floats

Before, the same formatting string was used for floats and integrals.
This meant that large floats were formatted using exponent notaion and
small floats were not, and it also meant there was an extra period in a
time, as the subseconds could be fractions of a subsecond (say .4
nanoseconds). Now if the subseconds are floats, we force fixed
formatting to get the right number of leading zeroes and a precision of
0 to round off fractions of subseconds.

* Floor subseconds
  • Loading branch information
eldakesh-ms authored Apr 20, 2021
1 parent d488200 commit cc2651d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions stl/inc/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -5469,9 +5469,14 @@ namespace chrono {
void _Write_seconds(basic_ostream<_CharT, _Traits>& _Os, const hh_mm_ss<_Duration>& _Val) {
_Os << _STD format(_STATICALLY_WIDEN(_CharT, "{:02}"), _Val.seconds().count());
if constexpr (hh_mm_ss<_Duration>::fractional_width > 0) {
_Os << _STD format(_STATICALLY_WIDEN(_CharT, "{0}{1:0{2}}"),
_STD use_facet<numpunct<_CharT>>(_Os.getloc()).decimal_point(), _Val.subseconds().count(),
_Val.fractional_width);
_Os << _STD use_facet<numpunct<_CharT>>(_Os.getloc()).decimal_point();
if constexpr (treat_as_floating_point_v<typename hh_mm_ss<_Duration>::precision::rep>) {
_Os << _STD format(_STATICALLY_WIDEN(_CharT, "{:0{}.0f}"), _STD floor(_Val.subseconds().count()),
_Val.fractional_width);
} else {
_Os << _STD format(
_STATICALLY_WIDEN(_CharT, "{:0{}}"), _Val.subseconds().count(), _Val.fractional_width);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,14 @@ void test_hh_mm_ss_formatter() {
empty_braces_helper(hh_mm_ss{4083007ms}, STR("01:08:03.007"));
empty_braces_helper(hh_mm_ss{65745123ms}, STR("18:15:45.123"));
empty_braces_helper(hh_mm_ss{65745s}, STR("18:15:45"));
empty_braces_helper(hh_mm_ss{0.1ns}, STR("00:00:00.000000000"));
empty_braces_helper(hh_mm_ss{1.45ns}, STR("00:00:00.000000001"));
empty_braces_helper(hh_mm_ss{1.56ns}, STR("00:00:00.000000001"));
empty_braces_helper(hh_mm_ss{1e+8ns}, STR("00:00:00.100000000"));
empty_braces_helper(hh_mm_ss{999'999.9us}, STR("00:00:00.999999"));
empty_braces_helper(hh_mm_ss{59'999'999.9us}, STR("00:00:59.999999"));
empty_braces_helper(hh_mm_ss{3'599'999'999.9us}, STR("00:59:59.999999"));
empty_braces_helper(hh_mm_ss{86'399'999'999.9us}, STR("23:59:59.999999"));

assert(format(STR("{:%H %I %M %S %r %R %T %p}"), hh_mm_ss{13h + 14min + 15351ms})
== STR("13 01 14 15.351 01:14:15 PM 13:14 13:14:15.351 PM"));
Expand Down

0 comments on commit cc2651d

Please sign in to comment.