From: Martin Basti Date: Tue, 14 Jul 2026 09:09:10 +0000 (+0200) Subject: Replace python deprecated datetime utc functions X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=e8cc1747a99bc450d5d7b2aab18e4545567a1961;p=thirdparty%2Fbind9.git Replace python deprecated datetime utc functions 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 --- diff --git a/bin/tests/system/statschannel/generic.py b/bin/tests/system/statschannel/generic.py index 874baf97d1a..db3ae30c692 100644 --- a/bin/tests/system/statschannel/generic.py +++ b/bin/tests/system/statschannel/generic.py @@ -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 diff --git a/contrib/gitchangelog/gitchangelog.py b/contrib/gitchangelog/gitchangelog.py index 262a41c5521..e5e9d9832b4 100755 --- a/contrib/gitchangelog/gitchangelog.py +++ b/contrib/gitchangelog/gitchangelog.py @@ -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