[PATCH 2/2 v2] compat/timegm: new compat function for systems lacking timegm()

John Keeping john at keeping.me.uk
Thu Apr 4 16:46:33 CEST 2013


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 <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;
> >            }
> 
> 
> 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 = <new value>;
    gmtime_r(&t, tm);

so I wonder whether it would be equivalent to use mktime/localtime_r
instead of timegm/gmtime_r.




More information about the CGit mailing list