Skip to content

Commit 716e85b

Browse files
committed
Parse timezone names with multiple slash-separated components
AI assistance: OpenAI Codex was used for investigation, implementation, regression tests, and automated validation.
1 parent 7840d47 commit 716e85b

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎docs/docs/instantiation.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ It also accepts a `tz` keyword argument to specify the timezone:
119119
'1975-05-21T22:00:00+01:00'
120120
```
121121

122+
Use the `z` token to read a timezone name from the input, including names with multiple slashes:
123+
124+
```python
125+
>>> dt = pendulum.from_format('2024-01-15 America/Argentina/Buenos_Aires', 'YYYY-MM-DD z')
126+
>>> dt.timezone_name
127+
'America/Argentina/Buenos_Aires'
128+
```
129+
122130
The final helper is for working with unix timestamps.
123131
`from_timestamp()` will create a `DateTime` instance equal to the given timestamp
124132
and will set the timezone as well or default it to `UTC`.

‎src/pendulum/formatting/formatter.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"['a-z\u00a0-\u05ff\u0700-\ud7ff\uf900-\ufdcf\ufdf0-\uffef]+"
4343
r"|[\u0600-\u06FF/]+(\s*?[\u0600-\u06FF]+){1,2}"
4444
)
45-
_MATCH_TIMEZONE = "[A-Za-z0-9-+]+(/[A-Za-z0-9-+_]+)?"
45+
_MATCH_TIMEZONE = "[A-Za-z0-9-+]+(/[A-Za-z0-9-+_]+)*"
4646

4747

4848
class Formatter:

‎tests/datetime/test_from_format.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,52 @@ def test_from_format_with_square_bracket_in_timezone():
4545
)
4646

4747

48+
@pytest.mark.parametrize(
49+
"zone",
50+
[
51+
"America/Argentina/Buenos_Aires",
52+
"America/Indiana/Indianapolis",
53+
"America/Kentucky/Louisville",
54+
"America/North_Dakota/New_Salem",
55+
"Europe/Paris",
56+
"UTC",
57+
],
58+
)
59+
@pytest.mark.parametrize("month", [1, 7])
60+
@pytest.mark.parametrize(
61+
"fmt",
62+
[
63+
"YYYY-MM-DD HH:mm:ss.SSSSSS z",
64+
"z YYYY-MM-DD HH:mm:ss.SSSSSS",
65+
"z/YYYY-MM-DD HH:mm:ss.SSSSSS",
66+
],
67+
)
68+
def test_from_format_with_nested_timezone(zone: str, month: int, fmt: str) -> None:
69+
expected = pendulum.datetime(2024, month, 15, 12, 34, 56, 123456, tz=zone)
70+
71+
actual = pendulum.from_format(expected.format(fmt), fmt, tz="Europe/London")
72+
73+
assert actual == expected
74+
assert actual.timezone_name == zone
75+
assert actual.utcoffset() == expected.utcoffset()
76+
77+
78+
@pytest.mark.parametrize(
79+
"zone",
80+
[
81+
"America/Argentina/Not_A_City",
82+
"America/Argentina/Buenos_Aires/Extra",
83+
"America//Argentina/Buenos_Aires",
84+
"America/Argentina/Buenos_Aires/",
85+
"/America/Argentina/Buenos_Aires",
86+
"America/Argentina/Buenos_Aires extra",
87+
],
88+
)
89+
def test_from_format_rejects_invalid_nested_timezone(zone: str) -> None:
90+
with pytest.raises(ValueError):
91+
pendulum.from_format(f"2024-01-15 12:34:56 {zone}", "YYYY-MM-DD HH:mm:ss z")
92+
93+
4894
def test_from_format_with_escaped_elements():
4995
d = pendulum.from_format("1975-05-21T22:32:11+00:00", "YYYY-MM-DD[T]HH:mm:ssZ")
5096
assert_datetime(d, 1975, 5, 21, 22, 32, 11)

0 commit comments

Comments
 (0)