Someday I might forgive the standards bodies for omitting timegm().

Maybe.  In the meantime, my workarounds for trying to coax UTC without
timegm() are getting uglier and uglier.  Apparently, some systems
don't support setenv()/unsetenv(), so you can't set the TZ env var and
hope thereby to coax mktime() into generating UTC.  Without that, I
don't see a really good alternative to just giving up and converting to
localtime with mktime().  (I suppose I should research the Perl library
approach for computing an inverse function to gmtime(); that might
actually be simpler than this growing list of hacks.)
This commit is contained in:
Tim Kientzle 2008-02-19 06:02:01 +00:00
parent 334a6ee707
commit 54c845efb9

View File

@ -1064,24 +1064,28 @@ time_from_tm(struct tm *t)
if (t->tm_isdst)
t->tm_hour -= 1;
return (mktime(t)); /* Re-convert. */
#else
/*
* If you don't have tm_gmtoff, let's try resetting the timezone
* (yecch!).
*/
#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV) && defined(HAVE_TZSET)
/* No timegm() and no tm_gmtoff, let's try forcing mktime() to UTC. */
time_t ret;
char *tz;
/* Reset the timezone, remember the old one. */
tz = getenv("TZ");
setenv("TZ", "UTC 0", 1);
tzset();
ret = mktime(t);
/* Restore the previous timezone. */
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
return ret;
#else
/* <sigh> We have no choice but to use localtime instead of UTC. */
return (mktime(t));
#endif
}