Unix timestamp conversion
In a project I'm working on right now I have to convert between unix timestamps and datetime objects in Python.
It was really easy to find on the internet but I add a little note here anyway for my self the next time I need it :-)
import datetime
import time
def datetime_to_unixtime(dt):
return time.mktime(dt.timetuple())+(dt.microsecond/1000000.0)
def unixtime_to_datetime(ut):
return(datetime.datetime.fromtimestamp(float(ut)))
now = datetime.datetime.now()
print now
ut = datetime_to_unixtime(now)
print ut
dt = unixtime_to_datetime(ut)
print dt
Comment this note:
No messages yet.