Here are my two relevant settings in settings.py for my django project.
USE_TZ = True
TIME_ZONE = 'Europe/Stockholm'
I have a DateTimeField defined like this in models.
triggeredTime = models.DateTimeField('Utlöses')
This is managed by the Django admin app and a form. So the form allows me to enter a date and time into this field of the database. The DB in this case is sqlite3.
(I wasn’t allowed to post an image as spam prevention so here it is as a link)
https://i.stack.imgur.com/9g1Ts.png
For the sake of the example the time I enter is 14:10:05.
Then I start the django shell.
$ python manage.py shell
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from scheduler.models import Task, Schedule
>>> s = Schedule.objects.all()
>>> for row in s:
... print row.triggeredTime
...
2012-09-11 12:10:05
This is clearly in UTC. Even if I try to add pytz to the mix and set tzinfo I get a value that is not in DST. I should not have to hard code DST into my code.
>>> import pytz
>>> from pytz import timezone
>>> stockholm = timezone('Europe/Stockholm')
>>> for row in s:
... print row.triggeredTime.replace(tzinfo=stockholm)
...
2012-09-11 12:10:05+01:00
For that matter, I shouldn’t even have to hard code the timezone since django claims to have that covered. So can someone please explain how to use timezones in Django?
FYI the server is physically located in the CET timezone so Europe/Stockholm is its current system wide setting.
SQLite doesn’t store TZ information, so you either have to convert dates to UTC when save into DB, set USE_TZ to False or change to Postgre