From patchwork Wed Jul 19 15:51:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 122698 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2532294vqt; Wed, 19 Jul 2023 08:54:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlEjHHjSZB7ElgBeWWKxvSOOnvPxH84CHS5dP+Z1Ae4iP6MgSxF7JWDVVinibh4G5F4tCLh+ X-Received: by 2002:aa7:d991:0:b0:51d:e7b5:547d with SMTP id u17-20020aa7d991000000b0051de7b5547dmr2743655eds.34.1689782051526; Wed, 19 Jul 2023 08:54:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689782051; cv=none; d=google.com; s=arc-20160816; b=iTLl8Y8UW4L8IXCtZYctIP2ODVE1suzEQzofAeswFwKN+y/Sdq+qTGuafDodGYARxQ WzSXgN6y9/AF4IOFJYapkYqOfUzdUGjN8yQQisrMiLuxs6zR0MLbd9ecODynoYBdNjbN S/71lHMGaVUsA3Iw/Uk/Cxg1T0R2cJgOJC2a4d6osw8WAwGylOJi6+LXximsBJk1ktbz qqWRh9kOEifgEbZD4/ZFAK7KqOP8UtjPpFpjQMuDBKfWmyl/VAVFH6mOsZDnCGa2+cWx 9AmnNYXIdw4nse0a0bdYAUXnIlBvoWXnKdAjCr2br+PdhrAJ0/Coo6Jil9SM/UR/k2Oa dAtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=V6DjwHqCiNTHdFsgrBMubWrJDCWVZghBA5Nyx9MACXc=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=uBBvYXjE8OQvYgFWdrbUg4Yz+hwZt4803fjqO5X2I/G3Eqx197bMl256DTsSd8x0f+ KiQ3jJnPfl4agzBYW6HX3rVpXj6IDtwCB6dFeN+yTLEmIfAn+Tok/8/Cc6XyBv9btRTe PROZA+9yewQ7zTXfbdsPOf/1GjV/32juw8trx48qdkq5m+ge70L2Rmikz998dRLT/hMd IPdtmpX9WoQB3dR/2Qqr2Ge9f50VBGhEsvoA0rUDIlp4z10ZXZEh35vM7zQOvAtctrwn xS4H4DSgt/FMlYUqqqpI/Z2j5R2eSxkih7BbtRzerKBZ7XTcu7Lld2vkIo5VI/+vtqQh u04A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=a7pCxoS+; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d15-20020a05640208cf00b0051dd49a94c2si3085812edz.583.2023.07.19.08.54.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 08:54:11 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=a7pCxoS+; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2923D385DC02 for ; Wed, 19 Jul 2023 15:53:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2923D385DC02 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689781986; bh=V6DjwHqCiNTHdFsgrBMubWrJDCWVZghBA5Nyx9MACXc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=a7pCxoS+d6v86B/faEG4Pp18Bk7fYYVabQO2H7KmH7YcHkMihScdM3vzrwzwxIAER gDsCDnB005z1brbhyrZGcezw2D/zsJDtu3/ZPn3Rgi7VVXgX7FuknVOZsCgtVrRPMU hkH0gAPKiVNLkc08huIebcerpgyMaNEO+ij29qKE= 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 3F3DA385773C for ; Wed, 19 Jul 2023 15:52:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F3DA385773C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-135-hX7AkrHvO2CWFO2DVBbQNg-1; Wed, 19 Jul 2023 11:52:13 -0400 X-MC-Unique: hX7AkrHvO2CWFO2DVBbQNg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B41F9104458B; Wed, 19 Jul 2023 15:52:12 +0000 (UTC) Received: from localhost (unknown [10.42.28.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 773E61454142; Wed, 19 Jul 2023 15:52:12 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix locale-specific duration formatting [PR110719] Date: Wed, 19 Jul 2023 16:51:54 +0100 Message-ID: <20230719155211.2545541-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771864904478734043 X-GMAIL-MSGID: 1771864904478734043 Tested x86_64-linux. Pushed to trunk. -- >8 -- The r14-2640-gf4bce119f617dc commit only removed fractional seconds for time points, but it needs to be done for durations and hh_mm_ss types too. libstdc++-v3/ChangeLog: PR libstdc++/110719 * include/bits/chrono_io.h (__formatter_chrono::_S_floor_seconds): Handle duration and hh_mm_ss. * testsuite/20_util/duration/io.cc: Check locale-specific formats. * testsuite/std/time/hh_mm_ss/io.cc: Likewise. --- libstdc++-v3/include/bits/chrono_io.h | 22 ++++++++++++----- libstdc++-v3/testsuite/20_util/duration/io.cc | 4 ++++ .../testsuite/std/time/hh_mm_ss/io.cc | 24 +++++++++++++++++-- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h index 43eeab42869..0c5f9f5058b 100644 --- a/libstdc++-v3/include/bits/chrono_io.h +++ b/libstdc++-v3/include/bits/chrono_io.h @@ -1144,11 +1144,11 @@ namespace __format __out = __format::__write(std::move(__out), _S_two_digits(__hms.seconds().count())); - using rep = typename decltype(__hms)::precision::rep; if constexpr (__hms.fractional_width != 0) { locale __loc = _M_locale(__ctx); auto __ss = __hms.subseconds(); + using rep = typename decltype(__ss)::rep; if constexpr (is_floating_point_v) { __out = std::format_to(__loc, std::move(__out), @@ -1546,11 +1546,21 @@ namespace __format _S_floor_seconds(const _Tp& __t) { using chrono::__detail::__local_time_fmt; - if constexpr (chrono::__is_time_point_v<_Tp>) - if constexpr (_Tp::period::den != 1) - return chrono::floor(__t); - else - return __t; + if constexpr (chrono::__is_time_point_v<_Tp> + || chrono::__is_duration_v<_Tp>) + { + if constexpr (_Tp::period::den != 1) + return chrono::floor(__t); + else + return __t; + } + else if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>) + { + if constexpr (_Tp::fractional_width != 0) + return chrono::floor(__t.to_duration()); + else + return __t; + } else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>) return _S_floor_seconds(__t._M_time); else diff --git a/libstdc++-v3/testsuite/20_util/duration/io.cc b/libstdc++-v3/testsuite/20_util/duration/io.cc index 27586b54392..ea94b062d96 100644 --- a/libstdc++-v3/testsuite/20_util/duration/io.cc +++ b/libstdc++-v3/testsuite/20_util/duration/io.cc @@ -71,6 +71,10 @@ test_format() s = std::format("{:%t} {:%t%M}", -2h, -123s); VERIFY( s == "\t \t-02" ); + // Locale-specific formats: + s = std::format(std::locale::classic(), "{:%r %OH:%OM:%OS}", 123456ms); + VERIFY( s == "12:02:03 AM 00:02:03" ); + std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ"; std::string_view my_specs = "HIjMpqQrRSTX"; for (char c : specs) diff --git a/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc b/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc index 3b50f40c1f6..072234328c7 100644 --- a/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc +++ b/libstdc++-v3/testsuite/std/time/hh_mm_ss/io.cc @@ -6,7 +6,7 @@ #include void -test01() +test_ostream() { using std::ostringstream; using std::chrono::hh_mm_ss; @@ -40,7 +40,27 @@ test01() VERIFY( out.str() == "18:15:45" ); } +void +test_format() +{ + using namespace std::chrono; + + auto s = std::format("{}", hh_mm_ss{1h + 23min + 45s}); + VERIFY( s == "01:23:45" ); + + auto ws = std::format(L"{}", hh_mm_ss{1h + 23min + 45s}); + VERIFY( ws == L"01:23:45" ); + + // Locale-specific formats: + auto loc = std::locale::classic(); + s = std::format(loc, "{:%r %OH:%OM:%OS}", hh_mm_ss{123456ms}); + VERIFY( s == "12:02:03 AM 00:02:03" ); + ws = std::format(loc, L"{:%r %OH:%OM:%OS}", hh_mm_ss{123456ms}); + VERIFY( ws == L"12:02:03 AM 00:02:03" ); +} + int main() { - test01(); + test_ostream(); + test_format(); }