From 9fdcbc38abbbed91efddd044a163f4a8009e29f5 Mon Sep 17 00:00:00 2001 From: Ole Laursen Date: Mon, 17 Sep 2012 15:51:33 +0000 Subject: [PATCH] Add time zone helpers for converting between local IETF db time and UTC - Legacy-Id: 4849 --- ietf/utils/timezone.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ietf/utils/timezone.py diff --git a/ietf/utils/timezone.py b/ietf/utils/timezone.py new file mode 100644 index 000000000..2d0c3db1d --- /dev/null +++ b/ietf/utils/timezone.py @@ -0,0 +1,37 @@ +import pytz +import email.utils +import datetime + +from django.conf import settings + +def local_timezone_to_utc(d): + """Takes a naive datetime in the local timezone and returns a + naive datetime with the corresponding UTC time.""" + local_timezone = pytz.timezone(settings.TIME_ZONE) + + d = local_timezone.localize(d).astimezone(pytz.utc) + + return d.replace(tzinfo=None) + +def utc_to_local_timezone(d): + """Takes a naive datetime UTC and returns a naive datetime in the + local time zone.""" + local_timezone = pytz.timezone(settings.TIME_ZONE) + + d = local_timezone.normalize(d.replace(tzinfo=pytz.utc).astimezone(local_timezone)) + + return d.replace(tzinfo=None) + +def email_time_to_local_timezone(date_string): + """Takes a time string from an email and returns a naive datetime + in the local time zone.""" + + t = email.utils.parsedate_tz(date_string) + d = datetime.datetime(*t[:6]) + + if t[7] != None: + d += datetime.timedelta(seconds=t[9]) + + return utc_to_local_timezone(d) + +