From 1cc81f5cb0df2b66a795ff0c26d7bbc4d16e13c6 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 17 Jul 2013 05:24:50 -0400 Subject: the big time handling overhaul this commit has two major user-visible parts: zoneinfo-format time zones are now supported, and overflow handling is intended to be complete in the sense that all functions return a correct result if and only if the result fits in the destination type, and otherwise return an error. also, some noticable bugs in the way DST detection and normalization worked have been fixed, and performance may be better than before, but it has not been tested. --- src/time/mktime.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'src/time/mktime.c') diff --git a/src/time/mktime.c b/src/time/mktime.c index 858cd50d..e38b4619 100644 --- a/src/time/mktime.c +++ b/src/time/mktime.c @@ -1,24 +1,30 @@ -#include - -#include "__time.h" +#include "time_impl.h" +#include +#include +#include time_t mktime(struct tm *tm) { - int isdst = tm->tm_isdst; - time_t t, lt; + struct tm new; + long opp; + long long t = __tm_to_secs(tm); + + __secs_to_zone(t, 1, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone); - __tzset(); + if (tm->tm_isdst>=0 && new.tm_isdst!=tm->tm_isdst) + t += opp - new.__tm_gmtoff; - tm->tm_sec += __timezone; - if (isdst > 0) tm->tm_sec += __dst_offset; + t += new.__tm_gmtoff; + if ((time_t)t != t) goto error; - t = __tm_to_time(tm); - - lt = t - __timezone; - if (isdst > 0) lt -= __dst_offset; - __time_to_tm(lt, tm); + __secs_to_zone(t, 0, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone); - __dst_adjust(tm); - + if (__secs_to_tm(t - new.__tm_gmtoff, &new) < 0) goto error; + + *tm = new; return t; + +error: + errno = EINVAL; + return -1; } -- cgit v1.2.1