Return he d'th day (0 <= d <= 6) of week n of month m of the  year.

    (1 <= n <= 5, 1 <= m <= 12,  where  week 5 means "the last d day in
    month m" which may  occur in  either  the  fourth  or  the  fifth week).
    Week 1 is  the  first  week  in which the d'th day occurs.  Day zero is
    Sunday.

    :param year:
    :param m: month
    :param n: week number
    :param d: day (0 <= d <= 6) of week
    :return: datetime

def dth_day_of_week_n(y, m, n, d):
    """Return he d'th day (0 <= d <= 6) of week n of month m of the  year.

    (1 <= n <= 5, 1 <= m <= 12,  where  week 5 means "the last d day in
    month m" which may  occur in  either  the  fourth  or  the  fifth week).
    Week 1 is  the  first  week  in which the d'th day occurs.  Day zero is
    Sunday.

    :param year:
    :param m: month
    :param n: week number
    :param d: day (0 <= d <= 6) of week
    :return: datetime
    """
    if n == 5:
        # Compute the last dow of the month.
        y, m = next_month(y, m)
        dt = datetime(y, m, 1) - timedelta(1)
        # Move back to the given dow.
        dt -= timedelta((dt.weekday() - d + 1) % 7)
    else:
        dt = datetime(y, m, 1)
        dt += timedelta((d - dt.weekday() - 1) % 7 + 7 * (n - 1))
    return dt