Skip to content

Commit

Permalink
Merge pull request #103 from Snuffy2/Fix-timestamp
Browse files Browse the repository at this point in the history
Make datetime tz aware
  • Loading branch information
enkama committed Nov 21, 2023
2 parents 30766f7 + 141a9af commit b7ae428
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions custom_components/variable/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import datetime
import logging

import homeassistant.util.dt as dt_util

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -61,6 +63,11 @@ def value_to_type(init_val, dest_type): # noqa: C901
_LOGGER.debug(
f"[value_to_type] return value: {value_datetime}, type: {type(value_datetime)}"
)
if (
value_datetime.tzinfo is None
or value_datetime.tzinfo.utcoffset(value_datetime) is None
):
return value_datetime.replace(tzinfo=dt_util.UTC)
return value_datetime

elif dest_type == "number":
Expand Down Expand Up @@ -113,6 +120,11 @@ def value_to_type(init_val, dest_type): # noqa: C901
_LOGGER.debug(
f"[value_to_type] return value: {value_datetime}, type: {type(value_datetime)}"
)
if (
value_datetime.tzinfo is None
or value_datetime.tzinfo.utcoffset(value_datetime) is None
):
return value_datetime.replace(tzinfo=dt_util.UTC)
return value_datetime
elif dest_type == "number":
_LOGGER.debug(
Expand Down

0 comments on commit b7ae428

Please sign in to comment.