vendredi 29 janvier 2016

Django datetimefield timezone aware CET

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>)

Aucun commentaire:

Enregistrer un commentaire