Find the latest legitimate day.

def replace_u_end_day(day, year, month):
    """Find the latest legitimate day."""
    day = day.lstrip('-')
    year = int(year)
    month = int(month.lstrip('-'))
    if day == 'uu' or day == '3u':
        # Use the last day of the month for a given year/month.
        return str(calendar.monthrange(year, month)[1])
    if day == '0u' or day == '1u':
        return day.replace('u', '9')
    if day == '2u' or day == 'u9':
        if month != '02' or calendar.isleap(year):
            return '29'
        elif day == '2u':
            # It is Feburary and not a leap year.
            return '28'
        else:
            # It is February, not a leap year, day ends in 9.
            return '19'
    # 'u2' 'u3' 'u4' 'u5' 'u6' 'u7' 'u8'
    if 1 < int(day[1]) < 9:
        return day.replace('u', '2')
    # 'u0' 'u1'
    if day == 'u1':
        if calendar.monthrange(year, month)[1] == 31:
            # See if the month has a 31st.
            return '31'
        else:
            return '21'
    if day == 'u0':
        if calendar.monthrange(year, month)[1] >= 30:
            return '30'
    else:
        return '20'