Opened 13 years ago
Closed 13 years ago
#17937 closed Bug (fixed)
timeuntil doesn't work with datetime.date objects
Reported by: | 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)
Change History (11)
comment:1 by , 13 years ago
Summary: | django.utils.timezone methods don't work with datetime.date objects → django.utils.timezone methods doesn't work with datetime.date objects |
---|
comment:2 by , 13 years ago
Component: | Uncategorized → Template system |
---|---|
Has patch: | unset |
Severity: | Normal → Release blocker |
Triage Stage: | Unreviewed → Accepted |
by , 13 years ago
Attachment: | timezone.diff added |
---|
Objects of date class should be always naive according to python docs
comment:3 by , 13 years ago
Has patch: | set |
---|
follow-ups: 5 6 comment:4 by , 13 years ago
Needs tests: | set |
---|---|
Owner: | changed from | to
Patch needs improvement: | set |
The patch is at 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.
comment:5 by , 13 years ago
Replying to aaugustin:
Thanks for the patch. Unfortunately, I think it puts the check in the wrong layer --
is_aware
andis_naive
are only intended to receivedatetime
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 , 13 years ago
Attachment: | timeuntil2.diff added |
---|
comment:6 by , 13 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Replying to aaugustin:
Thanks for the patch. Unfortunately, I think it puts the check in the wrong layer --
is_aware
andis_naive
are only intended to receivedatetime
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 , 13 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
comment:8 by , 13 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
Summary: | django.utils.timezone methods doesn't work with datetime.date objects → timeuntil 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
.
Indeed, the docs clearly mean that timesince accepts either a date or a datetime.