Functional Weave
Code in TypeScript

health.waiting-time@1.0.1

impl/python.py

1,529 bytes · the Python implementation · view 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 Optional

from .dates_add_days import add_days, days_between, parse_iso_date  ← from dates.add-days ^1.0.0 · built alongside by fune
from .health_waiting_time_types import RttWait

#: 18 weeks: a wait of exactly 126 days is still within the standard.
_EIGHTEEN_WEEKS_DAYS = 126


def rtt_waiting_time(clock_start: str, clock_stop: Optional[str], on_date: str) -> RttWait:
    """The length of a referral-to-treatment pathway, counted from the clock
    start to the clock stop, or to ``on_date`` while the patient is still
    waiting. There are no pauses: NHS England removed them in 2015."""
    parse_iso_date(clock_start)
    parse_iso_date(on_date)
    if on_date < clock_start:
        raise ValueError("onDate %s is before clockStart %s" % (on_date, clock_start))
    if clock_stop is not None:
        parse_iso_date(clock_stop)
        if clock_stop < clock_start:
            raise ValueError("clockStop %s is before clockStart %s" % (clock_stop, clock_start))
        if clock_stop > on_date:
            raise ValueError("clockStop %s is after onDate %s" % (clock_stop, on_date))
    days = days_between(clock_start, clock_stop if clock_stop is not None else on_date)
    return RttWait(
        days=days,
        completed_weeks=days // 7,
        # The 0-1 week band holds eight days (0 to 7), every later band seven.
        week_band=1 if days <= 7 else (days + 6) // 7,
        breached18_weeks=days > _EIGHTEEN_WEEKS_DAYS,
        eighteen_week_date=add_days(clock_start, _EIGHTEEN_WEEKS_DAYS),
        clock_stopped=clock_stop is not None,
    )