From b9c7dd05d7b8154526e02c29433edc7472d92c3e Mon Sep 17 00:00:00 2001 From: aad1tyaaaaa Date: Fri, 27 Nov 2026 20:02:04 +0530 Subject: [PATCH 1/2] Fix parse_date losing a microsecond on some fractional seconds --- CHANGES.rst | 5 +++++ libcloud/test/compute/test_scaleway.py | 2 +- libcloud/test/test_utils.py | 18 ++++++++++++++++++ libcloud/utils/iso8601.py | 4 +++- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 048d163435..9a3aa5fb39 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -43,6 +43,11 @@ Common (#2185) [Ashish - @Ashishjob] +- [Utils] Fix ``parse_date`` returning a timestamp one microsecond short for + some fractional seconds (e.g. ``.000249`` was parsed as 248 microseconds), + because the fraction went through a float. + [@aad1tyaaaaa] + - [Utils] Fix ``get_pubkey_openssh_fingerprint`` returning a wrong fingerprint for Ed25519 keys and raising ``binascii.Error`` for ECDSA keys. The function assumed a 7 character ``ssh-rsa`` key type prefix. This also affects the diff --git a/libcloud/test/compute/test_scaleway.py b/libcloud/test/compute/test_scaleway.py index eb3c1b563e..a63e7821f1 100644 --- a/libcloud/test/compute/test_scaleway.py +++ b/libcloud/test/compute/test_scaleway.py @@ -109,7 +109,7 @@ def test_list_nodes_success(self): def test_list_nodes_fills_created_datetime(self): nodes = self.driver.list_nodes() - self.assertEqual(nodes[0].created_at, datetime(2014, 5, 22, 12, 57, 22, 514298, tzinfo=UTC)) + self.assertEqual(nodes[0].created_at, datetime(2014, 5, 22, 12, 57, 22, 514299, tzinfo=UTC)) def test_create_node_success(self): image = self.driver.list_images()[0] diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py index 47b5ed59d8..83298a6731 100644 --- a/libcloud/test/test_utils.py +++ b/libcloud/test/test_utils.py @@ -30,6 +30,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, @@ -487,6 +488,23 @@ def test_increment_ipv4_segments(self): self.assertEqual(result, incremented_ip) +@pytest.mark.parametrize( + "fraction, microsecond", + [ + ("5", 500000), + ("123456", 123456), + ("1234567", 123456), + # These used to come out one microsecond short through float rounding + ("000249", 249), + ("000251", 251), + ("000493", 493), + ], +) +def test_parse_date_fraction(fraction, microsecond): + parsed = parse_date("2020-01-01T00:00:00.%sZ" % fraction) + assert parsed.microsecond == microsecond + + class TestPublicKeyUtils(unittest.TestCase): PUBKEY = ( "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDOfbWSXOlqvYjZmRO84/lIoV4gvuX+" diff --git a/libcloud/utils/iso8601.py b/libcloud/utils/iso8601.py index aac9ad55b7..ba9fc28c13 100644 --- a/libcloud/utils/iso8601.py +++ b/libcloud/utils/iso8601.py @@ -128,7 +128,9 @@ def parse_date(datestring, default_timezone=UTC, allow_empty=False): if groups["fraction"] is None: groups["fraction"] = 0 else: - groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6) + # Take the microseconds from the digits: going through a float, e.g. + # int(0.000249 * 1e6), can come out one microsecond short + groups["fraction"] = int(groups["fraction"][:6].ljust(6, "0")) return datetime( int(groups["year"]), int(groups["month"]), From b163fd271db2f335f392da1fcde917979eee97f9 Mon Sep 17 00:00:00 2001 From: aad1tyaaaaa Date: Fri, 27 Nov 2026 20:02:24 +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 9a3aa5fb39..3f719dfe4e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -46,6 +46,7 @@ Common - [Utils] Fix ``parse_date`` returning a timestamp one microsecond short for some fractional seconds (e.g. ``.000249`` was parsed as 248 microseconds), because the fraction went through a float. + (#2243) [@aad1tyaaaaa] - [Utils] Fix ``get_pubkey_openssh_fingerprint`` returning a wrong fingerprint