mktime() like function to produce GMT?

Fredrik Lundh fredrik at pythonware.com
Mon May 3 18:41:44 EDT 1999


Guido van Rossum wrote:
> Here's a simple all-Python implementation of the timegm() algorithm,
> which doesn't use the time module (it uses the calendar module, but
> only to access the month length table and leap year calculations).

fwiw, here's a much faster (but more obscure) version
of the same code...

def _d(y, m, d, days=(0,31,59,90,120,151,181,212,243,273,304,334,365)):
    return (((y - 1901)*1461)/4 + days[m-1] + d +
     ((m > 2 and not y % 4 and (y % 100 or not y % 400)) and 1))

def timegm(tm, epoch=_d(1970,1,1)):
    year, month, day, h, m, s = tm[:6]
    assert year >= 1970
    assert 1 <= month <= 12
    return (_d(year, month, day) - epoch)*86400 + h*3600 + m*60 + s

doesn't handle leap seconds, though...

</F>





More information about the Python-list mailing list