I have this function that converts a timestamp to a datetime object that says either; "Today at 10:56 pm" or "Yesterday at 1:06 am" or others...
I'm having some issues with it currently.
otherdate = 1504246379
now = time.time()
if otherdate:
dt = now - otherdate
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 + timedelta(days=-delta_d, hours=-delta_h, minutes=-delta_m)
return date.strftime('%A, %Y %B %m, %H:%I')
else:
wday = now + 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
The current error I receive is:
Traceback (most recent call last):
File "test.py", line 69, in <module>
date = get_long_date(timestamp)
File "test.py", line 40, in get_long_date
offset = dt.seconds + (dt.days * 60 * 60 * 24)
AttributeError: 'float' object has no attribute 'seconds'
So I went to add:
dt = timedelta(dt)
and that clears the error, however if I print the delta_d (days offset) its a negative number... Can someone help me complete my function?
Thank you,
Denis Angell