mktime() like function to produce GMT?

Guido van Rossum guido at eric.cnri.reston.va.us
Mon May 3 15:05:32 EDT 1999


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.

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

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list