Skip to content

Instantly share code, notes, and snippets.

@afonasev
Created November 24, 2015 14:10
Show Gist options
  • Save afonasev/cba5340a60b17e470df9 to your computer and use it in GitHub Desktop.
Save afonasev/cba5340a60b17e470df9 to your computer and use it in GitHub Desktop.
def convert_timestamp_to_local_tz(dt):
if dt.tzinfo is None:
return dt
# convert to utc
utc_dt = dt - dt.utcoffset()
utc_dt = utc_dt.replace(tzinfo=None)
offset = (
dt.datetime.now().replace(minute=0, second=0, microsecond=0) -
dt.datetime.utcnow().replace(minute=0, second=0, microsecond=0)
)
# convert to local
local_dt = utc_dt + offset
return local_dt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment