Created
October 11, 2009 11:38
-
-
Save nzjrs/207624 to your computer and use it in GitHub Desktop.
Humanize date differences
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe that line 9 should be
dt = now - otherdate
Thanks for sharing the gist!