from datetime import datetime


def get_year_ticks(start_date, end_date, max_ticks=5):
    """ Get `max_ticks` or less evenly distributed yearly ticks, including
    start and end years. All ticks fall on January 1.
    """

    years = range(start_date.year, end_date.year + 1)
    n_years = len(years)
    max_ticks = min(max_ticks, n_years)
    # Avoid N2 < ticks < N, where there will be odd looking gaps
    if n_years > 3:
        if round(n_years / 2) < max_ticks < n_years:
            max_ticks = round(n_years / 2)

    # -2 for the ends
    # +1 because cutting a cake in n+1 pieces gives n cuts
    if max_ticks > 1:
        cuts = n_years / (max_ticks - 2 + 1)
    else:
        cuts = 0
    selected_years = [years[int(x * cuts)] for x in range(0, max_ticks - 1)]

    # add last year
    if max_ticks > 0:
        selected_years.append(years[-1])

    # Ticks should be on the first day of the year
    selected_dates = [datetime(y, 1, 1) for y in selected_years]
    return selected_dates