[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

converting datetime with tzinfo to unix timestamp

Michael Torrie

3/12/2010 11:16:00 PM

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:


import pytz
import datetime
import time

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
2010-03-12 09:00:00-07:00

>>> print date2
2010-03-12 09:00:00-05:00

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?

thanks.