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



The information in this article applies to:

  • Microsoft Win32s 1.3c
  • Microsoft Win32s 1.1
  • Microsoft Win32s 1.15
  • Microsoft Win32s 1.2
  • Microsoft Win32s 1.3

This article was previously published under Q117893

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.

Modification Type:MajorLast Reviewed:3/15/2004
Keywords:KB117893