| 69 | |
| 70 | def naturalday(value, arg=None): |
| 71 | """ |
| 72 | For date values that are tomorrow, today or yesterday compared to |
| 73 | present day returns representing string. Otherwise, returns a string |
| 74 | formatted according to settings.DATE_FORMAT. |
| 75 | """ |
| 76 | try: |
| 77 | value = date(value.year, value.month, value.day) |
| 78 | except AttributeError: |
| 79 | # Passed value wasn't a date object |
| 80 | return value |
| 81 | except ValueError: |
| 82 | # Date arguments out of range |
| 83 | return value |
| 84 | today = date.today() |
| 85 | delta = timedelta(days=1) |
| 86 | if value == today: |
| 87 | return _('today') |
| 88 | elif value == today + delta: |
| 89 | return _('tomorrow') |
| 90 | elif value == today - delta: |
| 91 | return _('yesterday') |
| 92 | return defaultfilters.date(value, arg) |
| 93 | register.filter(naturalday) |