e38b46191c50b1346cb8cafddfe0b85f5ecd14ac
[musl] / src / time / mktime.c
1 #include "time_impl.h"
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 time_t mktime(struct tm *tm)
7 {
8         struct tm new;
9         long opp;
10         long long t = __tm_to_secs(tm);
11
12         __secs_to_zone(t, 1, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone);
13
14         if (tm->tm_isdst>=0 && new.tm_isdst!=tm->tm_isdst)
15                 t += opp - new.__tm_gmtoff;
16
17         t += new.__tm_gmtoff;
18         if ((time_t)t != t) goto error;
19
20         __secs_to_zone(t, 0, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone);
21
22         if (__secs_to_tm(t - new.__tm_gmtoff, &new) < 0) goto error;
23
24         *tm = new;
25         return t;
26
27 error:
28         errno = EINVAL;
29         return -1;
30 }