diff --git a/docs/docs/parsing.md b/docs/docs/parsing.md index dd78fd45..719dbce6 100644 --- a/docs/docs/parsing.md +++ b/docs/docs/parsing.md @@ -37,66 +37,74 @@ ParserError: Unable to parse string [31-01-01] '2031-01-01T00:00:00+00:00' ``` - ## RFC 3339 -| String | Output | -| --------------------------------- | ------------------------------------------| -| 1996-12-19T16:39:57-08:00 | 1996-12-19T16:39:57-08:00 | -| 1990-12-31T23:59:59Z | 1990-12-31T23:59:59+00:00 | +| String | Output | +| ------------------------- | ------------------------- | +| 1996-12-19T16:39:57-08:00 | 1996-12-19T16:39:57-08:00 | +| 1990-12-31T23:59:59Z | 1990-12-31T23:59:59+00:00 | ## ISO 8601 ### Datetime -| String | Output | -| --------------------------------- | ----------------------------------------- | -| 20161001T143028+0530 | 2016-10-01T14:30:28+05:30 | -| 20161001T14 | 2016-10-01T14:00:00+00:00 | +| String | Output | +| -------------------- | ------------------------- | +| 20161001T143028+0530 | 2016-10-01T14:30:28+05:30 | +| 20161001T14 | 2016-10-01T14:00:00+00:00 | ### Date -| String | Output | -| --------------------------------- | ----------------------------------------- | -| 2012 | 2012-01-01T00:00:00+00:00 | -| 2012-05-03 | 2012-05-03T00:00:00+00:00 | -| 20120503 | 2012-05-03T00:00:00+00:00 | -| 2012-05 | 2012-05-01T00:00:00+00:00 | +| String | Output | +| ---------- | ------------------------- | +| 2012 | 2012-01-01T00:00:00+00:00 | +| 2012-05-03 | 2012-05-03T00:00:00+00:00 | +| 20120503 | 2012-05-03T00:00:00+00:00 | +| 2012-05 | 2012-05-01T00:00:00+00:00 | ### Ordinal day -| String | Output | -| ---------------------------------- | ----------------------------------------- | -| 2012-007 | 2012-01-07T00:00:00+00:00 | -| 2012007 | 2012-01-07T00:00:00+00:00 | +| String | Output | +| -------- | ------------------------- | +| 2012-007 | 2012-01-07T00:00:00+00:00 | +| 2012007 | 2012-01-07T00:00:00+00:00 | ### Week number -| String | Output | -| --------------------------------- | ----------------------------------------- | -| 2012-W05 | 2012-01-30T00:00:00+00:00 | -| 2012W05 | 2012-01-30T00:00:00+00:00 | -| 2012-W05-5 | 2012-02-03T00:00:00+00:00 | -| 2012W055 | 2012-02-03T00:00:00+00:00 | +| String | Output | +| ---------- | ------------------------- | +| 2012-W05 | 2012-01-30T00:00:00+00:00 | +| 2012W05 | 2012-01-30T00:00:00+00:00 | +| 2012-W05-5 | 2012-02-03T00:00:00+00:00 | +| 2012W055 | 2012-02-03T00:00:00+00:00 | ### Time When passing only time information the date will default to today. -| String | Output | -| --------------------------------- | ------------------------------------------ | -| 00:00 | 2016-12-17T00:00:00+00:00 | -| 12:04:23 | 2016-12-17T12:04:23+00:00 | -| 120423 | 2016-12-17T12:04:23+00:00 | -| 12:04:23.45 | 2016-12-17T12:04:23.450000+00:00 | +| String | Output | +| ----------- | -------------------------------- | +| 00:00 | 2016-12-17T00:00:00+00:00 | +| 12:04:23 | 2016-12-17T12:04:23+00:00 | +| 120423 | 2016-12-17T12:04:23+00:00 | +| 12:04:23.45 | 2016-12-17T12:04:23.450000+00:00 | ### Intervals -| String | Output | -| ----------------------------------------- | ------------------------------------------------------ | -| 2007-03-01T13:00:00Z/2008-05-11T15:30:00Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | -| 2008-05-11T15:30:00Z/P1Y2M10DT2H30M | 2008-05-11T15:30:00+00:00 -> 2009-07-21T18:00:00+00:00 | -| P1Y2M10DT2H30M/2008-05-11T15:30:00Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| String | Output | +| ------------------------------------------ | ------------------------------------------------------ | +| 2007-03-01T13:00:00Z/2008-05-11T15:30:00Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| 2008-05-11T15:30:00Z/P1Y2M10DT2H30M | 2008-05-11T15:30:00+00:00 -> 2009-07-21T18:00:00+00:00 | +| P1Y2M10DT2H30M/2008-05-11T15:30:00Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| 20070301T130000Z/20080511T153000Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| 20080511T153000Z/P1Y2M10DT2H30M | 2008-05-11T15:30:00+00:00 -> 2009-07-21T18:00:00+00:00 | +| P1Y2M10DT2H30M/20080511T153000Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| 2007-03-01T13:00:00Z--2008-05-11T15:30:00Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| 2008-05-11T15:30:00Z--P1Y2M10DT2H30M | 2008-05-11T15:30:00+00:00 -> 2009-07-21T18:00:00+00:00 | +| P1Y2M10DT2H30M--2008-05-11T15:30:00Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| 20070301T130000Z--20080511T153000Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | +| 20080511T153000Z--P1Y2M10DT2H30M | 2008-05-11T15:30:00+00:00 -> 2009-07-21T18:00:00+00:00 | +| P1Y2M10DT2H30M--20080511T153000Z | 2007-03-01T13:00:00+00:00 -> 2008-05-11T15:30:00+00:00 | !!!note diff --git a/pendulum/parsing/__init__.py b/pendulum/parsing/__init__.py index 1d15023b..26c76a38 100644 --- a/pendulum/parsing/__init__.py +++ b/pendulum/parsing/__init__.py @@ -212,10 +212,11 @@ def __init__(self, start=None, end=None, duration=None): def _parse_iso8601_interval(text): - if "/" not in text: + if "/" not in text and "--" not in text: raise ParserError("Invalid interval") - first, last = text.split("/") + # first, last = text.split("/") + first, last = re.split("/|--", text) start = end = duration = None if first[0] == "P": diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 3dcf050b..5f685145 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -101,6 +101,16 @@ def test_parse_interval(): assert_datetime(period.end, 2009, 7, 21, 18, 0, 0, 0) assert period.end.offset == 0 + text = "20080511T153000Z/P1Y2M10DT2H30M" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2008, 5, 11, 15, 30, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2009, 7, 21, 18, 0, 0, 0) + assert period.end.offset == 0 + text = "P1Y2M10DT2H30M/2008-05-11T15:30:00Z" period = pendulum.parse(text) @@ -111,6 +121,16 @@ def test_parse_interval(): assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) assert period.end.offset == 0 + text = "P1Y2M10DT2H30M/20080511T153000Z" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2007, 3, 1, 13, 0, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) + assert period.end.offset == 0 + text = "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z" period = pendulum.parse(text) @@ -121,6 +141,76 @@ def test_parse_interval(): assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) assert period.end.offset == 0 + text = "20070301T130000Z/20080511T153000Z" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2007, 3, 1, 13, 0, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) + assert period.end.offset == 0 + + text = "2008-05-11T15:30:00Z--P1Y2M10DT2H30M" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2008, 5, 11, 15, 30, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2009, 7, 21, 18, 0, 0, 0) + assert period.end.offset == 0 + + text = "20080511T153000Z--P1Y2M10DT2H30M" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2008, 5, 11, 15, 30, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2009, 7, 21, 18, 0, 0, 0) + assert period.end.offset == 0 + + text = "P1Y2M10DT2H30M--2008-05-11T15:30:00Z" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2007, 3, 1, 13, 0, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) + assert period.end.offset == 0 + + text = "P1Y2M10DT2H30M--20080511T153000Z" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2007, 3, 1, 13, 0, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) + assert period.end.offset == 0 + + text = "2007-03-01T13:00:00Z--2008-05-11T15:30:00Z" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2007, 3, 1, 13, 0, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) + assert period.end.offset == 0 + + text = "20070301T130000Z--20080511T153000Z" + + period = pendulum.parse(text) + + assert isinstance(period, pendulum.Period) + assert_datetime(period.start, 2007, 3, 1, 13, 0, 0, 0) + assert period.start.offset == 0 + assert_datetime(period.end, 2008, 5, 11, 15, 30, 0, 0) + assert period.end.offset == 0 + def test_parse_now(): dt = pendulum.parse("now")