# dutyTotals

Industry: Workforce

## Signature

```ts
dutyTotals(log: DutyEntry[], window: Interval, statuses: string[]): string
```

Return the time a duty log spends in any of `statuses` inside a window, as an exact ISO 8601 duration.

- The interval algebra of CORE-6: the entries in `statuses` are merged, each run is cut to `window` (half-open), and what is left is summed. An entry that crosses the window's edge counts only its part inside.
- Hours are the largest unit, as `sumIntervals` writes it: `"PT49H30M"`, never days.
- `[]` as `statuses`, an empty log or an empty window gives `"PT0S"`.
- Returns `""` when the log fails `isValidDutyLog`, when `window` is not a valid `Interval`, and when `statuses` is not an array of strings.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `log` | `DutyEntry[]` | the duty log, ordered and non-overlapping |
| `window` | `Interval` | `{ start, end }` record of ISO 8601 instant strings to total inside |
| `statuses` | `string[]` | the statuses whose time is totalled |

## Returns

ISO 8601 duration string, or "" on invalid input

## Related types

- [`DutyEntry`](/gmt/reference/types/DutyEntry/)
- [`Interval`](/gmt/reference/types/Interval/)

## Examples

```ts
import { dutyTotals } from "@northguild/gmt/workforce/calculate";

dutyTotals([{ status: "driving", start: "2024-01-01T10:00:00Z", end: "2024-01-01T15:00:00Z" }, { status: "onDuty", start: "2024-01-01T15:00:00Z", end: "2024-01-01T17:00:00Z" }, { status: "driving", start: "2024-01-01T17:00:00Z", end: "2024-01-01T20:00:00Z" }], { start: "2024-01-01T00:00:00Z", end: "2024-01-02T00:00:00Z" }, ["driving"]) // "PT8H"
```

## Source

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