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