From 9ef59488b50e1929d9a741b85f814cc5936c9968 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Mon, 5 May 2025 23:34:33 -0700 Subject: [PATCH] Use timezone-aware objects to represent datetimes in UTC. Signed-off-by: Tomoya Fujita --- sros2/sros2/_utilities.py | 2 +- .../test/sros2/commands/security/verbs/test_create_enclave.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sros2/sros2/_utilities.py b/sros2/sros2/_utilities.py index f35442f4..080b447a 100644 --- a/sros2/sros2/_utilities.py +++ b/sros2/sros2/_utilities.py @@ -80,7 +80,7 @@ def build_key_and_cert(subject_name, *, ca=False, ca_key=None, issuer_name=''): else: extension = x509.BasicConstraints(ca=False, path_length=None) - utcnow = datetime.datetime.utcnow() + utcnow = datetime.datetime.now(datetime.UTC) builder = x509.CertificateBuilder( ).issuer_name( issuer_name diff --git a/sros2/test/sros2/commands/security/verbs/test_create_enclave.py b/sros2/test/sros2/commands/security/verbs/test_create_enclave.py index 396d2082..f613c87d 100644 --- a/sros2/test/sros2/commands/security/verbs/test_create_enclave.py +++ b/sros2/test/sros2/commands/security/verbs/test_create_enclave.py @@ -71,6 +71,8 @@ def check_common_name(entity, expected_value): def _datetimes_are_close(actual, expected): # We can't check exact times, but an hour's resolution is fine for testing purposes + actual = actual.replace(tzinfo=datetime.timezone.utc) + expected = expected.replace(tzinfo=datetime.timezone.utc) return actual <= expected and actual >= (expected - datetime.timedelta(hours=1)) @@ -122,7 +124,7 @@ def test_cert_pem(enclave_keys_dir): assert isinstance(cert.signature_hash_algorithm, hashes.SHA256) # Verify the cert is valid for the expected timespan - utcnow = datetime.datetime.utcnow() + utcnow = datetime.datetime.now(datetime.UTC) # Using a day earlier here to prevent Connext (5.3.1) from complaining # when extracting it from the permissions file and thinking it's in the future