views:

62

answers:

1

The documentation says:

http://docs.djangoproject.com/en/dev/ref/settings/#time-zone

Note that this is the time zone to which Django will convert all dates/times -- not necessarily the timezone of the server. For example, one server may serve multiple Django-powered sites, each with a separate time-zone setting. Normally, Django sets the os.environ['TZ'] variable to the time zone you specify in the TIME_ZONE setting. Thus, all your views and models will automatically operate in the correct time zone.

I've read this serveral times and it's not clear to me what's going on with the TIME_ZONE setting.

Should I be managing utc offsets if I want models with a date-time stamp to display to the users local-time zone?

For example on save use, datetime.datetime.utcnow() instead of datetime.datetime.now(), and in the view do something like:

display_datetime = model.date_time + datetime.timedelta(USER_UTC_OFFSET)
+1  A: 

Much to my surprise, it does appear too.

web81:~/webapps/dominicrodger2/dominicrodger$ python2.5 manage.py shell
Python 2.5.4 (r254:67916, Aug  5 2009, 12:42:40)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import settings
>>> settings.TIME_ZONE
'Europe/London'
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2009, 10, 15, 6, 29, 58, 85662)
>>> exit()
web81:~/webapps/dominicrodger2/dominicrodger$ date
Thu Oct 15 00:31:10 CDT 2009

And yes, I did get distracted whilst writing this answer :-)

I use the TIME_ZONE setting so that my automatically added timestamps on object creation (using auto_now_add, which I believe is soon to be deprecated) show creation times in the timezone I set.

If you want to convert those times into the timezones of your website visitors, you'll need to do a bit more work, as per the example you gave. If you want to do lots of timezone conversion to display times in your website visitors' timezones, then I'd strongly advise you to set your TIME_ZONE settings to store times in UTC, because it'll make your life easier in the long run (you can just use UTC-offsets, rather than having to worry about daylight savings).

If you're interested, I believe the timezone is set from the TIME_ZONE setting here.

Edit, per your comment that it doesn't work on Windows, this is because of the following in the Django source:

if hasattr(time, 'tzset'):
    # Move the time zone info into os.environ. See ticket #2315 for why
    # we don't do this unconditionally (breaks Windows).
    os.environ['TZ'] = self.TIME_ZONE
    time.tzset()

Windows:

C:\Documents and Settings\drodger>python
ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec  5 2008, 13:58:38) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> hasattr(time, 'tzset')
False

Linux:

web81:~$ python2.5
Python 2.5.4 (r254:67916, Aug  5 2009, 12:42:40)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> hasattr(time, 'tzset')
True
Dominic Rodger
I just tried to repeat your test here using the manage shell and it didn't seem to... strange. >>> datetime.datetime.now() datetime.datetime(2009, 10, 15, 15, 37, 47, 869000) >>> import settings >>> settings.TIME_ZONE 'Europe/Zurich' >>> datetime.datetime.now() datetime.datetime(2009, 10, 15, 15, 37, 59, 838000) >>> exit() PS D:\> date Thu Oct 15 15:38:25 TST 2009
monkut
Anyway, as you mention it sounds like a good idea just to use UTC time.
monkut
I wonder if that's because of the comments on line 106-107 of the source I posted. Try running `hasattr(time, 'tzset')`.
Dominic Rodger
See my edit about Windows.
Dominic Rodger