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
RawDates.LunarDate 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.
Errors
The conversions that take a full date (SolarToLunar, LunarToSolar) return ErrInvalidDate for a date that does
not exist, like the charting entry points; the lookups that take only a year (LunarMonths,
LunarYearPillar) return ErrInvalidArgument for a year out of range, like horoscopes by lunar year.
import "github.com/x-haose/x-iztro/go/iztro"LunarMonths
Purpose Every month of a lunar year: its number, whether it is leap, its length, and the Gregorian date of its first day.
Signature
func LunarMonths(year int) ([]LunarMonthInfo, error)Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
year | int | Yes | — | Lunar year, 1–9999 |
Returns []LunarMonthInfo in calendar order; a leap month directly follows the month it repeats. Each item:
| Field | Type | Description |
|---|---|---|
Month | int | Month 1–12; a leap month carries the number it repeats, so leap 4th month is 4 |
IsLeap | bool | Whether the month is leap |
DayCount | int | Length: 29 (short month) or 30 (long month) |
FirstDay | RawSolarDate | Gregorian date of the first day: SolarYear, SolarMonth, SolarDay |
Example 2023 has a leap second month:
months, _ := iztro.LunarMonths(2023)
for _, m := range months[:4] {
d := m.FirstDay
fmt.Println(m.Month, m.IsLeap, m.DayCount, fmt.Sprintf("%d-%d-%d", d.SolarYear, d.SolarMonth, d.SolarDay))
}Output
1 false 29 2023-1-22
2 false 30 2023-2-20
2 true 29 2023-3-22
3 false 29 2023-4-20Edge cases
SolarToLunar
Purpose Convert a Gregorian date to the lunar calendar.
Signature
func SolarToLunar(year, month, day int) (RawLunarDate, error)Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
year | int | Yes | — | Gregorian year, 1–9999 |
month | int | Yes | — | Gregorian month, 1–12 |
day | int | Yes | — | Gregorian day |
Returns RawLunarDate: LunarYear, LunarMonth (1–12), LunarDay, IsLeap — the same type as the
chart's RawDates.LunarDate.
Example 2023-3-22 is the first day of the leap second month:
l, _ := iztro.SolarToLunar(2023, 3, 22)
fmt.Println(l.LunarYear, l.LunarMonth, l.LunarDay, l.IsLeap)Output
2023 2 1 trueEdge cases
LunarToSolar
Purpose Convert a lunar date to the Gregorian calendar.
Signature
func LunarToSolar(year, month, day int, isLeap bool) (RawSolarDate, error)Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
year | int | Yes | — | Lunar year, 1–9999 |
month | int | Yes | — | Lunar month, 1–12; for a leap month, the number it repeats |
day | int | Yes | — | Lunar day, within the month's length |
isLeap | bool | Yes | — | The day is in the leap month |
Returns RawSolarDate: SolarYear, SolarMonth, SolarDay.
Example
s, _ := iztro.LunarToSolar(2023, 2, 1, true)
fmt.Printf("%d-%d-%d\n", s.SolarYear, s.SolarMonth, s.SolarDay)
// 2024 has no leap second month: an error, not the regular second month
_, err := iztro.LunarToSolar(2024, 2, 1, true)
fmt.Println(errors.Is(err, iztro.ErrInvalidDate))Output
2023-3-22
trueEdge cases
LunarYearPillar
Purpose The stem-branch of a lunar year, as language-independent stem and branch keys.
Signature
func LunarYearPillar(year int) (Pillar, error)Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
year | int | Yes | — | Year number, 1–9999 |
Returns Pillar ([stem key, branch key]). Year 4 is jia-zi, and the cycle runs on by year number from there.
Example
p, _ := iztro.LunarYearPillar(2024)
fmt.Println(p[0], p[1])Output
jiaHeavenly chenEarthlyYear 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 (RawDates.ChineseDate.YearlyKeys) 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.