Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite parse_date util to not rely on strptime #522

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions hass_nabucasa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import ssl
from typing import Awaitable, Callable, TypeVar

import ciso8601

CALLABLE_T = TypeVar("CALLABLE_T", bound=Callable) # noqa pylint: disable=invalid-name
DATE_STR_FORMAT = "%Y-%m-%d"
UTC = dt.timezone.utc


Expand All @@ -25,7 +26,7 @@ def utc_from_timestamp(timestamp: float) -> dt.datetime:
def parse_date(dt_str: str) -> dt.date | None:
"""Convert a date string to a date object."""
try:
return dt.datetime.strptime(dt_str, DATE_STR_FORMAT).date()
return ciso8601.parse_datetime(dt_str).date()
except ValueError: # If dt_str did not match our format
return None

Expand Down
1 change: 1 addition & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ignore=tests_*
good-names=id,i,j,k,ex,Run,_,fp,T,cb

generated-members=botocore.errorfactory
extension-pkg-whitelist=ciso8601

disable=
abstract-method,
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
install_requires=[
"pycognito==2023.5.0",
"snitun==0.36.2",
"ciso8601>=2.3.0",
"acme==2.7.1",
"cryptography>=2.8",
"attrs>=19.3",
Expand Down
56 changes: 56 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Tests for hass_nabucaa utils."""
import pytest

from hass_nabucasa import utils


@pytest.mark.parametrize(
"input_str",
[
"2020-02-30",
"2019-02-29",
"2021-04-31",
"2023-06-31",
"2018-09-31",
"2015-11-31",
"2022-02-30",
"2020-04-31",
"2021-06-31",
"2017-09-31",
"2019-04-31",
"2023-11-31",
"2020-06-31",
"2016-02-30",
"2021-11-31",
"invalid",
"2023/12/12",
],
)
def test_parse_date_with_invalid_dates(input_str):
"""Test the parse_date util."""
assert utils.parse_date(input_str) is None


@pytest.mark.parametrize(
"input_str",
[
"2020-02-29",
"2019-03-15",
"2021-04-30",
"2023-06-15",
"2018-09-30",
"2015-12-25",
"2022-02-28",
"2020-07-04",
"2021-08-21",
"2017-10-31",
"2019-01-01",
"2023-11-30",
"2020-05-05",
"2016-12-12",
"2021-03-14",
],
)
def test_parse_date_with_valid_dates(input_str):
"""Test the parse_date util."""
assert utils.parse_date(input_str) is not None