mktime() like function to produce GMT?

M.-A. Lemburg mal at lemburg.com
Tue May 4 05:10:39 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).
> This assumes the POSIX algorithm.

The last short sentence pretty nicely covers up the problems with timegm()
:-) "POSIX algorithm" means that leap seconds are not accounted for.

> def timegm(tuple):
>         import calendar
>         EPOCH = 1970
>         year, month, day, hour, minute, second = tuple[:6]
>         assert year >= EPOCH
>         assert 1 <= month <= 12
>         days = 365*(year-EPOCH) + calendar.leapdays(EPOCH, year)
>         for i in range(1, month):
>                 days = days + calendar.mdays[i]
>         if month > 2 and calendar.isleap(year):
>                 days = days + 1
>         days = days + day - 1
>         hours = days*24 + hour
>         minutes = hours*60 + minute
>         seconds = minutes*60 + second
>         return seconds

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                            Y2000: 241 days left
Business:                                      http://www.lemburg.com/
Python Pages:                 http://starship.python.net/crew/lemburg/





More information about the Python-list mailing list