Functional Weave
Code in TypeScript

legal.time-billing Unreviewed

Fees from time entries: each rounded to 6-minute (or other) units and priced at the fee earner's hourly rate.

1.0.1 · published 2026-10-03 by charlie · Anterra

Pinned by 15 tests, run in TypeScript, Python and Rust.

Unreviewed. This capability’s implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified solicitor has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.

Not professional advice. This capability calculates legal figures from published rules. It is a software component for developers, not legal advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed in its README, and have a solicitor review how you use it, before anyone relies on the output. Provided “as is” under its licence, without warranty.

What it does

The time charges for a bill: every time entry rounded to whole billing units and priced at its fee earner's hourly rate from a rate card.

**Units.** Law firms record and bill in units of 6 minutes (a tenth of an hour), and the usual engagement-letter term is that a part unit is charged as a whole one: a 20-minute call is 4 units, 24 minutes. The unit and the rounding are arguments (`unitMinutes`, `mode`), so 15-minute units or rounding to the nearest unit work too. The rounding is time.round-to-increment's, applied to **each entry**, never to the day's or the matter's total: seven 1-minute emails are seven units, not two. That is what "each item of work is recorded in units" means, and why totalling first gives a smaller, wrong bill.

For example

  • timeBilling(entries ×4, rates ×3, 6, up) → lines ×4, by fee earner ×3, units 18, billed minutes 108, total £467.00 a matter's time in 6-minute units, part units charged in full
  • timeBilling(entries ×4, rates ×3, 6, half-up) → lines ×4, by fee earner ×3, units 15, billed minutes 90, total £375.00 the same time rounded to the nearest unit
  • timeBilling(entries ×7, rates ×3, 6, up) → lines ×7, by fee earner ×1, units 7, billed minutes 42, total £154.00 seven 1-minute emails are seven units, not the two a rounded total gives

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.

export function timeBilling(entries: readonly TimeEntry[], rates: readonly FeeEarnerRate[], unitMinutes: number, mode: RoundingMode): TimeBill
entriesTimeEntry[]time recorded, in the order it should appear on the bill
ratesFeeEarnerRate[]the rate card: one hourly rate per fee earner, all in one currency
unitMinutesintthe billing unit: 6 for tenths of an hour, 15 for quarter hours
modeRoundingModehow a part unit is billed: up (the usual engagement-letter term), half-up, half-even or down
returnsTimeBill

The types it declares, generated into your project

/** One piece of recorded time. */
export interface TimeEntry {
  /** the fee earner's code, as the rate card has it */
  readonly feeEarner: string;
  /** time actually spent, 0 or more */
  readonly minutes: number;
}

/** One line of a rate card. */
export interface FeeEarnerRate {
  readonly feeEarner: string;
  readonly hourlyRate: Money;
}

/** One time entry, priced. */
export interface TimeBillLine {
  readonly feeEarner: string;
  /** as recorded */
  readonly minutes: number;
  /** rounded to whole units */
  readonly billedMinutes: number;
  readonly units: number;
  readonly hourlyRate: Money;
  /** billed minutes at the hourly rate, rounded half-up to the minor unit */
  readonly fee: Money;
}

/** One fee earner's share of the bill. */
export interface FeeEarnerTotal {
  readonly feeEarner: string;
  readonly units: number;
  readonly fee: Money;
}

/** The time charges for a bill. */
export interface TimeBill {
  /** in entry order */
  readonly lines: readonly TimeBillLine[];
  /** in order of first appearance in the entries */
  readonly byFeeEarner: readonly FeeEarnerTotal[];
  readonly units: number;
  readonly billedMinutes: number;
  /** the sum of the line fees */
  readonly total: Money;
}

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

import { timeBilling } from "#fune/legal.time-billing@^1";
impl/typescript.ts · 59 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.

import { roundDiv, type RoundingMode } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune
import { type Money, money } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune
import { roundToIncrement } from "./time_round_to_increment.ts";  ← from time.round-to-increment ^1.0.0 · built alongside by fune
import { type FeeEarnerRate, type FeeEarnerTotal, type TimeBill, type TimeBillLine, type TimeEntry } from "./legal_time_billing_types.ts";

