Skip to content

Commit 2bebbf1

Browse files
committed
Fix ISO 8601 week dates with years < 1000
_get_iso_8601_week built the intermediate string as f"{year}-{ordinal}" without zero-padding, but strptime's %Y matches exactly four digits, so every week date in a year below 1000 (0999-W34-6, 0416W346, 0004-W10-5, ...) failed with a generic ParserError while datetime.fromisoformat accepts them and the same years parse fine in calendar form. Pad the year to 4 digits. Fixes #1076 Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
1 parent e06dace commit 2bebbf1

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎src/pendulum/parsing/iso8601.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,9 @@ def _get_iso_8601_week(
447447
year += 1
448448

449449
fmt = "%Y-%j"
450-
string = f"{year}-{ordinal}"
450+
# %Y requires a zero-padded 4-digit year in strptime, so years < 1000
451+
# must be padded or the lookup fails.
452+
string = f"{year:04d}-{ordinal}"
451453

452454
dt = datetime.datetime.strptime(string, fmt)
453455

‎tests/parsing/test_parse_iso8601.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
("2008-W39-6", date(2008, 9, 27)),
3030
("2009-W53-7", date(2010, 1, 3)),
3131
("2009-W01-1", date(2008, 12, 29)),
32+
# Week date with a year < 1000 (%Y in strptime needs the padding)
33+
("0999-W34-6", date(999, 8, 24)),
34+
("0416-W34-6", date(416, 8, 27)),
35+
("0004-W10-5", date(4, 3, 5)),
36+
("0416W346", date(416, 8, 27)),
37+
("0416-W34", date(416, 8, 22)),
3238
# Time
3339
("12:34", time(12, 34, 0)),
3440
("12:34:56", time(12, 34, 56)),

0 commit comments

Comments
 (0)