Skip to content
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
2 changes: 2 additions & 0 deletions docs/docs/parsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ ParserError: Unable to parse string [31-01-01]
### Time

When passing only time information the date will default to today.
An explicit UTC offset is preserved, just as it is for a complete datetime.

| 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 |
| 12:04:23+05:30 | 2016-12-17T12:04:23+05:30 |

### Intervals

Expand Down
1 change: 1 addition & 0 deletions src/pendulum/parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def _normalize(
parsed.minute,
parsed.second,
parsed.microsecond,
tzinfo=parsed.tzinfo,
)
elif isinstance(parsed, date) and not isinstance(parsed, datetime):
return datetime(parsed.year, parsed.month, parsed.day)
Expand Down
11 changes: 11 additions & 0 deletions tests/parsing/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,17 @@ def test_iso8601_time():
assert parsed.tzinfo is None


@pytest.mark.parametrize("offset", ["+05:30", "-03:45", "+00:00", "Z"])
def test_iso8601_time_preserves_offset(offset):
text = f"20:12:05.123456{offset}"
now = datetime.datetime(2015, 11, 12)
expected = datetime.datetime.fromisoformat(
f"2015-11-12T{text.replace('Z', '+00:00')}"
)

assert parse(text, now=now) == expected


def test_iso8601_ordinal_invalid():
text = "2012-007-05"

Expand Down
19 changes: 19 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

import datetime

import pytest

import pendulum

from tests.conftest import assert_date
Expand Down Expand Up @@ -48,6 +52,21 @@ def test_parse_with_timezone() -> None:
assert dt.offset == 7200


@pytest.mark.parametrize("offset", ["+05:30", "-03:45", "+00:00", "Z"])
@pytest.mark.parametrize("tz", ["UTC", "Europe/Paris"])
def test_parse_time_preserves_offset(offset: str, tz: str) -> None:
text = f"20:12:05.123456{offset}"
expected = datetime.datetime.fromisoformat(
f"2015-11-12T{text.replace('Z', '+00:00')}"
)

parsed = pendulum.parse(text, now=datetime.datetime(2015, 11, 12), tz=tz)

assert isinstance(parsed, pendulum.DateTime)
assert parsed == expected
assert parsed.utcoffset() == expected.utcoffset()


def test_parse_exact() -> None:
text = "2016-10-16T12:34:56.123456+01:30"

Expand Down