Microsoft KB Archive/117893

{|
 * width="100%"|

PRB: Result of localtime Differs on Win32s and Windows NT

 * }

Q117893

-

The information in this article applies to:


 * Microsoft Win32s versions 1.1, 1.15, 1.2, 1.3, 1.3c

-

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.

Additional query words: 1.10 1.20

Keywords : kbOSWin32s

Issue type :

Technology : kbWin32sSearch kbWin32s110 kbWin32s115 kbWin32s120 kbWin32s130 kbWin32s130c