Skip to content

Commit

Permalink
Cleaned up and added tests for date utilities. Bumped version to 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
aaront committed Dec 8, 2015
1 parent 9a98bb6 commit 72f451b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Credentials & Authentication

.. autoclass:: AuthenticationException

Utils
Dates
-----

.. automodule:: mygeotab.utils
.. automodule:: mygeotab.dates
:members:
2 changes: 1 addition & 1 deletion mygeotab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__title__ = 'mygeotab'
__author__ = 'Aaron Toth'
__version__ = '0.2'
__version__ = '0.3'
4 changes: 2 additions & 2 deletions mygeotab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import mygeotab
import mygeotab.api
import mygeotab.utils
import mygeotab.dates


class Session(object):
Expand Down Expand Up @@ -202,7 +202,7 @@ def console(session, database=None, user=None, password=None, server=None):
click.echo('Your session has expired. Please login again.')
api = login(session, user, password, database, server)

methods = dict(my=api, mygeotab=mygeotab, utils=mygeotab.utils)
methods = dict(my=api, mygeotab=mygeotab, dates=mygeotab.dates)
version = 'MyGeotab Console {0} [Python {1}]'.format(mygeotab.__version__,
sys.version.replace('\n', ''))
auth_line = ('Logged in as: %s' % session.credentials) if session.credentials else 'Not logged in'
Expand Down
12 changes: 1 addition & 11 deletions mygeotab/utils.py → mygeotab/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tzlocal import get_localzone


def datetime_to_iso8601(dt):
def format_iso_datetime(dt):
"""
Formats the given datetime as a UTC-zoned ISO 8601 date string
Expand All @@ -17,16 +17,6 @@ def datetime_to_iso8601(dt):
return dt.replace(tzinfo=None).isoformat() + 'Z'


def parse_iso8601_datetime(iso_date):
"""
Parses an ISO 8601 date string into a datetime object
:param iso_date: The date string
:return: The datetime object which represents the ISO 8601 string
"""
return datetime.strptime(iso_date, "%Y-%m-%dT%H:%M:%S.%fZ")


def localize_datetime(dt, tz=None):
"""
Converts a naive or UTC-localized date into the provided timezone or current machine's timezone
Expand Down
4 changes: 2 additions & 2 deletions mygeotab/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import six
from dateutil import parser

import mygeotab.utils
import mygeotab.dates

datetime_regex = re.compile(r'^\d{4}\-\d{2}\-\d{2}')


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


def object_deserializer(obj):
Expand Down
31 changes: 27 additions & 4 deletions tests/test_utils.py → tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytz

from mygeotab import utils
from mygeotab import dates


class TestGetUtcDate(unittest.TestCase):
Expand All @@ -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.localize_datetime(date, pytz.utc)
utc_date = dates.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.localize_datetime(date, pytz.utc)
utc_date = dates.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.localize_datetime(date, pytz.utc)
utc_date = dates.localize_datetime(date, pytz.utc)
check_date = date.astimezone(pytz.utc)
self.assertIsNotNone(utc_date.tzinfo)
self.assertIs(utc_date.tzinfo, pytz.utc)
Expand All @@ -45,5 +45,28 @@ def test_zoned_datetime_to_utc(self):
self.assertEqual(utc_date.hour, check_date.hour)


class TestFormatIsoDate(unittest.TestCase):
def setUp(self):
pass

def test_format_naive_datetime(self):
date = datetime(2015, 3, 12, 2, 45, 34)
check_fmt = '2015-03-12T02:45:34Z'
fmt_date = dates.format_iso_datetime(date)
self.assertEqual(fmt_date, check_fmt)

def test_format_utc_datetime(self):
date = pytz.utc.localize(datetime(2015, 3, 12, 2, 45, 34))
check_fmt = '2015-03-12T02:45:34Z'
fmt_date = dates.format_iso_datetime(date)
self.assertEqual(fmt_date, check_fmt)

def test_format_local_datetime(self):
est = pytz.timezone('US/Eastern')
date = est.localize(datetime(2015, 3, 12, 2, 45, 34))
check_fmt = '2015-03-12T06:45:34Z'
fmt_date = dates.format_iso_datetime(date)
self.assertEqual(fmt_date, check_fmt)

if __name__ == '__main__':
unittest.main()

0 comments on commit 72f451b

Please sign in to comment.