]> gcc.gnu.org Git - gcc.git/commit
libstdc++: Fix std::format for floating-point chrono::time_point [PR113500]
authorJonathan Wakely <jwakely@redhat.com>
Sun, 21 Jan 2024 18:16:14 +0000 (18:16 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 30 Jan 2024 14:54:37 +0000 (14:54 +0000)
commita5aca83ca9c7fac895d10eb7b3e14b1927ec1eac
treec851e93dad0067db7331b2c72c4559ae32925c13
parent4cdcfb4f84c9aa668731e3ee57dc686432097951
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.

(cherry picked from commit 7431fcea6b72beb54abb1932c254ac0e76bd0bde)
libstdc++-v3/include/bits/chrono_io.h
libstdc++-v3/testsuite/std/time/clock/file/io.cc
libstdc++-v3/testsuite/std/time/clock/gps/io.cc
libstdc++-v3/testsuite/std/time/clock/utc/io.cc
libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc
This page took 0.063263 seconds and 6 git commands to generate.