5D Library › Four-Pillar Calendrical Computation and Formal Rule Engines · 中文
Chapter 6. Two Chart Engines and a Set of Test Vectors
One birth, two engines
Every convention of the first five chapters is a line of code in a chart program: clock time or true solar time, how the zi hour changes the day, where the solar-term times come from, how hidden stems are weighted. If two programs differ in a single line, they may give the same birth different charts. The next program compares two engines: engine X reads the clock and changes the day at 23:00 (convention A of Chapter 2); engine Y converts to true solar time, changes the day at midnight and takes the late-zi stem rule (convention B). Year and month pillars depend only on the moment of birth and are the same in both. Every minute of 2026 is taken as an equally likely time of birth and compared.
Calculation
# Chapter 6. Two engines, one birth: how often do the pillars differ?
# Engine X reads the clock (UTC+8) and changes the day at 23:00 (rule A
# of p03). Engine Y converts to true solar time (longitude and the
# equation of time, as in p04) and changes the day at midnight with the
# late-zi stem rule (rule B). Year and month pillars depend only on the
# instant of birth and are the same in both. Every minute of 2026 is
# taken as an equally likely birth time.
import math
CITIES = (("Shanghai", 121.47), ("Beijing", 116.40),
("Singapore", 103.82), ("Kuala Lumpur", 101.69))
JDN_2026 = 2461042 # 1 January 2026
def equation_of_time(n):
g = 2 * math.pi / 365 * (n - 1)
return 229.18 * (0.000075 + 0.001868 * math.cos(g)
- 0.032077 * math.sin(g) - 0.014615 * math.cos(2 * g)
- 0.040849 * math.sin(2 * g))
def pillars(day_idx, minute, rule):
hb = ((minute + 60) // 120) % 12
late = minute >= 23 * 60
d = day_idx + (1 if late and rule == "A" else 0)
stem_day = day_idx + (1 if late else 0)
return d % 60, (2 * (stem_day % 10 % 5) + hb) % 10, hb
if __name__ == "__main__":
print(" city day pillar hour pillar any pillar")
for name, lon in CITIES:
day = hour = anyp = total = 0
for n in range(365):
di = (JDN_2026 + n + 49) % 60
off = 4 * (lon - 120) + equation_of_time(n + 1)
for t in range(1440):
x = pillars(di, t, "A")
ts = t + off
dj = di
if ts < 0:
ts, dj = ts + 1440, di - 1
elif ts >= 1440:
ts, dj = ts - 1440, di + 1
y = pillars(dj, int(ts), "B")
day += x[0] != y[0]
hour += x[1:] != y[1:]
anyp += x != y
total += 1
print(" {:12s} {:8.4f} {:8.4f} {:8.4f}".format(
name, day / total, hour / total, anyp / total))
print("\nReading: in Shanghai the two engines disagree on about one")
print("birth in ten; in Kuala Lumpur and Singapore on more than half.")
print("Most disagreements are in the hour pillar; the day pillar")
print("differs for four to nine births in a hundred, around midnight.")
# city day pillar hour pillar any pillar
# Shanghai 0.0379 0.0674 0.1043
# Beijing 0.0520 0.1252 0.1668
# Singapore 0.0869 0.5432 0.5801
# Kuala Lumpur 0.0929 0.6143 0.6463
#
# Reading: in Shanghai the two engines disagree on about one
# birth in ten; in Kuala Lumpur and Singapore on more than half.
# Most disagreements are in the hour pillar; the day pillar
# differs for four to nine births in a hundred, around midnight.In Shanghai at least one pillar differs between the two engines for 0.1043 of births, the hour pillar for 0.0674 and the day pillar for 0.0379; in Beijing at least one pillar differs for 0.1668. In Singapore and Kuala Lumpur the shares are 0.5801 and 0.6463. The disagreements are mainly in the hour pillar; those in the day pillar cluster around midnight and amount to four to nine per cent.
So when a user says “two programs gave me different charts”, the most likely cause is not an error on either side but two engines that settle the conventions of Chapter 2 differently. Such differences can be stated in the program, or left to the user to choose.
A set of test vectors
An engineering remedy is to give chart engines a public set of test vectors: invariants that must hold, and the fingerprint of a canonical table. Two engines that compute the same fingerprint for the canonical table agree on every entry of it; a different fingerprint says to look first for a difference of convention and only then for a bug.
Calculation
# Chapter 6. A conformance check that any two engines can run.
# Invariants that every correct day-and-hour engine must satisfy, checked
# on every day from 1900 to 2100, and a fingerprint (SHA-256) of a
# canonical table: day and hour pillars for 00:30, 12:00 and 23:30 of
# every day of 2026, under rules A and B of p03. Two engines that print
# the same fingerprint agree on all 2,190 entries.
import datetime as dt
import hashlib
STEMS = ("jia", "yi", "bing", "ding", "wu", "ji", "geng", "xin", "ren",
"gui")
BRANCHES = ("zi", "chou", "yin", "mao", "chen", "si", "wu", "wei", "shen",
"you", "xu", "hai")
def day_index(d):
return (d.toordinal() + 1721425 + 49) % 60
def pillars(d, minute, rule):
di = day_index(d)
hb = ((minute + 60) // 120) % 12
late = minute >= 23 * 60
dd = di + (1 if late and rule == "A" else 0)
sd = di + (1 if late else 0)
hs = (2 * (sd % 10 % 5) + hb) % 10
return "{}-{} {}-{}".format(STEMS[dd % 10], BRANCHES[dd % 12],
STEMS[hs], BRANCHES[hb])
if __name__ == "__main__":
days = [dt.date(1900, 1, 1) + dt.timedelta(k) for k in range(73414)]
step = all((day_index(b) - day_index(a)) % 60 == 1
for a, b in zip(days, days[1:]))
five = all(pillars(a, 600, "A").split()[1]
== pillars(b, 600, "A").split()[1]
for a, b in zip(days, days[5:]))
jiazi = all((day_index(d) % 10) % 5 == 0
for d in days if pillars(d, 30, "B").endswith("jia-zi"))
print(" day index rises by 1 (mod 60) every day, 1900-2100: {}".format(
step))
print(" hour pillars repeat every 5 days: {}".format(five))
print(" a jia-zi hour falls only on jia or ji days: {}".format(jiazi))
rows = []
for k in range(365):
d = dt.date(2026, 1, 1) + dt.timedelta(k)
for minute in (30, 720, 1410):
for rule in "AB":
rows.append("{} {:04d} {} {}".format(d.isoformat(), minute,
rule, pillars(d, minute,
rule)))
digest = hashlib.sha256("\n".join(rows).encode()).hexdigest()
print(" canonical table: {:,d} entries".format(len(rows)))
print(" first: {}".format(rows[0]))
print(" last: {}".format(rows[-1]))
print(" SHA-256 {}".format(digest[:32]))
print(" {}".format(digest[32:]))
print("\nReading: two engines that print the same fingerprint agree on")
print("every entry of the table; a different fingerprint says to look")
print("for a convention difference before looking for a bug.")
# day index rises by 1 (mod 60) every day, 1900-2100: True
# hour pillars repeat every 5 days: True
# a jia-zi hour falls only on jia or ji days: True
# canonical table: 2,190 entries
# first: 2026-01-01 0030 A yi-hai bing-zi
# last: 2026-12-31 1410 B ji-mao bing-zi
# SHA-256 91474ec7886aebba0086dbb2511a2456
# 82e96813b80dc902a7800e32084b66b9
#
# Reading: two engines that print the same fingerprint agree on
# every entry of the table; a different fingerprint says to look
# for a convention difference before looking for a bug.The program checks three invariants: on every day from 1900 to 2100 the day pillar’s index rises by exactly one; the hour pillars repeat every five days (60 double-hours make a cycle); a jia-zi hour occurs only on jia and ji days. It then computes the day and hour pillars at 00:30, noon and 23:30 of every day of 2026 under conventions A and B, 2,190 entries, and their SHA-256 fingerprint. Any engine can print the table in the same format and compare fingerprints.
Note
The scope of the test vectors. The table covers only the day and hour pillars, because they are pure arithmetic; year and month pillars depend on the times of the solar terms and need a second table built from an observatory almanac. Conversion to true solar time depends on the longitude and on the formula for the equation of time, and should also be listed separately, with the formula stated.
Summary
An engine that reads the clock and changes the day at 23:00 and an engine that uses true solar time and changes the day at midnight differ in at least one pillar for 0.1043 of births in Shanghai and 0.6463 in Kuala Lumpur. The differences come from conventions, not from errors. A public set of invariants and the fingerprint of a canonical table let any two engines check each other in seconds.
Fan, Y. (2026). Four-Pillar Calendrical Computation and Formal Rule Engines: Julian-Day Sexagenary Cycles, Midnight-Boundary Disagreements, Birth-Time Sensitivity Bounds and Weighted-Graph Strength Scoring [四柱排盘的历法计算与规则引擎] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090180 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).