PRB: Time Incorrect if TZ Variable Not Defined

ID: Q149702


The information in this article applies to:


SYMPTOMS

Under Windows NT, the C run-time (CRT) functions, localtime() or ctime() returns the correct local time. However, under Win32s, either ctime() or localtime() does not display the correct time from a 32-bit application when the TZ environment variable is not set to zero.


CAUSE

The ctime() and localtime() functions depends on time zone information that is not available in Win32s. This is the reason that the Win32 API GetLocalTime() is not supported under Win32s. The C Run-time time functions, like localtime(), use the TZ environment variable for time zone information.

The function that causes the time shift is ctime() or localtime(), not time(). The time() function returns the current local time under Win32s, then the call to localtime() or ctime() subtracts nine hours by default if the TZ environment variable is not defined.

Under Windows NT, time() and GetSystemTime() return GMT, therefore localtime( time() ) is the current local time.


RESOLUTION

If TZ is set to zero, the time is displayed correctly. To get the current local time under both Win32s and Windows NT, use the following code to clear the TZ environment variable and get the time:


   _putenv( "TZ=" );
   _tzset();

   localtime( time() ); 


Note that _putenv() affects only the TZ environment variable for the application. All other applications use the global environment settings and make their own modifications.


STATUS

This behavior is by design.


MORE INFORMATION

Steps to Reproduce Behavior

The following sample code demonstrates the problem (the time is shifted by several hours):


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

      time_t   timet;
      char  szMsg[256];

      time(&timet);
      wsprintf(szMsg, "The time is %s\n", ctime(&timet));
      MessageBox(NULL, szMsg, "Win32s Test - time() function", MB_OK); 

Additional query words: crt gmt


Keywords          : kbprg kbWin32s 
Version           : 1.3a 1.3c
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: March 10, 1999