jazzband / django-recurrence

Utility for working with recurring dates in Django.
https://django-recurrence.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
489 stars 190 forks source link

The between() method with inc=True includes dtstart even when there is no Recurrence then #204

Closed taut-and-yare closed 2 years ago

taut-and-yare commented 2 years ago

I have model with a RecurrenceField:

# models.py
class Session(models.Model):
    # other fields
    dates = RecurrenceField(null=True)

In the django admin, I add a rule: weekly, Mondays.

I need to get all the Session dates of the current month (including those in the past). However, when I call between() with inc=True, and dtstart = the first day of the month (as decribed in the docs), then that dtstart is returned too (which is a Tuesday for March 2022):

# 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
taut-and-yare commented 2 years ago

Ah ok I now see that I need to set include_dtstart=False. Closing