From 267a052f9edd5c9d9dbee3b7b824be8eb32908fd Mon Sep 17 00:00:00 2001 From: khushalx Date: Tue, 1 Dec 2026 23:17:22 +0530 Subject: [PATCH] Preserve explicit UTC when parsing ISO 8601 timestamps Apply the default timezone only when the timestamp has no timezone, including when the default is None. Fixes #2254 Assisted-by: Codex:GPT-6 --- CHANGES.rst | 6 ++++++ libcloud/test/test_utils.py | 23 +++++++++++++++++++++++ libcloud/utils/iso8601.py | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 048d163435..4951b76950 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,12 @@ Changes in Apache Libcloud 4.0.0 Common ~~~~~~ +- Preserve explicit UTC (``Z``) in ISO 8601 timestamps when a non-UTC or + ``None`` fallback timezone is supplied. The fallback now applies only to + timestamps without a timezone. + (GITHUB-2254) + [khushalx - @khushalx] + - Remove obsolete Dimension Data (Compute, Backup, and Load Balancer) and Internet Solutions (Compute) drivers, along with their associated documentation and tests. diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py index 47b5ed59d8..dd792347b5 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 timedelta 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 UTC, FixedOffset, parse_date, parse_timezone from libcloud.utils.publickey import ( get_pubkey_comment, get_pubkey_ssh2_fingerprint, @@ -63,6 +65,27 @@ def show_warning(msg, cat, fname, lno, file=None, line=None): original_func = warnings.showwarning +class Iso8601TimezoneTests(unittest.TestCase): + def test_explicit_utc_ignores_default_timezone(self): + for default in (FixedOffset(5, 30, "+05:30"), FixedOffset(-4, 0, "-04:00"), None): + with self.subTest(default=default): + self.assertIs(parse_timezone("Z", default_timezone=default), UTC) + timestamp = parse_date("2026-12-01T12:34:56Z", default_timezone=default) + self.assertEqual(timestamp.utcoffset(), timedelta(0)) + + def test_missing_timezone_uses_default(self): + for default in (FixedOffset(5, 30, "+05:30"), None): + with self.subTest(default=default): + timestamp = parse_date("2026-12-01T12:34:56", default_timezone=default) + self.assertIs(timestamp.tzinfo, default) + + def test_explicit_offset_ignores_default_timezone(self): + timestamp = parse_date( + "2026-12-01T12:34:56-03:30", default_timezone=FixedOffset(5, 30, "+05:30") + ) + self.assertEqual(timestamp.utcoffset(), -timedelta(hours=3, minutes=30)) + + class TestUtils(unittest.TestCase): def setUp(self): global WARNINGS_BUFFER diff --git a/libcloud/utils/iso8601.py b/libcloud/utils/iso8601.py index aac9ad55b7..3cd83d84ae 100644 --- a/libcloud/utils/iso8601.py +++ b/libcloud/utils/iso8601.py @@ -91,7 +91,7 @@ def __repr__(self): def parse_timezone(tzstring, default_timezone=UTC): """Parses ISO 8601 time zone specs into tzinfo offsets""" if tzstring == "Z": - return default_timezone + return UTC # This isn't strictly correct, but it's common to encounter dates without # timezones so I'll assume the default (which defaults to UTC). # Addresses issue 4.