Opened 13 years ago

Closed 13 years ago

#17937 closed Bug (fixed)

timeuntil doesn't work with datetime.date objects

Reported by: dmitry.guyvoronsky@… Owned by: Aymeric Augustin
Component: Template system Version: 1.4-beta-1
Severity: Release blocker Keywords: timezone
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It seems a bug exists in several django.utils.timezone methods, is_aware() and is_native(). This leads to error "AttributeError: 'datetime.date' object has no attribute 'tzinfo'" when timesince filter is applied to date object in Django template. Proof code

import time
from datetime import date
today = date.today()

from django.template import Context, Template
c = Context({"today": today})
t = Template("{{ today|timeuntil }}")

print t.render(c)

Error trace:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print t.render(c)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/template/base.py", line 874, in render
    output = self.filter_expression.resolve(context)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/template/base.py", line 599, in resolve
    new_obj = func(obj, *arg_vals)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/template/defaultfilters.py", line 755, in timeuntil_filter
    return timeuntil(value, arg)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/utils/timesince.py", line 61, in timeuntil
    now = datetime.datetime.now(utc if is_aware(d) else None)
  File "/cygdrive/c/Users/dmitryg/.virtualenvs/feedback/lib/python2.6/site-packages/django/utils/timezone.py", line 243, in is_aware
    return value.tzinfo is not None and value.tzinfo.utcoffset(value) is not None
AttributeError: 'datetime.date' object has no attribute 'tzinfo'

Attachments (2)

timezone.diff (1.3 KB ) - added by dmitry.guyvoronsky@… 13 years ago.
Objects of date class should be always naive according to python docs
timeuntil2.diff (1.4 KB ) - added by dreamiurg 13 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 by anonymous, 13 years ago

Summary: django.utils.timezone methods don't work with datetime.date objectsdjango.utils.timezone methods doesn't work with datetime.date objects

comment:2 by Aymeric Augustin, 13 years ago

Component: UncategorizedTemplate system
Has patch: unset
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Indeed, the docs clearly mean that timesince accepts either a date or a datetime.

by dmitry.guyvoronsky@…, 13 years ago

Attachment: timezone.diff added

Objects of date class should be always naive according to python docs

comment:3 by anonymous, 13 years ago

Has patch: set

comment:4 by Aymeric Augustin, 13 years ago

Needs tests: set
Owner: changed from nobody to Aymeric Augustin
Patch needs improvement: set

Thanks for the patch. Unfortunately, I think it puts the check in the wrong layer -- is_aware and is_naive are only intended to receive datetime objects (or subclasses, or API-compatible classes).

The check should be in timesince instead.

The same bug probably exists in timeuntil and possibly in other filters.

Last edited 13 years ago by Aymeric Augustin (previous) (diff)

in reply to:  4 comment:5 by dreamiurg, 13 years ago

Replying to aaugustin:

Thanks for the patch. Unfortunately, I think it puts the check in the wrong layer -- is_aware and is_naive are only intended to receive datetime objects (or subclasses, or API-compatible classes).

The check should be in timesince instead.

The same bug probably exists in timeuntil and possibly in other filters.

'Yes, I was thinking about patching at timesince/timeuntil level, however, after considerations it seemed more logical to make changes at the bottom level instead.

by dreamiurg, 13 years ago

Attachment: timeuntil2.diff added

in reply to:  4 comment:6 by dreamiurg, 13 years ago

Owner: changed from Aymeric Augustin to dreamiurg
Status: newassigned

Replying to aaugustin:

Thanks for the patch. Unfortunately, I think it puts the check in the wrong layer -- is_aware and is_naive are only intended to receive datetime objects (or subclasses, or API-compatible classes).

The check should be in timesince instead.

The same bug probably exists in timeuntil and possibly in other filters.

Please see second version of the patch, this one fixes problem on timeuntil() level + adds unittests.

comment:7 by dreamiurg, 13 years ago

Needs tests: unset
Patch needs improvement: unset

comment:8 by Aymeric Augustin, 13 years ago

Owner: changed from dreamiurg to Aymeric Augustin
Status: assignednew
Summary: django.utils.timezone methods doesn't work with datetime.date objectstimeuntil doesn't work with datetime.date objects

In fact, this problem only affects timeuntil, not timesince — there are tests checking that timeuntil accepts date objects.

It's a little bit hard to explain the problem in English but the patch is self-explanatory. It just avoids duplicating the preprocessing of parguments that is done correctly in timesince and incorrectly in timeuntil.

comment:9 by Aymeric Augustin, 13 years ago

Resolution: fixed
Status: newclosed

In [17774]:

Fixed #17937 -- Avoided an error in the timeuntil filter when it receives a date object. Thanks Dmitry Guyvoronsky for the report.

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