From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: from second.openwall.net (second.openwall.net [193.110.157.125]) by inbox.vuxu.org (Postfix) with SMTP id B2E1621727 for ; Mon, 25 Mar 2024 14:51:20 +0100 (CET) Received: (qmail 5425 invoked by uid 550); 25 Mar 2024 13:46:35 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 5390 invoked from network); 25 Mar 2024 13:46:35 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1711374667; x=1711633867; bh=nTKhngltIRAFeMIdqjS9Yi+4EtWu7gJWG4i0Z+7DOJM=; h=Date:To:From:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=d+FaEZzwlo+anCvhqCVzHopuVbARNbwXMYzllNzX6jwwfiiF64FYlrx+RwwdXW7QF cYYhhKFJZhWUTLn5S6SYyAlv3MwSLfrHFAhnXn4riBbA5c2QIQnPy6lfF0L551vkzY Qm8u6L7ClJNRObno09FFwkU18XHEBO+Q5u9clv4PcEhr7ZlM4RDDeB7/ENnwhp90cv TT2DywCODdvy0fxhxAtWAtSM7k2K++2cAdsAe1/Q2ul+TwQzHDSLH1yc6PuhEgDr/N BSwvTY2vbvVBipiwZ/mZDgHDPoOCacAABHoaZwpvas6lsiOPynMpZWJGtGV3WRq+ZI KSZec0CyJfbgA== Date: Mon, 25 Mar 2024 13:50:57 +0000 To: musl@lists.openwall.com From: Alexander Weps Message-ID: In-Reply-To: References: <4YlR0YRqzZlDIOVv6SP8UDoop89n8u7BvQl_7eXNTvDZnogXMxG1z-TLGIBf-O4edUphddXGfADbk_d7Uzb37g5JoH7vOIvvNRMFDxPWZok=@pm.me> <20240325122113.GB4163@brightrain.aerifal.cx> <20240325131318.GD4163@brightrain.aerifal.cx> <20240325134252.GE4163@brightrain.aerifal.cx> Feedback-ID: 20507743:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [musl] Broken mktime calculations when crossing DST boundary I also run it with tm_isdst =3D 1 & tm_mday =3D 31 - 1: Complete code to run: #include #include #include void test10() { time_t t =3D 0; struct tm tm =3D {0}; char buf[64]; tm.tm_year =3D 2011 - 1900; tm.tm_mon =3D 12 - 1; tm.tm_mday =3D 31 - 1; // <-- here is the change tm.tm_hour =3D 0; tm.tm_min =3D 0; tm.tm_sec =3D 0; tm.tm_isdst =3D 1; // <-- here is the change strftime(buf, sizeof buf, "%F %T %Z", &tm); printf("before: %s %ld\n", buf, t); t =3D mktime(&tm); strftime(buf, sizeof buf, "%F %T %Z", &tm); printf("after1: %s %ld\n", buf, t); tm.tm_mday -=3D 1; t =3D mktime(&tm); strftime(buf, sizeof buf, "%F %T %Z", &tm); printf("after2: %s %ld\n", buf, t); tm.tm_mday +=3D 1; t =3D mktime(&tm); strftime(buf, sizeof buf, "%F %T %Z", &tm); printf("after3: %s %ld\n", buf, t); } int main(int argc, char *argv[]) { test10(); return 0; } $ musl-gcc foo.c -o foo && TZ=3DPacific/Apia ./foo before: 2011-12-30 00:00:00 0 after1: 2011-12-29 00:00:00 -10 1325152800 after2: 2011-12-28 00:00:00 -10 1325066400 after3: 2011-12-29 00:00:00 -10 1325152800 $ gcc foo.c -o foo && TZ=3DPacific/Apia ./foo before: 2011-12-30 00:00:00 +14 0 after1: 2011-12-30 00:00:00 +14 -1 after2: 2011-12-29 00:00:00 -10 1325152800 after3: 2011-12-30 00:00:00 -10 -1 So this is a also a bug in struct tm interpretation. Behavior is consistent with tm_mday =3D 31; AW On Monday, March 25th, 2024 at 14:48, Alexander Weps wrot= e: > For a change you requested 31 - 1: > tm.tm_mday =3D 31 - 1; > > Complete code to run: > > #include > > #include > > #include > > > void test10() > { > time_t t =3D 0; > struct tm tm =3D {0}; > char buf[64]; > > tm.tm_year =3D 2011 - 1900; > tm.tm_mon =3D 12 - 1; > tm.tm_mday =3D 31 - 1; // <-- here is the change > tm.tm_hour =3D 0; > tm.tm_min =3D 0; > tm.tm_sec =3D 0; > tm.tm_isdst =3D 0; > > strftime(buf, sizeof buf, "%F %T %Z", &tm); > printf("before: %s %ld\n", buf, t); > > t =3D mktime(&tm); > > strftime(buf, sizeof buf, "%F %T %Z", &tm); > printf("after1: %s %ld\n", buf, t); > > tm.tm_mday -=3D 1; > t =3D mktime(&tm); > > strftime(buf, sizeof buf, "%F %T %Z", &tm); > printf("after2: %s %ld\n", buf, t); > > tm.tm_mday +=3D 1; > t =3D mktime(&tm); > > strftime(buf, sizeof buf, "%F %T %Z", &tm); > printf("after3: %s %ld\n", buf, t); > } > > int main(int argc, char *argv[]) > { > test10(); > return 0; > } > > $ musl-gcc foo.c -o foo && TZ=3DPacific/Apia ./foo > before: 2011-12-30 00:00:00 0 > after1: 2011-12-29 01:00:00 -10 1325156400 > after2: 2011-12-28 01:00:00 -10 1325070000 > after3: 2011-12-29 01:00:00 -10 1325156400 > > > $ gcc foo.c -o foo && TZ=3DPacific/Apia ./foo > before: 2011-12-30 00:00:00 +13 0 > after1: 2011-12-31 00:00:00 +14 1325239200 > after2: 2011-12-30 00:00:00 +14 -1 > after3: 2011-12-31 00:00:00 +14 1325239200 > > > So this is a also a bug in struct tm interpretation. > > Behavior is consistent with tm_mday =3D 31; > > AW > > > > > On Monday, March 25th, 2024 at 14:42, Rich Felker dalias@libc.org wrote: > > > On Mon, Mar 25, 2024 at 01:24:57PM +0000, Alexander Weps wrote: > > > > > See below. > > > > > > AW > > > > > > On Monday, March 25th, 2024 at 14:13, Rich Felker dalias@libc.org wro= te: > > > > > > > On Mon, Mar 25, 2024 at 12:55:28PM +0000, Alexander Weps wrote: > > > > > > > > > > If you take your test program and switch it to initialize with > > > > > > tm_mday=3D31, then do -=3D1 instead of +=3D1, you'll find that = it gives > > > > > > 2011-12-29 01:00:00 -10 as well, only now it seems like the cor= rect, > > > > > > expected thing to happen. Any change to "fix" the case you're > > > > > > complaining about would necessarily break this case. > > > > > > > > > > So (- day, +day): > > > > > > > > > > Musl: > > > > > 2011-12-31 01:00:00 +14 > > > > > 2011-12-29 01:00:00 -10 > > > > > 2011-12-29 01:00:00 -10 > > > > > > > > > > Glibc: > > > > > 2012-01-01 01:00:00 +14 > > > > > 2011-12-31 01:00:00 +14 > > > > > 2012-01-01 01:00:00 +14 > > > > > > > > > > Seems like musl doesn't even interpret the initial struct tm > > > > > correctly in that case. It is off by day. > > > > > > > > > > Because December only had 30 days, 31s day after normalization is > > > > > January 1st. > > > > > > > > This is nonsense. December has a day 31, which you can clearly see > > > > from the glibc output. For this particular year in this zone, with = the > > > > zone rule change, there are "only 30 days" in December, but they ar= e > > > > numbered 1-29 and 31, not 1-30. > > > > > > You confuse day of month which is represented in tm_mday with > > > calendar day that is interpreted by strftime. > > > > > > You said to set tm_mday =3D 31, which would be January 1st after norm= alization. > > > December 31s is 30th day of month represented as tm_mday =3D 30. > > > > OK, I meant tm_mday=3D31-1. > > > > > > What did you do that got glibc to output 2012-01-01? I guess you wr= ote > > > > code to do some wacky arithmetic after the original code you alread= y > > > > had, rather than changing the code to start with 2011-12-31 as I > > > > suggested to get a look at what's happening. > > > > > > > > > > In any case, the core issue you're hitting here is that time zo= nes are > > > > > > HARD to work with and that there is inherent complexity that li= bc > > > > > > cannot save you from. You only got lucky that what you were try= ing to > > > > > > do "worked" with glibc because you were iterating days forward;= if you > > > > > > were doing reverse, it would break exactly the same way. > > > > > > > > > > I am not really commenting on this, until you sort out the above > > > > > inconsistencies. > > > > > > > > I already have but you refuse to look. > > > > > > It was addressed, do didn't scroll at the end of the e-mail. > > > > Run the attached passing the starting date to check as the first/only > > argument, and these test dates: > > > > - "2011-12-29 00:00:00" > > - "2011-12-31 00:00:00" > > > > Hopefully that will clarify things for you. On musl you will see: > > > > normalized input: 2011-12-29 00:00:00 -10 > > +1day per mktime: 2011-12-29 00:00:00 -10 > > +1day via time_t: 2011-12-31 00:00:00 +14 > > -1day per mktime: 2011-12-28 00:00:00 -10 > > -1day via time_t: 2011-12-28 00:00:00 -10 > > > > normalized input: 2011-12-31 00:00:00 +14 > > +1day per mktime: 2012-01-01 00:00:00 +14 > > +1day via time_t: 2012-01-01 00:00:00 +14 > > -1day per mktime: 2011-12-29 00:00:00 -10 > > -1day via time_t: 2011-12-29 00:00:00 -10 > > > > You can see what you get on glibc. > > > > Rich