Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nth occurence of week day in a specified period #921

Open
imohitmayank opened this issue Jun 15, 2019 · 1 comment
Open

nth occurence of week day in a specified period #921

imohitmayank opened this issue Jun 15, 2019 · 1 comment

Comments

@imohitmayank
Copy link

A query to print all occurrence of a particular weekday in a specified period works.

Ex: - Get all Sundays between 23rd May 2019 and 30th June 2019.

>>> list(rrule(MONTHLY, byweekday= [SU],dtstart=parse("23rd May 2019"), until = parse('30th Jun 2019')))
[datetime.datetime(2019, 5, 26, 0, 0), datetime.datetime(2019, 6, 2, 0, 0), datetime.datetime(2019, 6, 9, 0, 0), datetime.datetime(2019, 6, 16, 0, 0), datetime.datetime(2019, 6, 23, 0, 0), datetime.datetime(2019, 6, 30, 0, 0)]

But if you want specific occurrence, it fails.

Ex:- Get 1st Sunday in the same period

>>> list(rrule(MONTHLY, byweekday= [SU(1)],dtstart=parse("23rd May 2019"), until = parse('30th Jun 2019')))
[datetime.datetime(2019, 6, 2, 0, 0)]

In fact, it skips to the next month and gives first Sunday of next month. It seems its due to MONTHLY freq, but then how to handle this type of scenarios?

@ffe4
Copy link
Member

ffe4 commented Apr 4, 2020

byweekday refers to the nth occurrence in the frequency, not the nth occurrence in the rule. The above rule therefore matches the first Sunday of each month. The only match in your period is therefore the first Sunday of June.

I am not sure why they are not in the current documentation, but I think what you should use in this case are the after(dt, inc=False) and xafter(dt, n, inc=Flase) methods. They return the first occurrence and the first n occurrences respectively. If you want dt to be included if it matches the rule you have to specify inc=True.

from dateutil.parser import parse
from dateutil.rrule import *

dtstart = parse("23rd May 2019")

rr = rrule(WEEKLY, byweekday=SU, dtstart=dtstart)
print(rr.after(dtstart, inc=True))
2019-05-26 00:00:00

These methods are actually documented in the code, so I will work on fixing the docs in #1025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants