Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Natural/Relative days in Python

I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.

Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more limited than what I'd like.

Please let me (and generations of future searchers) know if there is a good working solution already. Since this is a common enough task, I imagine there must be something.

like image 819
jamtoday Avatar asked Jan 04 '09 01:01

jamtoday


People also ask

How do you find relative time in python?

Display a human friendly, readable time based on a specified time period (date/time). def relative_time(date): """Take a datetime and return its "age" as a string. be returned.


1 Answers

Twitter dates in specific are interesting because they are relative only for the first day. After 24 hours they just show the month and day. After a year they start showing the last two digits of the year. Here's a sample function that does something more akin to Twitter relative dates, though it always shows the year too after 24 hours. It's US locale only, but you can always alter it as needed.

# tested in Python 2.7 import datetime def prettydate(d):     diff = datetime.datetime.utcnow() - d     s = diff.seconds     if diff.days > 7 or diff.days < 0:         return d.strftime('%d %b %y')     elif diff.days == 1:         return '1 day ago'     elif diff.days > 1:         return '{} days ago'.format(diff.days)     elif s <= 1:         return 'just now'     elif s < 60:         return '{} seconds ago'.format(s)     elif s < 120:         return '1 minute ago'     elif s < 3600:         return '{} minutes ago'.format(s/60)     elif s < 7200:         return '1 hour ago'     else:         return '{} hours ago'.format(s/3600) 
like image 123
Josh Segall Avatar answered Oct 04 '22 19:10

Josh Segall