Skip to content

Instantly share code, notes, and snippets.

@lithuak
Created February 28, 2014 11:28
Show Gist options
  • Save lithuak/9269497 to your computer and use it in GitHub Desktop.
Save lithuak/9269497 to your computer and use it in GitHub Desktop.
Saving/restoring local aware time as naive utc to/from DB
# timezones
tz_utc = pytz.utc
tz_ua = pytz.timezone('Europe/Kiev')
# get aware ukraine time
time_ua = datetime.now(tz_ua)
print "Aware ukraine time:", time_ua
# transform to utc
time_utc = time_ua.astimezone(tz_utc)
print "Aware utc time:", time_utc
# make naive
time_utc_naive = time_utc.replace(tzinfo=None)
print "Naive utc time:", time_utc_naive
# ...save to db... ...restore from db...
# restore aware utc from naive utc
time_utc_restored = time_utc_naive.replace(tzinfo=tz_utc)
print "Aware utc time:", time_utc_restored
# restore aware ukraine time from utc
time_ua_restored = time_utc_restored.astimezone(tz_ua)
print "Aware ukraine time:", time_ua_restored
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment