Mulitple Time Zone support?

800 views
Skip to first unread message

ydjango

unread,
Apr 12, 2009, 8:15:14 PM4/12/09
to Django users
What is the best way to provide multiple Time zone support?

The registered user visiting the website should see all times and
dates as per his/her time zone and not as per time zone in
settings.py?

We will keep user's time zone in our database. Dates and times will be
stored in Mysql DB and I think by default will be in Time zone of
Settings.py

(Also where can I get list of all time zones to populate the select
dropdown.)

I am sure like pretty much everything else Django has a easy solution
to it.

Alex Gaynor

unread,
Apr 12, 2009, 8:18:03 PM4/12/09
to django...@googlegroups.com
Take a look at django-timezones: http://code.google.com/p/django-timezones/ which uses the pytz module for this.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Malcolm Tredinnick

unread,
Apr 12, 2009, 8:34:11 PM4/12/09
to django...@googlegroups.com
On Sun, 2009-04-12 at 17:15 -0700, ydjango wrote:
> What is the best way to provide multiple Time zone support?
>
> The registered user visiting the website should see all times and
> dates as per his/her time zone and not as per time zone in
> settings.py?

If I were doing this, I'd create a templatetag that formats the datetime
object into the appropriate timezone. You could use, say, a context
processor or some consistent thing you call from your views to populate
the context with the user's timezone.

You could also use a filter for this, although the design has to be
slightly different. Filters are unaware of the template they're
rendering in, so they don't have access to the context. Therefore, you'd
have to pass the appropriate timezone (or an object containing the
timezone) to the filter as an argument.

So it's a choice between a couple of possibilities, at least:

(1) {% tz_convert obj as obj2 %}

Accepts "obj" (a date or datetime object), uses something in the
context to work out the timezone and then creates a new object
in the context ("obj2") that you can use later and which has the
right timezone value.

(2) {{ obj|tz_convert:tz }}

Converts a date or datetime object to an equivalent object in
the right timezone, the timezone being passed in via the "tz"
variable, in this case.

I can imagine either solution being more appropriate than the other in
different circumstances, so I'd just go for whichever approach seems
best, rather than worrying too much about whether there's a single best
answer here. There's no doubt other possibilities as well; the above
were the two I could come up with quickly that seem to hold up against a
few hypothetical usages I threw at them.

Regards,
Malcolm


Brian Neal

unread,
Apr 12, 2009, 9:41:37 PM4/12/09
to Django users
On Apr 12, 7:15 pm, ydjango <neerash...@gmail.com> wrote:
> What is the best way to provide multiple Time zone support?

Definitely check out the Python pytz module.

> (Also where can I get list of all time zones to populate the  select
> dropdown.)
>

There is almost 400 time zones in the "common timezones" in pytz. The
timezones are of the forum "area/city", so I created a drop down for
area and then a 2nd drop down for city (with Javascript that populates
the city based on the area chosen). That's still a lot of timezones to
choose from but it was better than having 400 in one list.
Reply all
Reply to author
Forward
0 new messages