75

I'm working with a code that gives me utc timestamps and I want to convert them to appropriate datetimes. Unfortunately when I test simple cases with pytz the datetime has an added 6 hours (the CST offset to UTC). I need to keep timezone data correct because I am calculating difference between other timezones as well. Any ideas why and how to convert a utc timestamp to a utc datetime?

In [1]: import pytz

In [2]: from datetime import datetime

In [3]: import time

In [4]: datetime.fromtimestamp(time.mktime(datetime(7,1,1, tzinfo=pytz.UTC).timetuple()), tz=pytz.UTC)
Out[4]: datetime.datetime(2007, 1, 1, 6, 0, tzinfo=<UTC>)

In [5]: datetime.fromtimestamp(time.mktime(datetime(7,1,1).utctimetuple()), tz=pytz.UTC)
Out[5]: datetime.datetime(2007, 1, 1, 6, 0, tzinfo=<UTC>)

In [6]: datetime.fromtimestamp(time.mktime(datetime(7,1,1).utctimetuple()))
Out[6]: datetime.datetime(2007, 1, 1, 0, 0)
aterrel
  • 1,992
  • 1
  • 14
  • 23
  • related: [How can I get a date converted to seconds since epoch according to UTC?](http://stackoverflow.com/a/8778548/4279) – jfs Feb 22 '14 at 09:00

2 Answers2

162

To get a naive datetime object that represents time in UTC from "seconds since the epoch" timestamp:

from datetime import datetime

utc_dt = datetime.utcfromtimestamp(ts)

If you want to get an aware datetime object for UTC timezone:

import pytz

aware_utc_dt = utc_dt.replace(tzinfo=pytz.utc)

To convert it to some other timezone:

tz = pytz.timezone('America/Montreal')
dt = aware_utc_dt.astimezone(tz)

To convert the timestamp to an aware datetime object in the given timezone directly:

dt = datetime.fromtimestamp(ts, tz)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • what is the variable `ts`? – IssamLaradji Feb 01 '19 at 14:29
  • 1
    @Curious the method has words: "from timestamp" in its name. It suggests that the argument is a timestamp. The text description before the code hints at it too. Writing it as 'utc_dt = datetime.utcfromtimestamp(timestamp)` I thought would be excessive so I've abbreviated `timestamp` to `ts`. – jfs Feb 01 '19 at 20:07
31

Hmm I found the answer here: How to specify time zone (UTC) when converting to Unix time? (Python)

In [101]: ts = calendar.timegm(datetime(2010, 7, 1, tzinfo=pytz.utc).timetuple())

In [102]: datetime.fromtimestamp(ts, tz=pytz.utc)
Out[102]: datetime.datetime(2010, 7, 1, 0, 0, tzinfo=<UTC>)
Community
  • 1
  • 1
aterrel
  • 1,992
  • 1
  • 14
  • 23