From 2bebbf18290ed3030ec9d99d7f424462a54e1d03 Mon Sep 17 00:00:00 2001 From: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:35:04 +0530 Subject: [PATCH] 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> --- src/pendulum/parsing/iso8601.py | 4 +++- tests/parsing/test_parse_iso8601.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pendulum/parsing/iso8601.py b/src/pendulum/parsing/iso8601.py index c65d249e5..b57bc63a8 100644 --- a/src/pendulum/parsing/iso8601.py +++ b/src/pendulum/parsing/iso8601.py @@ -447,7 +447,9 @@ def _get_iso_8601_week( year += 1 fmt = "%Y-%j" - string = f"{year}-{ordinal}" + # %Y requires a zero-padded 4-digit year in strptime, so years < 1000 + # must be padded or the lookup fails. + string = f"{year:04d}-{ordinal}" dt = datetime.datetime.strptime(string, fmt) diff --git a/tests/parsing/test_parse_iso8601.py b/tests/parsing/test_parse_iso8601.py index ed2d39887..636fc875b 100644 --- a/tests/parsing/test_parse_iso8601.py +++ b/tests/parsing/test_parse_iso8601.py @@ -29,6 +29,12 @@ ("2008-W39-6", date(2008, 9, 27)), ("2009-W53-7", date(2010, 1, 3)), ("2009-W01-1", date(2008, 12, 29)), + # Week date with a year < 1000 (%Y in strptime needs the padding) + ("0999-W34-6", date(999, 8, 24)), + ("0416-W34-6", date(416, 8, 27)), + ("0004-W10-5", date(4, 3, 5)), + ("0416W346", date(416, 8, 27)), + ("0416-W34", date(416, 8, 22)), # Time ("12:34", time(12, 34, 0)), ("12:34:56", time(12, 34, 56)),