The mktime function fails for January 1, 1970 in several time zones that are ahead of Coordinated Universal Time (148790)



The information in this article applies to:

  • Microsoft Visual C++ 1.5
  • Microsoft Visual C++ 1.51
  • Microsoft Visual C++ 1.52
  • Microsoft Visual C++ 2.0
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 2.2
  • Microsoft Visual C++ 4.0
  • Microsoft Visual C++ 4.1
  • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 4.2
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q148790

SUMMARY

For time zones that are ahead of Coordinated Universal Time (Greenwich Mean Time), if you call the mktime function with the argument set to correspond to January 1, 1970 00:00:00 (midnight), mktime returns -1 (failure).

For example, this behavior occurs in the time zone for Cairo, Egypt. This time zone is Coordinated Universal Time + 2. This means that the time in Cairo is two hours ahead of Coordinated Universal Time.

The Visual C++ 4.0 Books Online states:

"...mktime handles dates in any time zone from midnight, January 1, 1970, to midnight, February 5, 2036."

MORE INFORMATION

The problem is not that the mktime function is in error. Instead, the problem is that the documentation states that this function will handle midnight January 1, 1970 in any time zone. Actually, the mktime function returns the number of seconds that have elapsed since January 1, 1970 00:00:00, adjusted for the current time zone. Adjusted for current time zone means that the appropriate number of seconds will be added or subtracted so the mktime function actually returns the number of seconds that have elapsed since midnight January 1, 1970 Coordinated Universal Time. This means that if you call the mktime function for January 1, 1970 in the Pacific Time Zone (Coordinated Universal Time - 8), 28800 is returned. This value is the number of seconds that are contained in 8 hours. The return value of 28800 in the Pacific Time Zone means that 28,800 seconds have elapsed since January 1, 1970 00:00:00 Coordinated Universal Time when it is the same time in the Pacific Time Zone. The problem occurs in time zones that are ahead of Coordinated Universal Time. If you call the mktime function in Cairo (Coordinated Universal Time + 2), a value of -1 is returned for January 1, 1970 00:00:00, because January 1, 1970 00:00:00 had not yet occured in Coordinated Universal Time when the same time happened in Cairo.

Modification Type:MajorLast Reviewed:9/1/2005
Keywords:kbfunctions kbhowto kbdocerr kbdocfix kbOLDocs KB148790 kbAudDeveloper