From patchwork Sun Jan 21 22:25:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 189857 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2279279dyb; Sun, 21 Jan 2024 15:30:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IHlIWiaJrvLzYe1JqiD2KbafkXOinNqFkdOhZFUHs58WHLNE8Jzu7MoMG4jgQfh5NsecCr5 X-Received: by 2002:a05:622a:446:b0:429:c53d:e550 with SMTP id o6-20020a05622a044600b00429c53de550mr4567446qtx.119.1705879837156; Sun, 21 Jan 2024 15:30:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705879837; cv=pass; d=google.com; s=arc-20160816; b=Tw62zXB1T/8Q7U8DT7Mxf2wpe90VF3iPjbp3jxODCCV86EhE1gLMhqX1zIzY5Px82s SrYePxm9Z3i1JEtD0elb3olbMSIf836NH41C/OB048ZIiBtERkYeVAAO/vR4vRMxjRBK eAqItMYd3kKeqh0HpJpj59gIil/LE4Mso8P/zhx/D8oM4zDqlaApL8hsBgNe2vHCsa5C mB0lH+Xc7v9aXACjnVA37r+8RLFUz2h7BCKU2SLlugJ/YLU3yItnK2VJ5H2RJncWn9jx hy+RlxSiO0CrDbyZPGH7fyGljhoOVYsMc9C2N9XzbZIRGGqFXKtyDF6bxaQ6W9Q+QMNA flHA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=SavkKl/bmdpqm9xsnSvN9Hd5K6OXoqgGDQhQDaEtSt8=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=bwZ5/mQvA/meu1QlTTq3uLsU7g0+OmOOdqkdD2VrSfPiZVrsDWwHIh1GjIh+Wka58S KpOv4oJev7HlybMsbhKicOVu4Pj8+5vk4x6d6PCVJDlD52J8EvxylQRbgeKSzdC3q8It Cd7KgB40jFgAM876yNRdgMvuo+WCrw1G+QZqmkpMiq8tKIglbDhAS6RaR7MBRvntRLG6 0D8pZUTI3Y7/oTwJ5NL6q9dI2jp2gHew1+Q7v94xog+emi3jnkw0qC0UAuuIXtbS/ybn cwQd+ZXUabDJfOa36Swicki6VGzqOK5xpBvclrYU++UVkpUiZsw4ZmFJPFKw843YzLIl XMpg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S3GR1D11; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id g17-20020ac87751000000b0042a0dafc519si3970216qtu.781.2024.01.21.15.30.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 15:30:37 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=S3GR1D11; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 521073858015 for ; Sun, 21 Jan 2024 22:27:04 +0000 (GMT) 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 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788744655738276530 X-GMAIL-MSGID: 1788744655738276530 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(); }