Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sip
Cpython
Commits
dfc1b17a
Unverified
Commit
dfc1b17a
authored
Nov 12, 2022
by
naglis
Committed by
GitHub
Nov 12, 2022
Browse files
Options
Downloads
Patches
Plain Diff
gh-99392: Fix sqlite3 converter recipes (#99393)
parent
fb844e19
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/library/sqlite3.rst
+22
-3
22 additions, 3 deletions
Doc/library/sqlite3.rst
with
22 additions
and
3 deletions
Doc/library/sqlite3.rst
+
22
−
3
View file @
dfc1b17a
...
...
@@ -2112,20 +2112,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:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment