# Duration

Industry: Core

An amount of time has two shapes: an ISO 8601 duration string (`"PT15M"`) and a unit object
(`{ minutes: 15 }`). The `duration/` functions take the string, except `totalUnits` and
`durationFromUnits`, which take the object. The date arithmetic functions (`addDate`,
`subtractZoned` and their siblings) take the object. Passing the wrong shape is the most common
source of duration bugs.

## Mistakes

### Confusing duration strings with unit objects

Severity: high.

'P1Y2M' and { years: 1, months: 2 } are one amount in two shapes, and a function takes only one of them. The other shape returns the invalid-input sentinel. When you hold a unit object and the function takes a duration string, write the string with durationFromUnits.

Wrong:
```js
formatDuration({ years: 1, months: 2 }, "en"); // "" — a unit object, not a duration string
addDate("2024-01-15", "P1Y2M"); // "" — a duration string, not a unit object
```

Right:
```js
import { addDate, durationFromUnits, formatDuration } from "@northguild/gmt";

durationFromUnits({ years: 1, months: 2 }); // "P1Y2M"
formatDuration(durationFromUnits({ years: 1, months: 2 }), "en"); // "1 year and 2 months"
addDate("2024-01-15", { years: 1, months: 2 }); // "2025-03-15"
```

### Not validating before parsing

Severity: medium.

parseDuration returns an empty string for invalid input. Feeding unvalidated user input straight into duration arithmetic produces silent failures.

Wrong:
```js
const d = parseDuration(userInput); // userInput = "PX" → "" — no error thrown
```

Right:
```js
import { isValidDuration, parseDuration } from "@northguild/gmt";

if (isValidDuration(userInput)) {
  const d = parseDuration(userInput);
}
```

### Expecting a best-effort answer for calendar units without relativeTo

Severity: medium.

Converting a calendar-unit duration (years, months) to days requires a reference date. Without relativeTo, the conversion is ambiguous — different months have different lengths.

Wrong:
```js
totalDuration("P1M", "days"); // null — 28, 29, 30 or 31 days: without relativeTo, which month?
```

Right:
```js
import { totalDuration } from "@northguild/gmt";

totalDuration("P1M", "days", { relativeTo: "2024-02-01" }); // 29 — February 2024 has 29 days
```

### Giving totalUnits a week

Severity: medium.

totalUnits takes exact units only: days, hours, minutes, seconds, milliseconds, microseconds and nanoseconds. A week, a month or a year returns null, as a key or as the unit to total into, because TC39 Temporal gives those units no length without a date to measure from. Write a week as seven days. For a calendar amount, use totalDuration with relativeTo.

Wrong:
```js
totalUnits({ weeks: 1 }, "days"); // null — a week is not an exact unit
totalUnits({ days: 7 }, "weeks"); // null — the same rule for the unit to total into
```

Right:
```js
import { totalDuration, totalUnits } from "@northguild/gmt";

totalUnits({ days: 7 }, "hours"); // 168 — a day counts as 24 hours
totalDuration("P1W", "days", { relativeTo: "2024-02-01" }); // 7 — a week measured from a date
```

### Reading a component with getDurationUnit when you wanted a total

Severity: high.

getDurationUnit returns one component of a normalized duration (e.g. just the hours). If you need the total duration in a specific unit, use totalDuration instead.

Wrong:
```js
const d = normalizeDuration("P1DT2H3M");
const hours = getDurationUnit(d, "hours"); // 2 — just the hours component, not the total
```

Right:
```js
import { normalizeDuration, totalDuration } from "@northguild/gmt";

const d = normalizeDuration("P1DT2H3M");
const totalHours = totalDuration(d, "hours"); // 26.05 — full duration converted, 3 minutes included
```

### Using getUtcNow() as a relativeTo

Severity: medium.

A Z string is an instant with no time zone, and Temporal refuses it as relativeTo (GetTemporalRelativeToOption), so the call returns null. To start from now, pass getZonedNow('UTC'). For a length counted from now, totalUnitsFromNow does it without a relativeTo.

Wrong:
```js
totalDuration("P1Y", "seconds", { relativeTo: getUtcNow() }); // null — a Z string is refused
```

Right:
```js
import { getZonedNow, totalDuration } from "@northguild/gmt";

totalDuration("P1Y", "seconds", { relativeTo: getZonedNow("UTC") }); // 31622400 — from a clock at 2027-06-15T12:07:30Z
```

### Writing a year as 31_536_000 seconds

Severity: medium.

A year is 365 or 366 days depending on where the count starts. A hand-written 31_536_000 is 365 days, so it falls a day short when the next 12 months take in a 29 February. Let totalUnitsFromNow count from the clock.

Wrong:
```js
const maxAge = 31_536_000; // 365 days, always
```

Right:
```js
import { totalUnitsFromNow } from "@northguild/gmt";

totalUnitsFromNow({ years: 1 }, "seconds"); // 31622400 — from a clock at 2027-06-15T12:07:30Z, 366 days
```

### Expecting normalizeDuration to resolve calendar units against today

Severity: medium.

normalizeDuration has no implicit relativeTo. Whenever a calendar unit (years, months, weeks) is involved, in the duration or as largestUnit, it needs an explicit relativeTo; without one it returns the empty-string sentinel, never a conversion against today's date.

Wrong:
```js
normalizeDuration("P1M", { largestUnit: "day" }); // "" — no relativeTo, so no answer
```

Right:
```js
import { normalizeDuration } from "@northguild/gmt";

normalizeDuration("P1M", { largestUnit: "day", relativeTo: "2024-02-01" }); // "P29D"
```

### Expecting formatDuration to match native Intl.DurationFormat output exactly

Severity: medium.

GMT's formatDuration renders each non-zero component with Intl.NumberFormat's unit style and joins them with Intl.ListFormat. Intl.DurationFormat output varies by locale, unit set, and style options in its own way. They are not drop-in replacements for each other.

Wrong:
```js
// assuming formatDuration("PT1H30M") matches Intl.DurationFormat
```

Right:
```js
import { formatDuration } from "@northguild/gmt";

formatDuration("PT1H30M", "en"); // "1 hour and 30 minutes" — joined as a list
```

### Passing an array of units to totalDuration

Severity: medium.

totalDuration takes a single unit name, not an array. Passing a list of unit names returns null.

Wrong:
```js
totalDuration("P1DT2H3M", ["hours", "minutes"]); // null — expects a single unit string
```

Right:
```js
import { totalDuration } from "@northguild/gmt";

totalDuration("P1DT2H3M", "hours"); // 26.05
totalDuration("P1DT2H3M", "minutes"); // 1563 — one call per unit
```

## See also

- [Durations guide](/gmt/guides/core-date-operations/durations/)
- [`isValidDuration` reference](/gmt/reference/duration/validate/isValidDuration/)
- [`parseDuration` reference](/gmt/reference/duration/parse/parseDuration/)
- [`normalizeDuration` reference](/gmt/reference/duration/normalize/normalizeDuration/)
- [`formatDuration` reference](/gmt/reference/duration/format/formatDuration/)
- [`totalDuration` reference](/gmt/reference/duration/calculate/totalDuration/)
- [`totalUnits` reference](/gmt/reference/duration/calculate/totalUnits/)
- [`durationFromUnits` reference](/gmt/reference/duration/calculate/durationFromUnits/)
- [`getDurationUnit` reference](/gmt/reference/duration/calculate/getDurationUnit/)
