From 2e9aedfe155d1a8260fb4d96453f562107009ab7 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sun, 26 Oct 2025 23:28:56 -0500 Subject: [PATCH 1/2] added an additional datetime parsing test case --- appdaemon/parse.py | 3 +++ tests/unit/datetime/test_parse_datetime.py | 27 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/appdaemon/parse.py b/appdaemon/parse.py index 37498dce1..c4c483493 100644 --- a/appdaemon/parse.py +++ b/appdaemon/parse.py @@ -368,6 +368,9 @@ def parse_datetime( result = result.replace(tzinfo=now.tzinfo) else: result = result.astimezone(now.tzinfo) + else: + # Need to remove the timezone for now, so the comparison doesn't fail below + now = now.replace(tzinfo=None) # 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, From a4a0cd641dd16a9ae80ef98a222206fa916a76bb Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sun, 26 Oct 2025 23:46:06 -0500 Subject: [PATCH 2/2] timezone matching --- appdaemon/parse.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/appdaemon/parse.py b/appdaemon/parse.py index c4c483493..a56252d5f 100644 --- a/appdaemon/parse.py +++ b/appdaemon/parse.py @@ -362,15 +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: - # Need to remove the timezone for now, so the comparison doesn't fail below - now = now.replace(tzinfo=None) + 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: