django-timezones2

Timezone utilities for Django
Download

django-timezones2 Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Freeware
  • Price:
  • FREE
  • Publisher Name:
  • Rafal Stozek
  • Publisher web site:
  • https://bitbucket.org/sayane/

django-timezones2 Tags


django-timezones2 Description

django-timezones2 is a Django app for working with different timezones.Installation1. Install timezones2 & pytz2. Add 'timezones2' to your INSTALLED_APPS setting3. Add 'timezones2.middleware.TimezonesMiddleware' to your MIDDLEWARE_CLASSES setting BasicsThe 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 tzUTC>>> timezones2.activate("Europe/Warsaw")>>> print timezones2.get_current_tz()Europe/Warsaw>>> timezones2.deactivate()>>> print timezones2.get_current_tz()UTCDefault 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 timezones2.save_tz(request, tz) return redirect('/') else: 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 filtersTo use those filters, you have to load "tz2_tags" library in your templates.to_current_tzConverts datetime object to current (active) timezone.< b >Last login: {{ user.last_login|to_current_tz|date:"SHORT_DATETIME_FORMAT" }}< /b > to_default_tzConverts 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 >to_tzConverts 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 GeoIPUsing GeoIP database to detect user's timezone requires additional config.Getting GeoIP databaseDownload those files:- http://www.maxmind.com/timezone.txt- http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz- http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz 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: http://docs.djangoproject.com/en/1.2/ref/contrib/gis/geoip/Model fieldsTimezones2 provides two fields: TimeZoneField and DateTimeField2.TimeZoneFieldUse 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)u'Europe/Warsaw'Another example:>>> from testapp.models import TzModel>>> m = TzModel()>>> m.tz''>>> m.tz = 'Europe/Warsaw'>>> m.tz< DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD >>>> unicode(m.tz)u'Europe/Warsaw'DateTimeField2It'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 = datetime.datetime.now()>>> now # naive datetime object - default timezonedatetime.datetime(2010, 11, 21, 22, 9, 23, 686865)>>> a.add_date = now>>> a.add_date # and in active timezonedatetime.datetime(2010, 11, 21, 23, 9, 23, 686865, tzinfo=< DstTzInfo 'Europe/Warsaw' CET+1:00:00 STD >)>>> a.save()>>> a = Article.objects.get(pk=a.pk)>>> a.add_datedatetime.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 alreadydatetime.datetime(2010, 11, 21, 23, 9, 23, 686865, tzinfo=< DstTzInfo 'Europe/Warsaw' CET+1:00:00 STD >)>>> a = Article.objects.get(pk=a.pk)>>> a.add_date # now it's okdatetime.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. >)Product's homepage


django-timezones2 Related Software