In Python, how do you convert seconds since epoch to a `datetime` object?
datetime.datetime.fromtimestamp
will do, if you know the time zone, you could produce the same output as with time.gmtime
>>> datetime.datetime.fromtimestamp(1284286794)datetime.datetime(2010, 9, 12, 11, 19, 54)
or
>>> datetime.datetime.utcfromtimestamp(1284286794)datetime.datetime(2010, 9, 12, 10, 19, 54)
From the docs, the recommended way of getting a timezone aware datetime object from seconds since epoch is:
from datetime import datetime, timezonedatetime.fromtimestamp(timestamp, timezone.utc)
from datetime import datetimeimport pytzdatetime.fromtimestamp(timestamp, pytz.utc)