views:

1493

answers:

3

How well does Django handle the case of different timezones for each user? Ideally I would like to run the server in the UTC timezone (eg, in settings.py set TIME_ZONE="UTC") so all datetimes were stored in the database as UTC. Stuff like this scares me which is why I prefer UTC everywhere.

But how hard will it be to store a timezone for each user and still use the standard django datetime formatting and modelform wrappers. Do I anticipate having to write date handling code everywhere to convert dates into the user's timezone and back to UTC again?

I am still going through the django tutorial but I know how much of a pain it can be to deal with user timezones in some other frameworks that assume system timezone everywhere so I thought I'd ask now.

My research at the moment consisted of searching the django documentation and only finding one reference to timezones.


Additional:

A: 

You could start by taking a look at the django-timezones application. It makes available a number of timezone-based model fields (and their corresponding form fields, and some decorators), which you could use to at least store different timezone values per user (if nothing else).

ayaz
That's a shame. It looks like timezone handling in django will be fiddly unless I use a 3rd party module with little documentation and problems with mysql timestamps.
Nick Sonneveld
Time zone handling is likely to be fiddly just about anywhere. It's a fundamentally fiddly thing :(
Jon Skeet
A: 

Not a Django expert here, but afaik Django has no magic, and I can't even imagine any such magic that would work.

For example: you don't always want to save times in UTC. In a calendar application, for example, you want to save the datetime in the local time that the calendar event happens. Which can be different both from the servers and the users time zone. So having code that automatically converts every selected datetime to the servers time zone would be a Very Bad Thing.

So yes, you will have to handle this yourself. I'd recommend to store the time zone for everything, and of course run the server in UTC, and let all the datetimes generated by the application use UTC, and then convert them to the users time zone when displaying. It's not difficult, just annoying to remember. when it comes to datetimes that are inputted by the user, it's dependant on the application if you should convert to UTC or not. I would as a general recommendation not convert to UTC but save in the users time zone, with the information of which time zone that is.

Yes, time zones is a big problem. I've written a couple of blog posts on the annoying issue, like here: http://regebro.wordpress.com/2007/12/18/python-and-time-zones-fighting-the-beast/

In the end you will have to take care of time zone issues yourself, because there is no real correct answer to most of the issues.

Lennart Regebro
By problems with storing calendar entries in UTC, I assume you mean when you have events that happen on a particular date? Do you store it as "2009-05-16 Australia/Sydney" or "2009-05-15 14:00 UTC to 2009-05-16 14:00 UTC" ? :) Also weirdness with events that sit on a daylight savings changeover (as mentioned in your blog post)
Nick Sonneveld
Oh, edge cases. :) No, I mean storing it as "2009-05-15 14:00 Australia/Sydney", as the event is to happen there. All day events as in your first example is another tricky issue. They typically need to be time zone naive, so they happen from 00:00 to 23:59 no matter what time zone you are in.
Lennart Regebro
But storing calendar event as "2009-05-15 14:00 Australia/Sydney" is ambiguos on a daylight savings changeover, only well-defined time zones like UTC are not ambiguous.
Tomasz Zielinski
+1  A: 

I'm going to be working on this problem myself for my application. My first approach to this problem would be to go with django core developer Malcom Tredinnick's advice in this django-user's post. You'll want to store the user's timezone setting in their user profile, probably.

I would also highly encourage you to look into the pytz module, which makes working with timezones less painful. For the front end, I created a "timezone picker" based on the common timezones in pytz. I have one select box for the area, and another for the location (e.g. US/Central is rendered with two select boxes). It makes picking timezones slightly more convenient than wading through a list of 400+ choices.

Brian Neal