]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::format of chrono::local_days with {} [PR120293]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 15 May 2025 18:32:01 +0000 (19:32 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 19 May 2025 09:44:35 +0000 (10:44 +0100)
Formatting of chrono::local_days with an empty chrono-specs should be
equivalent to inserting it into an ostream, which should use the
overload for inserting chrono::sys_days into an ostream. The
implementation of empty chrono-specs in _M_format_to_ostream takes some
short cuts, and that wasn't being done correctly for chrono::local_days.

libstdc++-v3/ChangeLog:

PR libstdc++/120293
* include/bits/chrono_io.h (_M_format_to_ostream): Add special
case for local_time convertible to local_days.
* testsuite/std/time/clock/local/io.cc: Check formatting of
chrono::local_days.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/chrono_io.h
libstdc++-v3/testsuite/std/time/clock/local/io.cc

index ace8b9f2629208fba9f371b3cffea2c1aa8b3acc..92a3098e808c74384f9c0a6a2a7de762b6f3ecde 100644 (file)
@@ -766,6 +766,9 @@ namespace __format
                  // sys_time with period greater or equal to days:
                  if constexpr (is_convertible_v<_Tp, chrono::sys_days>)
                    __os << _S_date(__t);
+                 // Or a local_time with period greater or equal to days:
+                 else if constexpr (is_convertible_v<_Tp, chrono::local_days>)
+                   __os << _S_date(__t);
                  else // Or it's formatted as "{:L%F %T}":
                    {
                      auto __days = chrono::floor<chrono::days>(__t);
index b4d562f36d12061f9cd5699afcc653e51d301b62..67818e876497acc62a7fcd1ee7669c21e1ea422e 100644 (file)
@@ -89,6 +89,9 @@ test_format()
 
   s = std::format("{}", local_seconds{});
   VERIFY( s == "1970-01-01 00:00:00" );
+
+  s = std::format("{}", local_days{}); // PR libstdc++/120293
+  VERIFY( s == "1970-01-01" );
 }
 
 void