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 CB5723858C56 for ; Sun, 21 Jan 2024 22:26:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB5723858C56 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CB5723858C56 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705875982; cv=none; b=iqHr8myjncsoqEjNNgoebA9P6nOJrTMMFaj4uWBtARdn30CTxZCu4booN1jUsClIEcsdWzr236OahQOTDVqdm7CTHed8HEZ/bgYFJlnzSZW687Ow8diwDws+CrPYWG1kfmV2dGdBPH+L+Hg8b4vThS6cDYfxi4jRdzGqPQIQX8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705875982; c=relaxed/simple; bh=ujsRolI2n5dg2k+/1kZvjtHgMgmM1YDnlXpGs/VVkZA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=e15h6kFGY0ACPY5TMIsrxjeOCKHvBUQIzLT4ixokQr9LChRlUSVpTPfUt/nZrFWy0YcHSElHF67EQcaBDV1Jr38OpPHFIAxV7NZuhib01qSpc4d6CvCFMwkkdGva8cshfOuB3WgegfSBe5lmx48hjBp7RxPQ0dDnanffcZYJWCA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705875979; 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=SavkKl/bmdpqm9xsnSvN9Hd5K6OXoqgGDQhQDaEtSt8=; b=S3GR1D11NyCTM6CUEmVoTyTn1wvCcaBMu/eJdDsVWHHwWvQIWgGLVA7Jw4tcQ72x964Gtt y4R3a7hN+8sCIojOM2N0C9H5zdicH8ofebAXQ6sVs0YMdi5/JmN5Ca65f9Gan3W6WTlwnf H8uhoVuGrzo3Ev21tKhV4Oxaf/7FAkQ= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-or9O7QPINAmYDwyxlxIjSA-1; Sun, 21 Jan 2024 17:26:15 -0500 X-MC-Unique: or9O7QPINAmYDwyxlxIjSA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 847FF380606C; Sun, 21 Jan 2024 22:26:15 +0000 (UTC) Received: from localhost (unknown [10.42.28.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4F5402026D66; Sun, 21 Jan 2024 22:26:15 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::chrono::file_clock conversions for low-precision times Date: Sun, 21 Jan 2024 22:25:41 +0000 Message-ID: <20240121222614.452244-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.7 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=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 aarch64-linux. Pushed to trunk. Backport needed to gcc-13 too. -- >8 -- THe std::chrono::file_clock conversions were not using common_type and so failed to compile when converting anything that should have increased precision after arithmetic with a std::chrono::seconds value. libstdc++-v3/ChangeLog: * include/bits/chrono.h (__file_clock::from_sys) (__file_clock::to_sys, __file_clock::_S_from_sys) (__file_clock::_S_to_sys): Use common_type for return type. * testsuite/std/time/clock/file/members.cc: Check round trip conversion for time with lower precision that seconds. --- libstdc++-v3/include/bits/chrono.h | 14 ++++++++------ .../testsuite/std/time/clock/file/members.cc | 9 +++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h index 6f4fe55f9c0..0773867da71 100644 --- a/libstdc++-v3/include/bits/chrono.h +++ b/libstdc++-v3/include/bits/chrono.h @@ -1453,14 +1453,14 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) #if __cplusplus > 201703L template static - chrono::file_time<_Dur> + chrono::file_time> from_sys(const chrono::sys_time<_Dur>& __t) noexcept { return _S_from_sys(__t); } // For internal use only template static - chrono::sys_time<_Dur> + chrono::sys_time> to_sys(const chrono::file_time<_Dur>& __t) noexcept { return _S_to_sys(__t); } #endif // C++20 @@ -1477,20 +1477,22 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) // For internal use only template static - chrono::time_point<__file_clock, _Dur> + chrono::time_point<__file_clock, common_type_t<_Dur, chrono::seconds>> _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept { - using __file_time = chrono::time_point<__file_clock, _Dur>; + using _CDur = common_type_t<_Dur, chrono::seconds>; + using __file_time = chrono::time_point<__file_clock, _CDur>; return __file_time{__t.time_since_epoch()} - _S_epoch_diff; } // For internal use only template static - chrono::time_point<__sys_clock, _Dur> + chrono::time_point<__sys_clock, common_type_t<_Dur, chrono::seconds>> _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept { - using __sys_time = chrono::time_point<__sys_clock, _Dur>; + using _CDur = common_type_t<_Dur, chrono::seconds>; + using __sys_time = chrono::time_point<__sys_clock, _CDur>; return __sys_time{__t.time_since_epoch()} + _S_epoch_diff; } }; diff --git a/libstdc++-v3/testsuite/std/time/clock/file/members.cc b/libstdc++-v3/testsuite/std/time/clock/file/members.cc index 54459cc56a9..9d217b17811 100644 --- a/libstdc++-v3/testsuite/std/time/clock/file/members.cc +++ b/libstdc++-v3/testsuite/std/time/clock/file/members.cc @@ -41,9 +41,18 @@ test02() VERIFY( t - s < 1s ); } +void +test03() +{ + using namespace std::chrono; + auto st = sys_days(2024y/January/21); + VERIFY( file_clock::to_sys(file_clock::from_sys(st)) == st ); +} + int main() { test01(); test02(); + test03(); } -- 2.43.0