Opened 9 years ago

Closed 8 years ago

#3311 closed enhancement (fixed)

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

Reported by: exogen@… Owned by: adrian
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: UI/UX:

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@…> 9 years ago.
naturalday patch incl. tests
naturalday_docs_r4537.diff (658 bytes) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 9 years ago.
Documentation for naturalday
naturalday_docs_v2.diff (666 bytes) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 9 years ago.
ReST typo fixed
naturalday_docs_v3.diff (817 bytes) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 9 years ago.
New docs for latest changes
natural_day_tests_v3.diff (2.5 KB) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 9 years ago.
Tests against v3 patch
natural_day_v3.diff (1.4 KB) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 9 years ago.
New patch
natural_day_v4.diff (1.4 KB) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 9 years ago.
Naturalday patch with the time delta
naturalday_unicode.patch (4.1 KB) - added by Jyrki Pulliainen <jyrki.pulliainen@…> 8 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@…> 8 years ago.
Unicode-aware patch (fixed some typos) against 5722

Download all attachments as: .zip

Change History (21)

comment:1 Changed 9 years ago by mir@…

  • Summary changed from [Code] New humanize filter "naturalday" (yesterday, today, tomorrow) to New humanize filter "naturalday" (yesterday, today, tomorrow)
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 9 years ago by adrian

  • Triage Stage changed from Design decision needed to Accepted

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

comment:3 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • 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.

Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

naturalday patch incl. tests

comment:4 Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

  • 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

Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

Documentation for naturalday

Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

ReST typo fixed

comment:5 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • 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.

Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

New docs for latest changes

Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

Tests against v3 patch

Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

New patch

comment:6 Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

  • 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 follow-up: Changed 9 years ago by Brian Beck <exogen@…>

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!

Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

Naturalday patch with the time delta

comment:8 in reply to: ↑ 7 Changed 9 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

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 Changed 9 years ago by SmileyChris

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 Changed 8 years ago by Brian Rosner <brosner@…>

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 _

Changed 8 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

Merged patch with latest trunk and made it use unicode gettext

Changed 8 years ago by Jyrki Pulliainen <jyrki.pulliainen@…>

Unicode-aware patch (fixed some typos) against 5722

comment:11 Changed 8 years ago by Simon G. <dev@…>

  • Triage Stage changed from Accepted to Ready for checkin

Last version looks good - tests, docs & everything.

comment:12 Changed 8 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

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