pytimeago

Human-oriented representation of time deltas, a Python library.

Gmail has similar feature

Showing arrival time Gmail-style

Here's how you would do this with pytimeago assuming msg is email message object and msg.time is arriveal timestamp:

from pytimeago.english import english
from time import strftime, time
line = "%s (%s)" % \
   (strftime("%b %d", msg.time),
    english(time()-msg.time()))

And then pass line to the template.

First, I shall define what time delta is. Since Δ generally means “difference”, we take time delta to stand for difference between two events in time. Quite common, however, is to have the current time for the second event. In pytimeago we deal with time deltas from such a perspective.

Quite possibly, you can make use of this library if your application displays some dynamically generated items to the user, and you are looking for a good way to present information on how long ago specific item was created/processed/whatever.

Below is small code snippet demonstrating how simple it is to embed pytimeago:

from pytimeago.english import english
from time import time

message = queue.getNextMessage()
delta = time() - message.arrived_time
print "Message arrived %s" % english(delta)

Prints, e.g.

Message arrived 15mins ago

As you see, pytimeago is package, and has individual modules for every language supported. As of 2006-08-13 the only supported language is English. However, you can take a look at rather trivial implementation of English engine, write one for your language, and send it to to me (email at the bottom of page).

Every language should come with a set of doctests (I prefer them to casual unit tests), just like the English version does. Don't be too verbose, but check essential cases.

pytimeago is licenced under LGPL. I'd be happy to hear if you use this in your software. Note that language module(s) can also be taken standalone to other projects, if desired.

Getting pytimeago

You can download current version as a tarball.

You can then run doctests for English engine, e.g.

$ python pytimeago/english.py -v
...
22 tests in 2 items.
22 passed and 0 failed.
Test passed.

Note: be sure to put pytimeago on your include path, either by copying pytimeago directory to one of directories in sys.path, or manually adding the right directory to that list.

Return to the homepage.