Skip to content

Instantly share code, notes, and snippets.

@lithuak
Created February 28, 2014 11:56
Show Gist options
  • Save lithuak/9269867 to your computer and use it in GitHub Desktop.
Save lithuak/9269867 to your computer and use it in GitHub Desktop.
util functions to translate between aware and naive times with tz change
from datetime import datetime
import pytz
tz_utc = pytz.utc
tz_ua = pytz.timezone('Europe/Kiev')
def aware_time_to_naive(t):
return t.replace(tzinfo=None)
def naive_time_to_aware(t, tz):
return t.replace(tzinfo=tz)
def aware_time_to_another_tz(t, tz):
return t.astimezone(tz)
def now_in_ua_naive():
return aware_time_to_naive(datetime.now(tz_ua))
def ua_naive_to_utc_naive(t):
return aware_time_to_naive(aware_time_to_another_tz(naive_time_to_aware(t, tz_ua), tz_utc))
def utc_naive_to_ua_naive(t):
return aware_time_to_naive(aware_time_to_another_tz(naive_time_to_aware(t, tz_utc), tz_ua))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment