Skip to content

Stop ical.js rule quirks from yielding wrong occurrences #36

Open

Description

@PhilflowIO

Symptom

After #31 (PR #35) ical.js walks BY lists in order, but several of its rule evaluations still produce wrong occurrences while expandOccurrences reports complete: true. Confirmed during #31 and the review of #35 (ical.js 2.2.1):

  • YEARLY;BYMONTH=2;BYMONTHDAY=30 yields 2026-03-02, 2027-03-02, 2028-03-01: impossible dates become phantom occurrences (RFC 5545: invalid dates are ignored).
  • YEARLY with several BYHOUR/BYMINUTE values uses only one of them (now the smallest, before fix(recur): iterate rules in time order for any listed BY order #35 the first listed).
  • MONTHLY with BYDAY and more than one time of day adds a phantom instance on the 1st of the next month.
  • HOURLY with BYHOUR, and MONTHLY with BYMONTH, ignore INTERVAL.
  • BYDAY with BYMONTHDAY throws "Malformed values" on valid rules after a fixed 48-step search, e.g. MONTHLY;INTERVAL=3;BYMONTHDAY=13,5,20;BYDAY=TU (fails closed).
  • Reported earlier in expandOccurrences can drop occurrences of MONTHLY rules with BYMONTH #31, not re-checked: leap-day YEARLY on 1 March in other years, BYSETPOS ignored on YEARLY, BYWEEKNO wrong with WKST.

To do

  • Per case: a failing test against an independent expansion, then decide fix location (upstream ical.js with a report, or a guard in tsdav-utils that refuses with a typed error / reports complete: false instead of returning wrong occurrences).
  • Never return a wrong occurrence as complete.

Activity

  1. PhilflowIO commented on Nov 18, 2026

    @PhilflowIO
    OwnerAuthor

    Two more ical.js 2.2.1 defects, found in the review of #39. #39 works around both by walking these rules from DTSTART, so a call with from gives what the call without it gives. ical.js' own result stays as it is.

    • MONTHLY setup with a fifth or fifth-last weekday. Example: 5SU, from a month that has no fifth Sunday. The setup (init, the MONTHLY BYDAY branch) searches forward with increment_month until a month has that weekday. Each step moves the shared BYMONTH position, and resetting last to the start does not undo it. The setup then yields what it found untested, without BYSETPOS or the contracting rules. So the first instances depend on where the walk began. For example, FREQ=MONTHLY;BYDAY=5SU;BYSETPOS=3,-2 yields a fifth Sunday although BYSETPOS rules out every day.
    • Years 0 to 99. Time.toUnixTime goes through Date.UTC, which reads those years as 1900 to 1999. So ical.js compares and orders times before the year 100 wrongly, and the walk from such a DTSTART yields nothing past them, or misses some.
  2. PhilflowIO commented on Nov 18, 2026

    @PhilflowIO
    OwnerAuthor

    One more, from the re-review of #39: ical.js gives a rule up silently. RecurIterator.next ends a YEARLY rule after 28 years without an instance and a MONTHLY one after 336 months, then returns null as if the rule had ended. A fifth Sunday of February can be 40 years away: FREQ=YEARLY;BYMONTH=2;BYDAY=5SU from 2058 stops after 2088 and never yields 2128-02-29. The setup (init) has no such cut: it searches to UNTIL or the year 20000.

    #39 detects the give-up (null although COUNT is not reached and UNTIL not passed) and reports complete: false, stoppedBy: 'library'; a write check on such a series fails closed. The occurrences after the cut stay unknown until ical.js is fixed or replaced for these rules.

  3. PhilflowIO commented on Nov 18, 2026

    @PhilflowIO
    OwnerAuthor

    One more, from the final review of #39: COUNT is counted as ical.js counts it, not as RFC 5545 does, when DTSTART is off the rule. RFC 5545 (3.8.5.3) counts DTSTART as the first instance even where the rule would not produce it. ical.js counts only the instances the rule yields, so the series gets one occurrence more. Example: DTSTART:20580120T130000, RRULE:FREQ=YEARLY;BYMONTH=2;BYDAY=5SU;COUNT=3. RFC: 2058-01-20 plus two fifth Sundays of February. ical.js: 2058-01-20 plus three. tsdav-utils walks COUNT rules from DTSTART in full, with or without from, so both calls agree, but both follow ical.js here.

  4. PhilflowIO commented on Nov 18, 2026

    @PhilflowIO
    OwnerAuthor

    Three more ical.js 2.2.1 defects, found while fixing #42. All three give the same result with from and without it (so no contract break), but the result is not RFC 5545. Checked against a brute-force RFC 5545 expansion (test/rfcOracle.ts in the #42 PR).

    • MONTHLY with BYMONTH ignores INTERVAL. increment_month steps through the BYMONTH list and never reads INTERVAL there. FREQ=MONTHLY;INTERVAL=2;BYMONTH=1,2,3,4;BYMONTHDAY=1 from 2026-01-01 yields Jan, Feb, Mar, Apr. RFC 5545: Jan and Mar (every second month, limited to those listed).
    • MONTHLY BYDAY setup in a month BYMONTH leaves out. The setup (init, MONTHLY BYDAY branch) computes the weekday in DTSTART's month and yields it untested. FREQ=MONTHLY;BYMONTH=6;BYDAY=1MO from 2018-12-01 yields 2018-12-03, a December. The BYMONTHDAY branch had the same leak; the expandOccurrences with from shifts MONTHLY BYMONTHDAY=31,-31 grid #42 fix sets that one up in a listed month, the BYDAY branch is unchanged.
    • YEARLY BYMONTHDAY. expand_year_days builds a date from each listed day without checking that the month has it: FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30 yields 2 March every year (RFC 5545: never). Without BYMONTH it reads BYMONTHDAY in DTSTART's month only (RFC 5545: every month), so FREQ=YEARLY;BYMONTHDAY=-31 from a November yields nothing and ical.js gives the rule up (reported stoppedBy: 'library' since expandOccurrences with from shifts MONTHLY BYMONTHDAY=31,-31 grid #42's PR, which is the honest verdict there).

    Scale, from the #42 sweep (box, 2 seeds × 1,500 rules, date-only, compared with RFC 5545): MONTHLY with BYMONTH and INTERVAL > 1 differs in 260 of 305 rules; YEARLY with a month-end BYMONTHDAY (29, 30, 31, -29, -30, -31) in 389 of 570.

  5. PhilflowIO commented on Nov 18, 2026

    @PhilflowIO
    OwnerAuthor

    Update after the review of #43.

    Now handled in #43. Each MONTHLY candidate is checked against RFC 5545 and dropped if it is no instance. COUNT counts only the instances that remain:

    • MONTHLY with BYMONTH ignores INTERVAL: the off-grid months are dropped.
    • MONTHLY BYDAY setup in an unlisted month, or with BYSETPOS unread: the untested setup day is dropped (BYDAY=5MO;BYSETPOS=2 from 1993-05-08 gave 1993-05-31).
    • BYMONTHDAY with BYSETPOS and no BYDAY: ical.js ignores BYSETPOS; the days it does not name are dropped.
    • The 1st of a month under a negative BYSETPOS: next_month tested a new month's 1st only against position 1, so BYDAY=SA;BYSETPOS=-5 never gave 2020-08-01. That 1st is now kept.

    Still open: ical.js here yields no superset of the instances, so dropping candidates cannot fix these:

    • MONTHLY BYDAY with BYMONTHDAY: _byDayAndMonthDay steps month by month whatever INTERVAL says. FREQ=MONTHLY;INTERVAL=2;BYMONTHDAY=-1,30,29;BYDAY=FR,WE from 2012-12-26 yields January, March, May and so on, never the grid's August 2013 (30 August is a Friday).
    • MONTHLY BYDAY with BYSETPOS and more than one time of day: ical.js applies BYSETPOS to the days, RFC 5545 to the date-times. FREQ=MONTHLY;BYDAY=MO;BYHOUR=9,10;BYSETPOS=2 from 2026-02-01T09:00 gives 2 Feb (the setup) and 9 Feb, each at 9 and 10, and 1 March at 10:00, a Sunday. RFC 5545 gives the first Monday at 10:00 each month.
    • MONTHLY with BYMONTH and a fifth weekday (the setup search from the first comment): it loses the first year. FREQ=MONTHLY;BYMONTH=6,9;BYDAY=1SA,5TU;BYSETPOS=1 from 2022-06-08 misses 2022-09-03.
    • YEARLY BYMONTHDAY (first comment): unchanged.
  6. PhilflowIO commented on Nov 18, 2026

    @PhilflowIO
    OwnerAuthor

    Update after the re-review of #43: what remains.

    #43 now checks every MONTHLY and YEARLY candidate against RFC 5545 and drops non-instances; COUNT counts the kept instances. It also compares each period ical.js passes with RFC 5545's instances in it. If ical.js skipped one, the result is complete: false, stoppedBy: 'library', and a COUNT result stops at the first skipped instance. So for these frequencies ical.js' defects no longer give a wrong result reported as complete; they give a result that says it is incomplete. The YEARLY sweep against the review's oracle (2,000 rules, 6,000 from calls) finds no complete result that differs.

    Still open here, because ical.js cannot give the instances (now reported 'library', no longer wrong-but-complete):

    • YEARLY BYMONTHDAY without BYMONTH: ical.js reads it in DTSTART's month only; RFC 5545 takes every month (YEARLY;BYMONTHDAY=15).
    • YEARLY BYMONTHDAY with BYMONTH: ical.js reads negative days by the previous instance's month and skips some.
    • YEARLY BYDAY with BYMONTH and BYSETPOS: ical.js applies BYSETPOS per month; RFC 5545 applies it to the year.
    • MONTHLY BYDAY with BYMONTHDAY: _byDayAndMonthDay steps month by month whatever INTERVAL says.
    • MONTHLY BYDAY with BYSETPOS and several times of day: ical.js applies BYSETPOS to the days, RFC 5545 to the date-times.
    • A fifth weekday DTSTART's month lacks: ical.js' setup search can lose first instances. Where that search never ends, fix(expand): MONTHLY month days on their grid, RFC-checked candidates #43 does not set ical.js up: a rule with no instance gives DTSTART only, any other is refused.

    Not checked at all: BYWEEKNO, an ordinal weekday under BYMONTHDAY or BYYEARDAY (RFC 5545 leaves the scope open), and COUNT read as ical.js reads it (DTSTART aside, comment above). A fix of the missing instances means computing these rules without ical.js; the period model in src/recur.ts (periodModel) already gives their sets.

  7. PhilflowIO commented on Nov 18, 2026

    @PhilflowIO
    OwnerAuthor

    Two more, found in the review of PhilflowIO/dav-mcp#174 against an independent RFC 5545 expansion (python-dateutil). Both are returned as complete with no note, on 0.7.0 and 0.8.1 alike:

    • DTSTART;VALUE=DATE:20250101, RRULE:FREQ=YEARLY;BYWEEKNO=40,2;BYDAY=MO, window 2027-01-05..2027-02-14: every Monday from Jan 11 instead of only Jan 11 (week 2). Correct when DTSTART falls on a matching Monday.
    • DTSTART:20190102T100000Z, RRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=SU,WE;BYSETPOS=-1: includes the Wednesdays (2026-01-21, 02-04, 02-18); RFC 5545 gives only the Sundays (BYSETPOS ignored on WEEKLY).

    Like the MONTHLY/YEARLY candidate check in 0.8.1, these need either an RFC check of the candidates for YEARLY+BYWEEKNO and WEEKLY+BYSETPOS, or a 'library' report, never a silent complete.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions