Getting from a Python struct_time object holding time in UTC to datetime object with timezone information isn’t as trivial as it should be.

So a handy self note:

1
2
3
4
5
6
import pytz
import calendar
import datetime
def to_datetime_from_utc(time_tuple):
return datetime.fromtimestamp(calendar.timegm(time_tuple), tz=pytz.utc)

This converts struct_time object to epoch time using calendar.timegm(). timegm() function interprets passed time tuple as UTC time. Then we convert the epoch time to datetime object with added timezone information.

Hope this helps anyone else.