mktime() like function to produce GMT?

Mark Nottingham mnot at pobox.com
Thu May 6 08:37:42 EDT 1999


> BTW, any chance of getting the C API timegm() into the time module
> on those platforms where it is available and have your function
> as replacement on those that don't ?

That would be great. Just to flag it, the function Guido posted doesn't take
two-digit dates into account; rfc822.parsedate() produces two-digit years
from RFC 850-style dates (amazon.com brought this one up).

I'm sure there are prettier, faster ways to do this to the code  (BTW,
assert isn't clearly documented in the reference, AFAIK):

def timegm(tmtuple):
    import calendar
    EPOCH = 1970
    year, month, day, hour, minute, second = tmtuple[:6]
    try:
        assert year >= EPOCH
    except AssertionError:
        if year < 69:
            year = year + 2000
        else:
            year = year + 1900
        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





More information about the Python-list mailing list