1.0.4 Freeware    
  not rated
Timezone utilities for Django




django-timezones2 is a Django app for working with different timezones.


1. Install timezones2 & pytz
2. Add 'timezones2' to your INSTALLED_APPS setting
3. Add 'timezones2.middleware.TimezonesMiddleware' to your MIDDLEWARE_CLASSES setting


The most important thing that you must understand is that there are two "types" of datetime objects: naive (without timezone information) and "wise". Timezones2 treats naive datetime as objects with timezone set in TIME_ZONE setting.

Timezones2 is not a wrapper/replacement for pytz. It's just a helper.

Timezones2 has interface similar to translation utils:

>>> import timezones2
>>> tz = timezones2.get_current_tz()
>>> tz
< UTC >
>>> print tz
>>> timezones2.activate("Europe/Warsaw")
>>> print timezones2.get_current_tz()
>>> timezones2.deactivate()
>>> print timezones2.get_current_tz()

Default timezone is one set in TIME_ZONE settings. It's easy to get default timezone:

>>> timezones2.get_default_tz()
< UTC >

It is possible to set TIME_ZONE setting to values other than "UTC", but it's recommended to work with UTC timezone.

Function activate sets timezone only for current request. It's useful when you need to generate e-mail with user's timezone. If you need to change user's timezone, you will have to use save_tz function:

def change_timezone(request):
 if request.method == 'POST':
 form = TzForm(request.POST)
 if form.is_valid():
 tz = form.cleaned_data['tz']
 timezones2.save_tz(request, tz)
 return redirect('/')
 form = TzForm()
 return direct_to_template(request, 'change_timezone.html', {'form': form})

You probably want to store timezone in user's profile. In that case you should use save_tz and activate in your login view (warning: save_tz doesn't activate new timezone).

Template filters

To use those filters, you have to load "tz2_tags" library in your templates.

Converts datetime object to current (active) timezone.

< b >Last login: {{ user.last_login|to_current_tz|date:"SHORT_DATETIME_FORMAT" }}< /b >


Converts datetime object to default timezone (TIME_ZONE setting). Its useless for naive datetime objects, because they use default timezone.

< b >Server time: {{ current_time|to_default_tz|date:"SHORT_DATETIME_FORMAT" }}< /b >


Converts datetime to given timezone.

< b >In UTC: {{ add_date|to_tz:'UTC' }}< /b >< br / >
< b >In your localtime: {{ add_date|to_tz:user.get_profile.timezone }}< /b >

The magic: Detect timezone with GeoIP

Using GeoIP database to detect user's timezone requires additional config.

Getting GeoIP database

Download those files:


Then put them into separate folder (let's say /home/test/geoip/) and unpack last two. Also you have to install geoip library (it's already installed in most linux distributions). Open your settings file, set setting TZ2_USE_GEOIP to True and point GEOIP_PATH setting to directory with geoip data (/home/test/geoip/ in this example).

Since now timezones2 will automatically activate user's timezone based on his location.

If you have any problems with GeoIP configuration, please follow this link:

Model fields

Timezones2 provides two fields: TimeZoneField and DateTimeField2.


Use this field to store timezones. By default it will render combo field with list of available timezones. It's useful for saving user's timezone in his profile.

It normalizes it's value to datetime.tzinfo objects. If you need string representation of timezone, just stringify it:

>>> warsaw
< DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD >
>>> unicode(warsaw)

Another example:

>>> from testapp.models import TzModel
>>> m = TzModel()
>>> = 'Europe/Warsaw'
< DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD >
>>> unicode(


It's a modified DateTimeField. It knows how to deal with datetime objects which contains timezone informations. Moreover it will always convert it's value to current timezone. Example:

>>> from testapp.models import Article
>>> a = Article(text='foo', title='bar')
>>> timezones2.activate('Europe/Warsaw')
>>> import datetime
>>> now =
>>> now # naive datetime object - default timezone
datetime.datetime(2010, 11, 21, 22, 9, 23, 686865)
>>> a.add_date = now
>>> a.add_date # and in active timezone
datetime.datetime(2010, 11, 21, 23, 9, 23, 686865, tzinfo=< DstTzInfo 'Europe/Warsaw' CET+1:00:00 STD >)
>>> a = Article.objects.get(
>>> a.add_date
datetime.datetime(2010, 11, 21, 23, 9, 23, 686865, tzinfo=< DstTzInfo 'Europe/Warsaw' CET+1:00:00 STD >)
>>> timezones2.activate('Europe/Moscow')
>>> a.add_date # still in Europe/Warsaw, because query has been evaluated already
datetime.datetime(2010, 11, 21, 23, 9, 23, 686865, tzinfo=< DstTzInfo 'Europe/Warsaw' CET+1:00:00 STD >)
>>> a = Article.objects.get(
>>> a.add_date # now it's ok
datetime.datetime(2010, 11, 22, 1, 9, 23, 686865, tzinfo=< DstTzInfo 'Europe/Moscow' MSK+3:00:00 STDdjango-timezones2 is a Django app for working with different timezones. >)
Last updated on March 27th, 2012

0 User reviews so far.