Skip to content

Commit e70f547

Browse files
committed
tools/checkcerts.py: fix deprecated datetime.datetime.utcnow()
The replacement, datetime.datetime.now(datetime.UTC), returns a tz-aware item, so the expiry time must also be timezone-aware. datetime.strptime() does not handle that time format sanely. I don't know why. Luckily zoneinfo exists and can parse likely timezones into valid datetime.tzinfo instances, as far as I can tell. Why isn't this part of datetime? I don't know. But this code seems be somewhat reliable (assuming the timezone name is the last item in the date string, delimited by spaces). Signed-off-by: Dan Mick <dan.mick@redhat.com>
1 parent cf9d52c commit e70f547

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

tools/checkcerts.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tempfile
1010
import datetime
1111
import smtplib
12+
import zoneinfo
1213

1314
DAYS_BEFORE_WARN=7
1415

@@ -100,9 +101,15 @@ def main():
100101
errstr = f'{domain} cert error: {e}'
101102

102103
if not certerr:
103-
expire = datetime.datetime.strptime(cert['notAfter'],
104-
'%b %d %H:%M:%S %Y %Z')
105-
now = datetime.datetime.utcnow()
104+
exp=cert['notAfter'].split()
105+
tzname=exp[-1]
106+
exp = ' '.join(exp[:-1])
107+
108+
expire = datetime.datetime.strptime(exp,
109+
'%b %d %H:%M:%S %Y')
110+
tzinfo=zoneinfo.ZoneInfo(tzname)
111+
expire = expire.replace(tzinfo=tzinfo)
112+
now = datetime.datetime.now(datetime.UTC)
106113
left = expire - now
107114

108115
errstr = f'{domain:30s} cert: {str(left).rsplit(".",1)[0]} left until it expires'

0 commit comments

Comments
 (0)