Subject: time functions questions
To: 'tech-userlevel@netbsd.org' <tech-userlevel@netbsd.org>
From: Scott Burns <Scott.Burns@Netcontech.Com>
List: tech-userlevel
Date: 11/02/2000 11:22:25
Not sure if this is the correct list or not for a 'C' library question ?

I came across a problem piece of code which I cannot tell if it is using the libc time
functions correctly or not. If it is, I think there may be a bug.

The program needs to determine localtime midnight in GMT time. I get the same results on
WinNT Workstation V4 - SP5 (I know - I should not base accuracy on this platform ;-)).

This program contains:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( int argc, char *argv[] )
{
	time_t	BaseTime;
	struct tm	*Local_Tm;
	struct tm	*GMT_Tm;
	char	Formatted[ 1024 ];
	time( &BaseTime );
	Local_Tm = localtime( &BaseTime );
	strftime(	Formatted, 1024, "%Y-%m-%d %H:%M:%S %Z", Local_Tm );
	printf( "LocalTime = %s\n", Formatted );
	GMT_Tm = gmtime( &BaseTime );
	strftime(	Formatted, 1024, "%Y-%m-%d %H:%M:%S %Z", GMT_Tm );
	printf( "GMTTime = %s\n", Formatted );
	/*  Move to midnight */
	Local_Tm->tm_hour = 0;
	Local_Tm->tm_min = 0;
	Local_Tm->tm_sec = 0;
	strftime(	Formatted, 1024, "%Y-%m-%d %H:%M:%S %Z", Local_Tm );
	printf( "LocalTime midnight = %s\n", Formatted );
	BaseTime = mktime( Local_Tm );
	GMT_Tm = gmtime( &BaseTime );
	strftime(	Formatted, 1024, "%Y-%m-%d %H:%M:%S %Z", GMT_Tm );
	printf( "GMTTime midnight = %s\n", Formatted );
	exit( EXIT_SUCCESS );
}

ncti103# uname -a
NetBSD ncti103 1.3.3 NetBSD 1.3.3 (ncti103_100) #0: Wed Feb 23 20:59:26 EST 2000
     root@ncti103:/usr/src/sys/arch/i386/compile/ncti103_100 i386
ncti103# date
Thu Nov  2 10:20:11 EST 2000
ncti103# ./timetesting
LocalTime = 2000-11-02 10:20:14 EST
GMTTime = 2000-11-02 15:20:14 EST
LocalTime midnight = 2000-11-02 00:00:00 EST
GMTTime midnight = 2000-11-02 05:00:00 EST

Question 1:

Is the a correct usage of mktime() is my one question (ie shoving in a localtime composed tm
struct, and then reapplying gmtime() to it ?

Question 2:

Why does the timezone print as EST on printf() number 2 and 4 ? Should a call to gmtime()
not force the timezone to GMT ?

Thanks
Scott Burns
NETCON Technologies Inc.