From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 31B463858D35 for ; Mon, 7 Mar 2022 11:51:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 31B463858D35 Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-663-4bndiwbZPdqqlpTk3VGE3g-1; Mon, 07 Mar 2022 06:51:21 -0500 X-MC-Unique: 4bndiwbZPdqqlpTk3VGE3g-1 Received: by mail-yb1-f198.google.com with SMTP id h8-20020a25e208000000b00628c0565607so10720499ybe.0 for ; Mon, 07 Mar 2022 03:51:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jsXniYm2dheKea4qqVNZWpTbnYdoLBMzNqEit80Ij5g=; b=D2ZMhfDBpJiXKqniyCu/nQyvVuPum9bJUlDkcOLABPCs/LjDNMZkfWsdv/heaDhg+j +OM+dp73yX32dpkNuGn3VNz0ccV7AU/J27Fr/CagTOjsRssNh54rsGZxWKbctbc28tzA d3AvihEKWcwEAaLRwkeeJkIWfyGgb9TeRTF+jfgR8jCelqARyvysTgeIuJ5ns1l3pxrd B4iv8nEzly/Mo8L4Jwy3BDOMMCSPVnYNZWaJuaGBZOY/vGAF0/eu2BIMTThRHqGzXSG3 /j/Q3T6q/pl+v6/f7z5KXyu9PIBCZzR3dFyGB0eo9aqD72Y4go1jeyA6nMB//BZU/XL0 29fg== X-Gm-Message-State: AOAM530Ykif6k1lSU+PGrGLivW9HJuUxSPCWIsjwQpcchLdRPNUbdnLt JtCd0trqiAkSI1+bp2XCpjHyO3WngEawCE9nK1YeWmzQQel4h13BXwJoRAJMuw+Mb36QkHQRMSG 42KUxPLHuXfv57PzpuVRg97ogUMNRO4M= X-Received: by 2002:a81:6a86:0:b0:2dc:da43:7aca with SMTP id f128-20020a816a86000000b002dcda437acamr1359467ywc.473.1646653880488; Mon, 07 Mar 2022 03:51:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJx/ZXbqPHqhQvUZ36AG0JOOONk9/fCDnN467leLTDUElX01iyJ+Uu0YyLxESV/I88+ut2du7Fc2hMSFas4aay4= X-Received: by 2002:a81:6a86:0:b0:2dc:da43:7aca with SMTP id f128-20020a816a86000000b002dcda437acamr1359451ywc.473.1646653880158; Mon, 07 Mar 2022 03:51:20 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Mon, 7 Mar 2022 11:51:09 +0000 Message-ID: Subject: Re: std::get_time changed To: Markus Rathgeb Cc: "libstdc++" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-6.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Mar 2022 11:51:31 -0000 On Mon, 7 Mar 2022 at 07:45, Markus Rathgeb via Libstdc++ wrote: > > Hello, > I realized a changed behavior of the function "std::get_time" with respect > to tm_wday and tm_yday. > In older versions tm_wday and tm_yday has not been modified. In more recent > version it is modified and set to the correct value (similar to strptime). > IMHO the "new" behavior is more consistent but I wonder what is "correct" > or expected. > > Having a look at https://en.cppreference.com/w/cpp/io/manip/get_time the > fields tm_wday and tm_ydas are set by only a few conversion specifiers. > Can you give me some details about the changed behavior and where it is > documented. This changed with https://gcc.gnu.org/r12-6410 in order to fix https://gcc.gnu.org/PR77760 and make std::time_get act more like strptime. > > == Versions used == > > Fedora 36 > rpm -qi glibc > Name : glibc > Version : 2.35 > Release : 3.fc36 > rpm -qi gcc-c++ > Name : gcc-c++ > Version : 12.0.1 > Release : 0.9.fc36 > > Ubuntu Focal > apt info libc6 > Package: libc6 > Version: 2.31-0ubuntu9 > apt info g++ > Package: g++ > Version: 4:9.3.0-1ubuntu2 > > == Code Exmaple output for Ubuntu Focal and Fedora 36 == > > Initialize struct tm with 0x00 > Ubuntu Focal - std::get_time | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 0, yday: 0, isdst: 0, tm_gmtoff: 0, skipped: tm_zone > Ubuntu Focal - strptime | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: 0, tm_gmtoff: 0, skipped: tm_zone > Fedora 36 - std::get_time | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: 0, tm_gmtoff: 0, skipped: tm_zone > Fedora 36 - strptime | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: 0, tm_gmtoff: 0, skipped: tm_zone > > > Initialize struct tm with 0x01 > Ubuntu Focal - std::get_time | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 16843009, yday: 16843009, isdst: 16843009, tm_gmtoff: > 72340172838076673, skipped: tm_zone > Ubuntu Focal - strptime | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: 16843009, tm_gmtoff: > 72340172838076673, skipped: tm_zone > Fedora 36 - std::get_time | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: 16843009, tm_gmtoff: > 72340172838076673, skipped: tm_zone > Fedora 36 - strptime | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: 16843009, tm_gmtoff: > 72340172838076673, skipped: tm_zone > > Initialize struct tm with 0xFF > Ubuntu Focal - std::get_time | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: -1, yday: -1, isdst: -1, tm_gmtoff: -1, skipped: tm_zone > Ubuntu Focal - strptime | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: -1, tm_gmtoff: -1, skipped: tm_zone > Fedora 36 - std::get_time | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: -1, tm_gmtoff: -1, skipped: tm_zone > Fedora 36 - strptime | sec: 58, min: 24, hour: 7, mday: 16, mon: 5, > year: 121, wday: 3, yday: 166, isdst: -1, tm_gmtoff: -1, skipped: tm_zone > > > == Code Example == > > #include > > #include > #include > #include > #include > #include > #include > > namespace { > constexpr const char *k_defaultTimeFormat = "%Y-%m-%d %H:%M:%S"; > > bool parseGetTime(std::tm *tm, std::string time, std::string format) { > std::istringstream iss(time); > iss >> std::get_time(tm, format.c_str()); > return !iss.fail(); > } > > bool parseStrptime(std::tm *tm, const char *time, const char *format) { > const char *rv = strptime(time, format, tm); > if (rv == nullptr) { > return false; > } > return *rv == '\0'; > } > > void printTm(const std::tm &tm) { > std::printf("sec: %d, min: %d, hour: %d, mday: %d, mon: %d, year: > %d, wday: %d, yday: %d, isdst: %d, tm_gmtoff: %ld, skipped: tm_zone\n", > tm.tm_sec, tm.tm_min, tm.tm_hour, tm.tm_mday, > tm.tm_mon, tm.tm_year, tm.tm_wday, tm.tm_yday, tm.tm_isdst, tm.tm_gmtoff > /*, tm.tm_zone*/); > } > }// namespace > > int main() { > struct tm tm; > constexpr const char *timestr = "2021-06-16 07:24:58"; > > for (const int r: std::list{0x00, 0x01, 0xFF}) { > std::memset(&tm, r, sizeof(tm)); > if (parseGetTime(&tm, timestr, k_defaultTimeFormat)) { > printTm(tm); > } else { > std::cerr << "parsing failure: get time" << std::endl; > } > std::memset(&tm, r, sizeof(tm)); > if (parseStrptime(&tm, timestr, k_defaultTimeFormat)) { > printTm(tm); > } else { > std::cerr << "parsing failure: strptime" << std::endl; > } > } > return 0; > } >