diff --git a/appdaemon/parse.py b/appdaemon/parse.py index 37498dce1..a56252d5f 100644 --- a/appdaemon/parse.py +++ b/appdaemon/parse.py @@ -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: diff --git a/tests/unit/datetime/test_parse_datetime.py b/tests/unit/datetime/test_parse_datetime.py index b477bf156..47cd39de5 100644 --- a/tests/unit/datetime/test_parse_datetime.py +++ b/tests/unit/datetime/test_parse_datetime.py @@ -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 @@ -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,