#34828: DateTimeField breaks when given datetime that would be invalid in UTC
-------------------------------------+-------------------------------------
Reporter: Denis Cornehl | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by David Sanders):
The problem we're seeing here is that the time of `0001-01-01 00:00:00+1`
is stored in UTC, which will be a "negative" year in postgres. You can
test this by examining the timestamp (be sure to set your timezone to utc)
and observing the "BC" suffix.
When timestamps are retrieved from the database, they're retrieved as-is,
ie in UTC then converted back to the specified timezone upon request.
This assumption here:
> In this specific case I see that we have a timezone aware datetime in
python, store it into the database with timezone, and psycopg2 fetches it
as timezone aware datetime.
sounds like the common misconception that pg stores the timezone and it
can be retrieved… it can't because pg doesn't store the timezone. For
psycopg2 to create a datetime with a timezone it uses the timezone
specified for the connection. When `USE_TZ` is set, this is `UTC` hence
the error you see.
Some more information here:
https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql
--
Ticket URL: <https://code.djangoproject.com/ticket/34828#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018a83e3fdbf-12eecc94-5ef2-4599-a76b-86290d6bbcb6-000000%40eu-central-1.amazonses.com.