Skip to content

Commit

Permalink
Address upcoming deprecation of datetime.datetime.utcnow()
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya committed Jan 18, 2024
1 parent 00dcb66 commit e2820d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 2 additions & 3 deletions ecowitt2mqtt/helpers/calculator/time.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Define time calculators."""
from __future__ import annotations

from datetime import datetime, timezone

from ecowitt2mqtt.const import TIME_SECONDS
from ecowitt2mqtt.helpers.calculator import (
CalculatedDataPoint,
Expand All @@ -11,6 +9,7 @@
SimpleCalculator,
)
from ecowitt2mqtt.helpers.typing import PreCalculatedValueType
from ecowitt2mqtt.util.dt import utc_from_timestamp


class EpochCalculator(Calculator):
Expand All @@ -33,7 +32,7 @@ def calculate_from_value(
if isinstance(value, str):
raise CalculationFailedError("Cannot parse value as datetime")

timestamp = datetime.utcfromtimestamp(value).replace(tzinfo=timezone.utc)
timestamp = utc_from_timestamp(value)
return self.get_calculated_data_point(timestamp)


Expand Down
22 changes: 22 additions & 0 deletions ecowitt2mqtt/util/dt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Define datetime utilities."""
from datetime import datetime

try:
from datetime import UTC
except ImportError:
# In place for support of Python 3.10
from datetime import timezone

UTC = timezone.utc


def utc_from_timestamp(timestamp: float) -> datetime:
"""Return a UTC time from a timestamp.
Args:
timestamp: The epoch to convert.
Returns:
A parsed ``datetime.datetime`` object.
"""
return datetime.fromtimestamp(timestamp, tz=UTC)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api"
target-version = ["py39"]

[tool.coverage.report]
exclude_lines = ["TYPE_CHECKING", "NotImplementedError", "handle_exit_signal"]
exclude_lines = ["TYPE_CHECKING", "ImportError", "NotImplementedError", "handle_exit_signal"]
fail_under = 100
show_missing = true

Expand All @@ -31,7 +31,7 @@ ignore_missing_imports = true
no_implicit_optional = true
platform = "linux"
plugins = ["pydantic.mypy"]
python_version = "3.10"
python_version = "3.12"
show_error_codes = true
strict_equality = true
warn_incomplete_stub = true
Expand Down

0 comments on commit e2820d0

Please sign in to comment.