[tz] strftime %s

Paul Eggert eggert at cs.ucla.edu
Wed Jan 10 19:20:38 UTC 2024


On 2024-01-10 08:35, Dag-Erling Smørgrav via tz wrote:
> Currently, strftime() implements %s by calling mktime() and then
> printing the result.  This is fine when the struct tm passed to
> strftime() came from localtime() but not when it didn't.  A better
> solution would be to call timegm() and then manually adjust the result.
> Of course that's only possible in the TM_GMTOFF case but that's still
> better than nothing.

Thanks, I hadn't considered that use case.

This is a tricky area, as the C standard and POSIX both require strftime 
to look only at tm_isdst when formatting %z and %Z. If strftime simply 
called timegm and munged the result according to the input tm_gmtoff, it 
wouldn't conform to the standards.

Your example used %s which isn't standardized by C or by current POSIX. 
However, the latest draft for the next POSIX says for %s that strftime 
must act as if mktime was called with tm_isdst, and this would conflict 
with the implementation you're proposing.

Perhaps a better approach would be for tzcode to implement strftime_z a 
la NetBSD. That way, you could tell strftime_z that the struct tm came 
from gmtime. See:

https://man.netbsd.org/strftime_z.3

I vaguely recall thinking that strftime_z wasn't needed and therefore 
omitting it from tzcode, but your example suggests otherwise.


More information about the tz mailing list