From patchwork Thu Mar 7 23:45:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1909498 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=L0klsbfZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TrQtg0P9vz1yX4 for ; Fri, 8 Mar 2024 10:46:34 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AFE7C3857729 for ; Thu, 7 Mar 2024 23:46:32 +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 CFC9B3858C42 for ; Thu, 7 Mar 2024 23:45:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CFC9B3858C42 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 CFC9B3858C42 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=1709855131; cv=none; b=IZwuC4YJEMHzGAjHOHOC5TKShbiAFMnp9F8EjlQJXdqY0fnsEYfQHrcqhlA9j0TCcApXUxFojEdGKGugj/D25WQ0D3CIgxO1E1BBR3waO5JUl4LfLH0e+1IThmhBOEUr1iT4gAEKfCduatU6HcR1FmAQPklor4aAYvia7EM6kiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709855131; c=relaxed/simple; bh=FdDeeOc0bsH90rRvua/hFES0iCUKtWNLmk5MCpWrdnc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lB0SwpM4UU38yHtOKbB77kfl7KQjdLUSWrNQIAGiu1JTCf5VcWrGihJo2LF5CV7XgT63mRAIVClc247DAkUlkdB2wgHPUB2o/lpY4BND7/bzwFNAKv7Np30T53hgjF9MtOpF/Mgok4arPZnxuqWf7IY+RsZ6Bcn0T2QhSNyOEQY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709855129; 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=hojfVCN0AZlwKK5o8+sJs4Tp0rP5bGNEqjZczjLD/80=; b=L0klsbfZefV5ruRNUrsjT6hQJ9P+dpccsf89V1B5dAph1SurcWJtrs9wJgbpd9tjaBaLAn QH0JFJq8f2GDutV184lOoLd6Ym0wCfFDI6dxblRi0Haz3g7QI97NgQDwGlMXbCJUD5Gwz0 xtlOc5w2iSUhYUoUE4ulCBZNTgT5QJc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-511-CpHj-H34Nta9TJgnpId6DA-1; Thu, 07 Mar 2024 18:45:25 -0500 X-MC-Unique: CpHj-H34Nta9TJgnpId6DA-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 48B8B85A58C; Thu, 7 Mar 2024 23:45:25 +0000 (UTC) Received: from localhost (unknown [10.42.28.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id 15F96200B01D; Thu, 7 Mar 2024 23:45:24 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix parsing of fractional seconds [PR114244] Date: Thu, 7 Mar 2024 23:45:07 +0000 Message-ID: <20240307234524.647029-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.1 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- 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. --- libstdc++-v3/include/bits/chrono_io.h | 18 ++++++---- .../testsuite/20_util/duration/114244.cc | 36 +++++++++++++++++++ libstdc++-v3/testsuite/20_util/duration/io.cc | 12 +++++++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/duration/114244.cc 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()