Skip to content

Commit

Permalink
Fix datetime.utcnow deprecation in Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
sjagoe committed Dec 5, 2023
1 parent 7ecefd7 commit bfcb9cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions haas/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# of the 3-clause BSD license. See the LICENSE.txt file for details.
from __future__ import absolute_import, unicode_literals

from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC
from enum import Enum
from functools import wraps
import locale
Expand All @@ -19,6 +19,13 @@
from .error_holder import ErrorHolder


if sys.version_info >= (3, 12):
def datetime_utcnow():
return datetime.now(UTC)
else:
datetime_utcnow = datetime.utcnow


class TestCompletionStatus(Enum):
"""Enumeration to represent the status of a single test.
Expand Down Expand Up @@ -422,7 +429,7 @@ def startTest(self, test, start_time=None):
"""
if start_time is None:
start_time = datetime.utcnow()
start_time = datetime_utcnow()
self._test_timing[self._testcase_to_key(test)] = start_time
self._mirror_output = False
self._setup_stdout()
Expand Down Expand Up @@ -496,13 +503,13 @@ def _handle_result(self, test, status, exception=None, message=None):

started_time = self._test_timing.get(self._testcase_to_key(test))
if started_time is None and isinstance(test, ErrorHolder):
started_time = datetime.utcnow()
started_time = datetime_utcnow()
elif started_time is None:
raise RuntimeError(
'Missing test start! Please report this error as a bug in '
'haas.')

completion_time = datetime.utcnow()
completion_time = datetime_utcnow()
duration = TestDuration(started_time, completion_time)
result = TestResult.from_test_case(
test,
Expand Down
3 changes: 3 additions & 0 deletions haas/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ def utcnow(self):
return next(self.ret)
except StopIteration:
raise ValueError('No more mock values!')

def now(self, tz):
return self.utcnow()

0 comments on commit bfcb9cf

Please sign in to comment.