How Long Ago

From CodeCodex

This script displays an abbreviated description of some interval in the past. Invoke it as

ago seconds

Where seconds is the number of seconds ago. For instance

ago 60

will output “1 minute ago”,

ago 200000

will output “2 days ago”, and so on.

Python[edit]

import sys

units = \
    ( # sequence of triples, first element is multiplier to apply to
      # previous triple, or nr of seconds for first triple, second element
      # is singular unit name, third element is plural unit name.
        (1, "second", "seconds"),
        (60, "minute", "minutes"),
        (60, "hour", "hours"),
        (24, "day", "days"),
        (7, "week", "weeks"),
    )

assert(len(sys.argv) == 2)
interval = int(sys.argv[1])
for unitindex, unit = enumerate(units) :
    factor = unit[0]
    if interval < factor  :
        unit = units[unitindex - 1]
        break
    #end if
    interval /= factor
#end for
sys.stdout.write("%d %s ago\n" % (interval, unit[1:3][interval != 1]))