# Calendar (/en/docs/rust/calendar)

Months of a lunar year, Gregorian ↔ lunar conversion, and the stem-branch of a lunar year — on the same calendar core as charting.



Calendar lookups without casting a chart: which months a lunar year has, how long each is and which one is
leap, Gregorian ↔ lunar conversion, and the stem-branch of a lunar year. They run on the same calendar core as
charting (a function-by-function port of lunar-typescript), so they agree day for day with the chart's
`raw_dates.lunar_date` and year pillar; golden data generated by lunar-typescript checks every day of lunar years 1–9999.

Years 1–9999 are supported, Gregorian and lunar alike. Dates before 1582-10-15 are Julian, and 1582-10-05 to
10-14 do not exist.

<Callout title="Errors">
  The conversions that take a full date (`solar_to_lunar`, `lunar_to_solar`) return `IztroError::InvalidDate` for a date that does
  not exist, like the charting entry points; the lookups that take only a year (`lunar_months`,
  `lunar_year_pillar`) return `IztroError::InvalidArgument` for a year out of range, like horoscopes by lunar year.
</Callout>

```rust
use x_iztro::{lunar_months, lunar_to_solar, lunar_year_pillar, solar_to_lunar};
```

***

## lunar\_months [#lunar_months]

**Purpose**　Every month of a lunar year: its number, whether it is leap, its length, and the Gregorian date of
its first day.

**Signature**

```rust
pub fn lunar_months(year: i64) -> Result<Vec<LunarMonthInfo>, IztroError>
```

**Parameters**

| Parameter | Type  | Required | Default | Description        |
| --------- | ----- | -------- | ------- | ------------------ |
| `year`    | `i64` | Yes      | —       | Lunar year, 1–9999 |

**Returns**　`Vec<LunarMonthInfo>` in calendar order; a leap month directly follows the month it repeats. Each item:

| Field       | Type           | Description                                                                    |
| ----------- | -------------- | ------------------------------------------------------------------------------ |
| `month`     | `u32`          | Month 1–12; a leap month carries the number it repeats, so leap 4th month is 4 |
| `is_leap`   | `bool`         | Whether the month is leap                                                      |
| `day_count` | `u32`          | Length: 29 (short month) or 30 (long month)                                    |
| `first_day` | `RawSolarDate` | Gregorian date of the first day: `solar_year`, `solar_month`, `solar_day`      |

**Example**　2023 has a leap second month:

```rust
for m in lunar_months(2023)?.iter().take(4) {
    let d = m.first_day;
    println!("{} {} {} {}-{}-{}", m.month, m.is_leap, m.day_count, d.solar_year, d.solar_month, d.solar_day);
}
```

**Output**

```text
1 false 29 2023-1-22
2 false 30 2023-2-20
2 true 29 2023-3-22
3 false 29 2023-4-20
```

**Edge cases**

<Accordions>
  <Accordion title="Not always 12 or 13 months">
    A few ancient years, matching lunar-typescript, are a month short: lunar year 237 has 11 months and no third
    month. When iterating months, go by the returned list rather than looping 1–12.
  </Accordion>

  <Accordion title="The last 27 days of lunar year 9999 fall in Gregorian 10000">
    The 12th month of lunar year 9999 begins on 9999-12-30; every month's first day is within Gregorian 9999, so
    `first_day.solar_year` is never 10000. Days 3 to 29 of that month — 27 days — fall in Gregorian year 10000, and
    `lunar_to_solar` returns `solar_year` 10000 for them. Those days cannot be passed on to entry points that take a
    Gregorian year 1–9999 (charting, `solar_to_lunar`).
  </Accordion>
</Accordions>

***

## solar\_to\_lunar [#solar_to_lunar]

**Purpose**　Convert a Gregorian date to the lunar calendar.

**Signature**

```rust
pub fn solar_to_lunar(year: i64, month: u32, day: u32) -> Result<RawLunarDate, IztroError>
```

**Parameters**

| Parameter | Type  | Required | Default | Description            |
| --------- | ----- | -------- | ------- | ---------------------- |
| `year`    | `i64` | Yes      | —       | Gregorian year, 1–9999 |
| `month`   | `u32` | Yes      | —       | Gregorian month, 1–12  |
| `day`     | `u32` | Yes      | —       | Gregorian day          |

**Returns**　`RawLunarDate`: `lunar_year`, `lunar_month` (1–12), `lunar_day`, `is_leap` — the same type as the
chart's `raw_dates.lunar_date`.

**Example**　2023-3-22 is the first day of the leap second month:

