From f53fcacf492dd1a61a74aa41b531c60be834b9ed Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Wed, 11 Sep 2019 17:37:09 +0300 Subject: [PATCH] Make uk_transport sensor timezone/DST aware (#26577) * Make uk_transport sensor timezone/DST aware * Fix offset-naive and offset-aware datetime comparison --- homeassistant/components/uk_transport/sensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/uk_transport/sensor.py b/homeassistant/components/uk_transport/sensor.py index 8e6e46531e2..eb325d32212 100644 --- a/homeassistant/components/uk_transport/sensor.py +++ b/homeassistant/components/uk_transport/sensor.py @@ -11,6 +11,7 @@ import requests import voluptuous as vol import homeassistant.helpers.config_validation as cv +import homeassistant.util.dt as dt_util from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import CONF_MODE from homeassistant.helpers.entity import Entity @@ -277,12 +278,11 @@ class UkTransportLiveTrainTimeSensor(UkTransportSensor): def _delta_mins(hhmm_time_str): """Calculate time delta in minutes to a time in hh:mm format.""" - now = datetime.now() + now = dt_util.now() hhmm_time = datetime.strptime(hhmm_time_str, "%H:%M") - hhmm_datetime = datetime( - now.year, now.month, now.day, hour=hhmm_time.hour, minute=hhmm_time.minute - ) + hhmm_datetime = now.replace(hour=hhmm_time.hour, minute=hhmm_time.minute) + if hhmm_datetime < now: hhmm_datetime += timedelta(days=1)