Why sqlite time is different from my settings.py TIME_ZONE?

503 views
Skip to first unread message

Hyunseo Yang

unread,
Jun 2, 2015, 9:59:08 AM6/2/15
to django...@googlegroups.com

I'm using Sqlite for my project database.
I need date and time in my model so i'm using this field.
date = models.DateTimeField()

On the result template (results.html), the time is correct (localtime or  TIME_ZONE in my settings.py)
The problem is, when i check the database on Django admin page and sqlite db file, it seems like my timezone setting is not applied.(so, maybe UTC)

what is the problem and how could i fix this?

I think the template's parameters are from view, so the timezone.now() is correct and passes the right time.
So my guess is that 'date=timezone.now()' passes to sqlite like this: 'insert into table values (date=datetime('now'))'
and the timezone of sqlite is maybe set to UTC default. 
this is what i'm guessing. am i right? also i do not know how to set sqlite database timezone...

p.s On the other side, I also think when we set settings.py TIME_ZONE, django would manage all these things.
So don't know where to approach. Spending almost 2 days on this problem...

Dan Tagg

unread,
Jun 2, 2015, 11:22:30 AM6/2/15
to django...@googlegroups.com
Hi Hyunseo Yang,

SQLite does not save the timezone in the database. If you pass it a datetime with timezone encoding then it calculates the UTC time that that time represents and stores that. 

It is explained here:

As far as I understand whatever database you are using, Django always stores datetimes as UTC.

What version of Django are you using as timezone awareness is different for different versions.

Dan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6269c301-d704-4294-863d-bb6c65d7140f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Wildman and Herring Limited, Registered Office: 52 Great Eastern Street, London, EC2A 3EP, Company no: 05766374
Reply all
Reply to author
Forward
0 new messages