Skip to content

Commit

Permalink
Use zoneinfo instead of dateutil (home-assistant#50387)
Browse files Browse the repository at this point in the history
Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
balloob and bdraco authored May 10, 2021
1 parent 3fab21e commit f8584c3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion homeassistant/package_constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async-upnp-client==0.17.0
async_timeout==3.0.1
attrs==21.2.0
awesomeversion==21.2.3
backports.zoneinfo;python_version<"3.9"
bcrypt==3.1.7
certifi>=2020.12.5
ciso8601==2.1.3
Expand All @@ -24,7 +25,6 @@ paho-mqtt==1.5.1
pillow==8.1.2
pip>=8.0.3,<20.3
pyroute2==0.5.18
python-dateutil==2.8.1
python-slugify==4.0.1
pyyaml==5.4.1
requests==2.25.1
Expand Down
34 changes: 28 additions & 6 deletions homeassistant/util/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import re
from typing import Any

try:
import zoneinfo
except ImportError:
from backports import zoneinfo

import ciso8601
from dateutil import tz

from homeassistant.const import MATCH_ALL

Expand Down Expand Up @@ -43,7 +47,10 @@ def get_time_zone(time_zone_str: str) -> dt.tzinfo | None:
Async friendly.
"""
return tz.gettz(time_zone_str)
try:
return zoneinfo.ZoneInfo(time_zone_str) # type: ignore
except zoneinfo.ZoneInfoNotFoundError:
return None


def utcnow() -> dt.datetime:
Expand Down Expand Up @@ -311,7 +318,7 @@ def _lower_bound(arr: list[int], cmp: int) -> int | None:
if result.tzinfo in (None, UTC):
return result

if tz.datetime_ambiguous(result):
if _datetime_ambiguous(result):
# This happens when we're leaving daylight saving time and local
# clocks are rolled back. In this case, we want to trigger
# on both the DST and non-DST time. So when "now" is in the DST
Expand All @@ -320,7 +327,7 @@ def _lower_bound(arr: list[int], cmp: int) -> int | None:
if result.fold != fold:
result = result.replace(fold=fold)

if not tz.datetime_exists(result):
if not _datetime_exists(result):
# This happens when we're entering daylight saving time and local
# clocks are rolled forward, thus there are local times that do
# not exist. In this case, we want to trigger on the next time
Expand All @@ -337,11 +344,26 @@ def _lower_bound(arr: list[int], cmp: int) -> int | None:
# For example: if triggering on 2:30 and now is 28.10.2018 2:30 (in DST)
# we should trigger next on 28.10.2018 2:30 (out of DST), but our
# algorithm above would produce 29.10.2018 2:30 (out of DST)
if tz.datetime_ambiguous(now):
if _datetime_ambiguous(now):
check_result = find_next_time_expression_time(
now + _dst_offset_diff(now), seconds, minutes, hours
)
if tz.datetime_ambiguous(check_result):
if _datetime_ambiguous(check_result):
return check_result

return result


def _datetime_exists(dattim: dt.datetime) -> bool:
"""Check if a datetime exists."""
assert dattim.tzinfo is not None
original_tzinfo = dattim.tzinfo
# Check if we can round trip to UTC
return dattim == dattim.astimezone(UTC).astimezone(original_tzinfo)


def _datetime_ambiguous(dattim: dt.datetime) -> bool:
"""Check whether a datetime is ambiguous."""
assert dattim.tzinfo is not None
opposite_fold = dattim.replace(fold=not dattim.fold)
return _datetime_exists(dattim) and dattim.utcoffset() != opposite_fold.utcoffset()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ astral==2.2
async_timeout==3.0.1
attrs==21.2.0
awesomeversion==21.2.3
backports.zoneinfo;python_version<"3.9"
bcrypt==3.1.7
certifi>=2020.12.5
ciso8601==2.1.3
Expand All @@ -15,7 +16,6 @@ PyJWT==1.7.1
cryptography==3.3.2
pip>=8.0.3,<20.3
python-slugify==4.0.1
python-dateutil==2.8.1
pyyaml==5.4.1
requests==2.25.1
ruamel.yaml==0.15.100
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"async_timeout==3.0.1",
"attrs==21.2.0",
"awesomeversion==21.2.3",
'backports.zoneinfo;python_version<"3.9"',
"bcrypt==3.1.7",
"certifi>=2020.12.5",
"ciso8601==2.1.3",
Expand All @@ -47,7 +48,6 @@
"cryptography==3.3.2",
"pip>=8.0.3,<20.3",
"python-slugify==4.0.1",
"python-dateutil==2.8.1",
"pyyaml==5.4.1",
"requests==2.25.1",
"ruamel.yaml==0.15.100",
Expand Down

0 comments on commit f8584c3

Please sign in to comment.