import calendar


def truncate_to_month(day, month, year):
    """
    return the day given if its valid for the month + year,
    or the end of the month if the day exceeds the month's number of days
    """
    # enable passing of strings
    year = int(year)
    month = int(month)
    maxday = calendar.monthrange(year, month)[-1]
    if maxday < int(day):
        return maxday
    return day