timegm()
is a non-standard GNU extension. A portable version using mktime()
is below. This sets the TZ
environment variable to UTC, calls mktime()
and restores the value of TZ
. Since TZ
is modified this might not be thread safe. I understand the GNU libc version of tzset()
does use a mutex so should be thread safe.
See:
#include <time.h>
#include <stdlib.h>
time_t
my_timegm(struct tm *tm)
{
time_t ret;
char *tz;
tz = getenv("TZ");
setenv("TZ", "", 1);
tzset();
ret = mktime(tm);
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
return ret;
}