PRB: Time Incorrect if TZ Variable Not Defined

Last reviewed: April 10, 1997
Article ID: Q149702
The information in this article applies to:
  • Microsoft Win32s, versions 1.30a, 1.3c

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 W32s
Version : 1.3a 1.3c
Platform : WINDOWS
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.