From ec6b0f8039cfd03d89cec740b4584c64d88f6009 Mon Sep 17 00:00:00 2001 From: aad1tyaaaaa Date: Fri, 27 Nov 2026 21:40:53 +0530 Subject: [PATCH 1/2] Fix parse_date raising TypeError for dates without time or seconds The regex makes everything after the year optional, but the missing groups were passed to int() as None. Default them like upstream pyiso8601 does. --- CHANGES.rst | 6 ++++++ libcloud/test/test_utils.py | 14 ++++++++++++++ libcloud/utils/iso8601.py | 12 +++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 048d163435..d1977c89f0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -50,6 +50,12 @@ Common (#2229) [0z5a - @0z5a] +- [Utils] Fix ``parse_date`` raising ``TypeError`` for valid ISO 8601 strings + without a time or without seconds, such as ``2020-01-01`` or + ``2020-01-01T10:20Z``. Missing time fields now default to zero (and a + missing month or day to 1), as in upstream pyiso8601. + [@aad1tyaaaaa] + Compute ~~~~~~~ diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py index 47b5ed59d8..3e3a65ed22 100644 --- a/libcloud/test/test_utils.py +++ b/libcloud/test/test_utils.py @@ -21,6 +21,7 @@ import platform import unittest import warnings +from datetime import datetime, timezone from itertools import chain import pytest @@ -30,6 +31,7 @@ from libcloud.utils.misc import get_driver, set_driver, get_secure_random_string from libcloud.common.types import LibcloudError from libcloud.compute.types import Provider +from libcloud.utils.iso8601 import parse_date from libcloud.utils.publickey import ( get_pubkey_comment, get_pubkey_ssh2_fingerprint, @@ -538,5 +540,17 @@ def foo(): foo() +@pytest.mark.parametrize( + "datestring, expected", + [ + ("2020-01-01", datetime(2020, 1, 1, tzinfo=timezone.utc)), + ("2020-01-01T10:20Z", datetime(2020, 1, 1, 10, 20, tzinfo=timezone.utc)), + ("2020-01-01T10:20+02:00", datetime(2020, 1, 1, 8, 20, tzinfo=timezone.utc)), + ], +) +def test_parse_date_without_time_or_seconds(datestring, expected): + assert parse_date(datestring) == expected + + if __name__ == "__main__": sys.exit(unittest.main()) diff --git a/libcloud/utils/iso8601.py b/libcloud/utils/iso8601.py index aac9ad55b7..f80473dd1e 100644 --- a/libcloud/utils/iso8601.py +++ b/libcloud/utils/iso8601.py @@ -131,11 +131,13 @@ def parse_date(datestring, default_timezone=UTC, allow_empty=False): groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6) return datetime( int(groups["year"]), - int(groups["month"]), - int(groups["day"]), - int(groups["hour"]), - int(groups["minute"]), - int(groups["second"]), + # Everything after the year is optional in the regex, so default the + # missing parts like upstream pyiso8601 does (e.g. "2020-01-01") + int(groups["month"] or 1), + int(groups["day"] or 1), + int(groups["hour"] or 0), + int(groups["minute"] or 0), + int(groups["second"] or 0), int(groups["fraction"]), tz, ) From 33d35ac5611b29589b06fbf2d6f73f07e8f4c663 Mon Sep 17 00:00:00 2001 From: aad1tyaaaaa Date: Fri, 27 Nov 2026 22:01:35 +0530 Subject: [PATCH 2/2] Add the PR number to the changelog entry --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index d1977c89f0..612614beb6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -54,6 +54,7 @@ Common without a time or without seconds, such as ``2020-01-01`` or ``2020-01-01T10:20Z``. Missing time fields now default to zero (and a missing month or day to 1), as in upstream pyiso8601. + (#2245) [@aad1tyaaaaa] Compute