Opened 18 years ago
Closed 17 years ago
#3311 closed enhancement (fixed)
New humanize filter "naturalday" (yesterday, today, tomorrow)
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Template system | Version: | |
Severity: | normal | Keywords: | |
Cc: | jyrki.pulliainen@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Just something I think people might want in humanize or defaultfilters. This will return 'yesterday', 'today', and 'tomorrow' when the given date has the appropriate relation with the current date. Otherwise, return the full day name. Maybe it could even be extended to do 'last Tuesday', 'this Tuesday', or 'next Tuesday' when appropriate. Anyway the idea is that it is very common to hear this method of communicating the day in speech.
import datetime def naturalday(value): today = datetime.date.today() value = datetime.date(value.year, value.month, value.day) delta = datetime.timedelta(days=1) if value == today: return 'today' elif value == today + delta: return 'tomorrow' elif value == today - delta: return 'yesterday' return value.strftime('%A')
Attachments (9)
Change History (21)
comment:1 by , 18 years ago
Summary: | [Code] New humanize filter "naturalday" (yesterday, today, tomorrow) → New humanize filter "naturalday" (yesterday, today, tomorrow) |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:2 by , 18 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:3 by , 18 years ago
Has patch: | set |
---|---|
Needs documentation: | set |
Needs tests: | set |
Patch needs improvement: | set |
I wouldn't think you would want to output just the day of the week for dates and datetimes more/less than 1 day away/ago. Maybe it should instead default to settings.DATE_FORMAT
or settings.DATETIME_FORMAT
, also taking an optional date format string that would override the default.
comment:4 by , 18 years ago
Cc: | added |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
Added a patch including unittests. Presented patch now does the fallback formatting according to settings.DATE_FORMAT. If passed value is not a date value, it returns the value without throwing an exception
comment:5 by , 18 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
I would suggest allowing a format string to be passed in as well and call django.template.defaultfilters.date()
when the date is not yesterday, today, or tomorrow. That way, the default settings.DATE_FORMAT
could be overridden in the template just as with the date
template filter.
comment:6 by , 18 years ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
New patch, uses now the defaultfilter date().
Argument using documented and new tests added.
follow-up: 8 comment:7 by , 18 years ago
This is pretty minor, but I have a hunch it would be a little quicker to do:
delta = value - today if delta.days == 0: return _('today') if delta.days == 1: return _('tomorrow') elif delta.days == -1: return _('yesterday')
...so that only one delta is calculated and less comparisons will happen (no date.__eq__/__cmp__
).
P.S. Awesome job on the patches Jyrki!
comment:8 by , 18 years ago
Replying to Brian Beck <exogen@gmail.com>:
...so that only one delta is calculated and less comparisons will happen (no
date.__eq__/__cmp__
).
Changed the code to do less compares. Thank you for the tip
P.S. Awesome job on the patches Jyrki!
Thanks!
comment:9 by , 18 years ago
I'm not up with the play on the whole gettext
side of things, but I thought I remembered there is a move away from using _
as the name for gettext
. Or maybe not... just checking.
comment:10 by , 17 years ago
FYI, this patch needs to get updated to trunk. The unicode branch is now requiring a slight change.
from django.utils.translation import gettext as _
to
from django.utils.translation import ugettext as _
by , 17 years ago
Attachment: | naturalday_unicode.patch added |
---|
Merged patch with latest trunk and made it use unicode gettext
by , 17 years ago
Attachment: | naturalday_unicode-5722.patch added |
---|
Unicode-aware patch (fixed some typos) against 5722
comment:11 by , 17 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Last version looks good - tests, docs & everything.
comment:12 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Sounds good to me. Could you provide a patch plus tests and docs?