#35267 closed Cleanup/optimization (fixed)
Correct TIME_ZONE setting reference in time zone topic.
Reported by: | Josh Smeaton | Owned by: | Josh Smeaton |
---|---|---|---|
Component: | Documentation | Version: | 5.0 |
Severity: | Normal | Keywords: | orm datetimefield timezones |
Cc: | Aymeric Augustin | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql
The PostgreSQL backend stores datetimes as timestamp with time zone. In practice, this means it converts datetimes from the connection’s time zone to UTC on storage, and from UTC to the connection’s time zone on retrieval.
This wording seems to suggest that Django DateTimeField will return datetimes with the tzinfo set to settings.TIME_ZONE
, but that's not what I observe. All data returned is in utc.
Am I mistaking the blurb on the timezone support docs?
https://github.com/django/django/blob/f5ed4306bbfd2e5543dd02cf5a22326a29253cdf/django/db/models/fields/__init__.py#L1594 defines a to_python
which will make a datetime aware, but that's only used by the serialization framework.
I would expect a from_db_value
to convert the returned value from utc
to the timezone defined by settings.TIME_ZONE.
Change History (10)
comment:1 by , 10 months ago
follow-up: 3 comment:2 by , 10 months ago
Thank you!
I think the docs are ambiguous. It sounds like the configured time zone will be used for the connection from the docs I linked, but in practice the connection uses UTC unless a query is executed on the session? Or is there another connection parameter that controls the session timezone?
Might be worth a docs update to clarify the current behaviour?
comment:3 by , 10 months ago
Cc: | added |
---|
Replying to Josh Smeaton:
Thank you!
I think the docs are ambiguous. It sounds like the configured time zone will be used for the connection from the docs I linked, but in practice the connection uses UTC unless a query is executed on the session? Or is there another connection parameter that controls the session timezone?
Might be worth a docs update to clarify the current behaviour?
Doc clarifications are always welcome. I hope that Aymeric will chime in ;)
follow-ups: 5 6 comment:4 by , 10 months ago
The database connection settings have their own TIME_ZONE setting: https://docs.djangoproject.com/en/5.0/ref/settings/#time-zone, which is what the previous docs I was referring to must have been referencing: https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql
It makes sense to me now.
comment:5 by , 10 months ago
Replying to Josh Smeaton:
The database connection settings have their own TIME_ZONE setting: https://docs.djangoproject.com/en/5.0/ref/settings/#time-zone, which is what the previous docs I was referring to must have been referencing: https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql
It makes sense to me now.
Amazing, thank you for letting us know. Do you think the first docs you read still need adjustment? I'm thinking that the TIME_ZONE
setting link in the paragraph would be better pointed to this instead: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
comment:6 by , 10 months ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Easy pickings: | set |
Owner: | changed from | to
Status: | new → assigned |
Summary: | DateTimeField does not seem to be translated back to the local timezone on data retrieval on Postgres → Correct TIME_ZONE setting reference in time zone topic. |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
Replying to Josh Smeaton:
The database connection settings have their own TIME_ZONE setting: https://docs.djangoproject.com/en/5.0/ref/settings/#time-zone, which is what the previous docs I was referring to must have been referencing: https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql
It makes sense to me now.
Thanks Josh. Assigned to you, I hope you don't mind fixing it ;)
comment:8 by , 10 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
This is not a strict duplicate of #21214, but the discussion there should shed some light on the current behavior.