Skip to content

Fix sqlite timezone-naive adapter recipe #131825

Closed
@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()

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Status

    Done

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy