Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISO8601 Interval Separator #611

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions docs/docs/parsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions pendulum/parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
90 changes: 90 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand Down