每周一次,持续三周,然后一周不重复,重复

rrule weekly for three weeks, then one week not, repeat

我正在使用 rrule from python dateutil,但不知道如何为以下示例创建规则集:

关于为此创建规则(集)有什么建议吗?

一种方法是使用 rrulesetWEEKLY 规则以及每第 4 周相应的例外规则:

from dateutil.rrule import rrule, rruleset
from dateutil.rrule import WEEKLY

from dateutil.relativedelta import relativedelta
from datetime import datetime, timedelta

dtstart = datetime(2011, 1, 1)

rrset = rruleset()

weekly_rule = rrule(freq=WEEKLY, dtstart=dtstart)
every_4_weeks = rrule(freq=WEEKLY, interval=4,
                      dtstart=dtstart + relativedelta(weeks=4))

rrset.rrule(weekly_rule)
rrset.exrule(every_4_weeks)

rrset.between(dtstart, dtstart + timedelta(days=65))

结果:

[datetime.datetime(2011, 1, 8, 0, 0),
 datetime.datetime(2011, 1, 15, 0, 0),
 datetime.datetime(2011, 1, 22, 0, 0),
 datetime.datetime(2011, 2, 5, 0, 0),
 datetime.datetime(2011, 2, 12, 0, 0),
 datetime.datetime(2011, 2, 19, 0, 0),
 datetime.datetime(2011, 3, 5, 0, 0)]

它的工作方式是weekly_rule每周生成一个日期,every_4_weeks每4周生成一次,从dtstart之后的第4周开始。这给了你一个 3 上 1 下的时间表。