mktime.c 601 字节
Newer Older
R
Rich Felker 已提交
1 2 3 4
#include "time_impl.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
R
Rich Felker 已提交
5 6 7

time_t mktime(struct tm *tm)
{
R
Rich Felker 已提交
8 9 10 11 12
	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);
R
Rich Felker 已提交
13

R
Rich Felker 已提交
14 15
	if (tm->tm_isdst>=0 && new.tm_isdst!=tm->tm_isdst)
		t += opp - new.__tm_gmtoff;
R
Rich Felker 已提交
16

R
Rich Felker 已提交
17 18
	t += new.__tm_gmtoff;
	if ((time_t)t != t) goto error;
R
Rich Felker 已提交
19

R
Rich Felker 已提交
20
	__secs_to_zone(t, 0, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone);
R
Rich Felker 已提交
21

R
Rich Felker 已提交
22 23 24
	if (__secs_to_tm(t - new.__tm_gmtoff, &new) < 0) goto error;

	*tm = new;
R
Rich Felker 已提交
25
	return t;
R
Rich Felker 已提交
26 27

error:
28
	errno = EOVERFLOW;
R
Rich Felker 已提交
29
	return -1;
R
Rich Felker 已提交
30
}