]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Fix parsing of fractional seconds [PR114244]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 7 Mar 2024 13:15:41 +0000 (13:15 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 7 Mar 2024 22:52:49 +0000 (22:52 +0000)
commit5f9d7a5b6cf64639274e63051caf70fbc8418ea2
tree97b2ed6bc5d5d5aa40943fe82a06aef91ddda9a4
parent9ccd03dee4c35a24c6699a58a7251a5277a91cf5
libstdc++: Fix parsing of fractional seconds [PR114244]

When converting a chrono::duration<long double> 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<long double> because its period is ratio<1>. We should
also consider treat_as_floating_point<rep> 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.
libstdc++-v3/include/bits/chrono_io.h
libstdc++-v3/testsuite/20_util/duration/114244.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/duration/io.cc