in python/python-guestbook/src/frontend/front.py [0:0]
def format_duration(timestamp):
""" Format the time since the input timestamp in a human readable way """
now = datetime.datetime.fromtimestamp(time.time())
prev = datetime.datetime.fromtimestamp(timestamp)
rd = dateutil.relativedelta.relativedelta(now, prev)
for n, unit in [(rd.years, "year"), (rd.days, "day"), (rd.hours, "hour"),
(rd.minutes, "minute")]:
if n == 1:
return "{} {} ago".format(n, unit)
elif n > 1:
return "{} {}s ago".format(n, unit)
return "just now"