mirror of
https://github.com/home-assistant/core.git
synced 2025-12-25 05:26:47 +00:00
Allow conversion from date strings to "unix" timestamp. (#1985)
"unix" timestamp is number of seconds since Jan 1, 1970 UTC.
This allows scripts that use templates to generate time
deltas in seconds if desired from state attributes such
as last_updated.
Some examples:
timestamp now is
{{ as_timestamp(now) }}
timstamp of last change is
{{ as_timestamp(states.binary_sensor.garage_door.last_changed) }}
seconds since last change is
{{ as_timestamp(now) - as_timestamp(states.binary_sensor.garage_door.last_changed) }}
This commit is contained in:
committed by
Paulus Schoutsen
parent
ca0ea6c2f3
commit
b86a1ece01
@@ -107,6 +107,19 @@ class TestDateUtil(unittest.TestCase):
|
||||
datetime(1986, 7, 9, tzinfo=dt_util.UTC),
|
||||
dt_util.utc_from_timestamp(521251200))
|
||||
|
||||
def test_as_timestamp(self):
|
||||
"""Test as_timestamp method."""
|
||||
ts = 1462401234
|
||||
utc_dt = dt_util.utc_from_timestamp(ts)
|
||||
self.assertEqual(ts, dt_util.as_timestamp(utc_dt))
|
||||
utc_iso = utc_dt.isoformat()
|
||||
self.assertEqual(ts, dt_util.as_timestamp(utc_iso))
|
||||
|
||||
# confirm the ability to handle a string passed in
|
||||
delta = dt_util.as_timestamp("2016-01-01 12:12:12")
|
||||
delta -= dt_util.as_timestamp("2016-01-01 12:12:11")
|
||||
self.assertEquals(1, delta)
|
||||
|
||||
def test_parse_datetime_converts_correctly(self):
|
||||
"""Test parse_datetime converts strings."""
|
||||
assert \
|
||||
|
||||
Reference in New Issue
Block a user