#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 Denis Cornehl):
Replying to [comment:1 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
>
Thank you for the quick response!
Yes, when the system timezone is UTC, postgres will have BC in the DB,
you're absolutely right.
But shouldn't then the `DateTimeField` fail and not accept this datetime?
Or is it expected behaviour like this?
--
Ticket URL: <https://code.djangoproject.com/ticket/34828#comment:2>
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/0107018a83e7c7f5-52f020d0-d3f9-4c6d-9a68-08350b469f33-000000%40eu-central-1.amazonses.com.