diff --git a/homeassistant/util/dt.py b/homeassistant/util/dt.py index 3b202dad1cb..8b52170f0e4 100644 --- a/homeassistant/util/dt.py +++ b/homeassistant/util/dt.py @@ -11,6 +11,7 @@ import pytz.tzinfo as pytzinfo from homeassistant.const import MATCH_ALL DATE_STR_FORMAT = "%Y-%m-%d" +NATIVE_UTC = dt.timezone.utc UTC = pytz.utc DEFAULT_TIME_ZONE: dt.tzinfo = pytz.utc @@ -52,7 +53,7 @@ def get_time_zone(time_zone_str: str) -> Optional[dt.tzinfo]: def utcnow() -> dt.datetime: """Get now in UTC time.""" - return dt.datetime.utcnow().replace(tzinfo=UTC) + return dt.datetime.now(NATIVE_UTC) def now(time_zone: Optional[dt.tzinfo] = None) -> dt.datetime: @@ -314,7 +315,7 @@ def find_next_time_expression_time( # Now we need to handle timezones. We will make this datetime object # "naive" first and then re-convert it to the target timezone. # This is so that we can call pytz's localize and handle DST changes. - tzinfo: pytzinfo.DstTzInfo = result.tzinfo + tzinfo: pytzinfo.DstTzInfo = UTC if result.tzinfo == NATIVE_UTC else result.tzinfo result = result.replace(tzinfo=None) try: @@ -337,7 +338,7 @@ def find_next_time_expression_time( return find_next_time_expression_time(result, seconds, minutes, hours) result_dst = cast(dt.timedelta, result.dst()) - now_dst = cast(dt.timedelta, now.dst()) + now_dst = cast(dt.timedelta, now.dst()) or dt.timedelta(0) if result_dst >= now_dst: return result