[gcc r13-10318] libstdc++: Fix std::format of chrono::local_days with {} [PR120293]

Jonathan Wakely redi@gcc.gnu.org
Thu May 14 13:15:52 GMT 2026


https://gcc.gnu.org/g:7206d4b9f95a48ab11cebc51bb10a67964a129f3

commit r13-10318-g7206d4b9f95a48ab11cebc51bb10a67964a129f3
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu May 15 19:32:01 2025 +0100

    libstdc++: Fix std::format of chrono::local_days with {} [PR120293]
    
    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>
    (cherry picked from commit 1ed7585bf60ba9940ca5dc6d2c72dba86eea7b4d)

Diff:
---
 libstdc++-v3/include/bits/chrono_io.h             | 3 +++
 libstdc++-v3/testsuite/std/time/clock/local/io.cc | 6 ++++++
 2 files changed, 9 insertions(+)

diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h
index 63c0a8949bcd..2a7527cae2d6 100644
--- a/libstdc++-v3/include/bits/chrono_io.h
+++ b/libstdc++-v3/include/bits/chrono_io.h
@@ -728,6 +728,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);
diff --git a/libstdc++-v3/testsuite/std/time/clock/local/io.cc b/libstdc++-v3/testsuite/std/time/clock/local/io.cc
index 33eb9f08e806..85fd734fd634 100644
--- a/libstdc++-v3/testsuite/std/time/clock/local/io.cc
+++ b/libstdc++-v3/testsuite/std/time/clock/local/io.cc
@@ -87,6 +87,12 @@ test_format()
   s = std::format("{:%Z %T %F %z %Ez}", ltf);
   __builtin_puts(s.c_str());
   VERIFY( s == "FOO 20:22:02 2024-07-28 -0100 -01:00" );
+
+  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" );
 }
 
 int main()


More information about the Libstdc++-cvs mailing list