From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CD4A63858D20; Tue, 5 Mar 2024 17:23:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD4A63858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709659429; bh=rbKMydew8UVHR+VlI/woWdxZFNmG39iojZM+di/27qc=; h=From:To:Subject:Date:From; b=R5q1tfy+ct2ztINiv5wc79Y5NiH54r/Daf4f1xi6DZzCY2i4ZS4w5boVoyRWmUKSt 6XYZ54w61wyHAxSd75ypCiAS0dQruCG7gLLRQrSBRTJBubft+NAgMM1zFChp3Z+fsq 485PfouFoqSvV5Oku6zrLchAXO+TCPSzpuuX2VjM= From: "howard.hinnant at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/114244] New: Need to use round when parsing fractional seconds Date: Tue, 05 Mar 2024 17:23:49 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: howard.hinnant at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114244 Bug ID: 114244 Summary: Need to use round when parsing fractional seconds Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: howard.hinnant at gmail dot com Target Milestone: --- I'm not sure if this is a bug or just QOI. But consider: #include #include #include using time_point_t =3D std::chrono::sys_time; time_point_t decrypt(std::string s) { time_point_t tp; std::istringstream in(s); in >> parse("%Y%m%d-%T", tp); return tp; } int main() { std::cout << decrypt("20240304-13:00:00.002"); } I expect the output to be: 2024-03-04 13:00:00.002 but it is: 2024-03-04 13:00:00.001 I believe the issue is the use of duration_cast or time_point_cast somewhere under the parse (not sure exactly where). Since 0.002 is not exactly representable in floating point, we're getting a truncate towards zero because this parses as just barely under 0.= 002. I highly recommend the use of round when converting floating point based durations and time_points to integral based.=