NetBSD Problem Report #57588
From www@netbsd.org Sat Aug 19 08:45:37 2023
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
by mollari.NetBSD.org (Postfix) with ESMTPS id A8F421A923B
for <gnats-bugs@gnats.NetBSD.org>; Sat, 19 Aug 2023 08:45:37 +0000 (UTC)
Message-Id: <20230819084536.DBAB71A923C@mollari.NetBSD.org>
Date: Sat, 19 Aug 2023 08:45:36 +0000 (UTC)
From: roland.illig@gmx.de
Reply-To: roland.illig@gmx.de
To: gnats-bugs@NetBSD.org
Subject: strftime(3): value of %s depends on TZ environment variable
X-Send-Pr-Version: www-1.0
>Number: 57588
>Category: lib
>Synopsis: strftime(3): value of %s depends on TZ environment variable
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: lib-bug-people
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sat Aug 19 08:50:00 +0000 2023
>Last-Modified: Sat Aug 19 18:30:02 +0000 2023
>Originator: Roland Illig
>Release: 10.99.3
>Organization:
>Environment:
NetBSD nbcurr.roland-illig.de 10.99.3 NetBSD 10.99.3 (GENERIC) #0: Fri Apr 21 02:17:32 UTC 2023 mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
As documented in usr.bin/make/unit-tests/varmod-gmtime.mk 1.16 line 149, calling gmtime followed by strftime("%s") depends on the timezone, even though the timezone should not affect gmtime.
This bug does not affect date(1), as that utility always uses localtime(3) (for undocumented reasons) and rather sets the TZ environment variable instead of using gmtime(3).
>How-To-Repeat:
TZ=Europe/Berlin make -r -f /dev/null -v '${%s:L:gmtime}'
TZ=Europe/Berlin date -u +%s
These two commands should output the same value, but they don't.
Example program:
#include <stdio.h>
#include <time.h>
int
main(void)
{
time_t t = time(NULL);
printf("time\t%lld\n", (long long)t);
const struct tm *tm = gmtime(&t);
printf("tm\t%04d-%02d-%02d %02d:%02d:%02d %s %+d\n",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
tm->tm_isdst ? "DST" : "no-DST",
(int)tm->tm_gmtoff);
char buf[80];
strftime(buf, sizeof(buf), "%s", tm);
printf("ftime\t%s\n", buf);
return 0;
}
>Fix:
>Release-Note:
>Audit-Trail:
From: mlelstv@serpens.de (Michael van Elst)
To: gnats-bugs@netbsd.org
Cc:
Subject: Re: lib/57588: gmtime(3): value of %s depends on TZ environment variable
Date: Sat, 19 Aug 2023 09:28:47 -0000 (UTC)
roland.illig@gmx.de writes:
>As documented in usr.bin/make/unit-tests/varmod-gmtime.mk 1.16 line 149, calling gmtime followed by strftime("%s") depends on the timezone, even though the timezone should not affect gmtime.
>This bug does not affect date(1), as that utility always uses localtime(3) (for undocumented reasons) and rather sets the TZ environment variable instead of using gmtime(3).
The problem is strftime. A standard struct tm doesn't have any information
about the time zone, so to convert a struct tm back to UTC seconds strftime
takes the local timezone. The %s format is not POSIX, our man page refers
to ctime(3), Linux refers to mktime(3) to indicate this.
strftime_z() lets you pass the missing information explicitely.
From: Valery Ushakov <uwe@stderr.spb.ru>
To: gnats-bugs@netbsd.org
Cc:
Subject: Re: lib/57588: gmtime(3): value of %s depends on TZ environment
variable
Date: Sat, 19 Aug 2023 13:35:22 +0300
There was a thread about this recently with some exegetical comments
from kre:
http://mail-index.netbsd.org/tech-userlevel/2022/10/thread1.html#013597
-uwe
From: Simon Gerraty <sjg@crufty.net>
To: gnats-bugs@netbsd.org
Cc: sjg@crufty.net
Subject: Re: lib/57588: gmtime(3): value of %s depends on TZ environment variable
Date: Sat, 19 Aug 2023 09:57:26 -0700
The real problem is mktime(3) (which strtime(3) uses for %s)
#include <sys/types.h>
#include <stdio.h>
#include <time.h>
int
main(void)
{
time_t now;
time_t utime;
time_t ltime;
time(&now);
utime = mktime(gmtime(&now));
ltime = mktime(localtime(&now));
printf("now=\t\t%ld\ngmtime=\t\t%ld\nlocaltime=\t%ld\n",
now, utime, ltime);
return 0;
}
utime and ltime should be the same as now, but
utime is not:
now= 1692464199
gmtime= 1692492999
localtime= 1692464199
From: Christos Zoulas <christos@zoulas.com>
To: gnats-bugs@netbsd.org
Cc: lib-bug-people@netbsd.org,
gnats-admin@netbsd.org,
netbsd-bugs@netbsd.org,
roland.illig@gmx.de
Subject: Re: lib/57588: gmtime(3): value of %s depends on TZ environment
variable
Date: Sat, 19 Aug 2023 14:25:33 -0400
--Apple-Mail=_9E6D08AD-2857-4AD1-81C1-44F33AC7C358
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=us-ascii
There is no problem here. mtkime(3) uses the current timezone to convert.
[2:23pm] 569>env TZ=UTC ./a.out
now= 1692469390
gmtime= 1692469390
localtime= 1692469390
christos
--Apple-Mail=_9E6D08AD-2857-4AD1-81C1-44F33AC7C358
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=signature.asc
Content-Type: application/pgp-signature;
name=signature.asc
Content-Description: Message signed with OpenPGP
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
iF0EARECAB0WIQS+BJlbqPkO0MDBdsRxESqxbLM7OgUCZOEJHQAKCRBxESqxbLM7
OmwbAKDD9tPx6aTzz04biJ35PF5FwL5cEwCfaJD33kozvIHxqrTRlHOL+0e6Po8=
=pKB/
-----END PGP SIGNATURE-----
--Apple-Mail=_9E6D08AD-2857-4AD1-81C1-44F33AC7C358--
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.