aboutsummaryrefslogtreecommitdiffstats
path: root/missing_d/timegm.c
blob: faf377ed7d249f0d544db5b3d2d7c31811959b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <time.h>
#include <stdlib.h>

/* timegm -- based on Linux timegm man page */

time_t
timegm(struct tm *tm)
{
	time_t ret;
	char *tz = getenv("TZ");
	const char *tzreq = "UTC+0";	/* more portable than ""? */

	if (tz)
		tz = estrdup(tz, strlen(tz));
	if (setenv("TZ", tzreq, 1) < 0) {
		warning(_("setenv(TZ, %s) failed (%s)"), tzreq, strerror(errno));
		return -1;
	}
	tzset();
	ret = mktime(tm);
	if (tz) {
		if (setenv("TZ", tz, 1) < 0)
			fatal(_("setenv(TZ, %s) restoration failed (%s)"), tz, strerror(errno));
		free(tz);
	} else {
		if (unsetenv("TZ") < 0)
			fatal(_("unsetenv(TZ) failed (%s)"), strerror(errno));
	}
	tzset();
	return ret;
}