mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57: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
|
from homeassistant.const import MATCH_ALL
|
||||||
|
|
||||||
DATE_STR_FORMAT = "%Y-%m-%d"
|
DATE_STR_FORMAT = "%Y-%m-%d"
|
||||||
|
NATIVE_UTC = dt.timezone.utc
|
||||||
UTC = pytz.utc
|
UTC = pytz.utc
|
||||||
DEFAULT_TIME_ZONE: dt.tzinfo = 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:
|
def utcnow() -> dt.datetime:
|
||||||
"""Get now in UTC time."""
|
"""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:
|
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
|
# Now we need to handle timezones. We will make this datetime object
|
||||||
# "naive" first and then re-convert it to the target timezone.
|
# "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.
|
# 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)
|
result = result.replace(tzinfo=None)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -337,7 +338,7 @@ def find_next_time_expression_time(
|
|||||||
return find_next_time_expression_time(result, seconds, minutes, hours)
|
return find_next_time_expression_time(result, seconds, minutes, hours)
|
||||||
|
|
||||||
result_dst = cast(dt.timedelta, result.dst())
|
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:
|
if result_dst >= now_dst:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user