From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 38F9F384F4A5; Fri, 28 Jul 2023 17:32:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38F9F384F4A5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690565577; bh=sYg7vp4KnhBZk3cD+ESwC4CQPnDtqPcfWHrs+2SP/jM=; h=From:To:Subject:Date:From; b=HQBTMKLG4MpvgyPPNhBKOvx3NsffQ7ZOK/Bfn1ADNulOUXUiULuw9b2ReJuw6l1up fMX0RMLHot/OFWpIG4Kg2N+fjHAyOtVYkAuNByY3EQOyKjuL53Kvcs3hMMUT3gfexl B0uhDMkgBdIJgVG2XdN8lHoPlSjQuA0l4wDuAYeY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7632] libstdc++: Fix formatting of negative chrono::hh_mm_ss X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 24c352c41eb944d29b09f33a42662efad3f6f811 X-Git-Newrev: 9fed693b161b2b1a799ffd22535d7e7c9fe8c3cf Message-Id: <20230728173257.38F9F384F4A5@sourceware.org> Date: Fri, 28 Jul 2023 17:32:57 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9fed693b161b2b1a799ffd22535d7e7c9fe8c3cf commit r13-7632-g9fed693b161b2b1a799ffd22535d7e7c9fe8c3cf Author: Jonathan Wakely Date: Wed Jul 19 14:42:51 2023 +0100 libstdc++: Fix formatting of negative chrono::hh_mm_ss When formatting with an empty chrono spec ("{}") two minus signs were being added to hh_mm_ss values. This is because the __is_neg flag was checked to add one explicitly, and then the ostream operator added another one. We should only check the __is_neg flag for durations, because those are the only types which are modified to be non-negative before calling _M_format. We don't change hh_mm_ss values to be negative, because that would require performing arithmetic on the hh_mm_ss members to sum them, and then again to construct a new hh_mm_ss object with the positive value. Instead, we can just be careful about using the __is_neg flag correctly. To fix the bug, _M_format_to_ostream no longer checks the __is_neg flag for non-durations, and _M_format doesn't set it for hh_mm_ss until after the call to _M_format_to_ostream. We can also avoid setting it for types that it doesn't apply to, by making the __print_sign lambda only inspect it for duration and hh_mm_ss types. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono::_M_format): Do not set __is_neg for hh_mm_ss before calling _M_format_to_ostream. Change __print_sign lambda to only check __is_neg for durations and hh_mm_ss types. (__formatter_chrono::_M_format_to_ostream): Only check __is_neg for duration types. * testsuite/std/time/hh_mm_ss/io.cc: Check negative values. (cherry picked from commit 344f41322023cdbf4532b5278443a22a4f391627) Diff: --- libstdc++-v3/include/bits/chrono_io.h | 27 ++++++++++++++------------ libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc | 4 ++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h index 0c5f9f5058b..c95301361d8 100644 --- a/libstdc++-v3/include/bits/chrono_io.h +++ b/libstdc++-v3/include/bits/chrono_io.h @@ -486,11 +486,6 @@ namespace __format _M_format(const _Tp& __t, _FormatContext& __fc, bool __is_neg = false) const { - if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>) - __is_neg = __t.is_negative(); - else if constexpr (!chrono::__is_duration_v<_Tp>) - __is_neg = false; - auto __first = _M_spec._M_chrono_specs.begin(); const auto __last = _M_spec._M_chrono_specs.end(); if (__first == __last) @@ -513,12 +508,19 @@ namespace __format else __out = __sink.out(); + // formatter passes the correct value of __is_neg + // for durations but for hh_mm_ss we decide it here. + if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>) + __is_neg = __t.is_negative(); + auto __print_sign = [&__is_neg, &__out] { - if (__is_neg) - { - *__out++ = _S_plus_minus[1]; - __is_neg = false; - } + if constexpr (chrono::__is_duration_v<_Tp> + || __is_specialization_of<_Tp, chrono::hh_mm_ss>) + if (__is_neg) + { + *__out++ = _S_plus_minus[1]; + __is_neg = false; + } return std::move(__out); }; @@ -708,8 +710,9 @@ namespace __format __os << __t._M_date << ' ' << __t._M_time; else { - if (__is_neg) [[unlikely]] - __os << _S_plus_minus[1]; + if constexpr (chrono::__is_duration_v<_Tp>) + if (__is_neg) [[unlikely]] + __os << _S_plus_minus[1]; __os << __t; } diff --git a/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc b/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc index 072234328c7..ddb1ad77d1e 100644 --- a/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc +++ b/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc @@ -47,9 +47,13 @@ test_format() auto s = std::format("{}", hh_mm_ss{1h + 23min + 45s}); VERIFY( s == "01:23:45" ); + s = std::format("{}", hh_mm_ss{-42min}); + VERIFY( s == "-00:42:00" ); auto ws = std::format(L"{}", hh_mm_ss{1h + 23min + 45s}); VERIFY( ws == L"01:23:45" ); + ws = std::format(L"{}", hh_mm_ss{-42min}); + VERIFY( ws == L"-00:42:00" ); // Locale-specific formats: auto loc = std::locale::classic();