[Bug libstdc++/110504] std::format("{:%S}", duration<long, std::ratio<1,2>>(4)) returns "02.0" instead of "02"

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Jun 30 23:16:24 GMT 2023


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110504

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I have a patch to fix this:

--- a/libstdc++-v3/include/bits/chrono_io.h
+++ b/libstdc++-v3/include/bits/chrono_io.h
@@ -1079,7 +1079,9 @@ namespace __format
            {
              locale __loc = _M_locale(__ctx);
              auto __ss = __hms.subseconds();
-             if constexpr (is_floating_point_v<rep>)
+             if (__ss == __ss.zero())
+               ; // Do not write all-zero fractional part.
+             else if constexpr (is_floating_point_v<rep>)
                {
                  __out = std::format_to(__loc, std::move(__out),
                                         _GLIBCXX_WIDEN("{:.{}Lg}"),


However the example in [time.clock.utc.nonmembers] suggests that in fact
printing the zeros is correct. The sys_time<milliseconds> value is printed as
"2015-06-30 23:59:60.000 UTC" which would be altered by the patch above.


More information about the Gcc-bugs mailing list