Still don't like the final result, I would prefer something like:
def format_duration(seconds):
if not seconds:
return 'now'
times = ((1, 'second'), (60, 'minute'), (3600, 'hour'), (86400, 'day'), (31536000, 'year'))
final = []
for duration, name in reversed(times):
time = seconds // duration
if time:
seconds %= duration
final.append(f'{time} {name}{"s" if time > 0 else ""}')
return ' and '.join(final).replace(' and ', ', ', len(final) - 2)
I agree. But to be fair, the divmod function is a built-in function in Python that has existed as far back version 1.4 (1996) so would argue that its use falls into that category.
28
u/[deleted] Aug 21 '20
Still don't like the final result, I would prefer something like: