[Django] #28323: Redundant use of `normalize` in `django.utils.localtime`

18 views
Skip to first unread message

Django

unread,
Jun 19, 2017, 5:03:44 PM6/19/17
to django-...@googlegroups.com
#28323: Redundant use of `normalize` in `django.utils.localtime`
---------------------------------------------+------------------------
Reporter: Sergey Zherevchuk | Owner: nobody
Type: Uncategorized | Status: new
Component: Utilities | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
I'm not 100% sure , but I didn't find any test on this and can't think of
any.
What the purpose of using `timezone.normalize` method after
`dt.astimezone` if pytz installed?

Current implementation of `datetime.astimezone` (according to python 2/3
docs):

{{{
def astimezone(self, tz):
if self.tzinfo is tz:
return self
# Convert self to UTC, and attach the new time zone object.
utc = (self - self.utcoffset()).replace(tzinfo=tz)
# Convert from UTC to tz's local time.
return tz.fromutc(utc)
}}}

The implementation of `normalize` method for StaticTzInfo is `astimezone`:

{{{
if dt.tzinfo is self:
return dt
if dt.tzinfo is None:
raise ValueError('Naive time - no tzinfo set')
return dt.astimezone(self)
}}}

And for DstTzInfo `normalize` implementation is the same as
`datetime.astimezone`:

{{{
if dt.tzinfo is None:
raise ValueError('Naive time - no tzinfo set')

# Convert dt in localtime to UTC
offset = dt.tzinfo._utcoffset
dt = dt.replace(tzinfo=None)
dt = dt - offset
# convert it back, and return it
return self.fromutc(dt)
}}}

I've checked pytz 2011k and 2017.2 versions, looks like this code has
never changed.

Also I found this conversation
https://answers.launchpad.net/pytz/+question/249229

--
Ticket URL: <https://code.djangoproject.com/ticket/28323>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 19, 2017, 6:26:19 PM6/19/17
to django-...@googlegroups.com
#28323: Redundant use of normalize() in django.utils.timezone.localtime()
--------------------------------------+------------------------------------

Reporter: Sergey Zherevchuk | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

[https://github.com/django/django/pull/8659 PR] -- the tests seems to
pass, so unless we're missing a test case to show why `normalize()` is
needed, I'd think your analysis is correct.

--
Ticket URL: <https://code.djangoproject.com/ticket/28323#comment:1>

Django

unread,
Jun 20, 2017, 7:34:57 AM6/20/17
to django-...@googlegroups.com
#28323: Redundant use of normalize() in django.utils.timezone.localtime() and
make_naive()
--------------------------------------+------------------------------------

Reporter: Sergey Zherevchuk | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: 1.10

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/28323#comment:2>

Django

unread,
Jun 20, 2017, 7:35:33 AM6/20/17
to django-...@googlegroups.com
#28323: Redundant use of normalize() in django.utils.timezone.localtime() and
make_naive()
--------------------------------------+------------------------------------
Reporter: Sergey Zherevchuk | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Utilities | Version: 1.10
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"bdf20c383f1a08248ec4be6cfc972c12dcbded04" bdf20c38]:
{{{
#!CommitTicketReference repository=""
revision="bdf20c383f1a08248ec4be6cfc972c12dcbded04"
Fixed #28323 -- Removed unneeded normalize() in timezone.localtime() and
make_naive().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28323#comment:3>

Django

unread,
Jun 25, 2017, 3:38:17 PM6/25/17
to django-...@googlegroups.com
#28323: Redundant use of normalize() in django.utils.timezone.localtime() and
make_naive()
--------------------------------------+------------------------------------
Reporter: Sergey Zherevchuk | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Utilities | Version: 1.10

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Aymeric Augustin):

I was merely following the pytz docs, before they were updated in version
2016.4.

--
Ticket URL: <https://code.djangoproject.com/ticket/28323#comment:4>

Reply all
Reply to author
Forward
0 new messages