epoch seconds from a datetime

Richard Rossel henhiskan at gmail.com
Thu Aug 28 13:18:48 EDT 2008


Hi friends,
I need a little help here, I 'm stuck with epoch calculation issue.
I have this datetime:
date_new = datetime(*time.strptime('20080101T000000','%Y%m%dT%H%M%S')
[0:6])
This date_new is in UTC
Now I need to know the seconds since epoch of this new date, so I run
this:
seconds = int(time.mktime(date_new.timetuple()))
but the seconds returned belongs to :
Tue, 01 Jan 2008 03:00:00 GMT
because the  localtime is in timezone 'America/Santiago': -3

I fix this trying to alter the TZ with time.tzset():
 os.environ['TZ'] = 'UTC'
time.tzset()

.... and now I can gets the right epoch, but I can't restore the
previous TimeZone, I try with:
os.environ['TZ'] = '', but the time.tzset() doesn't back to the
original ( America/Santiago)

A solution should be set the os.environ['TZ']   to  'America/Santiago'
but I can't make a TZ hardcode because
the software should works on different timezones.

So the question, how can restore the system into original timezone, or
how to know the seconds since epoch
from UTC datetime without change the local system TIMEZONE.

please help




More information about the Python-list mailing list