From ccadbec82555c53eefa889160510f5af1e224709 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 3 Apr 2022 13:44:57 -0400 Subject: 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 --- lib/sqlalchemy/dialects/sqlite/base.py | 36 ++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'lib/sqlalchemy/dialects/sqlite') diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index b1ac20383..f21ab9083 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -926,6 +926,12 @@ class DATETIME(_DateTimeMixin, sqltypes.DateTime): 2021-03-15 12:05:57.105542 + The incoming storage format is by default parsed using the + Python ``datetime.fromisoformat()`` function. + + .. versionchanged:: 2.0 ``datetime.fromisoformat()`` is used for default + datetime string parsing. + The storage format can be customized to some degree using the ``storage_format`` and ``regexp`` parameters, such as:: @@ -941,7 +947,8 @@ class DATETIME(_DateTimeMixin, sqltypes.DateTime): with keys year, month, day, hour, minute, second, and microsecond. :param regexp: regular expression which will be applied to incoming result - rows. If the regexp contains named groups, the resulting match dict is + rows, replacing the use of ``datetime.fromisoformat()`` to parse incoming + strings. If the regexp contains named groups, the resulting match dict is applied to the Python datetime() constructor as keyword arguments. Otherwise, if positional groups are used, the datetime() constructor is called with positional arguments via @@ -1027,6 +1034,13 @@ class DATE(_DateTimeMixin, sqltypes.Date): 2011-03-15 + The incoming storage format is by default parsed using the + Python ``date.fromisoformat()`` function. + + .. versionchanged:: 2.0 ``date.fromisoformat()`` is used for default + date string parsing. + + The storage format can be customized to some degree using the ``storage_format`` and ``regexp`` parameters, such as:: @@ -1042,11 +1056,13 @@ class DATE(_DateTimeMixin, sqltypes.Date): dict with keys year, month, and day. :param regexp: regular expression which will be applied to - incoming result rows. If the regexp contains named groups, the - resulting match dict is applied to the Python date() constructor - as keyword arguments. Otherwise, if positional groups are used, the - date() constructor is called with positional arguments via + incoming result rows, replacing the use of ``date.fromisoformat()`` to + parse incoming strings. If the regexp contains named groups, the resulting + match dict is applied to the Python date() constructor as keyword + arguments. Otherwise, if positional groups are used, the date() + constructor is called with positional arguments via ``*map(int, match_obj.groups(0))``. + """ _storage_format = "%(year)04d-%(month)02d-%(day)02d" @@ -1092,6 +1108,12 @@ class TIME(_DateTimeMixin, sqltypes.Time): 12:05:57.10558 + The incoming storage format is by default parsed using the + Python ``time.fromisoformat()`` function. + + .. versionchanged:: 2.0 ``time.fromisoformat()`` is used for default + time string parsing. + The storage format can be customized to some degree using the ``storage_format`` and ``regexp`` parameters, such as:: @@ -1107,10 +1129,12 @@ class TIME(_DateTimeMixin, sqltypes.Time): with keys hour, minute, second, and microsecond. :param regexp: regular expression which will be applied to incoming result - rows. If the regexp contains named groups, the resulting match dict is + rows, replacing the use of ``datetime.fromisoformat()`` to parse incoming + strings. If the regexp contains named groups, the resulting match dict is applied to the Python time() constructor as keyword arguments. Otherwise, if positional groups are used, the time() constructor is called with positional arguments via ``*map(int, match_obj.groups(0))``. + """ _storage_format = "%(hour)02d:%(minute)02d:%(second)02d.%(microsecond)06d" -- cgit v1.2.1