@@ -209,10 +209,12 @@ def naturalday(value, arg=None):
# This filter doesn't require expects_localtime=True because it deals properly
# with both naive and aware datetimes. Therefore avoid the cost of conversion.
@register.filter
-def naturaltime(value):
+def naturaltime(value, use_in_phrasing
=False):
"""
For date and time values show how many seconds, minutes, or hours ago
- compared to current timestamp return representing string.
+ compared to current timestamp return representing string. If use_in_phrasing
+ is True, returns a string like 'in 5 hours' for future times, rather than
+ '5 hours from now'.
"""
if not isinstance(value, date): # datetime is a subclass of date
return value
@@ -271,22 +273,45 @@ def naturaltime(value):
elif delta.seconds == 0:
return _('now')
elif delta.seconds < 60:
- return ngettext(
- # Translators: please keep a non-breaking space (U+00A0)
- # between count and time unit.
- 'a second from now', '%(count)s seconds from now', delta.seconds
- ) % {'count': delta.seconds}
+ if use_in_phrasing:
+ # Note 'in a second' is not used because it has an idiomatic connotation of 'about'
+ return ngettext(
+ # Translators: please keep a non-breaking space (U+00A0)
+ # between count and time unit.
+ 'in 1 second', 'in %(count)s seconds', delta.seconds
+ ) % {'count': delta.seconds}
+ else:
+ return ngettext(
+ # Translators: please keep a non-breaking space (U+00A0)
+ # between count and time unit.
+ 'a second from now', '%(count)s seconds from now', delta.seconds
+ ) % {'count': delta.seconds}
elif delta.seconds // 60 < 60:
count = delta.seconds // 60
- return ngettext(
- # Translators: please keep a non-breaking space (U+00A0)
- # between count and time unit.
- 'a minute from now', '%(count)s minutes from now', count
- ) % {'count': count}
+ if use_in_phrasing:
+ # Note 'in a minute' is not used because it has an idiomatic connotation of 'about'
+ return ngettext(
+ # Translators: please keep a non-breaking space (U+00A0)
+ # between count and time unit.
+ 'in 1 minute', 'in %(count)s minutes', count
+ ) % {'count': count}
+ else:
+ return ngettext(
+ # Translators: please keep a non-breaking space (U+00A0)
+ # between count and time unit.
+ 'a minute from now', '%(count)s minutes from now', count
+ ) % {'count': count}
else:
count = delta.seconds // 60 // 60
- return ngettext(
- # Translators: please keep a non-breaking space (U+00A0)
- # between count and time unit.
- 'an hour from now', '%(count)s hours from now', count
- ) % {'count': count}
+ if use_in_phrasing:
+ return ngettext(
+ # Translators: please keep a non-breaking space (U+00A0)
+ # between count and time unit.
+ 'in an hour', 'in %(count)s hours', count
+ ) % {'count': count}
+ else:
+ return ngettext(
+ # Translators: please keep a non-breaking space (U+00A0)
+ # between count and time unit.
+ 'an hour from now', '%(count)s hours from now', count
+ ) % {'count': count}