Skip to content

Commit

Permalink
Added support for parsing dates from Unicode response strings
Browse files Browse the repository at this point in the history
Only effects Python 2.*. Adds to fix for freakboy3742#88 and is based on issue identified in freakboy3742#96.
  • Loading branch information
thisismyrobot committed Aug 23, 2015
1 parent aec8cf1 commit 4f8b322
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 25 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ def test_json_hook(self):
}
)

# And in unicode
example_input = {
'date': u'2015-04-29T10:21:03',
}

self.assertEqual(
xero.utils.json_load_object_hook(example_input),
{
'date': datetime.datetime(2015, 4, 29, 10, 21, 3)
}
)

# Not a string type
self.assertEqual(
xero.utils.json_load_object_hook({'date': 6}),
{'date': 6}
)

self.assertEqual(
xero.utils.json_load_object_hook({'date': None}),
{'date': None}
)


def test_parse_date(self):
""" Tests of the parse_date input formats.
"""
Expand Down Expand Up @@ -78,4 +102,4 @@ def test_parse_date(self):
self.assertEqual(
xero.utils.parse_date('not a date'),
None
)
)
9 changes: 6 additions & 3 deletions xero/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import datetime
import re
import six


DATE = re.compile(
Expand Down Expand Up @@ -82,8 +83,10 @@ def parse_date(string, force_datetime=False):


def json_load_object_hook(dct):
for key,value in dct.items():
if isinstance(value, str):
""" Hook for json.parse(...) to parse Xero date formats.
"""
for key, value in dct.items():
if isinstance(value, six.string_types):
value = parse_date(value)
if value:
dct[key] = value
Expand Down

0 comments on commit 4f8b322

Please sign in to comment.