Skip to content

Fix sqlite timezone-naive adapter recipe #131825

Open
@drf5n

Description

@drf5n

Documentation

This recipe for an adapter does not work as advertised:

   def adapt_datetime_iso(val):
       """Adapt datetime.datetime to timezone-naive ISO 8601 date."""
       return val.isoformat()

from: https://github.com/erlend-aasland/cpython/blob/a3711d1541c1b7987941b41d2247f87dae347117/Doc/library/sqlite3.rst?plain=1#L2287-L2289
or https://docs.python.org/3/library/sqlite3.html#sqlite3-adapter-converter-recipes

If the argument has a timezone, it will produce a timezone-aware result, contrary to its documentation
Consider the difference between these:

import datetime
print(datetime.datetime.now(datetime.timezone.utc).isoformat())
print(datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None).isoformat())

"""
2025-03-28T08:41:09.240880+00:00
2025-03-28T08:41:09.241046
"""

The recipe should instead be like this:

   def adapt_datetime_iso(val):
       """Adapt datetime.datetime to timezone-naive ISO 8601 date."""
       return val.replace(tzinfo=None).isoformat()

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirtopic-sqlite3type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions