Skip to content

Commit a4a1a7e

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. The code seems to require a gmtoffset along with the timezone name (so what's the point of the timezone name). Shrug. Supply it one for GMT. If we ever get a certificate whose expiry date is not in GMT, it'll be a problem. Signed-off-by: Dan Mick <dan.mick@redhat.com>
1 parent cf9d52c commit a4a1a7e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tools/checkcerts.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,18 @@ def main():
100100
errstr = f'{domain} cert error: {e}'
101101

102102
if not certerr:
103-
expire = datetime.datetime.strptime(cert['notAfter'],
104-
'%b %d %H:%M:%S %Y %Z')
105-
now = datetime.datetime.utcnow()
103+
# apparently strptime only returns a tz-aware datetime iff it gets both
104+
# a timezone name and a UTC offset. Add an offset. 'Z' is magic for -0000.
105+
#
106+
# yes, this only handles GMT
107+
exp=cert['notAfter'].split()
108+
assert(exp[-1] == 'GMT')
109+
exp.append('Z')
110+
exp = ' '.join(exp)
111+
112+
expire = datetime.datetime.strptime(exp,
113+
'%b %d %H:%M:%S %Y %Z %z')
114+
now = datetime.datetime.now(datetime.UTC)
106115
left = expire - now
107116

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

0 commit comments

Comments
 (0)