FIX: time() Function Overcompensates for Daylight Savings TimeLast reviewed: March 6, 1998Article ID: Q182042 |
The information in this article applies to:
SYMPTOMSThe C run-time library time() function may, in some geographic locations and at certain times, return a time that is off by one hour to the actual time. This problem is specific to areas that do not use Daylight Savings Time (Japan, for example, or in parts of Indiana in the United States), and the problem occurs only at times at the beginning or end of Daylight Savings Time.
CAUSEThe time() function calls GetLocalTime, which returns the exact time, compensated for time zones and Daylight Savings Time. However, before the function ends it also calls the isindst() function. This function uses a standard formula for determining if the current date and time are part of Daylight Savings Time. If isindst() returns TRUE, the time is altered by 3600 seconds (one hour).
RESOLUTIONThis problem has been corrected in Microsoft Visual C++ versions 5.0 and later. If you cannot upgrade to Visual C++ 5.0, there are two workarounds to this problem: Workaround 1. Call GetLocalTime() instead of time(). This method is documented in the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q99456 TITLE : Win32 Equivalents to C Run-Time FunctionsWorkaround 2. If possible, start the Date/Time application and clear the check box to "Automatically adjust clock for daylight saving changes" (please note that this workaround may not be available in all areas). When time() is called, GetLocalTime will return the time adjusted for the local time zone, and isindst will cause time() to adjust the time for Daylight Savings Time.
STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been corrected in Microsoft Visual C++, 32-bit edition, versions 5.0 and later.
MORE INFORMATIONTo reproduce this problem:
/* Compiler options needed: none */ #include <time.h> #include <stdio.h> #include <fstream.h> #include <windows.h> int main(void) { time_t t1; ofstream ostr("timebug.txt"); for (;;) { time(&t1); ostr << t1 << endl; Sleep(1000); } ostr.close(); return 0; } |
Additional query words: time mktime asctime _ftime gmtime tzset
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |