tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: strftime(3) oddities with %s, %z



On Tue, Oct 25, 2022 at 02:56:23PM +0700, Robert Elz wrote:
 > I am not sure that calling strftime() on something obtained from gmtime()
 > makes any sense.  If one wants UTC conversions, either use strftime_z()
 > (not a POSIX interface) or set the timezone to UTC (tzset() with TZ="UTC").

Where is this documented?

I would expect strftime to print GMT if fed the results of gmtime(),
and indeed ours does. Its man page says it "formats the information
from timeptr" (timeptr being the struct tm argument) and says nothing
about either including or depending on other random information, or
restrictions about where the struct tm is allowed to have come from.

There is also nothing in gmtime(3) or tm(3) about this supposed
restriction.

This is what I get printing %c %Z %z %s:

   % ./test 2000000000
   localtime: Tue May 17 23:33:20 2033 EDT -0400 (2000000000)
   gmtime: Wed May 18 03:33:20 2033 EST -0500 (2000018000)

That is, it prints the correct time but the wrong zone, gets the %s
value wrong by the offset of the zone it prints, and _also_ doesn't
even manage to print the local zone correctly by virtue of apparently
fumbling summer time. Or maybe by using the GMT/UTC rules for summer
time (that is, none) instead of using the rules for the zone it
printed.

I see no reason to consider this anything but a bug, especially since
our struct tm contains the timezone info.

(And while I'm complaining, strftime_z(3) says nothing about what the
passed-in zone does or how it modifies the output, let alone whether
it has to be consistent with a previous call to localtime_rz(3) that
fetched the struct tm.)

-- 
David A. Holland
dholland%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index