From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id D68F03858D35; Thu, 7 Mar 2024 23:45:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D68F03858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709855106; bh=wbcZdwA3QPJ6YgOJcG81Q2/MuMRwVHfWfcsS3FUqC9Y=; h=From:To:Subject:Date:From; b=Kor1JG8D3UU2y52KvehRIxarw8bIycANiXOVb9uwID3V4Z7PihjDeCxPKNX7s24mw mVGbwzHuZRTLo5f9eRGzvtGoHT7WJtG/duER8dY0i0+w49MLCq+3VUx/2LVh2XzKCZ p2PlQaPaE5mjoZ318b5A5Vpm5ywhoFRbd8IxSgBE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-9376] libstdc++: Fix parsing of fractional seconds [PR114244] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 9ccd03dee4c35a24c6699a58a7251a5277a91cf5 X-Git-Newrev: 5f9d7a5b6cf64639274e63051caf70fbc8418ea2 Message-Id: <20240307234506.D68F03858D35@sourceware.org> Date: Thu, 7 Mar 2024 23:45:06 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5f9d7a5b6cf64639274e63051caf70fbc8418ea2 commit r14-9376-g5f9d7a5b6cf64639274e63051caf70fbc8418ea2 Author: Jonathan Wakely Date: Thu Mar 7 13:15:41 2024 +0000 libstdc++: Fix parsing of fractional seconds [PR114244] When converting a chrono::duration to a result type with an integer representation we should use chrono::round<_Duration> so that we don't truncate towards zero. Rounding ensures that e.g. 0.001999s becomes 2ms not 1ms. We can also remove some redundant uses of chrono::duration_cast to convert from seconds to _Duration, because the _Parser class template requires _Duration type to be able to represent seconds without loss of precision. This also fixes a bug where no fractional part would be parsed for chrono::duration because its period is ratio<1>. We should also consider treat_as_floating_point when deciding whether to skip reading a fractional part. libstdc++-v3/ChangeLog: PR libstdc++/114244 * include/bits/chrono_io.h (_Parser::operator()): Remove redundant uses of duration_cast. Use chrono::round to convert long double value to durations with integer representations. Check represenation type when deciding whether to skip parsing fractional seconds. * testsuite/20_util/duration/114244.cc: New test. * testsuite/20_util/duration/io.cc: Check that a floating-point duration with ratio<1> precision can be parsed. Diff: --- libstdc++-v3/include/bits/chrono_io.h | 18 ++++++++---- libstdc++-v3/testsuite/20_util/duration/114244.cc | 36 +++++++++++++++++++++++ libstdc++-v3/testsuite/20_util/duration/io.cc | 12 ++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h index 82f2d39ec44..b8f0657bee9 100644 --- a/libstdc++-v3/include/bits/chrono_io.h +++ b/libstdc++-v3/include/bits/chrono_io.h @@ -3113,6 +3113,9 @@ namespace __detail unsigned __num = 0; // Non-zero for N modifier. bool __is_flag = false; // True if we're processing a % flag. + constexpr bool __is_floating + = treat_as_floating_point_v; + // If an out-of-range value is extracted (e.g. 61min for %M), // do not set failbit immediately because we might not need it // (e.g. parsing chrono::year doesn't care about invalid %M values). @@ -3195,7 +3198,7 @@ namespace __detail __d = day(__tm.tm_mday); __h = hours(__tm.tm_hour); __min = minutes(__tm.tm_min); - __s = duration_cast<_Duration>(seconds(__tm.tm_sec)); + __s = seconds(__tm.tm_sec); } } __parts |= _ChronoParts::_DateTime; @@ -3564,8 +3567,8 @@ namespace __detail if (!__is_failed(__err)) __s = seconds(__tm.tm_sec); } - else if constexpr (ratio_equal_v>) + else if constexpr (_Duration::period::den == 1 + && !__is_floating) { auto __val = __read_unsigned(__num ? __num : 2); if (0 <= __val && __val <= 59) [[likely]] @@ -3577,7 +3580,7 @@ namespace __detail break; } } - else + else // Read fractional seconds { basic_stringstream<_CharT> __buf; auto __digit = _S_try_read_digit(__is, __err); @@ -3626,7 +3629,10 @@ namespace __detail else { duration __fs(__val); - __s = duration_cast<_Duration>(__fs); + if constexpr (__is_floating) + __s = __fs; + else + __s = chrono::round<_Duration>(__fs); } } } @@ -3737,7 +3743,7 @@ namespace __detail { __h = hours(__tm.tm_hour); __min = minutes(__tm.tm_min); - __s = duration_cast<_Duration>(seconds(__tm.tm_sec)); + __s = seconds(__tm.tm_sec); } } __parts |= _ChronoParts::_TimeOfDay; diff --git a/libstdc++-v3/testsuite/20_util/duration/114244.cc b/libstdc++-v3/testsuite/20_util/duration/114244.cc new file mode 100644 index 00000000000..55a7670522a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/duration/114244.cc @@ -0,0 +1,36 @@ +// { dg-do run { target c++20 } } +// { dg-timeout-factor 2 } +// { dg-require-namedlocale "en_US.ISO8859-1" } + +// PR libstdc++/114244 Need to use round when parsing fractional seconds + +#include +#include +#include + +void +test_pr114244() +{ + using namespace std::chrono; + seconds s; + milliseconds ms; + microseconds us; + + std::istringstream is; + + is.clear(); + is.str("0.002"); + VERIFY( is >> parse("%S", ms) ); + VERIFY( ms == 2ms ); // not 1ms + + is.imbue(std::locale(ISO_8859(1,en_US))); + is.clear(); + is.str("0.002"); + VERIFY( is >> parse("%S", us) ); + VERIFY( us == 2000us ); // not 1999us +} + +int main() +{ + test_pr114244(); +} diff --git a/libstdc++-v3/testsuite/20_util/duration/io.cc b/libstdc++-v3/testsuite/20_util/duration/io.cc index e141baf42dc..2f940ef86b7 100644 --- a/libstdc++-v3/testsuite/20_util/duration/io.cc +++ b/libstdc++-v3/testsuite/20_util/duration/io.cc @@ -200,6 +200,18 @@ test_parse() VERIFY( is >> parse("%S", us) ); VERIFY( us == 976us ); VERIFY( is.get() == '5' ); + + is.clear(); + is.str("0.5"); + std::chrono::duration ds; + VERIFY( is >> parse("%S", ds) ); + VERIFY( ds == 0.5s ); + + is.clear(); + is.str("0.125"); + std::chrono::duration dms; + VERIFY( is >> parse("%S", dms) ); + VERIFY( dms == 0.125s ); } int main()