根据 week_of_month 和 day_of_week 条件构造日期

Construct date based on week_of_month and day_of_week criteria

在给定 yearmonthweek_of_monthday_of_week 的情况下,我不确定如何构建 datetime 对象。有什么线索吗?使用这个我试图实现以下目标:

(start_month, start_year)(end_month, end_year) 查找 每月week_of_monthday_of_week 参数指定的日期。这里 1 <= week_of_month <= 51 <= day_of_week <= 7。现在,

  1. 每个月可能没有 5 周(例如,平年的 2 月)
  2. 第 1 周和第 5 周可能没有 7 天。

在这种情况下,基于布尔值 is_to_next_day,如果 True 则指定下一个日历日,如果 False 则跳过它。

样本input/outputs:

  1. 输入参数:start_month=1 start_year=2020, end_month=12, end_year=2020, week_of_month=5, day_of_week=3, is_to_next_day=True
    期望的输出:[datetime(2020, 1, 29), datetime(2020, 2, 26), datetime(2020, 3, 25), datetime(2020, 4, 29), datetime(2020, 5, 27), datetime(2020, 7, 1), datetime(2020, 7, 29), datetime(2020, 8, 26), datetime(2020, 9, 30), datetime(2020, 10, 28), datetime(2020, 11, 25), datetime(2020, 12, 30)]
  2. 输入参数:start_month=1 start_year=2020, end_month=12, end_year=2020, week_of_month=5, day_of_week=3, is_to_next_day=False
    期望的输出:[datetime(2020, 1, 29), datetime(2020, 2, 26), datetime(2020, 3, 25), datetime(2020, 4, 29), datetime(2020, 5, 27), datetime(2020, 7, 29), datetime(2020, 8, 26), datetime(2020, 9, 30), datetime(2020, 10, 28), datetime(2020, 11, 25), datetime(2020, 12, 30)]
import calendar
from datetime import datetime
def get_date(year, month, week_of_month, day_of_week, is_to_next_day):
    mnth = calendar.monthcalendar(year, month)
    if (week_of_month > 1) and (week_of_month < 5):
        day = mnth[week_of_month - 1][day_of_week - 1]
        return datetime(year, month, day)
    elif week_of_month == 1:
        last_day_of_first_week = mnth[0][6]
        if day_of_week <= last_day_of_first_week:
            return datetime(year, month, day_of_week)
        elif is_to_next_day:
            return datetime(year, month, mnth[1][0])
        else:
            return None
    else:
        if (len(mnth) >= week_of_month):
            day = mnth[week_of_month - 1][day_of_week - 1]
            if(day==0) and is_to_next_day:
                return datetime(year + int((month + 1)/12), (month + 1)%12, 1)
            elif(day==0):
                return None
            else:
                return datetime(year, month, day)
        if (len(mnth) < week_of_month):
            if is_to_next_day:
                return datetime(year + int((month + 1)/12), (month + 1)%12, 1)
            else:
                return None
# First output
[get_date(yy, mm, 5, 3, True) for mm in range(1, 13) for yy in [2020]]
# Second output
[get_date(yy, mm, 5, 3, False) for mm in range(1, 13) for yy in [2020]] # Iterate again to drop None.