From: guido at eric.cnri.reston.va.us (Guido van Rossum)
Date: 30 Apr 1999 11:22:50 -0400
Subject: mktime() like function to produce GMT?
References: <00be01be9308$c649cf60$0301a8c0@cbd.net.au> <3729B855.3A7C6B5A@lemburg.com>
Message-ID: <5lu2ty9lpx.fsf@eric.cnri.reston.va.us>
Content-Length: 1428
X-UID: 556                                                  

> 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/)




