Conversion of time-string to Unix time

 

Alexander Liss

 

03/27/2007

 

A usual way of conversion of time-string as 03/27/2006-15:55 to Unix time (a number of seconds from Epoch time_t) is via a combination of strptime and mktime.

The function strptime converts a string into structure tm, and mktime converts this structure into Unix time.

To account for Daylight Savings Time (DST), there is a member in this structure tm_isdst. When it is zero no DST, when it is 1 there is DST.

Function mktime according to its description is smart, and should correct values of this structure; supposedly it should correct tm_isdst, when it is not set right.

When tm_isdst is set to -1 we explicitly request such correction.

Unfortunately, strptime only clears tm_isdst, it does not set it properly. When tm_isdst is zero, mktime does not check for DST and does not correct the structure. This behavior is not clear from functions description.

The working sequence of calls: strptime, set tm_isdst to -1, mktime.