Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 13 May 2024 08:18:04 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: strptime(s, "%Y-%j", &tm) does not update month and day

On Sun, May 12, 2024 at 09:54:13AM +0200, Petr Pisar wrote:
> V Sat, May 11, 2024 at 05:28:54PM -0400, Rich Felker napsal(a):
> > On Sat, May 11, 2024 at 05:24:59PM +0200, Petr Pisar wrote:
> > > Hello,
> > > 
> > > When debugging a libisds test failure with musl-1.2.5
> > > <https://bugs.gentoo.org/show_bug.cgi?id=928107>, I found that musl's
> > > strptime() does not handle "%Y-%j" properly. It accepts the format, it parses
> > > the input string, it returns a correct pointer past the input string, but it
> > > does not update tm.tm_mon and tm.tm_mday fileds of the third argument.
> > 
> > This is behaving as specified. Previously, the behavior was
> > unspecified. POSIX has amended it for future issues as the resolution
> > of https://austingroupbugs.net/view.php?id=1727 to read:
> > 
> >     The tm_yday member of the tm structure pointed to by tm shall be
> >     set to this number minus 1.
> > 
> > strptime generally does not behave like mktime, doing
> > normalizations/conversions.
> > 
> I see. I thought that strptime() does the normalization. Reading
> mktime() specification reveals it has a side effect of normalizing tm_yday
> from tm_year, tm_mon, and tm_mday.
> 
> However, I need the oposite and I cannot find any standard library function
> for that. I will probaly resort to my own implementation with the drawback
> of assumption that rules for leap years in Gregorian calendar won't change.

That's certainly an option. I think you can also use mktime, though,
if you want. Starting from a valid struct tm for Jan 1, then adding
the tm_yday number to tm_mday, should get you most of the way there,
short of timezone/dst mess. You could avoid that by using timegm, or
if you assume POSIX-compatible time_t, just adding 86400*tm_yday to
the time_t.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.