替换“ tzinfo”并以当地时间打印会更改六分钟 [英] Replace "tzinfo" and print with localtime amends six minutes

查看:115
本文介绍了替换“ tzinfo”并以当地时间打印会更改六分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个DataTimeField无时区。不久,我将手动编辑此TZ,然后要求读取本地TZ的值。看到最终结果修改了六分钟!



逻辑:

 >>>导入日期时间
>>从django.utils导入时区
>>测试= 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天,20:54:00 STD>)

>> timezone.activate(pytz.timezone(’America / Sao_Paulo’))
>>> timezone.localtime(test)
datetime.datetime(2016、9、28、10、16、10,tzinfo =< DstTzInfo'美国/圣保罗'BRT-1天,21:00:00 STD>) b $ b

注意:想法是,这发生在两个阶段。首先,我想在TimeZone创作中保持领先地位。然后,我想向用户显示用户的TimeZone。在这种情况下,两个用户都是同一地区。



对不起我的英语






遵循@ user6897474的响应并付诸实践,我得到了以下解决方案:



我在保存之前获取了POST和serializabel文件的日期时间,以下内容:

  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)

返回super(CheckControllerSerializer,self).create(validated_data)

替换tzinfo = None,我保证您会没有以下错误的问题:


不是天真的日期时间(已设置tzinfo)



解决方案

使用timezone.localize代替datetim e.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天,21:00:00 STD>)

>> timezone.activate(pytz.timezone(’America / Sao_Paulo’))
>>> timezone.localtime(test)
datetime.datetime(2016,9,28,10,10,10,tzinfo =< DstTzInfo'美国/圣保罗'BRT-1天,21:00:00 STD>) b $ b

参考:


  1. pytz.localize与datetime.replace


  2. datetime对象,呈现给Django模板时,总是6分钟关闭



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)

解决方案

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

这篇关于替换“ tzinfo”并以当地时间打印会更改六分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