Building a BaZi chart calculator: the calendar edge cases that break naive code
Disclosure: I build BaZi Calculator, a free Four Pillars calculator. This post is about the dating problems, not the interpretation. I used AI to help edit and to re-check the calendar claims against a reference ephemeri
Disclosure: I build BaZi Calculator, a free Four Pillars calculator. This post is about the dating problems, not the interpretation. I used AI to help edit and to re-check the calendar claims against a reference ephemeris. The example dates below are real dates you can check against any astronomy source.
A BaZi (Four Pillars) chart looks like a text problem: split a birth date into year, month, day and hour, attach stems and branches. Every "build a BaZi app" request I have seen assumes exactly that.
It fails on the first test date. Turns out the pillars are not months and years at all — they are solar terms, and getting from a civil timestamp to a pillar is an astronomy problem.
I want to write down the four places where a naive implementation produces a wrong chart, because each one produces output that looks plausible and passes a casual eyeball check.
Edge case 1: the year pillar does not turn at New Year
The year pillar changes at Lichun (立春), when the sun reaches 315° of ecliptic longitude — around 4 February. Not 1 January, and not Chinese New Year either. Chinese New Year is a lunar date and moves between 21 January and 20 February; using it shifts a whole January-to-February cohort into the wrong year.
So a birth on 2026-01-20 belongs to the 2025 year pillar. A validator built on date.getFullYear() gets this wrong for roughly six weeks every single year.
Edge case 2: months are solar terms, not lunar months
Each month pillar turns at one of the twelve jie (节), at 15° intervals of solar longitude. The month containing Lichun starts the year pillar's first month; the one at Jingzhe (惊蛰) starts the second, and so on.
Consequences for anyone parsing a "lunar month" field:
- the month pillars are unevenly spaced in days, because the sun moves faster in January than in July
- a lunar month can span two month pillars
- a leap lunar month does not create a pillar and does not break the sequence
The fix is the same as above: compute solar longitude and find which 15° sector the birth falls in. Calendar libraries that hand you a lunar month number cannot answer this on their own.
Edge case 3: the hour pillar needs true solar time
The hour pillar is defined in shi chen blocks, two hours each, and it is conventionally read in true solar time — mean solar time corrected by longitude plus the equation of time.
Two corrections, both real:
// Both terms matter. Ignoring either moves the hour pillar.
const longitudeOffsetMin = 4 * (longitude - 15 * utcOffsetHours); // 4 min per degree
const eotMin = equationOfTime(date); // +/- ~16 min across the year
const trueSolarMs = utcMs + (longitudeOffsetMin + eotMin) * 60_000;
At the edges of a two-hour block, 16 minutes of equation of time plus up to 30 minutes of longitude shift flip the pillar. A birth at 00:50 in Urumqi (87.6°E, UTC+8) is not in the same shi chen as 00:50 in Shanghai (121.5°E).
There is a second trap underneath: historical time zones. Some regions changed offset and DST rules mid-century. A timestamp taken from a birth certificate is local clock time; you need the offset in force at that moment, not today's offset.
Edge case 4: day pillars cross midnight — but which midnight
The day pillar advances at midnight in the local true-solar frame in the convention most references use, and some lineages advance it at 23:00 (the start of the Zi hour). Picking one and documenting it is fine. Mixing them is not: a chart computed with clock midnight and a chart computed with Zi-hour boundary disagree for every birth between 23:00 and 24:00.
This one cannot be solved with code alone. It is a convention decision, and it has to be exposed in the interface rather than buried in a default.
What actually works
Once the four cases are framed as astronomy, the implementation is boring:
- Resolve the local timestamp to UTC with the offset in force at that time, not the current one.
- Compute solar longitude for the moment (VSOP87-truncated, or a maintained ephemeris; sxtwl and lunar-javascript both ship the term tables).
- Derive year and month pillars from the longitude sector, not from calendar fields.
- Apply longitude and equation of time, then bucket the hour.
- State the Zi-hour convention in the output.
Test the boundaries, not the middle
Mid-month, mid-day births are almost impossible to get wrong, which is why they make bad tests. Every case above lives at a boundary, so the test fixture is essentially a list of boundaries:
const cases = [
{ at: "2026-02-03T23:00+08:00", expect: { year: 2025, reason: "one day before Lichun" } },
{ at: "2026-02-04T10:00+08:00", expect: { year: 2026, reason: "after Lichun" } },
{ at: "2026-02-17T12:00+08:00", expect: { year: 2026, reason: "Chinese New Year, not a pillar boundary" } },
{ at: "2024-02-10T23:30+08:00", expect: { dayShift: true, reason: "Zi hour, both conventions" } },
];
Two notes from building this for real. First, Lichun itself lands within a few hours of a calculated time, so the boundary is a range, not a date — test an hour either side of the computed instant and expect the pillars to differ. Second, the equation of time peaks around 3 November (+16 min) and bottoms out around 11 February (−14 min), so a fixture with dates only in spring and autumn will not exercise it.
The chart above is the easy part of a BaZi tool. The interpretation — Day Master, Five Elements, Ten Gods — is a mapping table over the pillars. All the difficulty is in getting four characters right from a timestamp, and every one of the four traps above is silent: the output stays well-formed and looks reasonable.
If you build one, the pillar math is worth writing tests for before the reading interface, not after.
Chart, terms and conventions: bazicalculators.com
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.