From patchwork Wed Jul 19 15:52:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 122699 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2532649vqt; Wed, 19 Jul 2023 08:54:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlFQ3+J8OKhSz6s9deCNiPx4AcdelmURwsBIBi4uGR/zvuZmQTH3S60vAk/sjixTlEbSwPIa X-Received: by 2002:a17:906:153:b0:982:487c:7508 with SMTP id 19-20020a170906015300b00982487c7508mr2417520ejh.38.1689782092943; Wed, 19 Jul 2023 08:54:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689782092; cv=none; d=google.com; s=arc-20160816; b=Ah2ACV6pn52RF1IOo+E2x054UhXdrZuyxGr/lxiLFAUpblKDq+3R+gL1Z+Vs5TglcS H+TRZd43qYBdb4/4afK6RdXipmq3AsjmH0cr5IKzmezqTh1Vfm7PJrIKCCezwgZ+d4Dt AuBTgeDY6L8UZ2iZtu3ZvQXnbyHRspjR3LpX/0tjEBIz87daIz9KzQmCD6PbdI3hwewT i5ukgg0NooG+/XR6XeCtvZZKijBj+RcGG/Lx6UTSET9Xyrh651yz0V99yDJEC8WtO430 QfvCnXbu/0BwSr0AzXCAG+Si8IZh5xyP/kHz+0iV1I3IIChkQcoIuINvbHvyj5c1SZpS EOCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=dUCjcgzeA2QwTu0bTarRmcmKdYUHtrp3y7VGEOAD+tk=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=ArV+X41/opbxKlmY9MSmlffjc79qISn4I/ELmWDuEozJEouDUHXAxzNwnFAvlUaYj1 D4Dhw3iXXykalEqrldN21z69JTd9p+ZD9d4wI5o4GtjdeT4RdWOgo1wG+s+iVSt4+RHW AOMc9xndlOhYZHmttWGrPczRLvNrcr4ljMJl6Vut9touLJL2KM7eDf6/xC1b856CBleD xDNUCJpGp5kTiV/Bq+yDPyT2ubbivE3edLS0FCW/mcA+cDCIQ67AFEyfXkrWruHLQ1MF iQRLa+bbVLlpVvRGXHa4qojE6JtofcUGQax1tB2YWQ/6r1BWrGDTrkCZm24HrM4M90th Y0iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cfGSjCOs; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a17-20020a1709066d5100b00991ece4c966si2968380ejt.101.2023.07.19.08.54.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 08:54:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cfGSjCOs; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A36223853D3C for ; Wed, 19 Jul 2023 15:53:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A36223853D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689782020; bh=dUCjcgzeA2QwTu0bTarRmcmKdYUHtrp3y7VGEOAD+tk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=cfGSjCOsil6tzJRKgcvspbb9OEyHmuRooJLeL1bO0VUD4Q7RkEBcjNkUAWlYAIagv Zr+/e1gf4IAU/KTsnjgVHkosR/ydsIviIm6ZQ5tIFVnu1R1LkSrSeeIYZ2OKKHbSE7 IRzIKAE9qmm2kKqwNUW2kF/0sRaqVnsKFo68CT/c= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 89F27385AF91 for ; Wed, 19 Jul 2023 15:52:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 89F27385AF91 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-605-D3ie5AbmMBS_uXPYn0mWxQ-1; Wed, 19 Jul 2023 11:52:17 -0400 X-MC-Unique: D3ie5AbmMBS_uXPYn0mWxQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EF9638F184F; Wed, 19 Jul 2023 15:52:16 +0000 (UTC) Received: from localhost (unknown [10.42.28.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id B5EA74CD0FD; Wed, 19 Jul 2023 15:52:16 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix formatting of negative chrono::hh_mm_ss Date: Wed, 19 Jul 2023 16:52:12 +0100 Message-ID: <20230719155216.2545575-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771864947893429130 X-GMAIL-MSGID: 1771864947893429130 Tested x86_64-linux. Pushed to trunk. -- >8 -- 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. --- libstdc++-v3/include/bits/chrono_io.h | 27 ++++++++++--------- .../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();