Skip to content

Commit

Permalink
Remove deprecated datetime.datetime.utcnow()
Browse files Browse the repository at this point in the history
This function is deprecated since Python 3.12.
`datetime.datetime.now(datetime.UTC)` is a timezone-aware equivalent.
  • Loading branch information
m-horky committed Jan 11, 2024
1 parent 47d2729 commit 4fd7332
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/rhsm/certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions src/rhsm/certificate2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/rhsm/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/rhsmlib/facts/hwprobe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions test/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#

from collections import defaultdict
from datetime import datetime, timedelta
import datetime
import io
from unittest import mock
import random
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 4fd7332

Please sign in to comment.