converting datetime with tzinfo to unix timestamp

M

Michael Torrie

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top