public
Last active

Humanize date differences

  • Download Gist
datedifference.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/env python
# This function prints the difference between two python datetime objects
# in a more human readable form
#
# Adapted from: http://www.chimeric.de/blog/2008/0711_smart_dates_in_python
 
def humanize_date_difference(now, otherdate=None, offset=None):
if otherdate:
dt = otherdate - now
offset = dt.seconds + (dt.days * 60*60*24)
if offset:
delta_s = offset % 60
offset /= 60
delta_m = offset % 60
offset /= 60
delta_h = offset % 24
offset /= 24
delta_d = offset
else:
raise ValueError("Must supply otherdate or offset (from now)")
 
if delta_d > 1:
if delta_d > 6:
date = now + datetime.timedelta(days=-delta_d, hours=-delta_h, minutes=-delta_m)
return date.strftime('%A, %Y %B %m, %H:%I')
else:
wday = now + datetime.timedelta(days=-delta_d)
return wday.strftime('%A')
if delta_d == 1:
return "Yesterday"
if delta_h > 0:
return "%dh%dm ago" % (delta_h, delta_m)
if delta_m > 0:
return "%dm%ds ago" % (delta_m, delta_s)
else:
return "%ds ago" % delta_s

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.