Opened 6 years ago

Closed 5 years ago

#23820 closed New feature (fixed)

Make it possible to override USE_TZ & TIME_ZONE on a per-database basis

Reported by: Aymeric Augustin Owned by: Aymeric Augustin
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When integrating with a legacy database, datetimes are most often encoded in local time. This doesn't work well with projects where USE_TZ = True.

The configuration for each database backend should accept a TIME_ZONE option to enforce interpretation of datetimes stored in that database as naive values in that time zone.

Setting this option when USE_TZ = False could either be an error or perform a conversion to the global default TIME_ZONE. The latter may be too complicated to be worth the effort.

Change History (10)

comment:1 Changed 6 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by Aymeric Augustin

At least for SQLite, this will require getting rid of the global converters as there is no way to register converters per connection.

I'll try to replace the converters, which are quite messy anyway, with an implementation of DateTimeField.from_db_value.

comment:3 Changed 5 years ago by Aymeric Augustin

DatabaseOperations.convert_datetimefield_value seems to be a better match for what I'm trying to achieve.

Version 0, edited 5 years ago by Aymeric Augustin (next)

comment:4 Changed 5 years ago by Aymeric Augustin

Owner: changed from nobody to Aymeric Augustin
Status: newassigned

comment:5 Changed 5 years ago by Aymeric Augustin

This will required getting rid of the global converters and adapters for datetimes, because they're hardcoded to use UTC regardless of the database connection being used.

Proposal: https://groups.google.com/d/msg/django-developers/Ci_cs0KTagM/DchJIB8-vRsJ

comment:6 Changed 5 years ago by Aymeric Augustin

Has patch: set

comment:7 Changed 5 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:8 Changed 5 years ago by Aymeric Augustin <aymeric.augustin@…>

In ec18657:

Removed global timezone-aware datetime converters.

Refs #23820.

comment:9 Changed 5 years ago by Aymeric Augustin <aymeric.augustin@…>

In d9521f6:

Removed global timezone-aware datetime adapters.

Refs #23820.

Fixed #19738.

Refs #17755. In order not to introduce a regression for raw queries,
parameters are passed through the connection.ops.value_to_db_* methods,
depending on their type.

comment:10 Changed 5 years ago by Aymeric Augustin <aymeric.augustin@…>

Resolution: fixed
Status: assignedclosed

In ed83881:

Fixed #23820 -- Supported per-database time zone.

The primary use case is to interact with a third-party database (not
primarily managed by Django) that doesn't support time zones and where
datetimes are stored in local time when USE_TZ is True.

Configuring a PostgreSQL database with the TIME_ZONE option while USE_TZ
is False used to result in silent data corruption. Now this is an error.

Note: See TracTickets for help on using tickets.
Back to Top