Introducing Django-Easy-Timezones

by Rich Jones

Timezones. Ugh. The bane of every web programmer's existence. Let's make them easier!

For some reason, back when the language of the web was still being solidified, people forgot about timezones. The W3C remembered that not everybody speaks English, so they included the 'Accept-Language' HTTP Header, but apparently they never considered that users might not be in the same timezone as their server, so they forgot to include an 'Accept-Timezone' header. Since then, all web developers have been had the responsibility for figuring out the timezones of their users, rather than allowing their clients to do it automatically. This has resulted in a lot of horrible user experiences and misprinted time information over the years.

Easy!

So, I've come up with an incredibly simple solution for Django developers who have to deal with timezones. It's called django-easy-timezones and, like the name implies, it's extremely simple to use. You can find django-easy-timezones on GitHub here.

Fundamentally, it is a Django middleware which uses the gratis MaxMind GeoIP database to find a user's location, figures out their timezone, and then stores it in their session context. Let's get started!

Installation

So just how easy is it?

django-easy-timezones is already published to pip, so all you have to do is..

pip install django-easy-timezones

or clone the git repository and copy the application into your project.

Then, add it to your project's INSTALLED_APPS like this:

INSTALLED_APPS = (
  ...
  'easy_timezones',
)

then hook up the middleware like this:

MIDDLEWARE_CLASSES = (
  ...
  'easy_timezones.middleware.EasyTimezoneMiddleware',
)

and add the path to the MaxMind GeoIP database (downloadable here) in your settings file:

GEOIP_DATABASE = '/path/to/your/geoip/database/GeoIP.dat'

Finally, to activate it in your templates, remember to turn on 'localtime', like this:


    The UTC time is 

    The local time is 

And that's it! You're done! Easy! Just remember that all of your datetime models must be saved with UTC data.

That's it! If there are any problems, let me know on GitHub so I can fix them right away!