lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, May 02, 2008 at 02:46:05PM +0300, Asko Kauppi wrote:
...
> Oh, well...
> "The timegm function is not specified by any standard; its function  
> cannot be completely emulated using the standard functions described  
> above."
> http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=timegm
> 
> It's annoying to see the ANSI limitations limiting Lua time and again.

As the calculations involved are not particularily complicated,
there is no good reason to rely on a library function
(and dietlibc's timegm is awfully broken).

A simple version valid throughout the domain of 32bit signed time_t is:

static const unsigned short mondays[12]
  = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
/* like BSD's timegm, but not fixing tm */
/* 1 29th Feb for every full 48 months since Feb 68 - best before end of 2099 */
#define gmseconds(t) (3600*(t).tm_hour + 60*(t).tm_min + (t).tm_sec \
  + 86400*(/*days*/ (t).tm_mday-1 + mondays[(t).tm_mon] \
    + 365*((t).tm_year-70) + (((t).tm_year-68)*12+(t).tm_mon-2)/48))

Add some more checks to deal with the full centuries
which are not multiples of 400 (and I am not entirely sure
whether this is correct before 1970, too).

Dates before pope Gregor are a different issue,
but given 32bit time_t no timegm deals with them anyway.


regards