Skip to content

Commit

Permalink
Cleanup of utils methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aaront committed Aug 14, 2015
1 parent 3e0c5b3 commit f3c324f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion mygeotab/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def object_serializer(obj):
return mygeotab.utils.date_to_iso_str(obj) if hasattr(obj, 'isoformat') else obj
return mygeotab.utils.datetime_to_iso8601(obj) if hasattr(obj, 'isoformat') else obj


def object_deserializer(obj):
Expand Down
34 changes: 18 additions & 16 deletions mygeotab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
from tzlocal import get_localzone


def get_utc_date(date):
if not date.tzinfo:
date = pytz.utc.localize(date)
return date.astimezone(pytz.utc)
def datetime_to_iso8601(dt):
"""
Formats the given datetime as a UTC-zoned ISO 8601 date string
def date_to_iso_str(date):
date = get_utc_date(date)
return date.replace(tzinfo=None).isoformat() + 'Z'
:param dt: The datetime object
:return: The datetime object in 8601 string form
"""
dt = localize_datetime(dt, pytz.utc)
return dt.replace(tzinfo=None).isoformat() + 'Z'


def parse_date(iso_date):
def parse_iso8601_datetime(iso_date):
"""
Parses an ISO 8601 date string into a datetime object
Expand All @@ -26,15 +27,16 @@ def parse_date(iso_date):
return datetime.strptime(iso_date, "%Y-%m-%dT%H:%M:%S.%fZ")


def localize_date(date):
def localize_datetime(dt, tz=None):
"""
Converts a naive or UTC-localized date into the current machine's timezone
Converts a naive or UTC-localized date into the provided timezone or current machine's timezone
:param date: The datetime object
:return: The datetime object, localized in the machine's timezone
:param dt: The datetime object
:param tz: The timezone. If blank or None, the user's native timezone is used
:return: The localized datetime object
"""
localzone = get_localzone()
if not date.tzinfo:
return localzone.localize(date)
tz = tz or get_localzone()
if not dt.tzinfo:
return tz.localize(dt)
else:
return date.astimezone(localzone)
return dt.astimezone(tz)
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUp(self):

def test_naive_datetime_to_utc(self):
date = datetime(2015, 3, 12, 2, 45, 34)
utc_date = utils.get_utc_date(date)
utc_date = utils.localize_datetime(date, pytz.utc)
self.assertIsNotNone(utc_date.tzinfo)
self.assertIs(utc_date.tzinfo, pytz.utc)
self.assertEqual(utc_date.year, date.year)
Expand All @@ -24,7 +24,7 @@ def test_naive_datetime_to_utc(self):

def test_utc_datetime_to_utc(self):
date = pytz.utc.localize(datetime(2015, 3, 12, 2, 45, 34))
utc_date = utils.get_utc_date(date)
utc_date = utils.localize_datetime(date, pytz.utc)
self.assertIsNotNone(utc_date.tzinfo)
self.assertIs(utc_date.tzinfo, pytz.utc)
self.assertEqual(utc_date.year, date.year)
Expand All @@ -35,7 +35,7 @@ def test_utc_datetime_to_utc(self):
def test_zoned_datetime_to_utc(self):
tz = pytz.timezone('US/Eastern')
date = tz.localize(datetime(2015, 3, 12, 2, 45, 34))
utc_date = utils.get_utc_date(date)
utc_date = utils.localize_datetime(date, pytz.utc)
check_date = date.astimezone(pytz.utc)
self.assertIsNotNone(utc_date.tzinfo)
self.assertIs(utc_date.tzinfo, pytz.utc)
Expand Down

0 comments on commit f3c324f

Please sign in to comment.