Improves ISO8601 parsing

This commit is contained in:
Sébastien Eustace
2017-02-09 19:39:18 -05:00
parent f8d83c0370
commit 15a447491a
4 changed files with 51 additions and 11 deletions
+1 -2
View File
@@ -2,10 +2,9 @@
humanize = "*"
pytz = "*"
dateparser = "*"
iso8601 = "*"
python-dateutil = "*"
"ruamel.yaml" = "*"
tzlocal = "*"
pendulum = ">=1.0"
[dev-packages]
pytest = "*"
+4 -6
View File
@@ -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)
+2 -3
View File
@@ -28,10 +28,9 @@ required = [
'humanize',
'pytz',
'dateparser',
'iso8601',
'python-dateutil',
'ruamel.yaml',
'tzlocal'
'tzlocal',
'pendulum'
]
setup(
+44
View File
@@ -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')