[Django]-Replace "tzinfo" and print with localtime amends six minutes

8👍

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

Leave a comment