mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add easy converting string timestamps/dates to datetime objects in templates (#51576)
This commit is contained in:
@@ -526,6 +526,33 @@ def test_timestamp_local(hass):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input",
|
||||
(
|
||||
"2021-06-03 13:00:00.000000+00:00",
|
||||
"1986-07-09T12:00:00Z",
|
||||
"2016-10-19 15:22:05.588122+0100",
|
||||
"2016-10-19",
|
||||
"2021-01-01 00:00:01",
|
||||
"invalid",
|
||||
),
|
||||
)
|
||||
def test_as_datetime(hass, input):
|
||||
"""Test converting a timestamp string to a date object."""
|
||||
expected = dt_util.parse_datetime(input)
|
||||
if expected is not None:
|
||||
expected = str(expected)
|
||||
|
||||
assert (
|
||||
template.Template(f"{{{{ as_datetime('{input}') }}}}", hass).async_render()
|
||||
== expected
|
||||
)
|
||||
assert (
|
||||
template.Template(f"{{{{ '{input}' | as_datetime }}}}", hass).async_render()
|
||||
== expected
|
||||
)
|
||||
|
||||
|
||||
def test_as_local(hass):
|
||||
"""Test converting time to local."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user