From 33d44af795bf21b9ffc63f73fcf708ac324f6f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Tue, 5 Dec 2023 08:33:32 -0800 Subject: [PATCH] Handle timestamp to UTC datetime in 3.10 --- src/hal/helpers/overhead.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/hal/helpers/overhead.py b/src/hal/helpers/overhead.py index fa2edca..d5816ab 100644 --- a/src/hal/helpers/overhead.py +++ b/src/hal/helpers/overhead.py @@ -9,6 +9,7 @@ from __future__ import annotations import asyncio +import sys import time from dataclasses import dataclass from datetime import UTC, datetime @@ -78,6 +79,17 @@ async def emit_keywords(self): command.info(stage_duration=[self.macro.name, self.stage, self.elapsed]) + def _get_datetime(self, timestamp: float | None): + """Converts a timestamp to a datetime object.""" + + if timestamp is None: + return None + + if sys.version_info >= (3, 11): + return datetime.fromtimestamp(timestamp, tz=UTC) + + return datetime.utcfromtimestamp(timestamp) + def update_database(self): """Updates the database with the overhead.""" @@ -95,15 +107,8 @@ def update_database(self): configuration = actor.helpers.jaeger.configuration cid = configuration.configuration_id if configuration else None - if self.start_time: - start_time_dt = datetime.fromtimestamp(self.start_time, UTC) - else: - start_time_dt = None - - if self.end_time: - end_time_dt = datetime.fromtimestamp(self.end_time, UTC) - else: - end_time_dt = None + start_time_dt = self._get_datetime(self.start_time) + end_time_dt = self._get_datetime(self.end_time) Overhead.insert( configuration_id=cid,