Functional Weave
Code in Python

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

  • time_billing(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
  • time_billing(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
  • time_billing(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.

def time_billing(entries: Sequence[TimeEntry], rates: Sequence[FeeEarnerRate], unit_minutes: int, 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
unit_minutesintthe 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

@dataclass(frozen=True)
class TimeEntry:
    """One piece of recorded time."""

    #: the fee earner's code, as the rate card has it
    fee_earner: str
    #: time actually spent, 0 or more
    minutes: int

@dataclass(frozen=True)
class FeeEarnerRate:
    """One line of a rate card."""

    fee_earner: str
    hourly_rate: Money

@dataclass(frozen=True)
class TimeBillLine:
    """One time entry, priced."""

    fee_earner: str
    #: as recorded
    minutes: int
    #: rounded to whole units
    billed_minutes: int
    units: int
    hourly_rate: Money
    #: billed minutes at the hourly rate, rounded half-up to the minor unit
    fee: Money

@dataclass(frozen=True)
class FeeEarnerTotal:
    """One fee earner's share of the bill."""

    fee_earner: str
    units: int
    fee: Money

@dataclass(frozen=True)
class TimeBill:
    """The time charges for a bill."""

    #: in entry order
    lines: List[TimeBillLine]
    #: in order of first appearance in the entries
    by_fee_earner: List[FeeEarnerTotal]
    units: int
    billed_minutes: int
    #: the sum of the line fees
    total: Money

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

from fune.legal.time_billing import time_billing  # legal.time-billing@^1
impl/python.py · 58 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.

from typing import Dict, List, Sequence

from .legal_time_billing_types import FeeEarnerRate, FeeEarnerTotal, TimeBill, TimeBillLine, TimeEntry
from .math_round_div import round_div  ← from math.round-div ^1.0.0 · built alongside by fune
from .math_round_div_types import RoundingMode
from .money_amount import Money, money  ← from money.amount ^1.0.0 · built alongside by fune
from .time_round_to_increment import round_to_increment  ← from time.round-to-increment ^1.0.0 · built alongside by fune


def time_billing(entries: Sequence[TimeEntry], rates: Sequence[FeeEarnerRate], unit_minutes: int, mode: RoundingMode) -> TimeBill:
    """Price time entries in whole billing units at each fee earner's hourly rate."""
    if len(rates) == 0:
        raise ValueError("rates must not be empty")
    currency = rates[0].hourly_rate.currency
    card: Dict[str, Money] = {}
    for rate in rates:
        if rate.fee_earner in card:
            raise ValueError('duplicate rate for fee earner "%s"' % (rate.fee_earner,))
        if rate.hourly_rate.currency != currency:
            raise ValueError("currency mismatch: %s and %s" % (currency, rate.hourly_rate.currency))
        if rate.hourly_rate.minor < 0:
            raise ValueError("hourly rates must not be negative, received %s" % (rate.hourly_rate.minor,))
        card[rate.fee_earner] = rate.hourly_rate

    lines: List[TimeBillLine] = []
    order: List[str] = []
    totals: Dict[str, List[int]] = {}
    units = 0
    billed_minutes = 0
    total = 0
    for entry in entries:
        hourly_rate = card.get(entry.fee_earner)
        if hourly_rate is None:
            raise ValueError('no rate for fee earner "%s"' % (entry.fee_earner,))
        # Rounded per entry: seven 1-minute emails are seven units, not two.
        billed = round_to_increment(entry.minutes, unit_minutes, mode)
        line_units = billed // unit_minutes
        fee = round_div(billed * hourly_rate.minor, 60, "half-up")
        lines.append(
            TimeBillLine(
                fee_earner=entry.fee_earner,
                minutes=entry.minutes,
                billed_minutes=billed,
                units=line_units,
                hourly_rate=hourly_rate,
                fee=money(fee, currency),
            )
        )
        if entry.fee_earner not in totals:
            totals[entry.fee_earner] = [0, 0]
            order.append(entry.fee_earner)
        totals[entry.fee_earner][0] += line_units
        totals[entry.fee_earner][1] += fee
        units += line_units
        billed_minutes += billed
        total += fee
    by_fee_earner = [FeeEarnerTotal(fee_earner=c, units=totals[c][0], fee=money(totals[c][1], currency)) for c in order]
    return TimeBill(lines=lines, by_fee_earner=by_fee_earner, units=units, billed_minutes=billed_minutes, total=money(total, currency))

Install

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 legal.time-billing
Download for Python legal.time-billing-1.0.1-python.fune · 28,115 bytes sha256 cb84b1c33e73d278cefb0a9757de66e58511826f6e922eed27338fe73adc9f79

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

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