Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning millisecond representation of datetime in python

Using Python, I'm storing a date & time as datetime.datetime into GAE. Is there a way to get the value of the date time in milliseconds as opposed to the fully formatted string version?

Based on the docs for datetime.datetime, I don't see any native methods on the date time class that does this. http://docs.python.org/release/2.5.2/lib/datetime-datetime.html

The original date value is stored this way:

date_time_float = 1015182600   #some date as timestamp
date_time_object = datetime.fromtimestamp(date_time_float);                                                

When I pull the data from the store, it is of type:

type(exported_date_time) # type: datetime.datetime

There's strftime to convert into a string representation but what I'm looking for is to convert 'exported_date_time' to milliseconds.

like image 947
Dan Holman Avatar asked Aug 11 '11 18:08

Dan Holman


1 Answers

To get the seconds since the epoch:

date_time_secs = time.mktime(datetimeobj.timetuple())

or for the whole thing in miliseconds

date_time_milis = time.mktime(datetimeobj.timetuple()) * 1000 + datetimeobj.microsecond / 1000

or similar.

like image 192
agf Avatar answered Oct 24 '22 10:10

agf