| 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 | delta = value - date.today() |
| 85 | if delta.days == 0: |
| 86 | return _('today') |
| 87 | elif delta.days == 1: |
| 88 | return _('tomorrow') |
| 89 | elif delta.days == -1: |
| 90 | return _('yesterday') |
| 91 | return defaultfilters.date(value, arg) |
| 92 | register.filter(naturalday) |