# `CalendarPeriod`

An amount of calendar time in whole years, months and days, written `P1Y2M10D`: unlike a `Duration`, a month is a month, however many days it has. It has no order, and `==` compares part by part.

[Time and calendar](types.md#time-and-calendar); not ordered; since Jadpo 0.1.0.

## Make one

Build one from its fields, `CalendarPeriod { years: ...  months: ...  days: ... }`.

`text.parse(raw, as: CalendarPeriod)` reads one from its text; text that isn't one at all fails `malformed`, such as `1 month`, `P` or `PT1H`.

This operation gives one: `temporal.period_between`.

## Fields

| Field | Type |
|---|---|
| `years` | `Int` |
| `months` | `Int` |
| `days` | `Int` |

## Text, JSON and storage

`text.from` writes its canonical text, the one form `text.parse` reads back. JSON sends its canonical text as a string.

| Text | JSON |
|---|---|
| `P1Y2M10D` | `"P1Y2M10D"` |
| `P1M` | `"P1M"` |
| `P-1M-3D` | `"P-1M-3D"` |
| `P0D` | `"P0D"` |

It is stored in 3 columns, each named for the field, `_` and its suffix. This is `P-1M-3D`:

| Column | Type | Holds |
|---|---|---|
| `<field>_years` | `integer` | `0` |
| `<field>_months` | `integer` | `-1` |
| `<field>_days` | `integer` | `-3` |

## Shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `P1Y2M10D` | `1 year, 2 months, 10 days` |
| `P1M` | `1 month` |
| `P-1M-3D` | `-1 month, -3 days` |
| `P0D` | `0 days` |

## Operations

- `temporal.period_between(start: CalendarDate, end: CalendarDate) -> CalendarPeriod`: Whole years, months and days from `start` to `end`, so `start` plus the period is `end`; each part negative when `end` is earlier.

Like every standard type, it also has the contract's own operations: `text.parse`, `text.from`, `text.format`, `json.read`, `json.write` and `==`. It isn't ordered, so `<` and sorting don't take it. [Every standard type meets one contract](../topics/every-standard-type-meets-one-contract.md) says how they work.
