Make python tests use timezone, py3.12 deprecated tz-less utcnow()

Python upstream is trying to eliminate tz-naive date functions that
imply anything to do with a timezone, even UTC.  They have deprecated
datetime.datetime.utcnow() in Python 3.12 and thus running tests emits
many warnings for us.

We switch to instantiate datetime objects taht are intended to reflect
UTC to have a timezone, or if we instantiate naive ones and then later
convert, we do the full conversion.

After the changes, the tests still work on Python 3.9, but now also on
Python 3.12 without warnings.

See PR description for #632 for more details.

Signed-off-by: Scott Mcdermott <scott@smemsh.net>
This commit is contained in:
Scott Mcdermott 2024-08-20 19:06:13 -07:00 committed by Thomas Lauf
parent 05b72bba6f
commit 05724a9d21
22 changed files with 220 additions and 207 deletions

View file

@ -29,7 +29,8 @@
import os
import sys
import unittest
from datetime import datetime, timedelta
from datetime import datetime, timezone, timedelta
from dateutil import tz
# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
@ -57,7 +58,7 @@ class TestLengthen(TestCase):
def test_lengthen_synthetic_interval(self):
"""Lengthen a synthetic interval."""
now = datetime.now()
now_utc = now.utcnow()
now_utc = now.replace(tzinfo=tz.tzlocal()).astimezone(timezone.utc)
three_hours_before = now - timedelta(hours=3)
four_hours_before = now - timedelta(hours=4)