PRB: Time Incorrect if TZ Variable Not DefinedLast reviewed: April 10, 1997Article ID: Q149702 |
The information in this article applies to:
SYMPTOMSUnder 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.
CAUSEThe 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.
RESOLUTIONIf 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.
STATUSThis behavior is by design.
MORE INFORMATION
Steps to Reproduce BehaviorThe 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |