From: Jonathan Wakely Date: Thu, 15 May 2025 18:32:01 +0000 (+0100) Subject: libstdc++: Fix std::format of chrono::local_days with {} [PR120293] X-Git-Tag: releases/gcc-15.2.0~491 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4bc5697341f1eda3b20c16dcf173948b2d1bd5c8;p=thirdparty%2Fgcc.git 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 (cherry picked from commit 1ed7585bf60ba9940ca5dc6d2c72dba86eea7b4d) --- diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h index f5aa8a3e18af..7ab989f59a1c 100644 --- a/libstdc++-v3/include/bits/chrono_io.h +++ b/libstdc++-v3/include/bits/chrono_io.h @@ -782,6 +782,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(__t); diff --git a/libstdc++-v3/testsuite/std/time/clock/local/io.cc b/libstdc++-v3/testsuite/std/time/clock/local/io.cc index b4d562f36d12..67818e876497 100644 --- a/libstdc++-v3/testsuite/std/time/clock/local/io.cc +++ b/libstdc++-v3/testsuite/std/time/clock/local/io.cc @@ -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