Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#29514 closed Bug (fixed)

get_default_timezone() for UTC is no longer equivalent to timezone.utc

Reported by: Carlton Gibson Owned by: Tim Graham <timograham@…>
Component: Utilities Version: 2.1
Severity: Release blocker Keywords:
Cc: Sergey Fedoseev Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

CI testing on Django REST Framework revealed a change in behaviour in timezone.get_default_timezone().

The essence is:

with override_settings(USE_TZ=True, TIME_ZONE='UTC'):
    self.assertIs(timezone.get_default_timezone(), timezone.utc)

This passes fails 2.0.x and (currently) fails on 2.1/master.

Regression in 27ca5ce19f5f184018a61611c1bc319113b1d107.

Change History (9)

comment:1 by Carlton Gibson, 7 years ago

Has patch: set

comment:2 by Tim Graham, 7 years ago

Cc: Sergey Fedoseev added
Summary: Regression in get_default_timezone()get_default_timezone() for UTC is no longer equivalent to timezone.utc

I'm not sure. It seems reasonable to fix the problem but having get_default_timezone() return a different type for a one-off case might lead to some issue.

While investing, I noticed that pytz.utc is described as "Optimized UTC implementation." yet the offending commit in Django says "Used datetime.timezone.utc instead of pytz.utc for better performance."

comment:3 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

The issue is also present anywhere a timezone is constructed with pytz.timezone(), e.g. timezone.activate():

from django.conf import settings
settings.configure(USE_TZ=True)
from django.utils.timezone import get_current_timezone, utc
from django.utils import timezone
timezone.activate('UTC')
get_current_timezone() is utc  # False in Django 2.1, True in older versions

If we accept the proposed patch, I'd think we would also want to fix the issue there, and perhaps add a backwards incompatible release note for anyone using pytz.timezone() in their code. It might be better to reconsider the original patch.

comment:4 by Carlton Gibson, 7 years ago

Owner: Carlton Gibson removed
Status: assignednew

I'm going to (temporarily) deassign myself here, as I will be unable to work on this for the next week. Happy to pick it up if it's still pending after that.

comment:5 by Tim Graham <timograham@…>, 6 years ago

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In 2ec151e3:

Fixed #29514 -- Reverted "Used datetime.timezone.utc instead of pytz.utc for better performance."

This reverts commit 27ca5ce19f5f184018a61611c1bc319113b1d107 due to a
regression.

comment:6 by Tim Graham <timograham@…>, 6 years ago

In f4ef71c6:

Refs #29514 -- Added test for get_default_timezone()/timezone.utc equality.

comment:7 by Daniel Hahler, 6 years ago

Just for information:

While investing, I noticed that pytz.utc is ​described as "Optimized UTC implementation." yet the ​offending commit in Django says "Used datetime.timezone.utc instead of pytz.utc for better performance."

The performance improvement basically comes from datetime being implemented in C completely, while using pytz.utc calls into Python code (at least) 2 times (for utcoffset/dst).

comment:8 by Tim Graham <timograham@…>, 6 years ago

In 3a604024:

[2.1.x] Fixed #29514 -- Reverted "Used datetime.timezone.utc instead of pytz.utc for better performance."

This reverts commit 27ca5ce19f5f184018a61611c1bc319113b1d107 due to a
regression.

Backport of 2ec151e35da93047acfeea1b79c27010f2cb8594 from master

comment:9 by Tim Graham <timograham@…>, 6 years ago

In 2784579:

[2.1.x] Refs #29514 -- Added test for get_default_timezone()/timezone.utc equality.

Backport of f4ef71c689b7bf3b0237d12c634fe5557f646a79 from master

Note: See TracTickets for help on using tickets.
Back to Top