# punctualityRate

Industry: Transport

## Signature

```ts
punctualityRate(pairs: PlannedActual[], tolerance: PunctualityTolerance): OnTimeRate | null
```

The share of planned/actual pairs that were on time under one tolerance.

- A rate is a count under a stated tolerance: the same arrivals give a different rate under a 60-minute tolerance than under a 120-minute one, so the tolerance travels with the number.
- Each pair is classified by `classifyPunctuality`'s rule under the same tolerance: `late` and `early` are read once, before any pair, so the whole list is judged under one tolerance. Only `"onTime"` counts: with an `early` tolerance, an early arrival is **not** on time; without one, it is.
- `rate` is `onTime / total` as a plain number (`0.75`), not rounded.
- Every pair must be valid: one invalid pair returns `null`, never a rate over the rest.
- An empty list returns `null`: no arrivals have no rate.
- Anything beyond a rate — percentiles, causes — is the consumer's.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `pairs` | `PlannedActual[]` | the planned and actual time of each arrival |
| `tolerance` | `PunctualityTolerance` | late (required) and early (optional) tolerances, as classifyPunctuality takes them |

## Returns

the on-time count, the total and the rate (null for an empty list), or null on invalid input

## Types

### PlannedActual

One planned/actual pair for `punctualityRate`.

```ts
import type { PlannedActual } from "@northguild/gmt";
```

| Member | Type | Description |
| --- | --- | --- |
| `planned` | `string` | The planned time, as an ISO 8601 instant string (`Z` or `±HH:MM`); a zoned string is not one. |
| `actual` | `string` | The actual time, as an ISO 8601 instant string (`Z` or `±HH:MM`); a zoned string is not one. |

### OnTimeRate

What `punctualityRate` returns: the on-time count, the total, and their ratio.

```ts
import type { OnTimeRate } from "@northguild/gmt";
```

| Member | Type | Description |
| --- | --- | --- |
| `onTime` | `number` | The number of pairs `classifyPunctuality` calls `"onTime"`. |
| `total` | `number` | Every pair counted. |
| `rate` | `number` | The on-time share, `onTime / total`, from 0 to 1. |

## Related types

- [`PunctualityTolerance`](/gmt/reference/types/PunctualityTolerance/)

## Examples

```ts
import { punctualityRate } from "@northguild/gmt/transport/calculate";

punctualityRate([{ planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:05:00Z" }, { planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:14:00Z" }, { planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T10:16:00Z" }, { planned: "2024-06-15T10:00:00Z", actual: "2024-06-15T09:57:00Z" }], { late: "PT15M" }) // { onTime: 3, total: 4, rate: 0.75 }
```

## Source

[transport/calculate/punctualityRate.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/transport/calculate/punctualityRate.ts)
