]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Replace python deprecated datetime utc functions 12399/head
authorMartin Basti <mbasti@isc.org>
Tue, 14 Jul 2026 09:09:10 +0000 (11:09 +0200)
committerMartin Basti <mbasti@isc.org>
Tue, 14 Jul 2026 12:33:49 +0000 (14:33 +0200)
Functions `utcnow` and `utcfromtimestamp` are deprecated in python and
print warnings into tests logs about it.

Use the python prefered way by defining `timezone.utc` in `now` and
`fromtimestamp` functions. Which are equivalent but safer than naive
objects without timezone.

To ensure comptibility `%z` was added to format string to properly
process `Z` as UTC timezone.

Assisted-by: Claude:claude-opus-4-8
bin/tests/system/statschannel/generic.py
contrib/gitchangelog/gitchangelog.py

index 874baf97d1aeef442afb31bbf134897100b09bb8..db3ae30c6928af32da58c2193ad27c846cd750c4 100644 (file)
@@ -10,7 +10,7 @@
 # information regarding copyright ownership.
 
 from collections import defaultdict
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, timezone
 from time import sleep
 
 import os
@@ -19,13 +19,14 @@ import dns.message
 
 import isctest
 
-# ISO datetime format without msec
-FMT = "%Y-%m-%dT%H:%M:%SZ"
+# ISO datetime format without msec (the %z directive parses the trailing "Z"
+# as UTC, yielding timezone-aware datetimes)
+FMT = "%Y-%m-%dT%H:%M:%S%z"
 
 # The constants were taken from BIND 9 source code (lib/dns/zone.c)
 max_refresh = timedelta(seconds=2419200)  # 4 weeks
 max_expires = timedelta(seconds=14515200)  # 24 weeks
-dayzero = datetime.utcfromtimestamp(0).replace(microsecond=0)
+dayzero = datetime.fromtimestamp(0, timezone.utc).replace(microsecond=0)
 
 # Wait for the secondary zone files to appear to extract their mtime
 MAX_SECONDARY_ZONE_WAITTIME_SEC = 5
@@ -49,7 +50,7 @@ def check_loaded(loaded, expected, now):
 
 
 def check_zone_timers(loaded, expires, refresh, loaded_exp):
-    now = datetime.utcnow().replace(microsecond=0)
+    now = datetime.now(timezone.utc).replace(microsecond=0)
     # Sanity checks the zone timers values
     if expires is not None:
         check_expires(expires, now, now + max_expires)
@@ -77,7 +78,7 @@ def zone_mtime(zonedir, name):
     except FileNotFoundError:
         return dayzero
 
-    mtime = datetime.utcfromtimestamp(si.st_mtime).replace(microsecond=0)
+    mtime = datetime.fromtimestamp(si.st_mtime, timezone.utc).replace(microsecond=0)
 
     return mtime
 
index 262a41c55219cd52781b6ab0e3e5ab4b689e087e..e5e9d9832b4f94bc528f82624e6d2937756ddcd4 100755 (executable)
@@ -1079,7 +1079,9 @@ class GitCommit(SubGitObjectMixin):
 
     @property
     def date(self):
-        d = datetime.datetime.utcfromtimestamp(float(self.author_date_timestamp))
+        d = datetime.datetime.fromtimestamp(
+            float(self.author_date_timestamp), datetime.timezone.utc
+        )
         return d.strftime("%Y-%m-%d")
 
     @property