Skip to content

Instantly share code, notes, and snippets.

@quest4i
Created November 16, 2015 16:30
Show Gist options
  • Save quest4i/63b9b68aed3191564a9f to your computer and use it in GitHub Desktop.
Save quest4i/63b9b68aed3191564a9f to your computer and use it in GitHub Desktop.
import time
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
# get local timezone
local_tz = get_localzone()
# test it
# utc_now, now = datetime.utcnow(), datetime.now()
ts = time.time()
utc_now, now = datetime.utcfromtimestamp(ts), datetime.fromtimestamp(ts)
local_now = utc_now.replace(tzinfo=pytz.utc).astimezone(local_tz) # utc -> local
assert local_now.replace(tzinfo=None) == now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment