From mboxrd@z Thu Jan 1 00:00:00 1970 From: john at keeping.me.uk (John Keeping) Date: Thu, 4 Apr 2013 15:46:33 +0100 Subject: [PATCH 2/2 v2] compat/timegm: new compat function for systems lacking timegm() In-Reply-To: References: <355839ae094873aef82ea1c8b7b7f9f7cc873b9c.1364821972.git.john@keeping.me.uk> <75a384665737f5f9a1768641bc673c91bfa7ebca.1364821972.git.john@keeping.me.uk> <20130401174758.GC2222@serenity.lan> Message-ID: <20130404144632.GL2222@serenity.lan> On Thu, Apr 04, 2013 at 04:13:04PM +0200, Jason A. Donenfeld wrote: > Is this strictly necessary? Would we be perhaps better off working > around gmtime by using an alternative function all together? Or, if > we do require a compat version, the man page suggests this much > simpler alternative: > > > For a portable version of timegm(), set the TZ environment variable to UTC, call mktime(3) and restore the value of TZ. Something like > > > #include > > #include > > 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; > > } > > > This might not be entirely safe, were cgit to ever become > multi-threaded, but it's certainly a lot simpler than rolling our own. I decided not to do that based on this message: http://lists.samba.org/archive/samba-technical/2002-November/025578.html but of course it doesn't provide any actual reason for why that version doesn't work. We only use timegm in week calculations, where we do: time_t t = timegm(tm); t = ; gmtime_r(&t, tm); so I wonder whether it would be equivalent to use mktime/localtime_r instead of timegm/gmtime_r.