Django

Code

Ticket #2626 (assigned)

Opened 4 years ago

Last modified 2 months ago

Datetime handling is broken when dealing with more than one time zone

Reported by: Tom Tobin Assigned to: jedie (accepted)
Milestone: Component: Core framework
Version: Keywords:
Cc: mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k, jedie, forest, bronger@physik.rwth-aachen.de Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description (Last modified by korpios)

Django doesn't handle multiple time zones well.

Datetimes are potentially stored with a time zone offset based on settings.TIME_ZONE (only for PostgreSQL), and retrieved as naive datetime objects. This is fine when working on a standard site publishing items within a single time zone, but quickly becomes pathological when working with more than one time zone, or when transitioning an existing site to a new time zone.

I propose that Django's datetime handling be altered to ignore settings.TIME_ZONE on all database backends, and let the appropriate Fields handle time zone conversion. I've already written a DateTimeZoneField that appropriately handles multiple time zones and returns timezone-aware datetime objects; for it to work consistently, however, the PostgreSQL backends need to stop using TIMESTAMP WITH TIME ZONE and SET TIME ZONE.

In summary:

1) The PostgreSQL backends should ignore time zones completely.

2) An appropriate Field, rather than a backend, should handle time zone support; such a field would require its backend storage to always be UTC, which can only happen cleanly if the backend ignores time zones.

Discussion thread:

http://groups.google.com/group/django-developers/browse_thread/thread/b8c885389374c040

Attachments

Change History

01/21/07 21:53:02 changed by SmileyChris

  • stage changed from Unreviewed to Accepted.

Go ahead, Tom - write us a patch! Check out #2447 which may be related.

I agree that the os.environ['TZ'] is an ugly hack, there are other related tickets that back this up ;)

07/12/07 12:55:05 changed by korpios

  • reporter changed from Tom Tobin <korpios@gmail.com> to Tom Tobin <korpios@korpios.com>.

09/13/07 18:23:21 changed by brosner

  • owner changed from nobody to brosner.
  • status changed from new to assigned.

09/14/07 00:00:27 changed by micsco

  • cc set to mike.scott@aquis.co.nz.

12/01/07 12:59:49 changed by brosner

  • owner deleted.
  • status changed from assigned to new.

12/01/07 13:00:01 changed by brosner

  • owner set to nobody.

02/18/08 11:44:24 changed by korpios

  • cc changed from mike.scott@aquis.co.nz to mike.scott@aquis.co.nz, korpios@korpios.com.

06/25/08 11:27:34 changed by korpios

  • description changed.

06/25/08 11:34:08 changed by korpios

  • description changed.

08/17/08 14:36:24 changed by anonymous

  • milestone set to 1.0.

08/17/08 14:39:29 changed by korpios

  • milestone changed from 1.0 to post-1.0.

Please don't change milestones just because you'd like to see something in 1.0.

08/21/08 11:08:30 changed by korpios

  • cc changed from mike.scott@aquis.co.nz, korpios@korpios.com to mike.scott@aquis.co.nz.

09/10/08 13:38:40 changed by korpios

  • reporter changed from Tom Tobin <korpios@korpios.com> to Tom Tobin.

Removing my email from Reporter in hopes that Trac stops emailing me.

10/06/08 22:22:44 changed by anonymous

  • cc changed from mike.scott@aquis.co.nz to mike.scott@aquis.co.nz, nreilly@nreilly.com.

10/12/08 11:51:11 changed by subsume

I only see the SQL element being discussed, however, there is a more general problem where even datetime.now() will (randomly?) report incorrectly.

Using Apache and both mod_python and wsgi I find that various uses of datetime.now() around a project will give another TZ.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

03/22/09 17:30:01 changed by jacob

#10587 was a dup; it has more details and a good proposed solution.

03/29/09 12:26:35 changed by gsong

  • cc changed from mike.scott@aquis.co.nz, nreilly@nreilly.com to mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong.

03/29/09 12:54:33 changed by gsong

I want to make sure I have some basic decisions straight before tackling this problem. We want to only affect how datetime.datetime objects behave, and not datetime.time and datetime.date objects, right? In other words, only models.DateTimeField, not models.TimeField and models.DateField. I don't think timezone conversions are meaningful in those cases, at least not generally.

Also when I work on this problem, I will follow my proposal in #10587 unless there's any strong objections.

09/28/09 16:56:01 changed by korpios

I'm deleting my earlier comment pointing to code at LaunchPad, since The Onion does not currently maintain any available open source code there (or anywhere, at the moment — we're very busy bees).

11/18/09 08:36:07 changed by miracle2k

  • cc changed from mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong to mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k.

11/26/09 09:28:53 changed by jedie

  • cc changed from mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k to mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k, jedie.
  • owner changed from nobody to jedie.
  • status changed from new to assigned.

01/26/10 11:46:50 changed by forest

  • cc changed from mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k, jedie to mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k, jedie, forest.

07/18/10 05:53:42 changed by bronger

  • cc changed from mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k, jedie, forest to mike.scott@aquis.co.nz, nreilly@nreilly.com, gsong, miracle2k, jedie, forest, bronger@physik.rwth-aachen.de.

Another very subtle problem arising from this is that conditinal view processing may break. If you read a last-modified model field from a PostgreSQL backend in your last_modified_func and return it, the resulting HTTP header field is incorrect if your timezone isn't GMT. The reason is that the decorator expects a datetime object with correct tzinfo, but the ORM doesn't set this field.


Add/Change #2626 (Datetime handling is broken when dealing with more than one time zone)




Change Properties
Action