By default, display years and days only when needed

This commit is contained in:
tcely authored and GitHub committed 2025-06-08 06:46:59 -04:00
1 parent 5838ed52f9
commit 54932f4951
1 file changed
+13 -2
+13 -2
View File
@@ -35,9 +35,10 @@ def timedelta(value, arg=None, /, *, fmt_2=None):
seconds_total = value
if arg is None:
arg = '{days_total} days, {hours2}:{minutes2}:{seconds2}'
if seconds_total < 1.0:
arg = f'{seconds_total:.6f} seconds'
return f'{seconds_total:.6f} seconds'
dynamic_arg = True
arg = '{hours2}:{minutes2}:{seconds2}'
if fmt_2 is None:
fmt_2 = '{:02d}'
@@ -57,6 +58,16 @@ def timedelta(value, arg=None, /, *, fmt_2=None):
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 = '{total_days} days, '
arg = prefix_years + prefix_days + arg
return arg.format(**{
'seconds': seconds,
'seconds2': fmt_2.format(seconds),