def human_time_diff(stamp): diff = dbiface.to_unixepoch(stamp) - int(time.time()) assert isinstance(diff, int) # if this doesn't hold, we'll generate rubbish if diff == 0: return "now" if diff < 0: whence = "ago" diff = -diff # must be positive for identity: x == (x/y)*y + (x%y) elif diff > 0: whence = "more" humanbits = [] for divisor, units in ((60, "s"), (60, "m"), (24, "h"), (None, "d")): if divisor is not None: bitval = diff % divisor else: bitval = diff humanbits.append(("%d%s" % (bitval, units)) if bitval else None) del bitval if divisor is None: break diff //= divisor if diff == 0: break del diff # take the 2 most significant bits, filter the zeroed ones return "".join(filter(None, itertools.islice(reversed(humanbits), 2))) + " " + whence
def test_stamp_collation(self): #self.assertEqual(1267111819, to_unixepoch('Fri, 26 Feb 2010 02:30:19 EST')) self.assertEqual(1267112159, to_unixepoch('Thu, 25 Feb 2010 15:35:59 UTC'))