Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix timestamp to iso 8601 time format

When I convert unix time 1463288494 to isoformat i get 2016-05-14T22:01:34. How can I get the output including the -07:00. In this format 2016-05-14T22:01:34-07:00

from datetime import datetime
t =  int("1463288494")
print(datetime.fromtimestamp(t).isoformat())
like image 745
kotlavaibhav Avatar asked Aug 09 '16 22:08

kotlavaibhav


1 Answers

You can pass a tzinfo instance representing your timezone offset to fromtimestamp(). The problem then is how to get the tzinfo object. The easiest way is to use the pytz module which provides a tzinfo compatible object:

import pytz
from datetime import datetime

tz = pytz.timezone('America/Los_Angeles')
print(datetime.fromtimestamp(1463288494, tz).isoformat())

#2016-05-14T22:01:34-07:00
like image 79
mhawke Avatar answered Oct 10 '22 12:10

mhawke