import calendar


def year_add(date, years):
    """Add number of years to date.

    >>> import datetime
    >>> year_add(datetime.datetime(2016, 2, 29), 1)
    datetime.date(2017, 2, 28)
    >>> year_add(datetime.date(2016, 2, 29), 1)
    datetime.date(2017, 2, 28)
    >>> year_add(datetime.date(2015, 2, 28), 1)
    datetime.date(2016, 2, 28)
    >>> year_add(datetime.date(2017, 2, 28, -1)
    datetime.date(2016, 2, 28)
    >>> year_add(datetime.datetime(2016, 2, 29), -1)
    datetime.datetime(2015, 2, 28)
    """
    if date.day == 29 and date.month == 2 and not calendar.isleap(date.year + years):
        return date.replace(day=28, year=date.year + years)
    return date.replace(year=date.year + years)