from datetime import datetime


def year_fraction(date):
    """Obtain the fraction of the year that a given date represents.

    Args:
        date (datetime): a datetime object.

    Returns:
        float representing the fraction of the year.

    """
    year = date.year
    this_year_start = datetime(year=year, month=1, day=1)
    next_year_start = datetime(year=year+1, month=1, day=1)
    days_elapsed = date.timetuple().tm_yday - 0.5
    days_total = (next_year_start - this_year_start).days
    return days_elapsed/days_total