Use ciso8601 library to parse datetime faster (#32128)

This commit is contained in:
Ruslan Sayfutdinov 2020-02-24 16:33:10 +00:00 committed by GitHub
parent d996a4a9a9
commit 15b4975681
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 1 deletions

View File

@ -7,6 +7,7 @@ async_timeout==3.0.1
attrs==19.3.0 attrs==19.3.0
bcrypt==3.1.7 bcrypt==3.1.7
certifi>=2019.11.28 certifi>=2019.11.28
ciso8601==2.1.3
cryptography==2.8 cryptography==2.8
defusedxml==0.6.0 defusedxml==0.6.0
distro==1.4.0 distro==1.4.0

View File

@ -3,6 +3,7 @@ import datetime as dt
import re import re
from typing import Any, Dict, List, Optional, Tuple, Union, cast from typing import Any, Dict, List, Optional, Tuple, Union, cast
import ciso8601
import pytz import pytz
import pytz.exceptions as pytzexceptions import pytz.exceptions as pytzexceptions
import pytz.tzinfo as pytzinfo import pytz.tzinfo as pytzinfo
@ -122,6 +123,10 @@ def parse_datetime(dt_str: str) -> Optional[dt.datetime]:
Raises ValueError if the input is well formatted but not a valid datetime. Raises ValueError if the input is well formatted but not a valid datetime.
Returns None if the input isn't well formatted. Returns None if the input isn't well formatted.
""" """
try:
return ciso8601.parse_datetime(dt_str)
except (ValueError, IndexError):
pass
match = DATETIME_RE.match(dt_str) match = DATETIME_RE.match(dt_str)
if not match: if not match:
return None return None

View File

@ -5,6 +5,7 @@ ignore=tests
jobs=2 jobs=2
load-plugins=pylint_strict_informational load-plugins=pylint_strict_informational
persistent=no persistent=no
extension-pkg-whitelist=ciso8601
[BASIC] [BASIC]
good-names=id,i,j,k,ex,Run,_,fp good-names=id,i,j,k,ex,Run,_,fp

View File

@ -5,6 +5,7 @@ async_timeout==3.0.1
attrs==19.3.0 attrs==19.3.0
bcrypt==3.1.7 bcrypt==3.1.7
certifi>=2019.11.28 certifi>=2019.11.28
ciso8601==2.1.3
importlib-metadata==1.5.0 importlib-metadata==1.5.0
jinja2>=2.10.3 jinja2>=2.10.3
PyJWT==1.7.1 PyJWT==1.7.1

View File

@ -38,6 +38,7 @@ REQUIRES = [
"attrs==19.3.0", "attrs==19.3.0",
"bcrypt==3.1.7", "bcrypt==3.1.7",
"certifi>=2019.11.28", "certifi>=2019.11.28",
"ciso8601==2.1.3",
"importlib-metadata==1.5.0", "importlib-metadata==1.5.0",
"jinja2>=2.10.3", "jinja2>=2.10.3",
"PyJWT==1.7.1", "PyJWT==1.7.1",

View File

@ -464,7 +464,7 @@ def test_time():
def test_datetime(): def test_datetime():
"""Test date time validation.""" """Test date time validation."""
schema = vol.Schema(cv.datetime) schema = vol.Schema(cv.datetime)
for value in [date.today(), "Wrong DateTime", "2016-11-23"]: for value in [date.today(), "Wrong DateTime"]:
with pytest.raises(vol.MultipleInvalid): with pytest.raises(vol.MultipleInvalid):
schema(value) schema(value)