Skip to content

Commit 7814dff

Browse files
authored
Merge pull request #2454 from AppDaemon/datetime-parse-fix
added an additional datetime parsing test case
2 parents 8bcdba6 + a4a0cd6 commit 7814dff

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

appdaemon/parse.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,12 @@ def parse_datetime(
362362
case _:
363363
raise NotImplementedError(f"Unsupported input type: {type(input_)}")
364364

365-
if aware:
366-
if result.tzinfo is None:
367-
# Just adds the timezone without changing the time values
368-
result = result.replace(tzinfo=now.tzinfo)
369-
else:
370-
result = result.astimezone(now.tzinfo)
365+
# Make the timezones match for the comparison below
366+
if result.tzinfo is None:
367+
# Just adds the timezone without changing the time values
368+
result = result.replace(tzinfo=now.tzinfo)
369+
else:
370+
result = result.astimezone(now.tzinfo)
371371

372372
# The the days offset is negative, the result can't be forced to today, so set today to False
373373
if days_offset < 0:

tests/unit/datetime/test_parse_datetime.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import itertools
12
from datetime import date, datetime, timedelta
23
from functools import partial
34
from typing import Literal
45

56
import appdaemon.parse
67
import pytest
8+
import pytz
79
from appdaemon.parse import resolve_time_str
810
from astral import SunDirection
911
from astral.location import Location
@@ -35,6 +37,31 @@ def test_parse_hour(
3537

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

40+
@pytest.mark.parametrize(
41+
("input_", "aware", "today"),
42+
itertools.product(
43+
["2025-10-25 13:51:42"],
44+
(True, False),
45+
(True, False),
46+
),
47+
)
48+
def test_parse_datetime(
49+
self,
50+
input_: str,
51+
aware: bool,
52+
today: bool,
53+
parser: partial[datetime],
54+
) -> None:
55+
try:
56+
result = parser(input_, aware=aware, today=today)
57+
except Exception as e:
58+
assert False, f"Parsing failed: {e}"
59+
else:
60+
correct = datetime(2025, 10, 25, 13, 51, 42)
61+
if aware:
62+
correct = pytz.timezone("America/New_York").localize(correct)
63+
assert result == correct
64+
3865
@pytest.mark.parametrize(*ParameterBuilder.sun_params())
3966
def test_parse_sun_offsets(
4067
self,

0 commit comments

Comments
 (0)