```rust
let l = solar_to_lunar(2023, 3, 22)?;
println!("{} {} {} {}", l.lunar_year, l.lunar_month, l.lunar_day, l.is_leap);
```

**Output**

```text
2023 2 1 true
```

**Edge cases**

<Accordions>
  <Accordion title="Early January of year 1 is lunar year 0">
    The first few dozen days of Gregorian year 1 are still in the 11th and 12th months of lunar year 0, so
    `lunar_year` is 0; that 0 cannot be passed back to lunar inputs, which accept 1–9999.
  </Accordion>

  <Accordion title="The calendar reform gap">
    1582-10-05 to 10-14 do not exist (the day after Julian 4 October is Gregorian 15 October); passing one returns
    `IztroError::InvalidDate`.
  </Accordion>
</Accordions>

***

## lunar\_to\_solar [#lunar_to_solar]

**Purpose**　Convert a lunar date to the Gregorian calendar.

**Signature**

```rust
pub fn lunar_to_solar(
    year: i64,
    month: u32,
    day: u32,
    is_leap: bool,
) -> Result<RawSolarDate, IztroError>
```

**Parameters**

| Parameter | Type   | Required | Default | Description                                                |
| --------- | ------ | -------- | ------- | ---------------------------------------------------------- |
| `year`    | `i64`  | Yes      | —       | Lunar year, 1–9999                                         |
| `month`   | `u32`  | Yes      | —       | Lunar month, 1–12; for a leap month, the number it repeats |
| `day`     | `u32`  | Yes      | —       | Lunar day, within the month's length                       |
| `is_leap` | `bool` | Yes      | —       | The day is in the leap month                               |

**Returns**　`RawSolarDate`: `solar_year`, `solar_month`, `solar_day`.

**Example**

```rust
let s = lunar_to_solar(2023, 2, 1, true)?;
println!("{}-{}-{}", s.solar_year, s.solar_month, s.solar_day);

// 2024 has no leap second month: an error, not the regular second month
let err = lunar_to_solar(2024, 2, 1, true).unwrap_err();
println!("{}", err.code());
```

**Output**

```text
2023-3-22
invalid_date
```

**Edge cases**

<Accordions>
  <Accordion title="No such leap month is an error">
    When `is_leap` is true but the year does not repeat that month, the result is `IztroError::InvalidDate`. The charting entry
    point (`by_lunar` with `LeapMonth`, as in iztro) ignores the leap flag in that case and charts the regular month; the
    calendar conversion does not, because falling back to the regular month gives a date a month off that looks
    perfectly valid.
  </Accordion>

  <Accordion title="No fix_leap">
    Charting's `LeapMonth::LeapFixed` (days after the 15th of a leap month count as the next month) decides which month the
    stars are placed by; it does not change which Gregorian day a date is, so the only parameter here is whether
    the month is leap.
  </Accordion>
</Accordions>

***

## lunar\_year\_pillar [#lunar_year_pillar]

**Purpose**　The stem-branch of a lunar year, as language-independent stem and branch keys.

**Signature**

```rust
pub fn lunar_year_pillar(year: i64) -> Result<(HeavenlyStem, EarthlyBranch), IztroError>
```

**Parameters**

| Parameter | Type  | Required | Default | Description         |
| --------- | ----- | -------- | ------- | ------------------- |
| `year`    | `i64` | Yes      | —       | Year number, 1–9999 |

**Returns**　`(HeavenlyStem, EarthlyBranch)`. Year 4 is jia-zi, and the cycle runs on by year number from there.

**Example**

```rust
let (stem, branch) = lunar_year_pillar(2024)?;
println!("{} {}", stem.as_key(), branch.as_key());
```

**Output**

```text
jiaHeavenly chenEarthly
```

**Year boundary**　The stem-branch depends only on the year number, not on `year_divide`; the boundary setting
decides from which day that pillar applies. The chart's year pillar (`raw_dates.chinese_date.yearly`) takes the pillar of the
year number given by this table:

| Setting                                        | Days the stem-branch of year number N covers                                         |
| ---------------------------------------------- | ------------------------------------------------------------------------------------ |
| Lunar New Year (`YearDivide::Normal`, default) | Lunar year N, from New Year's Day to New Year's Eve                                  |
| Start of spring (`YearDivide::Exact`)          | From the start of spring (lichun) in Gregorian year N to the day before the next one |

Take 2024: the start of spring falls on 2-4 and Lunar New Year on 2-10. The days 2-4 to 2-9 belong to gui-mao
(lunar 2023) under the New Year boundary and to jia-chen (year number 2024) under the start-of-spring boundary.
To get the year pillar for a date, settle the year number by the boundary first, then take its pillar.
