dates.recurrence
The next n dates of a repeating rule: monthly on a day, last weekday of the month, every k days or weeks, yearly.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 25 tests, run in TypeScript, Python and Rust.
What it does
The next `count` dates of a schedule, from a start date you pass. It never reads the clock: "the next three payment dates" is a question about a date, and the date is an argument.
The rule kinds:
For example
recurrence(kind monthly-on-day, interval 1, day 31, weekday —, 2026-01-01, 5)→ 2026-01-31, 2026-02-28, 2026-03-31, 2026-04-30, 2026-05-31 monthly on the 31st clamps each month from the 31st: March is back to 31, not stuck at 28recurrence(kind monthly-on-day, interval 1, day 31, weekday —, 2024-01-15, 3)→ 2024-01-31, 2024-02-29, 2024-03-31 monthly on the 31st in a leap year gives 29 Februaryrecurrence(kind monthly-on-day, interval 1, day 15, weekday —, 2026-01-15, 2)→ 2026-01-15, 2026-02-15 the start date counts when it is itself an occurrence
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
def recurrence(rule: RecurrenceRule, start_iso: str, count: int) -> List[str]
| rule | RecurrenceRule | what repeats and how often |
| start_iso | date | the first date the schedule may produce; also the anchor for every-days, every-weeks and yearly |
| count | int | how many dates to return, 0 or more |
| returns | date[] | the first count dates on or after startIso, ascending |
The types it declares, generated into your project
RecurrenceKind = Literal["monthly-on-day", "last-weekday-of-month", "every-days", "every-weeks", "yearly"]
@dataclass(frozen=True)
class RecurrenceRule:
"""A repeating date rule. Only the field its kind uses may be set; the other is null."""
kind: RecurrenceKind
#: every how many months, days, weeks or years; 1 or more
interval: int
#: monthly-on-day only: day of the month, 1 to 31, clamped to shorter months
day: Optional[int]
#: last-weekday-of-month only: 1 = Monday ... 7 = Sunday
weekday: Optional[int]
Your code names it in one line, in the file that uses it
from fune.dates.recurrence import recurrence # dates.recurrence@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import List
from .dates_add_days import days_from_civil, days_in_month, epoch_day_from_iso, iso_from_epoch_day, parse_iso_date ← from dates.add-days ^1.0.0 · built alongside by fune
from .dates_add_months import add_months ← from dates.add-months ^1.0.0 · built alongside by fune
from .dates_day_of_week import day_of_week ← from dates.day-of-week ^1.0.0 · built alongside by fune
from .dates_recurrence_types import RecurrenceRule
def _is_int(value: object) -> bool:
return isinstance(value, int) and not isinstance(value, bool)
def _check_range(name: str, value: object, low: int, high: int, kind: str) -> None:
if not _is_int(value) or value < low or value > high: # type: ignore[operator]
raise ValueError("%s must be %s-%s for %s, received %s" % (name, low, high, kind, value))
def _date_in_month(index: int, rule: RecurrenceRule) -> int:
"""The date of the ``day``-th (clamped) or last ``weekday`` in a month index counted from year 0."""
year = index // 12
month = index - year * 12 + 1
if year > 9999:
raise ValueError("the schedule runs past 9999-12-31, outside the supported range 0001-01-01 to 9999-12-31")
length = days_in_month(year, month)
if rule.kind == "monthly-on-day":
return days_from_civil(year, month, min(rule.day, length))
last = days_from_civil(year, month, length)
last_weekday = day_of_week(iso_from_epoch_day(last))
return last - (last_weekday - rule.weekday) % 7
def recurrence(rule: RecurrenceRule, start_iso: str, count: int) -> List[str]:
"""The first ``count`` dates of a rule on or after ``start_iso``.
Monthly dates are clamped from the rule's day every time, never from the
previous date, so the 31st does not decay to the 28th after February.
"""
if not _is_int(count) or count < 0:
raise ValueError("count must be a non-negative integer, received %s" % (count,))
if not _is_int(rule.interval) or rule.interval < 1:
raise ValueError("interval must be a positive integer, received %s" % (rule.interval,))
kind = rule.kind
if kind == "monthly-on-day":
if rule.day is None:
raise ValueError("day must be 1-31 for monthly-on-day, received null")
_check_range("day", rule.day, 1, 31, kind)
if rule.weekday is not None:
raise ValueError("weekday does not apply to monthly-on-day")
elif kind == "last-weekday-of-month":
if rule.weekday is None:
raise ValueError("weekday must be 1-7 for last-weekday-of-month, received null")
_check_range("weekday", rule.weekday, 1, 7, kind)
if rule.day is not None:
raise ValueError("day does not apply to last-weekday-of-month")
elif kind in ("every-days", "every-weeks", "yearly"):
if rule.day is not None:
raise ValueError("day does not apply to %s" % (kind,))
if rule.weekday is not None:
raise ValueError("weekday does not apply to %s" % (kind,))
else:
raise ValueError(
'unknown recurrence kind "%s": expected monthly-on-day, last-weekday-of-month, every-days, every-weeks or yearly'
% (kind,)
)
start = epoch_day_from_iso(start_iso)
out: List[str] = []
if kind in ("every-days", "every-weeks"):
step = 7 * rule.interval if kind == "every-weeks" else rule.interval
for k in range(count):
out.append(iso_from_epoch_day(start + k * step))
elif kind == "yearly":
# Always from the anchor, so 29 February comes back in leap years.
for k in range(count):
out.append(add_months(start_iso, 12 * rule.interval * k))
else:
date = parse_iso_date(start_iso)
first = date.year * 12 + (date.month - 1)
if count > 0 and _date_in_month(first, rule) < start:
first += 1
for k in range(count):
out.append(iso_from_epoch_day(_date_in_month(first + k * rule.interval, rule)))
return outInstall
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 3 dependencies, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add dates.recurrence
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./dates.recurrence-1.0.0-python.fune, or fetch it from a terminal with fune pull dates.recurrence@1.0.0:python.
The whole function, every language, is one file too: dates.recurrence-1.0.0.fune, 23,431 bytes, sha256 6b1e052bc18fadd07a3fc78683a4fcdcc31220b63b8d951e1d67f24a289cb99a. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
# fune: before dates.recurrence
after — your function gets the result and the arguments, and returns the final result.
# fune: after dates.recurrence
replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.
# fune: replace dates.add-days in dates.recurrence
# fune: replace dates.add-months in dates.recurrence
# fune: replace dates.day-of-week in dates.recurrence
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show dates.recurrence --steps.
# fune: step dates.recurrence after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| monthly on the 31st clamps each month from the 31st: March is back to 31, not stuck at 28 | kind monthly-on-day, interval 1, day 31, weekday —, 2026-01-01, 5 | → | 2026-01-31, 2026-02-28, 2026-03-31, 2026-04-30, 2026-05-31 |
| monthly on the 31st in a leap year gives 29 February | kind monthly-on-day, interval 1, day 31, weekday —, 2024-01-15, 3 | → | 2024-01-31, 2024-02-29, 2024-03-31 |
| the start date counts when it is itself an occurrence | kind monthly-on-day, interval 1, day 15, weekday —, 2026-01-15, 2 | → | 2026-01-15, 2026-02-15 |
| a day already passed in the start month moves the first occurrence to the next month | kind monthly-on-day, interval 1, day 15, weekday —, 2026-01-20, 3 | → | 2026-02-15, 2026-03-15, 2026-04-15 |
| quarterly on the 15th counts its interval from the first occurrence | kind monthly-on-day, interval 3, day 15, weekday —, 2026-01-20, 3 | → | 2026-02-15, 2026-05-15, 2026-08-15 |
| monthly on the 30th starting 28 February: the clamped 28th is on or after the start | kind monthly-on-day, interval 1, day 30, weekday —, 2026-02-28, 3 | → | 2026-02-28, 2026-03-30, 2026-04-30 |
| monthly on the 1st across a year end | kind monthly-on-day, interval 1, day 1, weekday —, 2026-11-02, 3 | → | 2026-12-01, 2027-01-01, 2027-02-01 |
| last Friday of each month from 1 September 2026 | kind last-weekday-of-month, interval 1, day —, weekday 5, 2026-09-01, 4 | → | 2026-09-25, 2026-10-30, 2026-11-27, 2026-12-25 |
| last Friday, starting after September's has passed | kind last-weekday-of-month, interval 1, day —, weekday 5, 2026-09-26, 2 | → | 2026-10-30, 2026-11-27 |
| last Sunday when the month ends on a Sunday is the last day itself | kind last-weekday-of-month, interval 1, day —, weekday 7, 2026-05-01, 3 | → | 2026-05-31, 2026-06-28, 2026-07-26 |
Show the other 15 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| last Friday every three months | kind last-weekday-of-month, interval 3, day —, weekday 5, 2026-09-26, 3 | → | 2026-10-30, 2027-01-29, 2027-04-30 |
| every two weeks from a Wednesday | kind every-weeks, interval 2, day —, weekday —, 2026-09-23, 3 | → | 2026-09-23, 2026-10-07, 2026-10-21 |
| every ten days across the end of February | kind every-days, interval 10, day —, weekday —, 2026-02-25, 3 | → | 2026-02-25, 2026-03-07, 2026-03-17 |
| yearly from 29 February: 28 February in ordinary years, 29 February again in 2028 | kind yearly, interval 1, day —, weekday —, 2024-02-29, 5 | → | 2024-02-29, 2025-02-28, 2026-02-28, 2027-02-28, 2028-02-29 |
| every two years | kind yearly, interval 2, day —, weekday —, 2026-09-23, 3 | → | 2026-09-23, 2028-09-23, 2030-09-23 |
| a count of zero is an empty schedule | kind monthly-on-day, interval 1, day 31, weekday —, 2026-01-01, 0 | → | |
| a negative count is an error | kind every-days, interval 1, day —, weekday —, 2026-01-01, -1 | → | error: count must be a non-negative integer |
| an interval of zero is an error | kind every-weeks, interval 0, day —, weekday —, 2026-01-01, 3 | → | error: interval must be a positive integer |
| monthly-on-day without a day is an error | kind monthly-on-day, interval 1, day —, weekday —, 2026-01-01, 3 | → | error: day must be 1-31 for monthly-on-day |
| day 32 is an error | kind monthly-on-day, interval 1, day 32, weekday —, 2026-01-01, 3 | → | error: day must be 1-31 for monthly-on-day |
| a weekday on a monthly-on-day rule is an error, not ignored | kind monthly-on-day, interval 1, day 15, weekday 5, 2026-01-01, 3 | → | error: weekday does not apply to monthly-on-day |
| weekday 8 is an error | kind last-weekday-of-month, interval 1, day —, weekday 8, 2026-01-01, 3 | → | error: weekday must be 1-7 for last-weekday-of-month |
| a day on an every-days rule is an error | kind every-days, interval 1, day 3, weekday —, 2026-01-01, 3 | → | error: day does not apply to every-days |
| an unknown kind is an error | kind fortnightly, interval 1, day —, weekday —, 2026-01-01, 3 | → | error: unknown recurrence kind "fortnightly" |
| a schedule running past 9999 is an error | kind every-days, interval 1, day —, weekday —, 9999-12-30, 3 | → | error: outside the supported range |
More from the author
- `monthly-on-day`: day `day` of every `interval`-th month, clamped to the month's last day. Monthly on the 31st is 31 Jan, 28 Feb, 31 Mar, 30 Apr: each date is clamped from the rule's day, never from the previous date, so a short February does not drag every later month to the 28th. That drift is the classic bug of adding one month to the last date. - `last-weekday-of-month`: the last `weekday` (ISO, 1 = Monday, 5 = Friday) of every `interval`-th month: the last Friday is payday in many UK payrolls. - `every-days` / `every-weeks`: the start date, then every `interval` days or weeks after it. - `yearly`: the start date's month and day every `interval` years, computed as start + 12k months with dates.add-months, so a 29 February anniversary is 28 February in ordinary years and 29 February again in leap years.
Where the schedule starts. For every-days, every-weeks and yearly, the start date is the first occurrence. For the two monthly kinds the start date is a lower bound: the first occurrence is the first matching date on or after it (in the start month if it has not passed yet, otherwise the next month), and the interval counts from that month. Quarterly on the 15th from 20 January is 15 February, 15 May, 15 August.
Dates are returned ascending, and count 0 gives an empty list. A rule that sets a field its kind does not use (a weekday on a monthly-on-day rule) is an error rather than silently ignored, since it almost always means the wrong kind was chosen. A schedule that would run past 9999-12-31 is an error. Business-day adjustment of the dates is a separate step (dates.add-business-days).
Files
| Path | Bytes |
|---|---|
| README.md | 1,855 |
| impl/python.py | 3,833 |
| impl/rust.rs | 4,775 |
| impl/typescript.ts | 3,712 |
| vectors.json | 5,383 |