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.
Django stores datetime information in UTC in the database, uses timezone-aware datetime objects internally, and translates them to the end user's time zone in templates and forms.
The solution to this problem is to use UTC in the code and use local time only when interacting with end users. Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default.
Python Django set timezone settings For saving information in the database, Django recommends using UTC. Even if your website is only accessible in a one-time zone, storing data in UTC in your database is still a good strategy. Daylight Saving Time is the primary reason for this.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With