I have a mysql DATETIME value that is stored in system time, UTC. I need to convert that to my local timezone in django. Here is what I currently have:
I've created a simple middleware to handle all of this stuff for you:
https://github.com/Miserlou/django-easy-timezones
Simply install it and follow the instructions and you're done!
Install django-easy-timezones
pip install django-easy-timezones pytz pygeoip
Add "easy-timezones" to your INSTALLED_APPS setting like this:
INSTALLED_APPS = (
...
'easy-timezones',
)
Add EasyTimezoneMiddleware to your MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = (
...
'easy-timezones.middleware.EasyTimezoneMiddleware',
)
Add a path to the MaxMind GeoIP database in your settings file:
GEOIP_DATABASE = '/path/to/your/geoip/database/GeoIP.dat'
Enable localtime in your templates.
{% load tz %}
The UTC time is {{ object.date }}
{% localtime on %}
The local time is {{ object.date }}
{% endlocaltime %}
Tada!