mysql - Is Django corrupting timezone-aware DateTimeField when saving it to the Database? -


i have django model described here

i create , save instance of model:

>>> django.db.models import max, f, func >>> django.conf import settings >>> import pytz, datetime >>> 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 try retrieve instance inserted, datetimefield in native format , 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 convert 2122848857 datetime, 2037-04-09t00:14:17+00:00. 4 hours greater time inserted. why? how correct seeming corruption? machine's timezone edt 4 hours behind utc. still doesn't explain why django saving utc time if in local timezone.

import pytz, datetime django.db.models import max, f, func django.conf import settings myapp.models import mymodel  local_tz = pytz.timezone(settings.time_zone)  local_datetime = local_tz.localize(datetime.datetime(2037, 4, 8, 20, 14, 17), is_dst=none) utc_datetime = local_datetime.astimezone(pytz.utc) # datetime.datetime(2037, 4, 9, 0, 14, 17, tzinfo=<utc>)  mymodel.objects.create(my_date=utc_datetime)  x = mymodel.objects.aggregate(max1=max('my_date'),max2=max(func(f('my_date'), function='unix_timestamp')))  pytz.utc.localize(datetime.datetime.fromtimestamp(x['max2'])).astimezone(local_tz) == x['max1'].astimezone(local_tz) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -