Functional Weave
Code in Rust

health.gestational-age@1.0.1

README.md

4,301 bytes · view raw

# health.gestational-age

Status: needs review and sign-off by a qualified clinician before it is published. Not a medical device; for decision support only; always follow local clinical guidelines.

Gestational age on a date, as completed weeks plus days (12+3), and the
estimated due date (EDD), from the first day of the last menstrual period
(LMP). This is the LMP estimate only: NICE recommends dating a pregnancy by
ultrasound between 11+2 and 14+1 weeks, and once a dating scan has been done
its dates supersede these.

**The rule.** Naegele's rule puts the due date 280 days (40+0 weeks) after the
LMP, which assumes a 28-day cycle with ovulation on day 14. A longer or
shorter cycle moves ovulation, so both the due date and the gestational age
are shifted by the difference from 28 days:

    dueDate   = LMP + 280 + (cycleLengthDays - 28)
    totalDays = (onDate - LMP) - (cycleLengthDays - 28)
    weeks     = totalDays div 7,  days = totalDays mod 7

With a 35-day cycle the due date is a week later and the pregnancy is a week
less advanced on any date. Pass 28 for the unadjusted rule. This is exactly
what the NHS due date calculator does, and its published answers are among
the vectors (LMP 15 March 2026: due 20 December 2026 on a 28-day cycle, 27
December on 35 days, 12 December on 20 days, 5 January 2027 on 44 days).

**Limits, and why they are errors.**

- `cycleLengthDays` must be a whole number from 20 to 44, the range the NHS
  calculator accepts.
- A measuring date before the adjusted start of pregnancy (the LMP plus the
  cycle shift) is an error, not a negative age: with a 35-day cycle, three
  days after the LMP is not yet 0+0.
- A gestational age above 44+0 weeks (308 days) is an error asking for the
  dates to be checked. Pregnancy normally lasts 37 to 42 weeks from the LMP;
  44 weeks is a guard rail against a wrong year or a transposed date, not a
  clinical threshold, and a clinician should confirm it.

Dates are ISO `YYYY-MM-DD`; the measuring date is always an argument, never
today's date. Leap days are counted like any other day.

## Sources

- NHS, "Due date calculator" (nhs.uk): takes the first day of the last period
  and a cycle length that "must be between 20 and 44 days"; pregnancy
  "normally lasts from 37 weeks to 42 weeks from the first day of your last
  period". Its outputs for the dates above were checked on 23 September 2026.
  https://www.nhs.uk/pregnancy/finding-out/due-date-calculator/
- NICE guideline NG201, "Antenatal care" (2021), recommendation 1.2.14: offer
  an ultrasound scan between 11+2 and 14+1 weeks to determine gestational age.
  https://www.nice.org.uk/guidance/ng201/chapter/Recommendations

## Before you rely on this

**Not professional advice.** This capability calculates health figures from published rules. It is a software component for developers, not medical 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 midwife or obstetrician review how you use it, before anyone relies on the output. Provided "as is" under its licence, without warranty.

**Not a medical device.** It is not intended to diagnose, treat or support clinical decisions about any individual. Anyone building it into clinical software is responsible for that software's regulatory status, and must validate it under their own clinical governance.

**Not for clinical use; regulatory review pending.** Whether publishing this capability makes it a medical device is under regulatory review. Until that is settled it is a developer library only.

**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 midwife or obstetrician 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 (not for clinical use; regulatory review pending). The code and the tests are unchanged.