Skip to content

Commit

Permalink
Handle timestamp to UTC datetime in 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 5, 2023
1 parent bbf902f commit 33d44af
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/hal/helpers/overhead.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from __future__ import annotations

import asyncio
import sys
import time
from dataclasses import dataclass
from datetime import UTC, datetime
Expand Down Expand Up @@ -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

Check warning on line 86 in src/hal/helpers/overhead.py

View check run for this annotation

Codecov / codecov/patch

src/hal/helpers/overhead.py#L86

Added line #L86 was not covered by tests

if sys.version_info >= (3, 11):
return datetime.fromtimestamp(timestamp, tz=UTC)

return datetime.utcfromtimestamp(timestamp)

Check warning on line 91 in src/hal/helpers/overhead.py

View check run for this annotation

Codecov / codecov/patch

src/hal/helpers/overhead.py#L91

Added line #L91 was not covered by tests

def update_database(self):
"""Updates the database with the overhead."""

Expand All @@ -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,
Expand Down

0 comments on commit 33d44af

Please sign in to comment.