Local time or UTC? If it's UTC, the easiest way to do the conversion is to avoid the C time API entirely and use the formula in POSIX for seconds since the epoch:
tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
(tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
Source: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15
If it's local time, the problem turns into hell due to the fact that time_t
is not guaranteed to be represented as seconds since the epoch except on POSIX systems, and the fact that it's difficult to compute a time_t
value corresponding to the epoch (mktime
will not work because it uses local time). Once you compute the time_t
for the epoch, though, it's just a matter of using mktime
for the time value you parsed and then calling difftime
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…