converting datetime with tzinfo to unix timestamp
torriem at gmail.com
Sat Mar 13 00:15:55 CET 2010
On Python 2.5 here.
I've searched and searched but I can't find any way to convert a
datetime object that includes a timezone (tzinfo) to a unix timestamp.
Folks on the net say to simply use the timetuple() method of the object
and feed that to time.mktime(). But that just doesn't seem to work for
me. At least if my understanding that the unix timestamp is always UTC.
I'm using the pytz module to create a datetime object of a certain
timezone. For example:
mountain = pytz.timezone("US/Mountain") # MST or MDT depending on date
eastern = pytz.timezone("US/Eastern") # EST or EDT depending on date
date1 = mountain.localize(datetime.datetime(2010, 3, 12, 9, 0))
date2 = eastern.localize(datetime.datetime(2010, 3, 12, 9, 0))
Now if I examine the two objects, I get:
>>> print date1
>>> print date2
However the standard time.mktime(date2.timetuple()) thing gives me a
timestamp, but it's in my local timezone and gives me the same answer
for both dates!
Can anyone point me in the right direction?
More information about the Python-list