Replace “tzinfo” and print with localtime amends six minutes

房东的猫 提交于 2019-12-11 08:36:30

问题


I am creating a DataTimeField no time zone. Soon I am editing this TZ "manually" and then asking to read the value with local TZ. See the end result amends six minutes!

Logic:

>>> import datetime
>>> from django.utils import timezone
>>> test = datetime.datetime(2016, 9, 28, 10, 10, 10)
datetime.datetime(2016, 9, 28, 10, 10, 10)

>>> test = teste.replace(tzinfo=pytz.timezone('America/Sao_Paulo'))
datetime.datetime(2016, 9, 28, 10, 10, 10, tzinfo=<DstTzInfo 'America/Sao_Paulo' LMT-1 day, 20:54:00 STD>)

>>> timezone.activate(pytz.timezone('America/Sao_Paulo'))
>>> timezone.localtime(test)
datetime.datetime(2016, 9, 28, 10, 16, 10, tzinfo=<DstTzInfo 'America/Sao_Paulo' BRT-1 day, 21:00:00 STD>)

NOTE: The idea is that this happens in two stages. First I want to keep on the bench with the TimeZone creation. Then I want to show to the user with the user's TimeZone. In this case both users was the same region.

sorry my English


Following the response from @user6897474 and putting in practice, I got this solution:

I'm getting a datetime for POST and serializabel file before saving, do the following:

class CheckControllerSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        datetime = validated_data['datetime'].replace(tzinfo=None)
        validated_data['datetime'] = pytz.timezone('America/Sao_Paulo').localize(datetime)

        return super(CheckControllerSerializer, self).create(validated_data)

Replace tzinfo = None, I guarantee you will not have problems with the following error:

Not naive datetime (tzinfo is already set)


回答1:


Use timezone.localize instead of datetime.replace

>>> test = datetime.datetime(2016, 9, 28, 10, 10, 10)
>>> test = pytz.timezone('America/Sao_Paulo').localize(test)
datetime.datetime(2016, 9, 28, 10, 10, 10, tzinfo=<DstTzInfo 'America/Sao_Paulo' BRT-1 day, 21:00:00 STD>)

>>> timezone.activate(pytz.timezone('America/Sao_Paulo'))
>>> timezone.localtime(test)
datetime.datetime(2016, 9, 28, 10, 10, 10, tzinfo=<DstTzInfo 'America/Sao_Paulo' BRT-1 day, 21:00:00 STD>)

Reference :

  1. pytz.localize vs datetime.replace

  2. datetime object, when rendered to Django template, was always 6 minutes off



来源:https://stackoverflow.com/questions/39759041/replace-tzinfo-and-print-with-localtime-amends-six-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!