diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2006-06-21 06:56:08 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2006-06-21 06:56:08 +0000 |
| commit | 239adf83d33c47ea6dd961904d79962cd7e5a65c (patch) | |
| tree | 8b5e9a87690b05b74dff89d6d8d128eaa398918c /django | |
| parent | c4fa8a158aa3ca0623c867ae67afc47b92277ed6 (diff) | |
Fixed #2053 -- added an optional comparison argument to the "timesince" filter.
Added a "timeuntil" filter that works analogously. Thanks, john@sneeu.com.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3185 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/template/defaultfilters.py | 15 | ||||
| -rw-r--r-- | django/utils/timesince.py | 5 |
2 files changed, 17 insertions, 3 deletions
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 453c34b0bd..9bd6e7cb3c 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -345,13 +345,25 @@ def time(value, arg=None): arg = settings.TIME_FORMAT return time_format(value, arg) -def timesince(value): +def timesince(value, arg=None): 'Formats a date as the time since that date (i.e. "4 days, 6 hours")' from django.utils.timesince import timesince if not value: return '' + if arg: + return timesince(arg, value) return timesince(value) +def timeuntil(value, arg=None): + 'Formats a date as the time until that date (i.e. "4 days, 6 hours")' + from django.utils.timesince import timesince + from datetime import datetime + if not value: + return '' + if arg: + return timesince(arg, value) + return timesince(datetime.now(), value) + ################### # LOGIC # ################### @@ -485,6 +497,7 @@ register.filter(stringformat) register.filter(striptags) register.filter(time) register.filter(timesince) +register.filter(timeuntil) register.filter(title) register.filter(truncatewords) register.filter(unordered_list) diff --git a/django/utils/timesince.py b/django/utils/timesince.py index bc4f969dc4..0b94d89bc6 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -47,10 +47,11 @@ def timesince(d, now=None): s += ', %d %s' % (count2, name2(count2)) return s -def timeuntil(d): +def timeuntil(d, now=None): """ Like timesince, but returns a string measuring the time until the given time. """ - now = datetime.datetime.now() + if now == None: + now = datetime.datetime.now() return timesince(now, d) |
