-
- Downloads
libstdc++: Fix std::format for floating-point chrono::time_point [PR113500]
Currently trying to use std::format with certain specializations of std::chrono::time_point is ill-formed, due to one member function of the __formatter_chrono type which tries to write a time_point to an ostream. For sys_time<floating-point> or sys_time with a period greater than days there is no operator<< that can be used. That operator<< is only needed when using an empty chrono-specs in the format string, like "{}", but the ill-formed expression gives an error even if not actually used. This means it's not possible to format some other specializations of chrono::time_point, even when using a non-empty chrono-specs. This fixes it by avoiding using 'os << t' for all chrono::time_point specializations, and instead using std::format("{:L%F %T}", t). So that we continue to reject std::format("{}", sys_time{1.0s}) a check for empty chrono-specs is added to the formatter<sys_time<D>, C> specialization. While testing this I noticed that the output for %S with a floating-point duration was incorrect, as the subseconds part was being appended to the seconds without a decimal point, and without the correct number of leading zeros. libstdc++-v3/ChangeLog: PR libstdc++/113500 * include/bits/chrono_io.h (__formatter_chrono::_M_S): Fix printing of subseconds with floating-point rep. (__formatter_chrono::_M_format_to_ostream): Do not write time_point specializations directly to the ostream. (formatter<chrono::sys_time<D>, C>::parse): Do not allow an empty chrono-spec if the type fails to meet the constraints for writing to an ostream with operator<<. * testsuite/std/time/clock/file/io.cc: Check formatting non-integral times with empty chrono-specs. * testsuite/std/time/clock/gps/io.cc: Likewise. * testsuite/std/time/clock/utc/io.cc: Likewise. * testsuite/std/time/hh_mm_ss/io.cc: Likewise.
Showing
- libstdc++-v3/include/bits/chrono_io.h 51 additions, 20 deletionslibstdc++-v3/include/bits/chrono_io.h
- libstdc++-v3/testsuite/std/time/clock/file/io.cc 17 additions, 0 deletionslibstdc++-v3/testsuite/std/time/clock/file/io.cc
- libstdc++-v3/testsuite/std/time/clock/gps/io.cc 27 additions, 0 deletionslibstdc++-v3/testsuite/std/time/clock/gps/io.cc
- libstdc++-v3/testsuite/std/time/clock/utc/io.cc 4 additions, 0 deletionslibstdc++-v3/testsuite/std/time/clock/utc/io.cc
- libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc 25 additions, 3 deletionslibstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc
Loading
Please register or sign in to comment