Microsoft KB Archive/182042

= FIX: time Function Overcompensates for Daylight Saving Time =

Article ID: 182042

Article Last Modified on 1/23/2007

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q182042

<div class="symptoms_section">

SYMPTOMS
The 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.

<div class="cause_section">

CAUSE
The time function calls GetLocalTime, which returns the exact time, compensated for time zones and Daylight Saving 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 Saving Time. If isindst returns TRUE, the time is altered by 3600 seconds (one hour).

<div class="resolution_section">

RESOLUTION
This 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:

99456 Win32 Equivalents to C Run-Time Functions

</li> Workaround 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.</li></ul>

<div class="status_section">

STATUS
Microsoft 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.

<div class="moreinformation_section">

MORE INFORMATION
To reproduce this problem: <ol> Compile the sample code below using Visual C++ 4.x.</li> Open the Date/Time application and make the following changes:

 Set the time zone to GMT + 9:00 (Tokyo, Osaka, Sapporo, Seoul, Yakutsk).</li> Set the time to October 25, 11:55 PM.</li></ul> </li> Start running the application on Windows NT before the clock changes to midnight, and stop running the application several seconds after midnight.</li>  Open the output file generated by the program. Here is a brief section of the output you should see: <pre class="fixed_text">     877849195 877849196     877849197      877849198      877849199      877852800  << this time is 3600 seconds, or 1 hour off 877849201            .             .             . </li></ol>

The following is sample code: /* Compiler options needed: none */


 * 1) include <time.h>
 * 2) include <stdio.h>
 * 3) include <fstream.h>
 * 4) 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

Keywords: kbbug kbcode kbcrt kbfix kbvc400fix KB182042

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.