In my application I need to calculate shifts using a pattern described in a file. Recently, at one of my customers the application was hanging because of the following reason:
If you fill in a 'struct tm' with the exact moment at the end of the wintertime (non-DST) _mktime seems to return an incorrect result.
The code looks like this:
struct tm tm_start;
tm_start.tm_mday = startday;
tm_start.tm_mon = startmonth-1;
tm_start.tm_year = startyear-1900;
tm_start.tm_hour = starthour;
tm_start.tm_min = startmin;
tm_start.tm_sec = startsec;
tm_start.tm_isdst = -1; // Don't know if DST is active at this moment
_int64 contTime = _mktime64(&tm_start);
Suppose that there is a switch from winter- to summer-time on the 5th of April at 2:00. In practice, this means we have the following time-moments:
5 April, 1:58
5 April, 1:59
5 April, 3:00
Since I don't know in the application when DST starts or ends (do I really want to know this?) I pass the date "5 april, 2:00" to _mktime64 using the code shown above.
I would expect _mktime64 to give me the time_t value that corresponds to 5 April, 3:00 (which is exactly the same moment as 5 April, 2:00).
However, this isn't what's happening. _mktime64 changes tm_start to 5 April, 1:00 and returns the corresponding time_t value. Which means that I get a totally different moment. (in fact: every moment between 2:00 and 3:00 causes _mktime64 to return a moment between 1:00 and 2:00)
I thought this was a bug in Visual Studio 2005, but apparently Visual Studio 2010 (Release Candidate) has the same behavior.
The problem appears on both XP and Windows7 (didn't check Vista).
Is this a known bug? Or are there other tips to tackle this problem?