# `CalendarPeriod`

An amount of calendar time in years, months and days: `P1Y2M10D`.

Use it for "one month from now", where a month is a month however many days it has. For an exact amount of time, use [`Duration`](/docs/types/type-duration.md).

## Shape

| Field | Type | Required? |
|---|---|---|
| `years` | `Int` | Yes |
| `months` | `Int` | Yes |
| `days` | `Int` | Yes |

## Example

```jadpo
CalendarPeriod { years: 1 months: 2 days: 10 }
```

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

This operation gives one: `temporal.period_between`.

## Good to know

- Two values are equal only part by part, so one month doesn't equal 30 days, and `<` doesn't take them.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it, so sort by one of its parts instead.

## In JSON

A string, in a request, a response or `json.write`:

```json
"P1Y2M10D"
"P1M"
"P-1M-3D"
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`CalendarPeriod` is in the group Time and calendar, and has been in Jadpo since 0.1.0.

### Text form

`text.from` writes one standard text form, the same form `text.parse` reads back: `P1Y2M10D`, `P1M` and `P-1M-3D`.

### How it is stored

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` |

### How it is 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.

It also takes `text.parse`, `text.from`, `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
