volkanunsal / postgres-rrule

Recurring dates in Postgres.
MIT License
109 stars 25 forks source link

Occurrences are not calculated correctly across year boundaries #22

Closed dspasojevic closed 9 months ago

dspasojevic commented 1 year ago

Because all_starts restricts its search space to the end of the year containing dtstart it may sometimes return an empty set. This leads to occurrences also being empty.

e.g:

select * from all_starts('RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=TH;COUNT=100'::text::rrule, '2029-12-31T10:00:00'::TIMESTAMP);

returns 0 rows, because it will not find a matching Thursday between dtstart and the end of 2029.

Changing https://github.com/volkanunsal/postgres-rrule/blob/master/src/functions/0017-all_starts.sql#L44 to search beyond year_end (e.g. until 12 months later, same as the subsequent generators) resolves this issue.

Given that the function may already process 365 days (e.g if dtstart was on Jan 1), is it an issue to increase the number of days that are processed here?

@volkanunsal happy to raise a PR if you are still maintaining this project.

dspasojevic commented 1 year ago

This function is probably also why https://github.com/volkanunsal/postgres-rrule/issues/21 occurs; multiple values are returned by all_starts when dtstart has the same day of the week as dayofweek:

postgres=# select * from all_starts('RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=TU;COUNT=100'::text::rrule, '2023-07-04T10:00:00'::TIMESTAMP);
     all_starts
---------------------
 2023-07-04 10:00:00
 2023-07-11 10:00:00
(2 rows)

which then produces two series of alternating dates when feed through here.

thomastthai commented 9 months ago

Should this issue be closed after the merge?