假设我想知道什么时候用 rrule 庆祝生日。然后频率 YEARLY 工作正常,除了闰日。事实上,它每 4 年才出现一次。
有什么办法可以直接用rrule处理吗?
from datetime import datetime
from dateutil.rrule import rrule, YEARLY
n = 1
print(list(rrule(freq=YEARLY, count=n + 1, dtstart=datetime(1990, 4, 28))))
print(list(rrule(freq=YEARLY, count=n + 1, dtstart=datetime(1992, 2, 29))))
给
[datetime.datetime(1990, 4, 28, 0, 0), datetime.datetime(1991, 4, 28, 0, 0)]
[datetime.datetime(1992, 2, 29, 0, 0), datetime.datetime(1996, 2, 29, 0, 0)]
文档中甚至没有提到闰日这一事实让我怀疑这是否可能只是一个错误。
过年
这可能会有所帮助,但仅限于 2 月 28 日:
from datetime import datetime
from dateutil.rrule import rrule, YEARLY
n = 5
bday = datetime(1990, 4, 28)
print(list(rrule(freq=YEARLY,
byyearday=bday.timetuple().tm_yday,
count=n + 1,
dtstart=bday)))
bday = datetime(1992, 2, 29)
print(list(rrule(freq=YEARLY,
byyearday=bday.timetuple().tm_yday,
count=n + 1,
dtstart=bday)))
给
[datetime.datetime(1990, 4, 28, 0, 0), datetime.datetime(1991, 4, 28, 0, 0), datetime.datetime(1992, 4, 27, 0, 0), datetime.datetime(1993, 4, 28, 0, 0), datetime.datetime(1994, 4, 28, 0, 0), datetime.datetime(1995, 4, 28, 0, 0)]
[datetime.datetime(1992, 2, 29, 0, 0), datetime.datetime(1993, 3, 1, 0, 0), datetime.datetime(1994, 3, 1, 0, 0), datetime.datetime(1995, 3, 1, 0, 0), datetime.datetime(1996, 2, 29, 0, 0), datetime.datetime(1997, 3, 1, 0, 0)]
相关分类