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

前端 未结 3 1744
耶瑟儿~
耶瑟儿~ 2021-01-16 08:08

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


        
3条回答
  •  不思量自难忘°
    2021-01-16 09:06

    import pytz, datetime
    from django.db.models import Max, F, Func
    from django.conf import settings
    from 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=)
    
    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)
    

提交回复
热议问题