/** Price time entries in whole billing units at each fee earner's hourly rate. */
export function timeBilling(entries: readonly TimeEntry[], rates: readonly FeeEarnerRate[], unitMinutes: number, mode: RoundingMode): TimeBill {
  if (rates.length === 0) {
    throw new RangeError("rates must not be empty");
  }
  const currency = rates[0].hourlyRate.currency;
  const card = new Map<string, Money>();
  for (const rate of rates) {
    if (card.has(rate.feeEarner)) {
      throw new RangeError(`duplicate rate for fee earner "${rate.feeEarner}"`);
    }
    if (rate.hourlyRate.currency !== currency) {
      throw new RangeError(`currency mismatch: ${currency} and ${rate.hourlyRate.currency}`);
    }
    if (rate.hourlyRate.minor < 0) {
      throw new RangeError(`hourly rates must not be negative, received ${rate.hourlyRate.minor}`);
    }
    card.set(rate.feeEarner, rate.hourlyRate);
  }

  const lines: TimeBillLine[] = [];
  const order: string[] = [];
  const totals = new Map<string, { units: number; fee: number }>();
  let units = 0;
  let billedMinutes = 0;
  let total = 0;
  for (const entry of entries) {
    const hourlyRate = card.get(entry.feeEarner);
    if (hourlyRate === undefined) {
      throw new RangeError(`no rate for fee earner "${entry.feeEarner}"`);
    }
    // Rounded per entry: seven 1-minute emails are seven units, not two.
    const billed = roundToIncrement(entry.minutes, unitMinutes, mode);
    const lineUnits = billed / unitMinutes;
    const fee = roundDiv(billed * hourlyRate.minor, 60, "half-up");
    lines.push({ feeEarner: entry.feeEarner, minutes: entry.minutes, billedMinutes: billed, units: lineUnits, hourlyRate, fee: money(fee, currency) });
    let t = totals.get(entry.feeEarner);
    if (t === undefined) {
      t = { units: 0, fee: 0 };
      totals.set(entry.feeEarner, t);
      order.push(entry.feeEarner);
    }
    t.units += lineUnits;
    t.fee += fee;
    units += lineUnits;
    billedMinutes += billed;
    total += fee;
  }
  const byFeeEarner: FeeEarnerTotal[] = order.map((code) => {
    const t = totals.get(code) as { units: number; fee: number };
    return { feeEarner: code, units: t.units, fee: money(t.fee, currency) };
  });
  return { lines, byFeeEarner, units, billedMinutes, total: money(total, currency) };
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 3 dependencies, pins them in fune.lock, downloads only the TypeScript 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 legal.time-billing
Download for TypeScript legal.time-billing-1.0.1-typescript.fune · 28,084 bytes sha256 26804d97e9a37c3217c4cf9e2a9292c031cb8be0cc3f95437d4cae6ec654b957

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./legal.time-billing-1.0.1-typescript.fune, or fetch it from a terminal with fune pull legal.time-billing@1.0.1:typescript.

The whole function, every language, is one file too: legal.time-billing-1.0.1.fune, 36,003 bytes, sha256 6b5652aae439ee26b499cf65290280d902f76bdb479118c9ae9ce73d33fa78ee. 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 legal.time-billing

after — your function gets the result and the arguments, and returns the final result.

// fune: after legal.time-billing

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 math.round-div in legal.time-billing
// fune: replace money.amount in legal.time-billing
// fune: replace time.round-to-increment in legal.time-billing

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 legal.time-billing --steps.

// fune: step legal.time-billing 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
a matter's time in 6-minute units, part units charged in full entries ×4, rates ×3, 6, up → lines ×4, by fee earner ×3, units 18, billed minutes 108, total £467.00
the same time rounded to the nearest unit entries ×4, rates ×3, 6, half-up → lines ×4, by fee earner ×3, units 15, billed minutes 90, total £375.00
seven 1-minute emails are seven units, not the two a rounded total gives entries ×7, rates ×3, 6, up → lines ×7, by fee earner ×1, units 7, billed minutes 42, total £154.00
quarter-hour units entries ×2, rates ×3, 15, up → lines ×2, by fee earner ×1, units 5, billed minutes 75, total £437.50
a unit at an odd hourly rate rounds half-up to the penny: 177.77 an hour is 17.78 a unit entries ×2, rates ×1, 6, up → lines ×2, by fee earner ×1, units 2, billed minutes 12, total £35.56
a zero-minute entry is kept as a nil line entries ×1, rates ×3, 6, up → lines ×1, by fee earner ×1, units 0, billed minutes 0, total £0.00
no entries bill nothing, in the rate card's currency , rates ×1, 6, up → lines , by fee earner , units 0, billed minutes 0, total €0.00
a fee earner's summary follows their first entry entries ×3, rates ×3, 6, up → lines ×3, by fee earner ×2, units 4, billed minutes 24, total £80.00
an entry whose fee earner has no rate is an error entries ×1, rates ×3, 6, up → error: no rate for fee earner "XYZ"
an empty rate card is an error , , 6, up → error: rates must not be empty
Show the other 5 tests
CaseArgumentsExpected
a fee earner twice on the rate card is an error , rates ×2, 6, up → error: duplicate rate for fee earner "PTN"
a rate card in two currencies is an error , rates ×2, 6, up → error: currency mismatch
a negative hourly rate is an error , rates ×1, 6, up → error: hourly rates must not be negative
negative minutes are an error entries ×1, rates ×3, 6, up → error: minutes must be a non-negative integer
a zero-minute unit is an error entries ×1, rates ×3, 0, up → error: increment must be a positive integer

More from the author

**Fees.** Each line's fee is billed minutes x hourly rate / 60, rounded half-up to the minor unit, and the bill total is the sum of the lines, so the lines on the bill add up to its total exactly. The summary per fee earner (units and fee) is in order of each fee earner's first entry.

**Rate card.** One rate per fee earner code, all in one currency; a duplicate code, an entry with no rate, a negative rate or mixed currencies is an error. A rate card that changes during a matter is the caller's job: split the entries by the date the new rates took effect and bill each part with its own card. VAT is not added here; pass the total to finance.tax.add-vat.

## Before you rely on this

**Not professional advice.** This capability calculates legal figures from published rules. It is a software component for developers, not legal advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed above, and have a solicitor review how you use it, before anyone relies on the output. Provided "as is" under its licence, without warranty.

**Unreviewed.** This capability's implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified solicitor has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.

1.0.1 marks it unreviewed. The code and the tests are unchanged.

Files

PathBytes
README.md2,497
impl/python.py2,664
impl/rust.rs4,978
impl/typescript.ts2,626
vectors.json15,636