5D Library › Four-Pillar Calendrical Computation and Formal Rule Engines · 中文

Chapter 1. The Julian Day and the Congruences of the Four Pillars

An unbroken count of days

The sexagenary count of days has run without a break since the Spring and Autumn period at the latest, never skipping or repeating a day at a change of calendar. The Julian Day Number is also an unbroken count of days, running from 4713 BCE and likewise untouched by calendar reforms (Reingold and Dershowitz 2018). Two unbroken counts differ by a constant, so the day pillar is a residue of the Julian day.

A Gregorian date converts to a Julian day by a formula in integer arithmetic alone (Fliegel and Van Flandern 1968): with a=⌊(14−m)/12⌋a = \lfloor (14 – m)/12 \rfloor, y′=y+4800−ay' = y + 4800 – a and m′=m+12a−3m' = m + 12a – 3,

JDN=d+⌊(153m′+2)/5⌋+365y′+⌊y′/4⌋−⌊y′/100⌋+⌊y′/400⌋−32045.\mathrm{JDN} = d + \lfloor (153m' + 2)/5 \rfloor + 365y' + \lfloor y'/4 \rfloor – \lfloor y'/100 \rfloor + \lfloor y'/400 \rfloor – 32045.

For a Julian-calendar date, drop the two century terms and replace the constant by 32083. Counting jia-zi as 0, the index of the day pillar is (JDN+49)mod⁡60(\mathrm{JDN} + 49) \bmod 60.

Calculation

# Chapter 1. Day pillars from the Julian Day Number.
# The Julian Day Number (JDN) counts days without a break across every
# calendar reform, from 1 January 4713 BCE (Julian calendar). For a
# Gregorian date (Fliegel and Van Flandern 1968):
#   a = (14 - m) // 12,  y' = y + 4800 - a,  m' = m + 12a - 3
#   JDN = d + (153m' + 2)//5 + 365y' + y'//4 - y'//100 + y'//400 - 32045
# The Julian-calendar version drops the two century terms and uses
# -32083. The sexagenary day cycle has also run without a break, so the
# day pillar is one residue: index = (JDN + 49) mod 60, 0 = jia-zi.
import datetime as dt

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 jdn_gregorian(y, m, d):
    a = (14 - m) // 12
    yy, mm = y + 4800 - a, m + 12 * a - 3
    return (d + (153 * mm + 2) // 5 + 365 * yy + yy // 4 - yy // 100
            + yy // 400 - 32045)

def jdn_julian(y, m, d):
    a = (14 - m) // 12
    yy, mm = y + 4800 - a, m + 12 * a - 3
    return d + (153 * mm + 2) // 5 + 365 * yy + yy // 4 - 32083

def day_index(jdn):
    return (jdn + 49) % 60

def name(i):
    return STEMS[i % 10] + "-" + BRANCHES[i % 12]

if __name__ == "__main__":
    print("   JDN of 1 January 2000 = {:,d}".format(
        jdn_gregorian(2000, 1, 1)))
    anchors = (((2000, 1, 1), "wu-wu"), ((1949, 10, 1), "jia-zi"))
    for (y, m, d), want in anchors:
        i = day_index(jdn_gregorian(y, m, d))
        print("   {}-{:02d}-{:02d}: index {:2d} = {}  (published: {})".format(
            y, m, d, i, name(i), want))
    ok = all(jdn_gregorian(t.year, t.month, t.day) == t.toordinal()
             + 1721425 for t in (dt.date(1900, 1, 1) + dt.timedelta(days=k)
                                 for k in range(0, 73414)))
    print("   formula = Python ordinal + 1,721,425 on 1900-2100: {}".format(
        ok))
    a, b = jdn_julian(1582, 10, 4), jdn_gregorian(1582, 10, 15)
    print("   4 Oct 1582 (Julian) JDN {:,d} -> {};  15 Oct 1582 (Gregorian)"
          .format(a, name(day_index(a))))
    print("   JDN {:,d} -> {}: consecutive days, consecutive pillars".format(
        b, name(day_index(b))))
    crt = all(name(i) == STEMS[s] + "-" + BRANCHES[r] for i in range(60)
              for s, r in [(i % 10, i % 12)])
    pairs = {(i % 10, i % 12) for i in range(60)}
    print("   60 indices give 60 distinct stem-branch pairs: {};"
          .format(len(pairs) == 60 and crt))
    print("   stem and branch always share parity: {}".format(
        all((s - r) % 2 == 0 for s, r in pairs)))
    print("\nReading: the day pillar is a residue of an unbroken day count,")
    print("so it needs no table and survives the calendar reform of 1582.")
    print("Only the 60 pairs of stem and branch with equal parity occur.")
#    JDN of 1 January 2000 = 2,451,545
#    2000-01-01: index 54 = wu-wu  (published: wu-wu)
#    1949-10-01: index  0 = jia-zi  (published: jia-zi)
#    formula = Python ordinal + 1,721,425 on 1900-2100: True
#    4 Oct 1582 (Julian) JDN 2,299,160 -> gui-you;  15 Oct 1582 (Gregorian)
#    JDN 2,299,161 -> jia-xu: consecutive days, consecutive pillars
#    60 indices give 60 distinct stem-branch pairs: True;
#    stem and branch always share parity: True
#
# Reading: the day pillar is a residue of an unbroken day count,
# so it needs no table and survives the calendar reform of 1582.
# Only the 60 pairs of stem and branch with equal parity occur.

The program checks the constant against two accepted day pillars: 1 January 2000 is wu-wu and 1 October 1949 is jia-zi. On every day from 1900 to 2100 the formula differs from Python’s internal day ordinal by the constant 1,721,425. The day after 4 October 1582 (Julian) is 15 October 1582 (Gregorian); their Julian days are the consecutive 2,299,160 and 2,299,161, and their pillars the consecutive gui-you and jia-xu: the reform skipped ten dates, but not a single stem-branch pair.

One structure is easily overlooked: there are 10 stems and 12 branches, yet only 60 pairs, not 120, because stem and branch advance together and their indices always share parity. By the Chinese remainder theorem, ℤ60\mathbb{Z}_{60} corresponds exactly to the pairs of equal parity.

Five tigers and five rats

The year pillar changes at the Beginning of Spring, as Volume 37 discussed. The stem of the month pillar is fixed by the year stem (the “five tigers”), the stem of the hour pillar by the day stem (the “five rats”). Both verses can be written as affine maps modulo 10: with jia as 0 and zi as 0, the stem of the yin month is (2×(year stemmod⁡5)+2)mod⁡10(2 \times (\text{year stem} \bmod 5) + 2) \bmod 10, rising by one each month; the stem of the zi hour is 2×(day stemmod⁡5)mod⁡102 \times (\text{day stem} \bmod 5) \bmod 10, rising by one each double-hour.

Calculation

# Chapter 1. Month and hour stems as affine maps (five tigers, five rats).
# Index stems 0-9 (jia ... gui) and branches 0-11 (zi ... hai).
# Month: the yin month (branch 2) of a year with stem Y has stem
#   (2 (Y mod 5) + 2) mod 10, and each later month adds one (the zi
#   and chou months close the year, after hai).
# Hour: the zi hour (branch 0) of a day with stem D has stem
#   2 (D mod 5) mod 10, and each later hour adds one.
# Both are checked against the rhyming tables as usually printed.
STEMS = ("jia", "yi", "bing", "ding", "wu", "ji", "geng", "xin", "ren",
         "gui")
BRANCHES = ("zi", "chou", "yin", "mao", "chen", "si", "wu", "wei", "shen",
            "you", "xu", "hai")
# five tigers: year stems -> stem of the yin month
TIGERS = {("jia", "ji"): "bing", ("yi", "geng"): "wu",
          ("bing", "xin"): "geng", ("ding", "ren"): "ren",
          ("wu", "gui"): "jia"}
# five rats: day stems -> stem of the zi hour
RATS = {("jia", "ji"): "jia", ("yi", "geng"): "bing",
        ("bing", "xin"): "wu", ("ding", "ren"): "geng",
        ("wu", "gui"): "ren"}

def month_stem(year_stem, branch):
    return (2 * (year_stem % 5) + 2 + (branch - 2) % 12) % 10

def hour_stem(day_stem, branch):
    return (2 * (day_stem % 5) + branch) % 10

if __name__ == "__main__":
    ok_t = all(STEMS[month_stem(STEMS.index(c), 2)] == v
               for k, v in TIGERS.items() for c in k)
    ok_r = all(STEMS[hour_stem(STEMS.index(c), 0)] == v
               for k, v in RATS.items() for c in k)
    print("   five tigers verse = (2 (Y mod 5) + 2) mod 10: {}".format(ok_t))
    print("   five rats verse   =  2 (D mod 5)      mod 10: {}".format(ok_r))
    hrs = [STEMS[hour_stem(0, b)] + "-" + BRANCHES[b] for b in range(12)]
    mos = [STEMS[month_stem(0, b)] + "-" + BRANCHES[b]
           for b in list(range(2, 12)) + [0, 1]]
    print("   hours of a jia day:")
    print("     " + " ".join(hrs[:6]))
    print("     " + " ".join(hrs[6:]))
    print("   months of a jia year, from the yin month:")
    print("     " + " ".join(mos[:6]))
    print("     " + " ".join(mos[6:]))
    par = all((month_stem(y, b) - b) % 2 == 0 and (hour_stem(y, b) - b) % 2
              == 0 for y in range(10) for b in range(12))
    print("   every month and hour pillar has stem and branch of equal")
    print("   parity: {}".format(par))
    n = 60 * 12 * 60 * 12
    print("   distinct charts: 60 years x 12 months x 60 days x 12 hours"
          " = {:,d}".format(n))
    print("\nReading: the month stem is fixed by the year stem and the")
    print("month, the hour stem by the day stem and the hour; the rhyming")
    print("tables are affine maps modulo 10. Of the eight characters only")
    print("four are free, giving 518,400 possible charts.")
#    five tigers verse = (2 (Y mod 5) + 2) mod 10: True
#    five rats verse   =  2 (D mod 5)      mod 10: True
#    hours of a jia day:
#      jia-zi yi-chou bing-yin ding-mao wu-chen ji-si
#      geng-wu xin-wei ren-shen gui-you jia-xu yi-hai
#    months of a jia year, from the yin month:
#      bing-yin ding-mao wu-chen ji-si geng-wu xin-wei
#      ren-shen gui-you jia-xu yi-hai bing-zi ding-chou
#    every month and hour pillar has stem and branch of equal
#    parity: True
#    distinct charts: 60 years x 12 months x 60 days x 12 hours = 518,400
#
# Reading: the month stem is fixed by the year stem and the
# month, the hour stem by the day stem and the hour; the rhyming
# tables are affine maps modulo 10. Of the eight characters only
# four are free, giving 518,400 possible charts.

The program compares the five-tigers verse (“in a jia or ji year, bing leads …”) and the five-rats verse (“jia and ji return to jia …”) with the formulas line by line; all match. It also lists the twelve month pillars of a jia year and the twelve hour pillars of a jia day. The stems and branches of month and hour pillars always share parity as well. It follows that only four of the eight characters are free: the year pillar, the month branch, the day pillar and the hour branch. There are 518,400 possible charts, and Chapter 5 computes all of them.

Note

The year and month boundaries are not here. The formulas of this chapter give the pillar of a given month or double-hour; which month and year a moment of birth falls in depends on whether it lies before or after the moment of a solar term. Solar terms are an astronomical calculation, taken up in Chapters 3 and 6.

Summary

The day pillar is the residue of the Julian day plus 49 modulo 60, continuous across the reform of 1582; the five tigers and five rats are affine maps modulo 10 that agree with the verses line by line. The parity constraint leaves only four of the eight characters free, giving 518,400 possible charts.

References

Fliegel, Henry F., and Thomas C. Van Flandern. 1968. “A Machine Algorithm for Processing Calendar Dates.” Communications of the ACM 11 (10): 657.
Reingold, Edward M., and Nachum Dershowitz. 2018. Calendrical Calculations: The Ultimate Edition. 4th ed. Cambridge University Press.

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).