生辰八字,每逢结婚生子的时候,总会从长辈的口中念叨出来。那到底什么是生辰八字呢?难到只有算命先生知道?是一门玄学?
其实不是,在我国,有农历和阳历两种纪年方法。而阳历属于舶来品,也是当下采用的历法。
但是在古代,我们采用的是农历。农历是一种阴阳历。古人认为太阳属阳, 月亮属阴。结合太阳和月亮的公转周期,加以“闰月"调和而形成的。调和的目的是因为: 地球围绕太阳转一圈(一年)的时间,并不严格等于月亮绕地球转十二圈的时间。而农历里面的节气是和农耕生活息息相关的,所以需要天文学家夜观天象去看,去算。也就是说,这个闰月不是固定的,要根据当年具体的温度和湿度,选择闰到哪一月,才能保证不误农时。
而古代是不采用数字纪年的,而是用对应的汉字表示。汉字分为两类,天干和地支。
- 天干是: “甲”, “乙”, “丙”, “丁”, “戊”, “己”, “庚”, “辛”, “壬”, “癸”
- 地支是: “子”, “丑”, “寅”, “卯”, “辰”, “巳”, “午”, “未”, “申”, “酉”, “戌”, “亥”
根据天干地址的排列组合,生成六十个组合, 写个脚本体验下:
#!/usr/bin/python3
heavenlyStreams = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
earthlyBranches = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
m = 0
n = 0
for i in range(0,60):
print('"'+heavenlyStreams[m%10] + earthlyBranches[n%12] + '",', end =' ')
m = m + 1
n = n + 1
说道这里,八字就是年月日时所代表的八个子。我们先说前面六个字. 比如2019年7月29日.翻阅万年历:
阳历 2019年7月29日. 就是己亥(年)辛未(月)丁卯(日),也就是农历2019年六月廿七日。
那问题来了,如何根据阳历找到对应的六字呢?网上有一些公式。仔细研究后发现对应年初的月份是不行的。因为这时阳历一个月里面,会跨度阴历的两个月。而公式算法的输入只有年份和月份,所以对于年初,结果必然是不准确的。
我的思路是:
- 从本质上将,所谓的年月日,就是天干地支所组合的60个字的循环。比如这次是甲子,那下次就该用乙丑了。
- 那我只需要知道某一天的信息,就可以根据天数推算出后面的信息了。
- 因为闰月不是固定的, 也就是说,我要搞到一份靠谱的农历数据。
撤远一点,我们用的农历又叫夏历,是因为不同的朝代,会选择不同的字作为开头。而夏朝代是以寅开头。现在沿用了这一方式。所以也叫夏历。
靠谱的数据源可以在香港天文台拿到, 搞了一个自动下载脚本。懒得挨个去点了。
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1901c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1902c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1903c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1904c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1905c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1906c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1907c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1908c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1909c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1910c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1911c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1912c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1913c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1914c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1915c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1916c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1917c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1918c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1919c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1920c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1921c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1922c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1923c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1924c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1925c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1926c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1927c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1928c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1929c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1930c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1931c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1932c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1933c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1934c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1935c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1936c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1937c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1938c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1939c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1940c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1941c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1942c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1943c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1944c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1945c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1946c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1947c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1948c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1949c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1950c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1951c.txt
wget --local-encoding=ENC --remote-encoding=ENC https://data.weather.gov.hk/gts/time/calendar/text/T1952c.txt
wget --local-encoding=ENC --remote-encoding=ENC