from datetime import datetime
import pytz


def semester_year_to_date(semester, year, ending=False):
    """
    Convert semester and year to a rough date

    Args:
        semester (str): Semester ("Fall", "Spring", etc)
        year (int): Year
        ending (boolean): True for end of semester, False for beginning

    Returns:
        datetime: The rough date of the course
    """
    if semester is None or year is None:
        return
    if semester.lower() == "fall":
        month_day = "12-31" if ending else "09-01"
    elif semester.lower() == "summer":
        month_day = "08-30" if ending else "06-01"
    elif semester.lower() == "spring":
        month_day = "05-31" if ending else "01-01"
    elif semester.lower() == "january iap":
        month_day = "01-31" if ending else "01-01"
    else:
        return
    return datetime.strptime("{}-{}".format(year, month_day), "%Y-%m-%d").replace(
        tzinfo=pytz.UTC
    )