from date/time string to seconds since epoch

Peter Kleiweg p.c.j.kleiweg at
Wed Jul 6 21:07:26 CEST 2005

Is there a safe and clean way to parse a date/time string into
seconds since epoch?

I have a string with date and time in GMT. I can get the correct
value using this:

#!/usr/bin/env python
import os
os.environ['TZ'] = 'UTC'
import time
s = 'Wed, 06 Jul 2005 16:49:38 GMT'
seconds = time.mktime(time.strptime(s, '%a, %d %b %Y %H:%M:%S %Z'))

However, I also need conversions to localtime. Setting TZ to UTC
before importing the time module won't let me do this. Changing
TZ after importing time has no effect.

Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)

More information about the Python-list mailing list