FAILURE MAP
← Case archive

FA-76871 / Calendar recurrence rules / Open access

Monthly BYMONTHDAY expansion with negative and invalid days: negative day · case 01

BYMONTHDAY=-1 produces the second-to-last day of each month.

Verified by executionVariant 1 · 7 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

The negative month-day offset omits the +1 that makes -1 the last day.

THE FAILURE

The negative month-day offset omits the +1 that makes -1 the last day.

Unsuccessful approach: The partial repair `md if md > 0 else 31 + md + 1` assumes every month has 31 days, so short months get the wrong day from the end.

Case contract

FREQ=MONTHLY with a BYMONTHDAY list. Positive values count from the first day, negative values from the last day (-1 is the last day). A value that does not exist in a month is skipped for that month, never clamped. Months step by interval from the dtstart month; instances before dtstart are dropped; coinciding values yield one instance. Return the first count ISO dates in chronological order.

Why this case matters

Recurring calendar series are expanded into concrete instances for display, reminders and conflict checks.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
import datetime as dt
import calendar
N = 1
observations = []
def solve(dtstart, interval, monthdays, count):
    start = dt.date.fromisoformat(dtstart)
    out = []
    for k in range(400):
        mi = start.month - 1 + k * interval
        y, m = start.year + mi // 12, mi % 12 + 1
        dim = calendar.monthrange(y, m)[1]
        days = []
        for md in monthdays:
            day = md if md > 0 else dim + md
            if 1 <= day <= dim:
                days.append(day)
        for day in sorted(set(days)):
            d = dt.date(y, m, day)
            if d < start:
                continue
            if len(out) >= count:
                return out
            out.append(d.isoformat())
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: 31st skips short months',
   ['2024-01-31', 1, [31], 5],
   ['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']),
  ('last day of month', ['2024-01-15', 1, [-1], 4], ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('31 and -1 coincide',
   ['2024-01-01', 1, [31, -1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('first and last listed last-first',
   ['2024-03-10', 1, [-1, 1], 5],
   ['2024-03-31', '2024-04-01', '2024-04-30', '2024-05-01', '2024-05-31']),
  ('crossing december interval 2',
   ['2024-11-05', 2, [5], 4],
   ['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']),
  ('december start interval 1',
   ['2024-12-10', 1, [10, -3], 4],
   ['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']),
  ('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03'])],
 [('31 and -1 coincide',
   ['2024-01-01', 1, [31, -1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('first and last listed last-first',
   ['2024-03-10', 1, [-1, 1], 5],
   ['2024-03-31', '2024-04-01', '2024-04-30', '2024-05-01', '2024-05-31']),
  ('crossing december interval 2',
   ['2024-11-05', 2, [5], 4],
   ['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']),
  ('december start interval 1',
   ['2024-12-10', 1, [10, -3], 4],
   ['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']),
  ('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']),
  ('negative beyond month',
   ['2023-02-01', 1, [-30], 4],
   ['2023-03-02', '2023-04-01', '2023-05-02', '2023-06-01']),
  ('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15'])],
 [('crossing december interval 2',
   ['2024-11-05', 2, [5], 4],
   ['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']),
  ('december start interval 1',
   ['2024-12-10', 1, [10, -3], 4],
   ['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']),
  ('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']),
  ('negative beyond month',
   ['2023-02-01', 1, [-30], 4],
   ['2023-03-02', '2023-04-01', '2023-05-02', '2023-06-01']),
  ('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15']),
  ('count zero', ['2024-01-01', 1, [1], 0], []),
  ('leap february 29', ['2023-12-29', 2, [29], 4], ['2023-12-29', '2024-02-29', '2024-04-29', '2024-06-29'])],
 [('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']),
  ('negative beyond month',
   ['2023-02-01', 1, [-30], 4],
   ['2023-03-02', '2023-04-01', '2023-05-02', '2023-06-01']),
  ('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15']),
  ('count zero', ['2024-01-01', 1, [1], 0], []),
  ('leap february 29', ['2023-12-29', 2, [29], 4], ['2023-12-29', '2024-02-29', '2024-04-29', '2024-06-29']),
  ('dtstart equals listed day',
   ['2024-06-30', 1, [30, 1], 4],
   ['2024-06-30', '2024-07-01', '2024-07-30', '2024-08-01']),
  ('regression: 31st skips short months',
   ['2024-01-31', 1, [31], 5],
   ['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31'])],
 [('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15']),
  ('count zero', ['2024-01-01', 1, [1], 0], []),
  ('leap february 29', ['2023-12-29', 2, [29], 4], ['2023-12-29', '2024-02-29', '2024-04-29', '2024-06-29']),
  ('dtstart equals listed day',
   ['2024-06-30', 1, [30, 1], 4],
   ['2024-06-30', '2024-07-01', '2024-07-30', '2024-08-01']),
  ('regression: 31st skips short months',
   ['2024-01-31', 1, [31], 5],
   ['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']),
  ('last day of month', ['2024-01-15', 1, [-1], 4], ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('31 and -1 coincide',
   ['2024-01-01', 1, [31, -1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression: 31st skips short months['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']Passed
last day of month['2024-01-30', '2024-02-28', '2024-03-30', '2024-04-29']['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']Failed
31 and -1 coincide['2024-01-30', '2024-01-31', '2024-02-28', '2024-03-30']['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']Failed
first and last listed last-first['2024-03-30', '2024-04-01', '2024-04-29', '2024-05-01', '2024-05-30']['2024-03-31', '2024-04-01', '2024-04-30', '2024-05-01', '2024-05-31']Failed
crossing december interval 2['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']Passed
december start interval 1['2024-12-10', '2024-12-28', '2025-01-10', '2025-01-28']['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']Failed
dtstart after listed day['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']Passed

SHA-256 / 3a47da0fb547b22ad9b99806ce2e495629b9521eccfceb2be61544d65e3681c7

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
import datetime as dt
import calendar
N = 1
observations = []
def solve(dtstart, interval, monthdays, count):
    start = dt.date.fromisoformat(dtstart)
    out = []
    for k in range(400):
        mi = start.month - 1 + k * interval
        y, m = start.year + mi // 12, mi % 12 + 1
        dim = calendar.monthrange(y, m)[1]
        days = []
        for md in monthdays:
            day = md if md > 0 else 31 + md + 1
            if 1 <= day <= dim:
                days.append(day)
        for day in sorted(set(days)):
            d = dt.date(y, m, day)
            if d < start:
                continue
            if len(out) >= count:
                return out
            out.append(d.isoformat())
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: 31st skips short months',
   ['2024-01-31', 1, [31], 5],
   ['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']),
  ('last day of month', ['2024-01-15', 1, [-1], 4], ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('31 and -1 coincide',
   ['2024-01-01', 1, [31, -1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('first and last listed last-first',
   ['2024-03-10', 1, [-1, 1], 5],
   ['2024-03-31', '2024-04-01', '2024-04-30', '2024-05-01', '2024-05-31']),
  ('crossing december interval 2',
   ['2024-11-05', 2, [5], 4],
   ['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']),
  ('december start interval 1',
   ['2024-12-10', 1, [10, -3], 4],
   ['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']),
  ('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03'])],
 [('31 and -1 coincide',
   ['2024-01-01', 1, [31, -1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('first and last listed last-first',
   ['2024-03-10', 1, [-1, 1], 5],
   ['2024-03-31', '2024-04-01', '2024-04-30', '2024-05-01', '2024-05-31']),
  ('crossing december interval 2',
   ['2024-11-05', 2, [5], 4],
   ['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']),
  ('december start interval 1',
   ['2024-12-10', 1, [10, -3], 4],
   ['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']),
  ('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']),
  ('negative beyond month',
   ['2023-02-01', 1, [-30], 4],
   ['2023-03-02', '2023-04-01', '2023-05-02', '2023-06-01']),
  ('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15'])],
 [('crossing december interval 2',
   ['2024-11-05', 2, [5], 4],
   ['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']),
  ('december start interval 1',
   ['2024-12-10', 1, [10, -3], 4],
   ['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']),
  ('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']),
  ('negative beyond month',
   ['2023-02-01', 1, [-30], 4],
   ['2023-03-02', '2023-04-01', '2023-05-02', '2023-06-01']),
  ('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15']),
  ('count zero', ['2024-01-01', 1, [1], 0], []),
  ('leap february 29', ['2023-12-29', 2, [29], 4], ['2023-12-29', '2024-02-29', '2024-04-29', '2024-06-29'])],
 [('dtstart after listed day',
   ['2024-05-20', 1, [3, 25], 4],
   ['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']),
  ('negative beyond month',
   ['2023-02-01', 1, [-30], 4],
   ['2023-03-02', '2023-04-01', '2023-05-02', '2023-06-01']),
  ('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15']),
  ('count zero', ['2024-01-01', 1, [1], 0], []),
  ('leap february 29', ['2023-12-29', 2, [29], 4], ['2023-12-29', '2024-02-29', '2024-04-29', '2024-06-29']),
  ('dtstart equals listed day',
   ['2024-06-30', 1, [30, 1], 4],
   ['2024-06-30', '2024-07-01', '2024-07-30', '2024-08-01']),
  ('regression: 31st skips short months',
   ['2024-01-31', 1, [31], 5],
   ['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31'])],
 [('interval 3 from october',
   ['2023-10-15', 3, [15, -1], 5],
   ['2023-10-15', '2023-10-31', '2024-01-15', '2024-01-31', '2024-04-15']),
  ('count zero', ['2024-01-01', 1, [1], 0], []),
  ('leap february 29', ['2023-12-29', 2, [29], 4], ['2023-12-29', '2024-02-29', '2024-04-29', '2024-06-29']),
  ('dtstart equals listed day',
   ['2024-06-30', 1, [30, 1], 4],
   ['2024-06-30', '2024-07-01', '2024-07-30', '2024-08-01']),
  ('regression: 31st skips short months',
   ['2024-01-31', 1, [31], 5],
   ['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']),
  ('last day of month', ['2024-01-15', 1, [-1], 4], ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']),
  ('31 and -1 coincide',
   ['2024-01-01', 1, [31, -1], 4],
   ['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression: 31st skips short months['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31', '2024-08-31']Passed
last day of month['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31']['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']Failed
31 and -1 coincide['2024-01-31', '2024-03-31', '2024-05-31', '2024-07-31']['2024-01-31', '2024-02-29', '2024-03-31', '2024-04-30']Failed
first and last listed last-first['2024-03-31', '2024-04-01', '2024-05-01', '2024-05-31', '2024-06-01']['2024-03-31', '2024-04-01', '2024-04-30', '2024-05-01', '2024-05-31']Failed
crossing december interval 2['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']['2024-11-05', '2025-01-05', '2025-03-05', '2025-05-05']Passed
december start interval 1['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']['2024-12-10', '2024-12-29', '2025-01-10', '2025-01-29']Passed
dtstart after listed day['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']['2024-05-25', '2024-06-03', '2024-06-25', '2024-07-03']Passed

SHA-256 / 97c1e9cd3ed8bbbccc18bac6e16d68f9bffc88239192f4669be7cb7dc1bb6487

HELD IN THE MEMBER ARCHIVE

The verified repair and its recorded checks are member-only.

This mechanism has 7 recorded checks per implementation. The open-access tier publishes the failure and the unsuccessful fix; the repaired source that passes every check, and the observations that prove it, are available to members.

Every case sharing this mechanism uses the same contract and the same repair, so this one record is held back for all of them.

Member access is invitation-based. Sign in with your invited account to inspect the repair.

Sign in to the archive ↗

Verification & scope

Stipulated, bounded recurrence-expansion model evaluated offline on explicit fixtures; it is not a complete iCalendar implementation and makes no claim of standards conformance. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:49:20.444387+00:00.

Case digest / d94fcc69bd4a66396ff65c5f157938b1b5bdd9d7cd24fe7930607cfa75f39276