8000 gh-99392: Fix SQLite converter recipes by naglis · Pull Request #99393 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99392: Fix SQLite converter recipes #99393

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

Merged
merged 2 commits into from
Nov 12, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Add doctests for common adapter/converter recipes
  • Loading branch information
naglis committed Nov 12, 2022
commit 76a4fff8ae9dc1b8ac52e64d54c680b1c3058b8a
19 changes: 19 additions & 0 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,25 @@ This section shows recipes for common adapters and converters.
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:

Expand Down
0