Functional Weave
Code in Rust

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 28
  • recurrence(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 February
  • recurrence(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.

pub fn recurrence(rule: &RecurrenceRule, start_iso: &str, count: i64) -> Vec<String>
ruleRecurrenceRulewhat repeats and how often
start_isodatethe first date the schedule may produce; also the anchor for every-days, every-weeks and yearly
countinthow many dates to return, 0 or more
returnsdate[]the first count dates on or after startIso, ascending

The types it declares, generated into your project

// RecurrenceKind is a string in Rust, one of: "monthly-on-day", "last-weekday-of-month", "every-days", "every-weeks", "yearly".
// Parameters take it as &str and results hold it as String.

/// A repeating date rule. Only the field its kind uses may be set; the other is null.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecurrenceRule {
    pub kind: String,
    /// every how many months, days, weeks or years; 1 or more
    pub interval: i64,
    /// monthly-on-day only: day of the month, 1 to 31, clamped to shorter months
    pub day: Option<i64>,
    /// last-weekday-of-month only: 1 = Monday ... 7 = Sunday
    pub weekday: Option<i64>,
}

Your code names it in one line, in the file that uses it

fune!(dates.recurrence@^1);  // then call recurrence(…)
impl/rust.rs · 130 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::dates_add_days::{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
use super::dates_add_months::add_months;  ← from dates.add-months ^1.0.0 · built alongside by fune
use super::dates_day_of_week::day_of_week;  ← from dates.day-of-week ^1.0.0 · built alongside by fune

fn check_range(name: &str, value: i64, low: i64, high: i64, kind: &str) {
    if value < low || value > high {
        panic!("{} must be {}-{} for {}, received {}", name, low, high, kind, value);
    }
}

/// The date of the `day`-th (clamped) or last `weekday` in a month index counted from year 0.
fn date_in_month(index: i64, rule: &RecurrenceRule) -> i64 {
    let year = index.div_euclid(12);
    let month = index.rem_euclid(12) + 1;
    if year > 9999 {
        panic!("the schedule runs past 9999-12-31, outside the supported range 0001-01-01 to 9999-12-31");
    }
    let length = days_in_month(year, month);
    if rule.kind == "monthly-on-day" {
        return days_from_civil(year, month, rule.day.unwrap().min(length));
    }
    let last = days_from_civil(year, month, length);
    let last_weekday = day_of_week(&iso_from_epoch_day(last));
    last - (last_weekday - rule.weekday.unwrap()).rem_euclid(7)
}

/// 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.
///
/// # Panics
/// Panics on an invalid rule, a malformed start date, a negative count, or a
/// schedule running past 9999-12-31.
pub fn recurrence(rule: &RecurrenceRule, start_iso: &str, count: i64) -> Vec<String> {
    if count < 0 {
        panic!("count must be a non-negative integer, received {}", count);
    }
    if rule.interval < 1 {
        panic!("interval must be a positive integer, received {}", rule.interval);
    }
    let kind = rule.kind.as_str();
    match kind {
        "monthly-on-day" => {
            match rule.day {
                None => panic!("day must be 1-31 for monthly-on-day, received null"),
                Some(day) => check_range("day", day, 1, 31, kind),
            }
            if rule.weekday.is_some() {
                panic!("weekday does not apply to monthly-on-day");
            }
        }
        "last-weekday-of-month" => {
            match rule.weekday {
                None => panic!("weekday must be 1-7 for last-weekday-of-month, received null"),
                Some(weekday) => check_range("weekday", weekday, 1, 7, kind),
            }
            if rule.day.is_some() {
                panic!("day does not apply to last-weekday-of-month");
            }
        }
        "every-days" | "every-weeks" | "yearly" => {
            if rule.day.is_some() {
                panic!("day does not apply to {}", kind);
            }
            if rule.weekday.is_some() {
                panic!("weekday does not apply to {}", kind);
            }
        }
        _ => panic!(
            "unknown recurrence kind \"{}\": expected monthly-on-day, last-weekday-of-month, every-days, every-weeks or yearly",
            kind
        ),
    }

    let start = epoch_day_from_iso(start_iso);
    let mut out: Vec<String> = Vec::new();
    match kind {
        "every-days" | "every-weeks" => {
            let step = if kind == "every-weeks" { 7 * rule.interval } else { rule.interval };
            for k in 0..count {
                out.push(iso_from_epoch_day(start + k * step));
            }
        }
        "yearly" => {
            // Always from the anchor, so 29 February comes back in leap years.
            for k in 0..count {
                out.push(add_months(start_iso, 12 * rule.interval * k));
            }
        }
        _ => {
            let date = parse_iso_date(start_iso);
            let mut first = date.year * 12 + (date.month - 1);
            if count > 0 && date_in_month(first, rule) < start {
                first += 1;
            }
            for k in 0..count {
                out.push(iso_from_epoch_day(date_in_month(first + k * rule.interval, rule)));
            }
        }
    }
    out
}

fn optional_int(value: &Value) -> Option<i64> {
    if value.is_null() {
        None
    } else {
        Some(value.as_i64())
    }
}

pub fn recurrence_rule_from_value(value: &Value) -> RecurrenceRule {
    RecurrenceRule {
        kind: value.get("kind").as_str().to_string(),
        interval: value.get("interval").as_i64(),
        day: optional_int(value.get("day")),
        weekday: optional_int(value.get("weekday")),
    }
}

pub fn fune_vector(args: &[Value]) -> Value {
    let rule = recurrence_rule_from_value(&args[0]);
    Value::Arr(
        recurrence(&rule, args[1].as_str(), args[2].as_i64())
            .iter()
            .map(|d| Value::str(d))
            .collect(),
    )
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 3 dependencies, pins them in fune.lock, downloads only the Rust 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. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add dates.recurrence
Download for Rust dates.recurrence-1.0.0-rust.fune · 15,608 bytes sha256 5cac59762086907a9d3e2f560934b9564026e2e1b6f2bc767b66d68ca1567106

The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./dates.recurrence-1.0.0-rust.fune, or fetch it from a terminal with fune pull dates.recurrence@1.0.0:rust.

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.

CaseArgumentsExpected
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
CaseArgumentsExpected
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

PathBytes
README.md1,855
impl/python.py3,833
impl/rust.rs4,775
impl/typescript.ts3,712
vectors.json5,383