"""Utility functions.""" from datetime import datetime def date_to_relative(d): """Converts the given datetime object to relative string representation, such as "2 days ago", etc. :param d: The datetime object to convert. :return: A string representing the given date relative to current time. """ diff = 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)