Django

Code

Ticket #1480 (closed: fixed)

Opened 4 years ago

Last modified 3 weeks ago

Don't Set TIME_ZONE unless it is explictly needed (or put it back to how you found it)

Reported by: Ian@holsman.net Assigned to: jedie
Milestone: 1.2 Component: Core framework
Version: SVN Keywords:
Cc: philz42, ramiro, rizumu, jedie, bas@usagebot.com, gabrielhurley Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

my understanding is that postgres needs the TIME_ZONE setting in order to work properly, and hence why it is in the settings.py file.

I just got bit by a nasty buy which was due to django setting/changing the timezone and then not setting it back to what it was after it was done. causing some pages to display CST and others to show PST (depending on weather the apache process had run a django request or not)

Is there a way django can either: a) not set the time_zone expliclty and just use whatever the system says? b) reset it back to what it was when it finishes up the request

Attachments

1480_time_zone.diff (4.8 kB) - added by ramiro on 01/28/08 16:38:13.
t1480-r7662.diff (4.6 kB) - added by ramiro on 06/16/08 09:41:47.
Updated patch so it applies cleanly to trunk as of r7662 and adding **New in Django development version** note to documentation
t1480-r9690.diff (5.2 kB) - added by ramiro on 12/31/08 12:52:18.
1480-r12590.diff (6.4 kB) - added by ramiro on 02/26/10 05:25:04.
Patch updated to r12590 (fixed docs merge conflict)

Change History

03/09/06 02:39:09 changed by hugo

Resetting it back won't work, as at least for the duration of the request you will have a switched timezone - so your problem would just change to "timezone depends on wether Apache at that moment serving something from Django in another thread". The problem is the global state of the timezone in the libc, so if Apache is running threaded, you get problems. It shouldn't be a problem with a full MPM installation, as in that case every process should have it's own timezone and since one process can just serve one request in that model, you should be safe.

The correct solution would be to move the timezone stuff away from the operating system and do all date/time handling in Django itself, at least if you want to be safe from thread-related timezone leakage :-/

Another option would be to switch away from mod_python to FCGI/SCGI with the process based FLUP server :-)

03/26/06 06:43:33 changed by marcink

I would vote for not setting TIME_ZONE at all, if possible. Setting the TZ environment variable on Windows is tricky, depends on the system language and is likely to break lots of time-related stuff like time.localtime and os.stat.

See my comment in #937 -- changing TZ causes problems with the autoreloader.

03/26/06 07:19:11 changed by marcink

See also #1119 and #1120 -- both are caused by trying to set TIME_ZONE on Windows.

hugo: threading is not the only problem here. Having a single global TIME_ZONE is similar to having a single global locale -- it is not enough when you have users living in different timezones.

06/12/06 07:10:10 changed by URL

  • type deleted.

09/18/06 09:58:53 changed by ubernostrum

  • type set to defect.

The problem with Apache is one that I've run into as well, and was rectified by using SetEnv in the httpd.conf to ensure that each virtual host had the correct TZ environment variable. I'm not sure what the best solution is for Django, though, because Django needs to have some way of ensuring that date and time functions return the correct values; re-inventing a whole set of time-zone conversion utilities within Django isn't something I'd be comfortable with.

01/23/07 21:07:34 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

See also, #2315

03/10/07 04:57:08 changed by mtredinnick

  • stage changed from Design decision needed to Accepted.

This is hard to fix for all cases. It's not ideal that Django wants to set the timezone everywhere on shared systems, but it's also possible to work around as a few comments have indicated

The solution we should implement here is to allow the TIMEZONE setting to be None (or unset), in which case we will not attempt to set the timezone -- it will remain in the server's timezone -- and the client code is responsible for doing any timezone manipulations they want to do.

01/28/08 16:38:13 changed by ramiro

  • attachment 1480_time_zone.diff added.

01/28/08 16:44:05 changed by ramiro

I've added a patch implementing my interpretation of Malcolm comments. Includes modifications to the project setting template file doc blurb comment above the TIME_ZONE setting and for the settings.txt documentation file.

01/28/08 16:46:19 changed by ramiro

  • has_patch set to 1.

06/16/08 09:41:47 changed by ramiro

  • attachment t1480-r7662.diff added.

Updated patch so it applies cleanly to trunk as of r7662 and adding **New in Django development version** note to documentation

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

  • milestone set to 1.0.

08/17/08 14:50:43 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.

12/31/08 12:52:18 changed by ramiro

  • attachment t1480-r9690.diff added.

02/23/09 14:40:00 changed by ramiro

  • owner changed from nobody to ramiro.

Something that was left out on purpose from documentation in the modifications introduced by the patch is the possibility to unset TIME_ZONE (in addition to setting it to None) as suggested by some comments above. Thus, semantics are kept simpler:

  • The only available/documented way to direct Django to NOT touch the TZ environment variable is to set TIME_ZONE to None.
  • If is is left untouched in your settings file, then the default 'America/Chicago' value is used.

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

  • milestone deleted.

Milestone post-1.0 deleted

03/10/09 12:00:13 changed by ramiro

  • cc set to ramiro.

08/05/09 19:51:41 changed by anonymous

  • needs_better_patch set to 1.
  • needs_docs set to 1.
  • version set to soc2009/multidb.
  • needs_tests set to 1.
  • milestone set to 1.2.

08/10/09 00:55:27 changed by rizumu

  • cc changed from ramiro to rizumu.

08/10/09 00:56:00 changed by rizumu

  • cc changed from rizumu to ramiro, rizumu.

11/26/09 09:29:03 changed by jedie

  • cc changed from ramiro, rizumu to ramiro, rizumu, jedie.
  • owner changed from ramiro to jedie.
  • status changed from new to assigned.

12/03/09 05:49:17 changed by russellm

  • version changed from soc2009/multidb to SVN.

Not sure why this was assigned to multidb.

01/19/10 21:36:58 changed by anonymous

  • cc changed from ramiro, rizumu, jedie to ramiro, rizumu, jedie, bas@usagebot.com.

02/25/10 18:54:00 changed by gabrielhurley

  • cc changed from ramiro, rizumu, jedie, bas@usagebot.com to ramiro, rizumu, jedie, bas@usagebot.com, gabrielhurley.

02/26/10 00:58:01 changed by philz42

  • cc changed from ramiro, rizumu, jedie, bas@usagebot.com, gabrielhurley to philz42, ramiro, rizumu, jedie, bas@usagebot.com, gabrielhurley.

02/26/10 01:27:49 changed by russellm

  • needs_better_patch deleted.
  • stage changed from Accepted to Ready for checkin.
  • needs_tests deleted.
  • needs_docs deleted.

02/26/10 05:25:04 changed by ramiro

  • attachment 1480-r12590.diff added.

Patch updated to r12590 (fixed docs merge conflict)

02/26/10 09:33:28 changed by russellm

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [12602]) Fixed #1480 -- Added the ability to use the system timezone. Thanks to Ramiro Morales for the patch.

02/26/10 09:38:51 changed by russellm

(In [12603]) [1.1.X] Fixed #1480 -- Added the ability to use the system timezone. Thanks to Ramiro Morales for the patch.

Backport of r12602 from trunk.


Add/Change #1480 (Don't Set TIME_ZONE unless it is explictly needed (or put it back to how you found it))




Change Properties
Action