PRB: Result of localtime() Differs on Win32s and Windows NT

ID: Q117893

1.10 1.15 1.20 1.30 1.30c WINDOWS kbprg

The information in this article applies to:

SYMPTOMS

Under Windows NT, localtime( time() ) returns the correct local time. However, under Win32s, the local time returned is not correct if the TZ environment variable is set. For example, suppose that you are in the Pacific time zone (GMT-08:00) and have set tz=pst8pdt. The time returned under Win32s is off by 8 hours.

CAUSE

This is by design.

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

The time() function returns the current local time under Win32s, then the call to localtime() adjusts the time by the offset of your time zone from GMT, which it finds by reading the tz environment variable.

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

RESOLUTION

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.

KBCategory: kbprg kbprb KBSubcategory: W32s Additional reference words: 1.10 1.15 1.20 1.30 1.30c

Keywords          : kbWin32s 
Version           : 1.10 1.15 1.20 1.30 1.30c
Platform          : WINDOWS

Last Reviewed: January 17, 1997