I'm currently generating the amount of time given the number of seconds. This is what I have come up with very quickly. It works well, but it's quite ugly.
I can't think of any tricks to make this more elegant (without making it complicated or rely on this and that), but maybe there's people here that have some tips.
def humanizeTime(seconds):
if seconds < 60:
return "%d seconds" % int(round(seconds))
else:
minutes = seconds / 60.0
if minutes < 60:
return "%d minutes %d seconds" % divmod(seconds, 60)
else:
hours = minutes / 60.0
if hours < 24:
return "%d hours %d minutes" % divmod(minutes, 60)
else:
days = hours / 24.0
if days < 7:
return "%d days" % int(round(days))
else:
weeks = days / 7.0
if weeks < 4:
return "%d weeks" % int(round(weeks))
else:
months = days / 30.0
if months < 12:
return "%d months" % int(round(months))
else:
return "%d years" % int(round(days / 365.0))
Edit
If there's a good library that could compute what I have above (which again, never exceeds 2 fields) with proper grammar, I would definitely jump on board.
Then again I can't find any that does that, as any library that can compute this will still require me to write code like this (or some of the answers shown below) to display only 2 fields max.