[Django] #30288: PostgreSQL connections forced into UTC if setting.USE_TZ is True.

7 views
Skip to first unread message

Django

unread,
Mar 25, 2019, 4:48:49 PM3/25/19
to django-...@googlegroups.com
#30288: PostgreSQL connections forced into UTC if setting.USE_TZ is True.
-------------------------------------+-------------------------------------
Reporter: chris- | Owner: nobody
bradley |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
`DatabaseWrapper.ensure_timezone()` in
`django/db/backends/postgresql/base.py` forces the database to use
`self.timezone_name()` as its timezone. On the base class,
`timezone_name()` uses the following logic:
- Return `settings.TIME_ZONE` if `USE_TZ` is not truthy.
- Return `self.settings_dict['TIME_ZONE']` if it is set.
- Return 'UTC' otherwise.

However, `BaseDatabaseWrapper.check_settings()` prevents 'TIME_ZONE' from
being set if the `supports_timezones` feature is set on the backend. The
database's timezone is thus always set to 'UTC' for databases such as
PostgreSQL if `USE_TZ` is set to True.

This creates problems if the database does anything involving the current
date, which may be off by one from the Django's date. For instance,
consider the following setup:
- Model `X` points at a view.
- `X` has a field named `age_in_days` that points to a column defined as
`CURRENT_DATE - y`, where `y` is a date column.
- The Django settings module has `USE_TZ` set to `True` and `TIME_ZONE`
set to 'US/Eastern'.

`X.age_in_days` will then be one too high if viewed between 7:00 PM (8:00
PM if DST is in effect.) and midnight.

A solution that would not break backwards compatibility is to allow
'TIME_ZONE' to be set on a databases settings dictionary even if
`supports_timezones` is set to True for the backend's features.

I have not checked if this is still an issue in later Django versions.

--
Ticket URL: <https://code.djangoproject.com/ticket/30288>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 25, 2019, 5:25:06 PM3/25/19
to django-...@googlegroups.com
#30288: PostgreSQL connections forced into UTC if setting.USE_TZ is True.
-------------------------------------+-------------------------------------
Reporter: chris-bradley | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #23524. Feel free to read the relevant threads on django-
developers and open a new one if you still believe the behavior should
change.

--
Ticket URL: <https://code.djangoproject.com/ticket/30288#comment:1>

Reply all
Reply to author
Forward
0 new messages