I have a Django model as described here
I create and save an instance of this model:
>>> from django.db.models import Max, F, Func
>>> from django.conf import settings
>>> import pytz, datetime
>>> from myapp.models import myModel
>>> myModel.objects.all().delete()
>>> myModel.objects.create(
my_date=datetime.datetime(2037,4,2,15,18,17,tzinfo=pytz.UTC),
my_string="asda"
)
<myModel: myModel object>
Then I try to retrieve the instance I just inserted, but I get the DateTimeField in native format and after applying UNIX_TIMESTAMP
:
>>> x = myModel.objects.values('my_string').aggregate(
max1=Max('my_date'),
max2=Max(Func(F('my_date'), function='UNIX_TIMESTAMP'))
)
>>> x
{
'max2': Decimal('2122848857.000000'),
'max1': datetime.datetime(2037, 4, 8, 20, 14, 17, tzinfo=<UTC>)
}
>>> datetime.datetime.utcfromtimestamp(x["max2"])
datetime.datetime(2037, 4, 9, 0, 14, 17)
>>> pytz.timezone(settings.TIME_ZONE)
<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>
>>>
If you convert 2122848857
back to a DateTime, you get 2037-04-09T00:14:17+00:00
. This is 4 hours greater than the time I actually inserted. Why? How to correct this seeming corruption? My machine's timezone is EDT which is 4 hours behind UTC. But that still doesn't explain why Django is saving this UTC time as if it was in my local timezone.
The primary difference between
datetime
andtimestamp
is thattimestamp
will automatically store a value as UTC, using the current mysqltime_zone
setting, anddatetime
will ignore thetime_zone
setting when inserting and retrieving records.You're using a
datetime
field, however you're also using the mysqlUNIX_TIMESTAMP
function against that field. When you do that, the docs explain that the server interprets the value as a local time value, based on thetime_zone
setting. This is the source of the conversion discrepancy.You have two choices.
time_zone
session variable is set to UTC before running your query.timestamp
field instead of adatetime
field.