1 #include <time.h>
2 #include <stdlib.h>
3
4 /* timegm -- based on Linux timegm man page */
5
6 time_t
7 timegm(struct tm *tm)
8 {
9 time_t ret;
10 char *tz = getenv("TZ");
11 const char *tzreq = "UTC+0"; /* more portable than ""? */
12
13 if (tz)
14 tz = estrdup(tz, strlen(tz));
15 if (setenv("TZ", tzreq, 1) < 0) {
16 warning(_("setenv(TZ, %s) failed (%s)"), tzreq, strerror(errno));
17 return -1;
18 }
19 tzset();
20 ret = mktime(tm);
21 if (tz) {
22 if (setenv("TZ", tz, 1) < 0)
23 fatal(_("setenv(TZ, %s) restoration failed (%s)"), tz, strerror(errno));
24 free(tz);
25 } else {
26 if (unsetenv("TZ") < 0)
27 fatal(_("unsetenv(TZ) failed (%s)"), strerror(errno));
28 }
29 tzset();
30 return ret;
31 }