Date: Tue, 4 Sep 2001 14:01:02 -0700 (PDT) From: Archie Cobbs <archie@packetdesign.com> To: FreeBSD-gnats-submit@freebsd.org Subject: bin/30321: strftime(3) '%s' format does not work properly Message-ID: <200109042101.f84L12873812@bubba.packetdesign.com>
next in thread | raw e-mail | index | archive | help
>Number: 30321
>Category: bin
>Synopsis: strftime(3) '%s' format does not work properly
>Confidential: no
>Severity: non-critical
>Priority: medium
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: sw-bug
>Submitter-Id: current-users
>Arrival-Date: Tue Sep 04 14:10:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Archie Cobbs
>Release: FreeBSD 4.3-RELEASE i386
>Organization:
Packet Design
>Environment:
System: FreeBSD bubba.packetdesign.com 4.3-RELEASE FreeBSD 4.3-RELEASE #0: Thu Apr 26 15:28:39 PDT 2001 root@bubba.packetdesign.com:/usr/obj/usr/src/sys/BUBBA i386
>Description:
The strftime(3) '%s' format is supposed to convert a
'struct tm' into the number of seconds since the epoch
(1/1/70 GMT).
When it does this conversion, it uses the currently set
system time zone instead of using the time zone from the
'struct tm' argument. Therefore, if the two timezones are
different, incorrect results are computed.
>How-To-Repeat:
Run this program with your system time zone set to
any timezone other than GMT. Notice that the value
printed for "now" is different before and after the
converstion.
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int
main(int ac, char **av)
{
time_t now;
char buf[32];
now = time(NULL);
printf("now = %lu\n", (u_long)now);
strftime(buf, sizeof(buf), "%s", gmtime(&now));
printf("now = %s\n", buf);
return (0);
}
>Fix:
I think this should fix it. From the timegm(3) man
page, "The tm_isdst and tm_gmtoff members are forced
to zero by timegm()" which I guess means that the
value of 'tm_gmtoff' is added into the computation
before being forced to zero.
Index: strftime.c
===================================================================
RCS file: /home/cvs/freebsd/src/lib/libc/stdtime/strftime.c,v
retrieving revision 1.25.2.3
diff -u -r1.25.2.3 strftime.c
--- strftime.c 2001/02/18 04:06:49 1.25.2.3
+++ strftime.c 2001/09/04 20:58:27
@@ -244,7 +244,7 @@
time_t mkt;
tm = *t;
- mkt = mktime(&tm);
+ mkt = timegm(&tm);
if (TYPE_SIGNED(time_t))
(void) sprintf(buf, "%ld",
(long) mkt);
>Release-Note:
>Audit-Trail:
>Unformatted:
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200109042101.f84L12873812>
