Knowledge Base

FIX: time() Function Overcompensates for Daylight Saving Time

Article ID: 182042

Article Last Modified on 1/23/2007


APPLIES TO


This article was previously published under Q182042

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.

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).

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:

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.

MORE INFORMATION

To reproduce this problem:
  1. Compile the sample code below using Visual C++ 4.x.
  2. Open the Date/Time application and make the following changes:

    • Set the time zone to GMT + 9:00 (Tokyo, Osaka, Sapporo, Seoul, Yakutsk).
    • Set the time to October 25, 11:55 PM.
  3. Start running the application on Windows NT before the clock changes to midnight, and stop running the application several seconds after midnight.
  4. Open the output file generated by the program. Here is a brief section of the output you should see:
          877849195
          877849196
          877849197
          877849198
          877849199
          877852800  << this time is 3600 seconds, or 1 hour off
          877849201
                 .
                 .
                 .
The following is sample code:
/* 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

Keywords: kbbug kbcode kbcrt kbfix kbvc400fix KB182042