Search code examples
djangopython-3.xdjango-modelspython-datetimedjango-timezone

Replace "tzinfo" and print with localtime amends six minutes


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)


Solution

  • 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