Created
May 24, 2013 02:45
-
-
Save wladston/5640961 to your computer and use it in GitHub Desktop.
Python relative time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
from dateutil import relativedelta | |
from datetime import datetime | |
from dateutil.tz import tzutc, tzlocal | |
def humanize_time(time): | |
''' | |
get a datetime object and return a relative time string like | |
"one hour ago", "yesterday", "3 months ago", "just now", etc. | |
''' | |
rd = relativedelta.relativedelta(datetime.now(tzutc()), time) | |
def line(number, unit): | |
if abs(number) < 10 and unit == "seconds": | |
return "just now" | |
if number == 1 and unit == "days": | |
return 'yesterday' | |
if number == -1 and unit == "days": | |
return "tomorrow" | |
prefix, suffix = '', '' | |
unit = unit if abs(number) > 1 else unit[:-1] # Unpluralizing. | |
if number > 0: | |
suffix = " ago" | |
else: | |
prefix = "in " | |
return "%s%d %s%s" % (prefix, abs(number), unit, suffix) | |
for attr in ['years', 'months', 'days', 'hours', 'minutes', 'seconds']: | |
value = getattr(rd, attr) | |
if value != 0: | |
return line(value, attr) | |
return "just now" | |
class UtilsTest(unittest.TestCase): | |
def test_humanize_time(self): | |
now = time = datetime.now(tzlocal()) | |
self.assertEqual('just now', humanize_time(time)) | |
time = now - timedelta(seconds=45) | |
self.assertEqual('45 seconds ago', humanize_time(time)) | |
time = now - timedelta(seconds=60) | |
self.assertEqual('1 minute ago', humanize_time(time)) | |
time = now - timedelta(seconds=15*60) | |
self.assertEqual('15 minutes ago', humanize_time(time)) | |
time = now - timedelta(hours=1) | |
self.assertEqual('1 hour ago', humanize_time(time)) | |
time = now - timedelta(hours=10) | |
self.assertEqual('10 hours ago', humanize_time(time)) | |
time = now - timedelta(days=1) | |
self.assertEqual('yesterday', humanize_time(time)) | |
time = now - timedelta(days=2) | |
self.assertEqual('2 days ago', humanize_time(time)) | |
time = now - timedelta(days=31) | |
self.assertEqual('1 month ago', humanize_time(time)) | |
time = now - timedelta(days=31*2) | |
self.assertEqual('2 months ago', humanize_time(time)) | |
time = now - timedelta(days=366) | |
self.assertEqual('1 year ago', humanize_time(time)) | |
time = now - timedelta(days=366*2) | |
self.assertEqual('2 years ago', humanize_time(time)) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment