From 15a447491a80d0c42e1d71822dfc01bfeed8f0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Thu, 9 Feb 2017 19:39:18 -0500 Subject: [PATCH] Improves ISO8601 parsing --- Pipfile | 3 +-- maya.py | 10 ++++------ setup.py | 5 ++--- test_maya.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Pipfile b/Pipfile index 03c4dc7..e451f41 100644 --- a/Pipfile +++ b/Pipfile @@ -2,10 +2,9 @@ humanize = "*" pytz = "*" dateparser = "*" -iso8601 = "*" -python-dateutil = "*" "ruamel.yaml" = "*" tzlocal = "*" +pendulum = ">=1.0" [dev-packages] pytest = "*" diff --git a/maya.py b/maya.py index 7a0c7df..84212c9 100644 --- a/maya.py +++ b/maya.py @@ -16,8 +16,7 @@ from datetime import datetime as Datetime import pytz import humanize import dateparser -import iso8601 -import dateutil.parser +import pendulum from tzlocal import get_localzone _EPOCH_START = (1970, 1, 1) @@ -131,8 +130,7 @@ class MayaDT(object): @classmethod def from_iso8601(klass, string): """Returns MayaDT instance from iso8601 string.""" - dt = iso8601.parse_date(string) - return klass.from_datetime(dt) + return parse(string) @staticmethod def from_rfc2822(string): @@ -272,11 +270,11 @@ def when(string, timezone='UTC'): def parse(string, day_first=False): """"Returns a MayaDT instance for the machine-produced moment specified. - Powered by dateutil. Accepts most known formats. Useful for working with data. + Powered by pendulum. Accepts most known formats. Useful for working with data. Keyword Arguments: string -- string to be parsed day_first -- if true, the first value (e.g. 01/05/2016) is parsed as day (default: False) """ - dt = dateutil.parser.parse(string, dayfirst=day_first) + dt = pendulum.parse(string, day_first=day_first) return MayaDT.from_datetime(dt) diff --git a/setup.py b/setup.py index 0b1d083..19a834e 100755 --- a/setup.py +++ b/setup.py @@ -28,10 +28,9 @@ required = [ 'humanize', 'pytz', 'dateparser', - 'iso8601', - 'python-dateutil', 'ruamel.yaml', - 'tzlocal' + 'tzlocal', + 'pendulum' ] setup( diff --git a/test_maya.py b/test_maya.py index bdcf470..b881d13 100644 --- a/test_maya.py +++ b/test_maya.py @@ -18,6 +18,50 @@ def test_iso8601(): assert r == d.iso8601() +def test_parse_iso8601(): + string = '20161001T1430.4+05:30' + expected = '2016-10-01T09:00:00.400000Z' + d = maya.MayaDT.from_iso8601(string) + + assert expected == d.iso8601() + + string = '2016T14' + expected = '2016-01-01T14:00:00Z' + d = maya.MayaDT.from_iso8601(string) + + assert expected == d.iso8601() + + string = '2016-10T14' + expected = '2016-10-01T14:00:00Z' + d = maya.MayaDT.from_iso8601(string) + + assert expected == d.iso8601() + + string = '2012W05' + expected = '2012-01-30T00:00:00Z' + d = maya.MayaDT.from_iso8601(string) + + assert expected == d.iso8601() + + string = '2012W055' + expected = '2012-02-03T00:00:00Z' + d = maya.MayaDT.from_iso8601(string) + + assert expected == d.iso8601() + + string = '2012007' + expected = '2012-01-07T00:00:00Z' + d = maya.MayaDT.from_iso8601(string) + + assert expected == d.iso8601() + + string = '2016-W07T09' + expected = '2016-02-15T09:00:00Z' + d = maya.MayaDT.from_iso8601(string) + + assert expected == d.iso8601() + + def test_human_when(): r1 = maya.when('yesterday') r2 = maya.when('today')