Created
February 28, 2014 11:56
-
-
Save lithuak/9269867 to your computer and use it in GitHub Desktop.
util functions to translate between aware and naive times with tz change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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