Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions appdaemon/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ def parse_datetime(
case _:
raise NotImplementedError(f"Unsupported input type: {type(input_)}")

if aware:
if result.tzinfo is None:
# Just adds the timezone without changing the time values
result = result.replace(tzinfo=now.tzinfo)
else:
result = result.astimezone(now.tzinfo)
# Make the timezones match for the comparison below
if result.tzinfo is None:
# Just adds the timezone without changing the time values
result = result.replace(tzinfo=now.tzinfo)
else:
result = result.astimezone(now.tzinfo)

# The the days offset is negative, the result can't be forced to today, so set today to False
if days_offset < 0:
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/datetime/test_parse_datetime.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import itertools
from datetime import date, datetime, timedelta
from functools import partial
from typing import Literal

import appdaemon.parse
import pytest
import pytz
from appdaemon.parse import resolve_time_str
from astral import SunDirection
from astral.location import Location
Expand Down Expand Up @@ -35,6 +37,31 @@ def test_parse_hour(

assert result.date() == default_now.date()

@pytest.mark.parametrize(
("input_", "aware", "today"),
itertools.product(
["2025-10-25 13:51:42"],
(True, False),
(True, False),
),
)
def test_parse_datetime(
self,
input_: str,
aware: bool,
today: bool,
parser: partial[datetime],
) -> None:
try:
result = parser(input_, aware=aware, today=today)
except Exception as e:
assert False, f"Parsing failed: {e}"
else:
correct = datetime(2025, 10, 25, 13, 51, 42)
if aware:
correct = pytz.timezone("America/New_York").localize(correct)
assert result == correct

@pytest.mark.parametrize(*ParameterBuilder.sun_params())
def test_parse_sun_offsets(
self,
Expand Down
Loading