def humantime(h,m,p,t):
    c = " past "
    m = 5*int((m+2.5)//5)
    if m > 30:
        m = 60 - m
        c = " to "
        h = h + 1
        if h == 12:
            if p == "AM":
                p ="PM"
            else:
                p ="AM"
        elif h == 13:
            h = 1
## ------ convert 12:00 to words
    if h == 12:
        if p == "AM":
            h = "midnight"
        else:
            h = "noon"
## ------ convert 0,15, 30 to words
    if m == 30:
        m = "half"
    elif m == 15:
        m = "quarter"
    elif m == 0:
        if h == "midnight" or h =="noon":
            m = ""
        else:
            m = "o'clock"

    if m == "o'clock" or m == "":
        r = str(h) +" " + str(m)
    else:
        r = str(m) + c + str(h)

    
    if p != t:
        if p == "PM" and h != "noon":
            #print "add afternoon"
            r = str(h) + ", in the afternoon"
        elif p == "AM" and h != "midnight":
            r = str(h) + ", in the morning"
    return (r)