Fixing a Python Function that returns a human readable time difference on FreeWork Exchange

Fixing a Python Function that returns a human readable time difference

0

Given below is a (poorly documented, but hopefully self-explanatory) python function that takes two datetime objects and returns the time-difference between the both of them i n a format we humans can understand, say '10 minutes ago' or '2 days ago', etc. 
But occasionally it returns inaccurate time-differences, like '23 hours ago' instead of '0 seconds ago'. 

The task here is to fix the function to handle all edge cases and possibly improve its efficiency.

from datetime import datetime
def timedelta(frm, to=datetime.utcnow()):
    '''Returns the human readable time between two datetimes'''
    if frm==to: return "0 seconds"
    det = (to-frm).days
    s=""
    if det>0:
        if det>365:
            det /= 365
            if  det>1: s="s"
            return str(det)+" year"+s
        if det>30:
            det/=30
            if  det>1: s="s"
            return str(det)+" month"+s
        if det>7:
            det/=7
            if  det>1: s="s"
            return str(det)+" week"+s
        if det>1:  s="s"
        return str(det)+" day"+s
    det = (to-frm).seconds
    if det>=3600:
        det/=3600
        if  det>1: s="s"
        return str(det)+" hour"+s
    if det>=60:
        det/=60
        if  det>1: s="s"
        return str(det)+" minute"+s
    if  det!=1: s="s"
    return str(det)+" second"+s

misc 5 years ago,
Sathvik(25)



Please Sign-in or Register to solve this problem