Subject: lib/25890: localtime_r() doesn't setup the timezone
To: None <gnats-bugs@gnats.NetBSD.org>
From: None <gcw@primenet.com.au>
List: netbsd-bugs
Date: 06/10/2004 13:15:25
>Number:         25890
>Category:       lib
>Synopsis:       reentrant time functions don't set timezone unless non-reentrant called prior
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jun 10 03:16:01 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     Geoff C. Wing
>Release:        NetBSD 2.0F (2004-06-09)
>Organization:
>Environment:
System: NetBSD g.primenet.com.au 2.0F NetBSD 2.0F (G) #0: Thu Jun 3 22:43:39 EST 2004 gcw@g.primenet.com.au:/usr/netbsd/src/sys/arch/i386/compile/G i386
Architecture: i386
Machine: i386
>Description:
	ctime_r() and localtime_r() don't set up the timezone and unless
	you previously use a non-reentrant version you'll get wrong
	information.

	Running the sample code below gives:

	ctime_r    : Thu Jun 10 03:04:03 2004
	localtime_r: Thu Jun 10 03:04:03 2004
	localtime_r offset: 0
	ctime      : Thu Jun 10 13:04:03 2004
	ctime_r    : Thu Jun 10 13:04:03 2004
	localtime_r: Thu Jun 10 13:04:03 2004
	localtime_r offset: 36000

>How-To-Repeat:

#include <stdio.h>
#include <time.h>

main()
{
    char buf[256], *ret;
    time_t now;
    struct tm ltm;

    time(&now);

    ctime_r(&now, buf);
    printf("ctime_r    : %s", buf);

    localtime_r(&now, &ltm);
    asctime_r(&ltm, buf);
    printf("localtime_r: %s", buf);
    printf("localtime_r offset: %d\n", ltm.tm_gmtoff);

    ret = ctime(&now);
    printf("ctime      : %s", ret);

    ctime_r(&now, buf);
    printf("ctime_r    : %s", buf);

    localtime_r(&now, &ltm);
    asctime_r(&ltm, buf);
    printf("localtime_r: %s", buf);
    printf("localtime_r offset: %d\n", ltm.tm_gmtoff);
}

>Fix:
	Maybe this?  I don't know all the ramifications for reentrant stuff.

--- lib/libc/time/localtime.c.org	2003-12-20 12:46:42.000000000 +1100
+++ lib/libc/time/localtime.c	2004-06-10 13:09:36.000000000 +1000
@@ -1132,6 +1132,7 @@
 struct tm *		tmp;
 {
 	rwlock_rdlock(&lcl_lock);
+	tzset_unlocked();
 	localsub(timep, 0L, tmp);
 	rwlock_unlock(&lcl_lock);
 	return tmp;
>Release-Note:
>Audit-Trail:
>Unformatted: