import calendar


def which_ten(d):
    """
    Determine which ten-day period a date falls in.

    :type d: datetime.date
    :param d: day of date, e.g. 2016-01-02 in format %Y-%m-%d is 2
    :return: an integer that is the prefix of the relevant ten-day period.
    """
    if not calendar.monthrange(d.year, d.month)[1] >= d.day > 0:
        raise RuntimeError('Out of range date')

    if d.day < 10:
        return 0
    elif d.day < 20:
        return 1
    elif d.day < 30:
        return 2
    else:
        return 3