Subject: lib/28324: localtime_r(3) returns GMT
To: None <lib-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: None <kochi@NetBSD.org>
List: netbsd-bugs
Date: 11/16/2004 04:06:00
>Number:         28324
>Category:       lib
>Synopsis:       localtime_r(3) returns GMT
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Nov 16 04:06:00 +0000 2004
>Originator:     Takayoshi Kochi
>Release:        2.0 RC3
>Organization:
>Environment:
NetBSD 2.0 RC3

>Description:
Whether environment variable 'TZ' is set or not, localtime_r(3)
doesn't return localtime but returns GMT.

This occurs on NetBSD RC3 but from the glance of the sourcecode
(libc/time/localtime.c) -current also has the same bug.

>How-To-Repeat:
Compile and run this program.
If functions are called in reverse order (localtime, localtime_r),
localtime_r returns correct localtime.
----------------------------------------------------------------------
#include <time.h>
#include <stdio.h>

int
main()
{
	struct tm t;
	struct tm *pt;
	time_t x;

	x = time(NULL);
	localtime_r(&x, &t);
	printf("localtime_r: %d %d %d\n", t.tm_hour, t.tm_min, t.tm_sec);

	pt = localtime(&x);
	printf("localtime  : %d %d %d\n", pt->tm_hour, pt->tm_min, pt->tm_sec);
}
----------------------------------------------------------------------

>Fix:
call tzset_unlocked() in localtime_r() as well as localtime()?
i haven't tried and not sure the locking rule.