diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-03 13:44:57 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-03 14:47:52 -0400 |
commit | ccadbec82555c53eefa889160510f5af1e224709 (patch) | |
tree | 959b4309fcc26191ef791034bda76e6fda0d1bdb /lib/sqlalchemy/engine/_py_processors.py | |
parent | 1dffb7cedeb009ca6c532db558bd0588dd846957 (diff) | |
download | sqlalchemy-ccadbec82555c53eefa889160510f5af1e224709.tar.gz |
use .fromisoformat() for sqlite datetime, date, time parsing
SQLite datetime, date, and time datatypes now use Python standard lib
``fromisoformat()`` methods in order to parse incoming datetime, date, and
time string values. This improves performance vs. the previous regular
expression-based approach, and also automatically accommodates for datetime
and time formats that contain either a six-digit "microseconds" format or a
three-digit "milliseconds" format.
Fixes: #7029
Change-Id: I67aab4fe5ee3055e5996050cf4564981413cc221
Diffstat (limited to 'lib/sqlalchemy/engine/_py_processors.py')
-rw-r--r-- | lib/sqlalchemy/engine/_py_processors.py | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/lib/sqlalchemy/engine/_py_processors.py b/lib/sqlalchemy/engine/_py_processors.py index 27cb9e939..63f03466a 100644 --- a/lib/sqlalchemy/engine/_py_processors.py +++ b/lib/sqlalchemy/engine/_py_processors.py @@ -16,8 +16,10 @@ They all share one common characteristic: None is passed through unchanged. from __future__ import annotations import datetime +from datetime import date as date_cls +from datetime import datetime as datetime_cls +from datetime import time as time_cls from decimal import Decimal -import re import typing from typing import Any from typing import Callable @@ -26,6 +28,7 @@ from typing import Type from typing import TypeVar from typing import Union + _DT = TypeVar( "_DT", bound=Union[datetime.datetime, datetime.time, datetime.date] ) @@ -50,6 +53,7 @@ def str_to_datetime_processor_factory( "Couldn't parse %s string '%r' " "- value is not a string." % (type_.__name__, value) ) from err + if m is None: raise ValueError( "Couldn't parse %s string: " @@ -108,12 +112,25 @@ def int_to_boolean(value: Optional[int]) -> Optional[bool]: return bool(value) -DATETIME_RE = re.compile(r"(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)(?:\.(\d+))?") -TIME_RE = re.compile(r"(\d+):(\d+):(\d+)(?:\.(\d+))?") -DATE_RE = re.compile(r"(\d+)-(\d+)-(\d+)") +def str_to_datetime(value: Optional[str]) -> Optional[datetime.datetime]: + if value is not None: + dt_value = datetime_cls.fromisoformat(value) + else: + dt_value = None + return dt_value -str_to_datetime = str_to_datetime_processor_factory( - DATETIME_RE, datetime.datetime -) -str_to_time = str_to_datetime_processor_factory(TIME_RE, datetime.time) -str_to_date = str_to_datetime_processor_factory(DATE_RE, datetime.date) + +def str_to_time(value: Optional[str]) -> Optional[datetime.time]: + if value is not None: + dt_value = time_cls.fromisoformat(value) + else: + dt_value = None + return dt_value + + +def str_to_date(value: Optional[str]) -> Optional[datetime.date]: + if value is not None: + dt_value = date_cls.fromisoformat(value) + else: + dt_value = None + return dt_value |