public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-8262] libstdc++: Fix std::chrono::file_clock conversions for low-precision times Date: Tue, 30 Jan 2024 14:54:53 +0000 (GMT) [thread overview] Message-ID: <20240130145454.9767D385840D@sourceware.org> (raw) https://gcc.gnu.org/g:4cdcfb4f84c9aa668731e3ee57dc686432097951 commit r13-8262-g4cdcfb4f84c9aa668731e3ee57dc686432097951 Author: Jonathan Wakely <jwakely@redhat.com> Date: Sun Jan 21 18:11:16 2024 +0000 libstdc++: Fix std::chrono::file_clock conversions for low-precision times 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. (cherry picked from commit fba15da8fa518bfb8d3e061795bc3ca2dea01d27) Diff: --- libstdc++-v3/include/bits/chrono.h | 14 ++++++++------ libstdc++-v3/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 0aeea0ae57eb..04078ff8f2df 100644 --- a/libstdc++-v3/include/bits/chrono.h +++ b/libstdc++-v3/include/bits/chrono.h @@ -1455,14 +1455,14 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) #if __cplusplus > 201703L template<typename _Dur> static - chrono::file_time<_Dur> + chrono::file_time<common_type_t<_Dur, chrono::seconds>> from_sys(const chrono::sys_time<_Dur>& __t) noexcept { return _S_from_sys(__t); } // For internal use only template<typename _Dur> static - chrono::sys_time<_Dur> + chrono::sys_time<common_type_t<_Dur, chrono::seconds>> to_sys(const chrono::file_time<_Dur>& __t) noexcept { return _S_to_sys(__t); } #endif // C++20 @@ -1479,20 +1479,22 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) // For internal use only template<typename _Dur> 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<typename _Dur> 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 b34df49c6500..2047b029e9bd 100644 --- a/libstdc++-v3/testsuite/std/time/clock/file/members.cc +++ b/libstdc++-v3/testsuite/std/time/clock/file/members.cc @@ -42,9 +42,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(); }
reply other threads:[~2024-01-30 14:54 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240130145454.9767D385840D@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).