Opened 18 years ago

Closed 17 years ago

#3311 closed enhancement (fixed)

New humanize filter "naturalday" (yesterday, today, tomorrow)

Reported by: exogen@… 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)

natural_day_r4537.diff (3.7 KB ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 18 years ago.
naturalday patch incl. tests
naturalday_docs_r4537.diff (658 bytes ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 18 years ago.
Documentation for naturalday
naturalday_docs_v2.diff (666 bytes ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 18 years ago.
ReST typo fixed
naturalday_docs_v3.diff (817 bytes ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 18 years ago.
New docs for latest changes
natural_day_tests_v3.diff (2.5 KB ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 18 years ago.
Tests against v3 patch
natural_day_v3.diff (1.4 KB ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 18 years ago.
New patch
natural_day_v4.diff (1.4 KB ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 18 years ago.
Naturalday patch with the time delta
naturalday_unicode.patch (4.1 KB ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 17 years ago.
Merged patch with latest trunk and made it use unicode gettext
naturalday_unicode-5722.patch (3.6 KB ) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 17 years ago.
Unicode-aware patch (fixed some typos) against 5722

Download all attachments as: .zip

Change History (21)

comment:1 by mir@…, 18 years ago

Summary: [Code] New humanize filter "naturalday" (yesterday, today, tomorrow)New humanize filter "naturalday" (yesterday, today, tomorrow)
Triage Stage: UnreviewedDesign decision needed

comment:2 by Adrian Holovaty, 18 years ago

Triage Stage: Design decision neededAccepted

Sounds good to me. Could you provide a patch plus tests and docs?

comment:3 by Gary Wilson <gary.wilson@…>, 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.

by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Attachment: natural_day_r4537.diff added

naturalday patch incl. tests

comment:4 by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Cc: jyrki.pulliainen@… 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

by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Attachment: naturalday_docs_r4537.diff added

Documentation for naturalday

by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Attachment: naturalday_docs_v2.diff added

ReST typo fixed

comment:5 by Gary Wilson <gary.wilson@…>, 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.

by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Attachment: naturalday_docs_v3.diff added

New docs for latest changes

by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Attachment: natural_day_tests_v3.diff added

Tests against v3 patch

by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Attachment: natural_day_v3.diff added

New patch

comment:6 by Jyrki Pulliainen <jyrki.pulliainen@…>, 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.

comment:7 by Brian Beck <exogen@…>, 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!

by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago

Attachment: natural_day_v4.diff added

Naturalday patch with the time delta

in reply to:  7 comment:8 by Jyrki Pulliainen <jyrki.pulliainen@…>, 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 Chris Beaven, 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 Brian Rosner <brosner@…>, 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 Jyrki Pulliainen <jyrki.pulliainen@…>, 17 years ago

Attachment: naturalday_unicode.patch added

Merged patch with latest trunk and made it use unicode gettext

by Jyrki Pulliainen <jyrki.pulliainen@…>, 17 years ago

Unicode-aware patch (fixed some typos) against 5722

comment:11 by Simon G. <dev@…>, 17 years ago

Triage Stage: AcceptedReady for checkin

Last version looks good - tests, docs & everything.

comment:12 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(In [5985]) Fixed #3311 -- Added naturalday filter to contrib.humanize. Thanks, Jyrki Pulliainen.

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