mktime() like function to produce GMT?
Guido van Rossum
guido at eric.cnri.reston.va.us
Fri Apr 30 11:22:50 EDT 1999
> Mark Nottingham wrote:
> >
> > I need a function to produce a Unix epoch time from a time tuple, a la
> > time.mktime(). Problem is, mktime() gives you the tuple in localtime, which
> > is dangerous if you're dealing with GMT times in the past (like in the
> > HTTP).
> >
> > Is there a function that will make a GMT epoch time straight from a time
> > tuple?
"M.-A. Lemburg" <mal at lemburg.com> replies:
> On some platforms there is gmtime() which does exactly this.
>
> It's available through mxDateTime, BTW, which also offers a
> work-around solution for those platforms where it is not
> available. See the Python Pages below.
Huh? The C library function gmtime() *returns* a time tuple in UTC
(the polutically correct name for GMT).
The standard way to use mktime() with a UTC is to add or subtract the
timezone offset (I admit I can never remember which :-) and force the
DST flag off.
The following function in rfc822 may be helpful (it takes a 10-tuple
whose last item is the tz offset; set this to 0 for UTC):
def mktime_tz(data):
"""Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp."""
if data[9] is None:
# No zone info, so localtime is better assumption than GMT
return time.mktime(data[:8] + (-1,))
else:
t = time.mktime(data[:8] + (0,))
return t - data[9] - time.timezone
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-list
mailing list