diff --git a/src/rhsm/certificate.py b/src/rhsm/certificate.py index c165b8c000..eb376bb2aa 100644 --- a/src/rhsm/certificate.py +++ b/src/rhsm/certificate.py @@ -194,7 +194,7 @@ def valid(self, on_date: datetime.datetime = None) -> bool: :return: True if valid. """ valid_range = self.validRange() - gmt = datetime.datetime.utcnow() + gmt = datetime.datetime.now(datetime.UTC) if on_date: gmt = on_date gmt = gmt.replace(tzinfo=GMT()) @@ -207,7 +207,7 @@ def expired(self, on_date: datetime.datetime = None) -> bool: :return: True if valid. """ valid_range = self.validRange() - gmt = datetime.datetime.utcnow() + gmt = datetime.datetime.now(datetime.UTC) if on_date: gmt = on_date gmt = gmt.replace(tzinfo=GMT()) @@ -612,7 +612,7 @@ def has_now(self) -> bool: :return: True if valid. """ - gmt: datetime.datetime = datetime.datetime.utcnow() + gmt: datetime.datetime = datetime.datetime.now(datetime.UTC) gmt = gmt.replace(tzinfo=GMT()) return self.has_date(gmt) diff --git a/src/rhsm/certificate2.py b/src/rhsm/certificate2.py index 89355245e1..6db805c06e 100644 --- a/src/rhsm/certificate2.py +++ b/src/rhsm/certificate2.py @@ -534,14 +534,14 @@ def __init__( self.issuer: Optional[dict] = issuer def is_valid(self, on_date: Optional[datetime.datetime] = None): - gmt = datetime.datetime.utcnow() + gmt = datetime.datetime.now(datetime.UTC) if on_date: gmt = on_date gmt = gmt.replace(tzinfo=GMT()) return self.valid_range.has_date(gmt) def is_expired(self, on_date: Optional[datetime.datetime] = None): - gmt = datetime.datetime.utcnow() + gmt = datetime.datetime.now(datetime.UTC) if on_date: gmt = on_date gmt = gmt.replace(tzinfo=GMT()) @@ -658,7 +658,7 @@ def provided_paths(self): return paths def is_expiring(self, on_date=None): - gmt = datetime.datetime.utcnow() + gmt = datetime.datetime.now(datetime.UTC) if on_date: gmt = on_date gmt = gmt.replace(tzinfo=GMT()) diff --git a/src/rhsm/connection.py b/src/rhsm/connection.py index 82cf04ea2a..fdc3842723 100644 --- a/src/rhsm/connection.py +++ b/src/rhsm/connection.py @@ -95,7 +95,7 @@ def drift_check(utc_time_string: str, hours: int = 1) -> bool: utc_datetime = dateutil.parser.parse(utc_time_string) # This should not have a timezone, but we know it will be utc. # We need our timezones to match in order to compare - local_datetime = datetime.datetime.utcnow().replace(tzinfo=utc_datetime.tzinfo) + local_datetime = datetime.datetime.now(datetime.UTC) delta = datetime.timedelta(hours=hours) drift = abs((utc_datetime - local_datetime)) > delta except Exception as e: diff --git a/src/rhsmlib/facts/hwprobe.py b/src/rhsmlib/facts/hwprobe.py index b1ffb78551..5d5ecfd27e 100644 --- a/src/rhsmlib/facts/hwprobe.py +++ b/src/rhsmlib/facts/hwprobe.py @@ -23,7 +23,7 @@ import subprocess import sys -from datetime import datetime, timedelta +import datetime from rhsmlib.facts import cpuinfo from rhsmlib.facts import collector @@ -230,9 +230,9 @@ def get_last_boot(self) -> Dict[str, str]: # spacewalk/blob/master/client/rhel/rhn-client-tools/src/bin/rhn_check.py try: uptime = float(open("/proc/uptime", "r").read().split()[0]) - uptime_delta = timedelta(seconds=uptime) - now = datetime.utcnow() - last_boot_date: datetime = now - uptime_delta + uptime_delta = datetime.timedelta(seconds=uptime) + now = datetime.datetime.now(datetime.UTC) + last_boot_date: datetime.datetime = now - uptime_delta last_boot: str = last_boot_date.strftime("%Y-%m-%d %H:%M:%S") + " UTC" except Exception as e: log.warning("Error reading uptime information %s", e) diff --git a/test/stubs.py b/test/stubs.py index 96afad0c4b..e8e64cf71c 100644 --- a/test/stubs.py +++ b/test/stubs.py @@ -13,7 +13,7 @@ # from collections import defaultdict -from datetime import datetime, timedelta +import datetime import io from unittest import mock import random @@ -228,9 +228,9 @@ def __init__(self, product, provided_products=None, start_date=None, end_date=No self.provided_tags = set(provided_tags) if not start_date: - start_date = datetime.now() - timedelta(days=100) + start_date = datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=100) if not end_date: - end_date = datetime.now() + timedelta(days=365) + end_date = datetime.datetime.now(datetime.UTC) + datetime.timedelta(days=365) path = "/path/to/fake_product.pem" @@ -285,9 +285,9 @@ def __init__( products = products + provided_products if not start_date: - start_date = datetime.utcnow() + start_date = datetime.datetime.now(datetime.UTC) if not end_date: - end_date = start_date + timedelta(days=365) + end_date = start_date + datetime.timedelta(days=365) # to simulate a cert with no product sku = None @@ -344,11 +344,11 @@ def delete(self): self.is_deleted = True def is_expiring(self, on_date=None): - gmt = datetime.utcnow() + gmt = datetime.datetime.now(datetime.UTC) if on_date: gmt = on_date gmt = gmt.replace(tzinfo=GMT()) - warning_time = timedelta(days=int(self.order.warning_period)) + warning_time = datetime.timedelta(days=int(self.order.warning_period)) return self.valid_range.end() - warning_time < gmt