mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +00:00
Use native utc timezone for utcnow() (#41727)
This allows timezone operations to happen in C code instead of the pure-python code of pytz
This commit is contained in:
parent
b5308bda09
commit
a9de9c62f1
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user