This bug has been brought on the forum: Topic 15651
In /core/ScheduledTime.php and core/ScheduledTime/Monthly.php I failed to precisely define what is a "week":
I didn't define precisely what a week is (is a week defined as starting from Monday? do we account for partial weeks?)
I didn't account for the fact that there is a variable number of weeks within a month
I didn't account for the fact that there can be more than 4 weeks within a month
I didn't think thoroughly whether we needed such a flexible API for monthly scheduled tasks.
From here, there are two ways to go.
The first solution is to keep the API as it is and add the following fixes:
1) The following code should schedule the task for the very first day of the month:
According to the 'day' attribute, setDay(1) means Monday.
If the first day of a month is not a Monday, we simply take the first day of the first week.
Therefore, the 'week' attribute can not define a week starting on Monday. It has to define a partial week.
2) The following code should schedule the task for the very last day of the month:
If the last day of a month is not a Sunday, we simply take the closest day to the one specified.
How about if the month has 6 partial weeks? In this case, even though the API user meant to schedule the task for the last day of the month, it wouldn't work.
The only solution I could think of is a comment in the code saying: "To be absolutely certain the task is scheduled for the last day, use ->setWeek(6)".
If a month has less than 6 weeks, we would simple take the last week.
3) The following code should schedule the task for the Tuesday of the first week of the month:
What happens if there is no Tuesday the first week of a given month? Do we set for Tuesday of the next week? do we take the first day of the first week?
Those 3 points clearly indicate that keeping this API is a mess from a usability point of view.
The API for monthly scheduled tasks should be remade.
Here are two proposals:
1) Remove setWeek() and use setDay()
In this solution, setWeek() would be removed and setDay() would be used with integers comprised within 1 and 31.
To schedule a task for the very last day of a month, we would use setDay(31) and pick the closest day for months with less than 31 days.
2) Remove setDay() and setWeek(), add runFirstDay(void) and runLastDay(void)
To schedule the task for the first day, runFirstDay() would be called, to schedule the task for the last day, runLastDay() would be called.
Please comment on the best way to go.
When this bug is fixed, TODO: 1837
setWeek will be removed and setDay will be used with integers comprised within 1 and 31
(In ) fixes #1835, setWeek removed, setDay updated
(In ) Refs #1835
Using date() as it is built-in (VS cal_days_in_month which requires library)
Unit tests pass so I think it is similar?
(In ) Refs #1835 update comment as per code update