@register.filter
def format_duration(seconds):
  if not seconds:
    return seconds
  seconds = int(seconds)
  prefix = ''
  if seconds < 0:
    prefix = '-'
    seconds = -seconds
  minutes = math.floor(seconds / 60)
  seconds -= minutes * 60
  hours = math.floor(minutes / 60)
  minutes -= hours * 60
  days = math.floor(hours / 24)
  hours -= days * 24
  out = []
  if days > 0:
    out.append('%dd' % days)
  if hours > 0 or days > 0:
    out.append('%02dh' % hours)
  if minutes > 0 or hours > 0 or days > 0:
    out.append('%02dm' % minutes)
  if seconds > 0 and not out:
    # Skip seconds unless there's only seconds.
    out.append('%02ds' % seconds)
  return prefix + ''.join(out).lstrip('0')
Convert a number of seconds into human readable compact string.
dataset/ETHPy150Open rietveld-codereview/rietveld/codereview/library.py/format_duration