Merge pull request #1097 from tcely/patch-15

Add `timedelta` filter
This commit is contained in:
meeb 2025-06-11 20:44:56 +10:00 committed by GitHub
commit fb7d8d20a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,6 @@
from django import template
from django.template.defaultfilters import filesizeformat
from math import ceil
register = template.Library()
@ -23,3 +24,64 @@ def sub(value, arg):
except Exception:
return ""
@register.filter
def timedelta(value, arg=None, /, *, fmt_2=None):
if hasattr(value, 'total_seconds') and callable(value.total_seconds):
seconds_total = value.total_seconds()
elif hasattr(value, 'seconds'):
seconds_total = value.seconds + (value.days * 24 * 60 * 60)
else:
seconds_total = value
dynamic_arg = False
if arg is None:
if seconds_total < 1.0:
return f'{seconds_total:.6f} seconds'
dynamic_arg = True
arg = '{hours2}:{minutes2}:{seconds2}'
if fmt_2 is None:
fmt_2 = '{:02d}'
seconds_total = ceil(seconds_total)
seconds = seconds_total % 60
minutes_total = seconds_total // 60
minutes = minutes_total % 60
hours_total = minutes_total // 60
hours = hours_total % 24
days_total = hours_total // 24
days = days_total % 365
years_total = days_total // 365
years = years_total
if dynamic_arg:
prefix_years = prefix_days = ''
if years_total > 0:
prefix_years = '{years_total} years, '
if prefix_years and days_total > 0:
prefix_days = '{days} days, '
elif days_total > 0:
prefix_days = '{days_total} days, '
arg = prefix_years + prefix_days + arg
return arg.format(**{
'seconds': seconds,
'seconds2': fmt_2.format(seconds),
'minutes': minutes,
'minutes2': fmt_2.format(minutes),
'hours': hours,
'hours2': fmt_2.format(hours),
'days': days,
'years': years,
'seconds_total': seconds_total,
'minutes_total': minutes_total,
'hours_total': hours_total,
'days_total': days_total,
'years_total': years_total,
})