8

I saw this post Is Django corrupting timezone-aware DateTimeField when saving it to the Database? but it specifically uses pytz and mysql and what not where I don't use pytz and use SQLite (incase it might have an impact).

I have the following model

class ScheduleItem(models.Model):
    work_date = models.DateTimeField('Work date')

And I insert data as follows:

from isoweek import Week
from dateutil import parser
from django.utils import timezone

def foo()
    year = 2016 #hardcoded for example purpose
    wknr = 2 #hardcoded for example purpose
    dateObj = parser.parse(Week(year, wknr).day(0).isoformat() + " 00:00:00")
    print(dateObj) # 2016-01-11 00:00:00 as expected
    final = timezone.make_aware(dateObj)
    print(final) # 2016-01-11 00:00:00+01:00 as expected
    return final


workdate = foo()
si = ScheduleItem(work_date=workdate)
si.save()

The print statements give me the right output, however once I look in the database (SQLite) I see 2016-01-10 23:00:00

My django settings say

TIME_ZONE = 'CET'
USE_TZ = True

Retrieving the data I get:

datetime.datetime(2016, 1, 10, 23, 0, tzinfo=<UTC>)

Why is it storing the data in another format then I specify and why if Django is set to be timezone aware do I get a UTC timezone back? I mean before insertion the datetime object says: datetime.datetime(2016, 1, 11, 0, 0, tzinfo=<DstTzInfo 'CET' CET+1:00:00 STD>)

update -

I found a work around in the meantime by setting TIME_ZONE on the database as described in the Django documentation here. This gives me the right timezone/date in the database, but according to that documentation I shouldn't need it because my DB is managed by Django

This allows interacting with third-party databases that store datetimes in local time rather than UTC. To avoid issues around DST changes, you shouldn’t set this option for databases managed by Django.

It is still unclear to me why Django does convert a datetime object with a CET timezone to UTC when storing it in the database, but isn't smart enough to convert it back to CET when retrieving.

1 Answer 1

5
+50

Django uses UTC time internally. TIME_ZONE will be used "for your views and models" (https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TIME_ZONE)

You started with 2016-01-11 00:00 CET, which is 2016-01-10 23:00 UTC! Your datetime was correctly saved to the database and later restored, so everything is working as expected.

4
  • I can now understand and accept that Django's ORM takes care of converting and storing a datetime in UTC in the database depending on the combination of USE_TZ and TIME_ZONE. However, from that same section "all your views and models will automatically operate in this time zone" This is not happening. When retrieving the datetime through the Model/ORM I get the UTC, not the CET version as indicated in my settings. Sure I can use Django's timezone.localtime() to convert what I retrieved, but that's not the point. It is doing just 1 half of the required operations.
    – ixje
    Commented Feb 2, 2016 at 10:58
  • That section could be a bit more clear: TIME_ZONE is used when displaying user-facing information. Try it using the humanize template tag, I believe you'll see CET time displayed.
    – knite
    Commented Feb 2, 2016 at 18:21
  • I opted to grant you the bounty/answer as it at least answered one part of the question and allowed me to understand part of the internals. Why it doesn't restore my aware datetime object might be a Django design choice and could be considered out side of the scope of the question.
    – ixje
    Commented Feb 9, 2016 at 7:20
  • I believe it's the only option - the database stores UTC, so the timezone information is lost . To restore to CET, you can keep timezone as a separate model field, and normalize to that timezone as needed.
    – knite
    Commented Feb 9, 2016 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.