diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-11-12 19:47:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-12 19:47:59 (GMT) |
commit | 5324893599bcdab7e8d56fbc6dce32b994f76443 (patch) | |
tree | f62934d67172a1d672e88706e62081df3d76d043 /Doc/library/sqlite3.rst | |
parent | 535027f470b680221c7695f015a0d8f0f4f8e1d0 (diff) | |
download | cpython-5324893599bcdab7e8d56fbc6dce32b994f76443.zip cpython-5324893599bcdab7e8d56fbc6dce32b994f76443.tar.gz cpython-5324893599bcdab7e8d56fbc6dce32b994f76443.tar.bz2 |
gh-99392: Fix sqlite3 converter recipes (GH-99393)
(cherry picked from commit dfc1b17a23fed933cffa09eec125a7e8c90ea867)
Co-authored-by: naglis <827324+naglis@users.noreply.github.com>
Diffstat (limited to 'Doc/library/sqlite3.rst')
-rw-r--r-- | Doc/library/sqlite3.rst | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 3a4714e..eb84f74 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -1683,20 +1683,39 @@ This section shows recipes for common adapters and converters. def convert_date(val): """Convert ISO 8601 date to datetime.date object.""" - return datetime.date.fromisoformat(val) + return datetime.date.fromisoformat(val.decode()) def convert_datetime(val): """Convert ISO 8601 datetime to datetime.datetime object.""" - return datetime.datetime.fromisoformat(val) + return datetime.datetime.fromisoformat(val.decode()) def convert_timestamp(val): """Convert Unix epoch timestamp to datetime.datetime object.""" - return datetime.datetime.fromtimestamp(val) + return datetime.datetime.fromtimestamp(int(val)) sqlite3.register_converter("date", convert_date) sqlite3.register_converter("datetime", convert_datetime) sqlite3.register_converter("timestamp", convert_timestamp) +.. testcode:: + :hide: + + dt = datetime.datetime(2019, 5, 18, 15, 17, 8, 123456) + + assert adapt_date_iso(dt.date()) == "2019-05-18" + assert convert_date(b"2019-05-18") == dt.date() + + assert adapt_datetime_iso(dt) == "2019-05-18T15:17:08.123456" + assert convert_datetime(b"2019-05-18T15:17:08.123456") == dt + + # Using current time as fromtimestamp() returns local date/time. + # Droping microseconds as adapt_datetime_epoch truncates fractional second part. + now = datetime.datetime.now().replace(microsecond=0) + current_timestamp = int(now.timestamp()) + + assert adapt_datetime_epoch(now) == current_timestamp + assert convert_timestamp(str(current_timestamp).encode()) == now + .. _sqlite3-connection-shortcuts: |