KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q182042: FIX: time() Function Overcompensates for Daylight Savings Time

Article: Q182042
Product(s): Microsoft C Compiler
Version(s): WINNT:4.0,4.0a,4.1,4.2,4.2b
Operating System(s): 
Keyword(s): kbcode kbCRT kbVC400fix
Last Modified: 17-JUL-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- The C Run-Time (CRT), included with:
   - Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.0a, 4.1, 4.2b 
   - Microsoft Visual C++, 32-bit Enterprise Edition, version 4.2 
   - Microsoft Visual C++, 32-bit Professional Edition, version 4.2 
-------------------------------------------------------------------------------

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

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:

  Q99456 Win32 Equivalents to C Run-Time Functions

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

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          : kbcode kbCRT kbVC400fix 
Technology        : kbVCsearch kbAudDeveloper kbCRT
Version           : WINNT:4.0,4.0a,4.1,4.2,4.2b
Issue type        : kbbug
Solution Type     : kbfix

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.