BUG: SetTimeZoneInformation() May Alter the System Time
ID: Q235955
|
The information in this article applies to:
-
Microsoft Win32 Application Programming Interface (API), used with:
SYMPTOMS
The SetTimeZoneInformation() function sets the current time-zone parameters. These parameters control translations from Coordinated Universal Time (UTC) to the local time.
If the time-zone parameters are configured for a time zone that does not observe daylight saving time, a call to SetTimeZoneInformation() sets the time-zone parameters, but it may also change the system time.
NOTE: This bug occurs only if the time zone selected at startup time does not observe daylight saving time.
RESOLUTION
The following algorithm can be used to work around this problem:
- Obtain the current system time by calling GetSystemTime().
- Issue a call to SetTimeZoneInformation() and specify the time zone you want to set.
- Call GetSystemTime() again to determine whether the system time has been altered. If the system time has been altered, set it back to the original time using SetSystemTime().
The following code is a console sample implementation of the algorithm detailed above.
#include <windows.h>
#include <stdio.h>
int AdjustTimeZone ();
int main(int argc, char* argv[])
{
BOOL bRetVal = FALSE;
bRetVal = AdjustTimeZone();
printf("Adjusting timezone information. Please wait...\n");
if ( bRetVal )
printf("TimeZone Adjusted.\n");
else
printf("Failed to adjust timezone information correctly.\n");
return 0;
}
int AdjustTimeZone()
{
BOOL bTZVal = 0;
SYSTEMTIME SystemTimeBegin;
SYSTEMTIME SystemTimeEnd;
TIME_ZONE_INFORMATION lpTZInfo;
// Get the system time by calling GetSystemTime
GetSystemTime(&SystemTimeBegin);
printf("Beginning Timezone Information:\n");
printf("Year: %d\n", SystemTimeBegin.wYear);
printf("Month: %d\n", SystemTimeBegin.wMonth);
printf("Day: %d\n", SystemTimeBegin.wDay);
printf("DOW: %d\n", SystemTimeBegin.wDayOfWeek);
printf("H:M:S - %d:%d:%d\n", SystemTimeBegin.wHour, SystemTimeBegin.wMinute, SystemTimeBegin.wSecond);
printf("\n\n");
// Issue a call to SetTimeZoneInformation() and
// specify the time zone you want to set.
// Change these values to the desired
// timezone information.
lpTZInfo.Bias = 0L;
lpTZInfo.StandardBias = 0L
// See TIME_ZONE_INFORMATION in the documentation
// for an explanation of StandardName and other members.
strcpy((char *)lpTZInfo.StandardName, "EST");
lpTZInfo.DaylightBias = 0L
SetTimeZoneInformation(&lpTZInfo);
// Call GetSystemTime() again to determine whether
// the system time has been altered.
GetSystemTime(&SystemTimeEnd);
printf("Ending Timezone Information:\n");
printf("Year: %d\n", SystemTimeBegin.wYear);
printf("Month: %d\n", SystemTimeBegin.wMonth);
printf("Day: %d\n", SystemTimeBegin.wDay);
printf("DOW: %d\n", SystemTimeBegin.wDayOfWeek);
printf("H:M:S - %d:%d:%d\n", SystemTimeBegin.wHour, SystemTimeBegin.wMinute, SystemTimeBegin.wSecond);
// If the system time has been altered, set it
// back to the original time using SetSystemTime().
if (SystemTimeEnd.wDay != SystemTimeBegin.wDay &&
SystemTimeEnd.wDayOfWeek != SystemTimeBegin.wDayOfWeek &&
SystemTimeEnd.wHour != SystemTimeBegin.wHour &&
SystemTimeEnd.wMinute != SystemTimeBegin.wMinute &&
SystemTimeEnd.wMonth != SystemTimeBegin.wMonth &&
SystemTimeEnd.wSecond != SystemTimeBegin.wSecond &&
SystemTimeEnd.wYear != SystemTimeBegin.wYear)
{
SetSystemTime(&SystemTimeBegin);
}
else
bTZVal = 1;
return bTZVal;
}
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article.
MORE INFORMATION
Additional query words:
Keywords : kbAPI kbDateTime kbKernBase kbNTOS400 kbSDKPlatform kbSDKWin32 kbDSupport kbGrpKernBase
Version : winnt:4.0
Platform : winnt
Issue type : kbbug