在 django-recurrence 中,带有 inc=True 的 between() 方法包括 dtstart,即使没有 Recurrence 那么
In django-recurrence the between() method with inc=True includes dtstart even when there is no Recurrence then
这是关于爵士乐队 django-recurrence 的问题。
我有一个带有 RecurrenceField 的模型:
# models.py
class Session(models.Model):
# other fields
dates = RecurrenceField(null=True)
在 django 管理中,我添加了一条规则:每周,星期一。
我需要获取当月的所有会话日期(包括过去的那些)。但是,当我使用 inc=True 调用 between() 并且 dtstart = 该月的第一天(如 docs 中所述)时,也会返回 dtstart(这是 2022 年 3 月的星期二):
# shell output:
In [7]: obj = Session.objects.all().first()
In [8]: for rule in obj.dates.rrules:
...: print(rule.to_text())
...:
hebdomadaire, chaque lundi # translates to: weekly, every Monday
In [9]: month_end
Out[9]: datetime.datetime(2022, 3, 31, 0, 0)
In [10]: month_start
Out[10]: datetime.datetime(2022, 3, 1, 0, 0)
In [11]: obj.dates.between(month_start,month_end,dtstart=month_start,inc=True,)
Out[11]:
[datetime.datetime(2022, 3, 1, 0, 0), # this is a Tuesday!
datetime.datetime(2022, 3, 7, 0, 0), # Monday
datetime.datetime(2022, 3, 14, 0, 0), # Monday
datetime.datetime(2022, 3, 21, 0, 0), # Monday
datetime.datetime(2022, 3, 28, 0, 0)] # Monday
In [12]: obj.dates.between(month_start,month_end,dtstart=month_start,inc=True,)[0].weekday()
Out[12]: 1
如 docs 中所述,默认包含 dtstart。要排除它 include_dtstart=False
应作为参数提供。
这是关于爵士乐队 django-recurrence 的问题。
我有一个带有 RecurrenceField 的模型:
# models.py
class Session(models.Model):
# other fields
dates = RecurrenceField(null=True)
在 django 管理中,我添加了一条规则:每周,星期一。
我需要获取当月的所有会话日期(包括过去的那些)。但是,当我使用 inc=True 调用 between() 并且 dtstart = 该月的第一天(如 docs 中所述)时,也会返回 dtstart(这是 2022 年 3 月的星期二):
# shell output:
In [7]: obj = Session.objects.all().first()
In [8]: for rule in obj.dates.rrules:
...: print(rule.to_text())
...:
hebdomadaire, chaque lundi # translates to: weekly, every Monday
In [9]: month_end
Out[9]: datetime.datetime(2022, 3, 31, 0, 0)
In [10]: month_start
Out[10]: datetime.datetime(2022, 3, 1, 0, 0)
In [11]: obj.dates.between(month_start,month_end,dtstart=month_start,inc=True,)
Out[11]:
[datetime.datetime(2022, 3, 1, 0, 0), # this is a Tuesday!
datetime.datetime(2022, 3, 7, 0, 0), # Monday
datetime.datetime(2022, 3, 14, 0, 0), # Monday
datetime.datetime(2022, 3, 21, 0, 0), # Monday
datetime.datetime(2022, 3, 28, 0, 0)] # Monday
In [12]: obj.dates.between(month_start,month_end,dtstart=month_start,inc=True,)[0].weekday()
Out[12]: 1
如 docs 中所述,默认包含 dtstart。要排除它 include_dtstart=False
应作为参数提供。