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 several 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)
Much to my surprise, it does appear to.
And yes, I did get distracted whilst writing this answer 🙂
I use the
TIME_ZONEsetting so that my automatically added timestamps on object creation (usingauto_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_ZONEsettings 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_ZONEsetting here.Edit, per your comment that it doesn’t work on Windows, this is because of the following in the Django source:
Windows:
Linux: