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

前端 未结 3 1741
耶瑟儿~
耶瑟儿~ 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 08:49

    Yes. Some of the data is discarded.

    Django DateTime field stored data in a database DateTime storage that lacks timezone information thus the information is removed before storage.

    This is whet the manual says:

    Note that if you set this to point to a DateTimeField, only the date portion of the > field will be considered. Besides, when USE_TZ is True, the check will be performed > in the current time zone at the time the object gets saved.

    The proper way to store full datetime date would be to use a DateTimeOffset field - that can be found in MS-Sql and others. But this is not supported (yet?)

    0 讨论(0)
  • 2021-01-16 08:51

    The primary difference between datetime and timestamp is that timestamp will automatically store a value as UTC, using the current mysql time_zone setting, and datetime will ignore the time_zone setting when inserting and retrieving records.

    You're using a datetime field, however you're also using the mysql UNIX_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 the time_zone setting. This is the source of the conversion discrepancy.

    You have two choices.

    1. Ensure the time_zone session variable is set to UTC before running your query.
    2. Store the value into a timestamp field instead of a datetime field.
    0 讨论(0)
  • 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=<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)
    
    0 讨论(0)
提交回复
热议问题