From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 A897738582BC for ; Thu, 5 Jan 2023 00:52:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A897738582BC Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672879949; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0N/ARsMwdusu54vkJsapYqnEnWKd1waq7hltu56mRLQ=; b=Oqd7Q3AaWZAjKdfkbynM14+f0fPA40SZBCpYem2iKLJksCs1bTAoiX7w8Y77ciIVOGSBnM cJnvWQKadBh+tTdGfuhkNk4MAJAZ0wnMvgPijWTIHDoS5SLLvf5wUcQ0o1DIEMnbD0472l UHTISkRV304JSk3fZxrM83DfsK3mZkE= 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-649-g1jibobkOMW-pWXkj22XDw-1; Wed, 04 Jan 2023 19:52:26 -0500 X-MC-Unique: g1jibobkOMW-pWXkj22XDw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1501B8027F8; Thu, 5 Jan 2023 00:52:26 +0000 (UTC) Received: from localhost (unknown [10.33.36.242]) by smtp.corp.redhat.com (Postfix) with ESMTP id D3EF9141511D; Thu, 5 Jan 2023 00:52:25 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::chrono::hh_mm_ss with unsigned rep [PR108265] Date: Thu, 5 Jan 2023 00:52:25 +0000 Message-Id: <20230105005225.140099-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit 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_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk, backports to gcc-11 and gcc-12 will follow. -- >8 -- libstdc++-v3/ChangeLog: PR libstdc++/108265 * include/std/chrono (hh_mm_ss): Do not use chrono::abs if duration rep is unsigned. * testsuite/std/time/hh_mm_ss/1.cc: Check unsigned rep. --- libstdc++-v3/include/std/chrono | 15 ++++++++++++--- libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index 27f391a1455..e7fd6ed57ab 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -2294,7 +2294,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } constexpr - hh_mm_ss(_Duration __d, bool __is_neg) noexcept + hh_mm_ss(_Duration __d, bool __is_neg) : _M_h (duration_cast(__d)), _M_m (duration_cast(__d - hours())), _M_s (duration_cast(__d - hours() - minutes())), @@ -2307,6 +2307,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_ss._M_r = duration_cast(__ss).count(); } + static constexpr _Duration + _S_abs(_Duration __d) + { + if constexpr (numeric_limits::is_signed) + return chrono::abs(__d); + else + return __d; + } + public: static constexpr unsigned fractional_width = {_S_fractional_width()}; @@ -2318,8 +2327,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr hh_mm_ss() noexcept = default; constexpr explicit - hh_mm_ss(_Duration __d) noexcept - : hh_mm_ss(chrono::abs(__d), __d < _Duration::zero()) + hh_mm_ss(_Duration __d) + : hh_mm_ss(_S_abs(__d), __d < _Duration::zero()) { } constexpr bool diff --git a/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc b/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc index 3f8a838c477..3c61145317c 100644 --- a/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc +++ b/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc @@ -115,3 +115,19 @@ size() struct S4 { long long h; char m, s; bool neg; double ss; }; static_assert(sizeof(hh_mm_ss>) == sizeof(S4)); } + +constexpr void +unsigned_rep() +{ + using namespace std::chrono; + + constexpr duration ms(3690001); + + constexpr hh_mm_ss hms(ms); // PR libstdc++/108265 + static_assert( ! hms.is_negative() ); + static_assert( hms.to_duration() == milliseconds(ms.count()) ); + static_assert( hms.hours() == 1h ); + static_assert( hms.minutes() == 1min ); + static_assert( hms.seconds() == 30s ); + static_assert( hms.subseconds() == 1ms ); +} -- 2.39.0