linux shell之天干地支算法-年干支/月干支计算

您所在的位置:网站首页 干支纪日计算公式 linux shell之天干地支算法-年干支/月干支计算

linux shell之天干地支算法-年干支/月干支计算

2024-05-08 15:25| 来源: 网络整理| 查看: 265

    干支纪日从商朝便已开始,但其顺序到现在有无间断或错乱过,尚待考证。目前已确切知道的从春秋鲁隐公三年(公元前720年)二月己巳日起到清宣统三年(公元1911年)止(1912年民国元年采用公元纪年后,民间仍沿用)2600多年中从未间断和错乱过。 干支纪日法是商朝历法的最大成就,这是现今已知世界最长的纪日法,对于我国历史学,尤其是科学技术发展史的考证和研究,都是极为重要的记时标志,是我国一份珍贵的科学文化遗……

#这是我跟据网上资料用shell脚本语言写的一个输出现在的天干地支 年和月 的程序,如有不对的地方还请大家给于帮助

#!/bin/bash

gan=$(($(date +%y)%10))

#计算天干数的公式

zhi=$(($(date +%Y)%12))

计算地支数的公式

z=$(date +%m%d)

#计算月日的公式

yue=$(date +%m)

ri=$(date +%d)

lichunri=$(( $(( $(( $(( $(date +%y) * 2422 ))+38700 )) / 10000 )) - $(( $(( $(date +%y) - 1 )) / 4 )) ))

#天干地支里,每个干支年的开始是从立春开始,所以确定立春时间是判断这一年是哪个干支的重要依据,

#$(( (( $(( $(( $(date +%y) * 2422 ))+38700 )) / 10000 )) - $(( $(( $(date +%y) - 1 )) / 4 )) ))

case $gan in

#这是计算当年天干的算法,如果当日的时间是立春以后,则天干 为c ,反之为 c-1

# 甲  乙  丙  丁  戊  己  庚  辛  壬  癸

# 4   5   6   7   8   9   10  1   2   3

4)

if [ "$z" -gt 020$lichunri ]

then

c="甲"

else

c="癸"

fi

;;

5)

       if [ "$z" -gt 020$lichunri ]

       then

       c="乙"

       else

       c="甲"

fi

;;

6)

       if [ "$z" -gt 020$lichunri ]

       then

       c="丙"

       else

       c="乙"

fi

;;

7)

       if [ "$z" -gt 020$lichunri ]

       then

       c="丁"

       else

       c="丙"

fi

;;

8)

       if [ "$z" -gt 020$lichunri ]

       then

       c="戊"

       else

       c="丁"

       fi

;;

9)

       if [ "$z" -gt 020$lichunri ]

       then

       c="已"

       else

       c="戊"

       fi

;;

0)

       if [ "$z" -gt 020$lichunri ]

       then

       c="庚"

       else

       c="已"

       fi

;;

1)

       if [ "$z" -gt 020$lichunri ]

       then

       c="辛"

       else

       c="庚"

       fi

;;

2)

       if [ "$z" -gt 020$lichunri ]

       then

       c="壬"

       else

       c="辛"

       fi

;;

3)

if [ "$z" -gt 020$lichunri ]

then

c="癸"

else

c="壬"

fi

esac

#计算当年地支,如果当天日期在立春以后 地支为 d  ,反之为 d-1

#   子 丑  寅  卯  辰  巳  午  未  申  酉  戌  亥

#   4   5  6   7   8   9   10  11  12  1   2  3

case $zhi in

4)

       if [ "$z" -gt 020$lichunri ]

       then

       d="子"

       else

       d="亥"

fi

;;

5)

       if [ "$z" -gt 020$lichunri ]

       then

       d="丑"

       else

       d="子"

       fi

;;

6)      

if [ "$z" -gt 020$lichunri ]

       then

       d="寅"

       else

       d="丑"

       fi

;;

7)

       if [ "$z" -gt 020$lichunri ]

       then

       d="卯"

       else

       d="寅"

       fi

;;

8)

       if [ "$z" -gt 020$lichunri ]

       then

       d="辰"

       else

       d="卯"

       fi

;;

9)

       if [ "$z" -gt 020$lichunri ]

       then

       d="巳"

       else

       d="辰"

       fi

;;

10)

       if [ "$z" -gt 020$lichunri ]

       then

       d="午"

       else

       d="巳"

       fi

;;

11)

       if [ "$z" -gt 020$lichunri ]

       then

       d="未"

       else

       d="午"

       fi

;;

0)

       if [ "$z" -gt 020$lichunri ]

       then

       d="申"

       else

       d="未"

       fi

;;

1)

       if [ "$z" -gt 020$lichunri ]

       then

       d="酉"

       else

       d="申"

       fi

;;

2)

       if [ "$z" -gt 020$lichunri ]

       then

       d="戌"

       else

       d="酉"

       fi

;;

3)

       if [ "$z" -gt 020$lichunri ]

       then

       d="亥"

       else

       d="戌"

       fi

esac

echo "$c$d年"

#输出当前的天干地支

#如果当前的天干地支为以下选项,则对应当年的 五行 和 ×××(这块也可以用数字代替汉字 “甲子”等)

case $c$d in

甲子)

f="金"

g="鼠"

;;

乙丑)

f="金"

g="牛"

;;

丙寅)

f="火"

g="虎"

;;

丁卯)

f="火"

g="免"

;;

戊辰)

f="木"

g="龙"

;;

已巳)

f="木"

g="蛇"

;;

庚午)

f="土"

g="马"

;;

辛未)

f="土"

g="羊"

;;

壬申)

f="金"

g="猴"

;;

癸酉)

f="金"

g="鸡"

;;

甲戌)

f="火"

g="狗"

;;

乙亥)

f="火"

g="猪"

;;

丙子)

f="水"

g="鼠"

;;

丁丑)

f="水"

g="牛"

;;

戊寅)

f="土"

g="虎"

;;

已卯)

f="土"

g="免"

;;

庚辰)

f="金"

g="龙"

;;

辛巳)

f="金"

g="蛇"

;;

壬午)

f="木"

g="马"

;;

癸未)

f="木"

g="羊"

;;

甲申)

f="水"

g="猴"

;;

乙酉)

f="水"

g="鸡"

;;

丙戌)

f="土"

g="狗"

;;

丁亥)

f="土"

g="猪"

;;

戊子)

f="火"

g="鼠"

;;

己丑)

f="火"

g="牛"

;;

庚寅)

f="木"

g="虎"

;;

辛卯)

f="木"

g="免"

;;

壬辰)

f="水"

g="龙"

;;

癸巳)

f="水"

g="蛇"

;;

甲午)

f="金"

g="马"

;;

乙未)

f="金"

g="羊"

;;

丙申)

f="火"

g="猴"

;;

丁酉)

f="火"

g="鸡"

;;

戊戌)

f="木"

g="狗"

;;

已亥)

f="木"

g="猪"

;;

庚子)

f="土"

g="鼠"

;;

辛丑)

f="土"

g="牛"

;;

壬寅)

f="金"

g="虎"

;;

癸卯)

f="金"

g="免"

;;

甲辰)

f="火"

g="龙"

;;

乙巳)

f="火"

g="蛇"

;;

丙午)

f="水"

g="马"

;;

丁未)

f="水"

g="羊"

;;

戊申)

f="土"

g="猴"

;;

已酉)

f="土"

g="鸡"

;;

庚戌)

f="金"

g="狗"

;;

辛亥)

f="金"

g="猪"

;;

壬子)

f="木"

g="鼠"

;;

癸丑)

f="木"

g="牛"

;;

甲寅)

f="水"

g="虎"

;;

乙卯)

f="水"

g="免"

;;

丙辰)

f="土"

g="龙"

;;

丁巳)

f="土"

g="蛇"

;;

戊午)

f="火"

g="马"

;;

已未)

f="火"

g="羊"

;;

庚申)

f="木"

g="猴"

;;

辛酉)

f="木"

g="鸡"

;;

壬戌)

f="水"

g="狗"

;;

癸亥)

f="水"

g="猪"

esac

echo "$f命"

echo "属$g"

#输出当前的 五行 和×××

#下面是计算月干支历,因每个干支月的交替是按24节气循环的,这里没有给出精确的24节气时间,所以在计算月干支时,如果在节气交替前后一天可能会不确。

# 1/5 ; 2/4 ; 3/6 ; 4/5; 5/6; 6/6 ; 7/7 ; 8/8 ; 9/8 ; 10/8 ; 11/7 ;12/7

#下面是计算月干支历

case $c in

#如果当前的天干在下面的选项里,则跟据节气交替日来判断这个月的干支。

甲 | 已 )

#如果现在的年天干 为 甲 或 乙。 则跟据节气交替日来判断这个月的干支

case $yue in

2)

#2月

if [ $ri -gt 4 ]

then

e="丙寅"

else

e="乙丑"

fi

;;

3)

#3月

       if [ $ri -gt 6 ]

       then

       e="丁卯"

       else

       e="丙寅"

       fi

;;

4)

#4月

       if [ $ri -gt 5 ]

       then

       e="戊辰"

       else

       e="丁卯"

       fi

;;

5)

#5月

       if [ $ri -gt 6 ]

       then

       e="已巳"

       else

       e="戊辰"

       fi

;;

6)

#6月

       if [ $ri -gt 6 ]

       then

       e="庚午"

       else

       e="已巳"

       fi

;;

7)

#7月

       if [ $ri -gt 7 ]

       then

       e="辛未"

       else

       e="庚午"

       fi

;;

8)

#8月    

       if [ $ri -gt 8 ]

       then

       e="壬申"

       else

       e="辛未"

       fi

;;

9)

#9月

       if [ $ri -gt 8 ]

       then

       e="癸酉"

       else

       e="壬申"

       fi

;;

10)

#10月

       if [ $ri -gt 8 ]

       then

       e="甲戌"

       else

       e="癸酉"

       fi

;;

11)

#11月

       if [ $ri -gt 7 ]

       then

       e="乙亥"

       else

       e="甲戌"

       fi

;;

12)

#12月

       if  [ $ri -gt 7 ]

       then

       e="丙子"

       else

       e="乙亥"

       fi

;;

1)

#1月

       if  [ $ri -gt 6 ]

       then

       e="丁丑"

       else

       e="丙子"

       fi

esac

;;

乙 | 庚 )

#如果现在的年天干 为 乙或庚。 则跟据节气交替日来判断这个月的干支

case $yue in

2)

if [ $ri -gt 4 ]

then

e="戊寅"

else

e="丁丑"

fi

;;

3)

       if [ $ri -gt 6 ]

       then

       e="已卯"

       else

       e="戊寅"

       fi

;;

4)

       if [ $ri -gt 5 ]

       then

       e="庚辰"

       else

       e="已卯"

       fi

;;

5)

       if [ $ri -gt 6 ]

       then

       e="辛巳"

       else

       e="庚辰"

       fi

;;

6)

       if [ $ri -gt 6 ]

       then

       e="壬午"

       else

       e="辛巳"

       fi

;;

7)

       if [ $ri -gt 7 ]

       then

       e="癸未"

       else

       e="壬午"

       fi

;;

8)

       if [ $ri -gt 8 ]

       then

       e="甲申"

       else

       e="癸未"

       fi

;;

9)

       if [ $ri -gt 8 ]

       then

       e="乙酉"

       else

       e="甲申"

       fi

;;

10)

       if [ $ri -gt 8 ]

       then

       e="丙戌"

       else

       e="乙酉"

       fi

;;

11)

       if [ $ri -gt 7 ]

       then

       e="丁亥"

       else

       e="丙戌"

       fi

;;

12)

       if  [ $ri -gt 7 ]

       then

       e="戊子"

       else

       e="丁亥"

       fi

;;

1)

       if  [ $ri -gt 6 ]

       then

       e="已丑"

       else

       e="戊子"

fi

esac

;;

丙 | 辛 )

#如果现在的年天干 为 丙或辛。 则跟据节气交替日来判断这个月的干支

case $yue in

2)

if [ $ri -gt 4 ]

then

e="庚寅"

else

e="已丑"

fi

;;

3)

       if [ $ri -gt 6 ]

       then

       e="辛卯"

       else

       e="庚寅"

       fi

;;

4)

       if [ $ri -gt 5 ]

       then

       e="壬辰"

       else

       e="辛卯"

       fi

;;

5)

       if [ $ri -gt 6 ]

       then

       e="癸巳"

       else

       e="壬辰"

       fi

;;

6)

       if [ $ri -gt 6 ]

       then

       e="甲午"

       else

       e="癸巳"

       fi

;;

7)

       if [ $ri -gt 7 ]

       then

       e="乙未"

       else

       e="甲午"

       fi

;;

8)

       if [ $ri -gt 8 ]

       then

       e="丙申"

       else

       e="乙未"

       fi

;;

9)

       if [ $ri -gt 8 ]

       then

       e="丁酉"

       else

       e="丙申"

       fi

;;

10)

       if [ $ri -gt 8 ]

       then

       e="戊戌"

       else

       e="丁酉"

       fi

;;

11)

       if [ $ri -gt 7 ]

       then

       e="已亥"

       else

       e="戊戌"

       fi

;;

12)

       if  [ $ri -gt 7 ]

       then

       e="庚子"

       else

       e="已亥"

       fi

;;

1)

       if  [ $ri -gt 6 ]

       then

       e="辛丑"

       else

       e="庚子"

fi

esac

;;

丁 | 壬 )

#如果现在的年天干 为 丁或壬。 则跟据节气交替日来判断这个月的干支

case $yue in

2)

if [ $ri -gt 4 ]

then

e="壬寅"

else

e="辛丑"

fi

;;

3)

       if [ $ri -gt 6 ]

       then

       e="癸卯"

       else

       e="壬寅"

       fi

;;

4)

       if [ $ri -gt 5 ]

       then

       e="甲辰"

       else

       e="癸卯"

       fi

;;

5)

       if [ $ri -gt 6 ]

       then

       e="乙巳"

       else

       e="甲辰"

       fi

;;

6)

       if [ $ri -gt 6 ]

       then

       e="丙午"

       else

       e="乙巳"

       fi

;;

7)

       if [ $ri -gt 7 ]

       then

       e="丁未"

       else

       e="丙午"

       fi

;;

8)

       if [ $ri -gt 8 ]

       then

       e="戊申"

       else

       e="丁未"

       fi

;;

9)

       if [ $ri -gt 8 ]

       then

       e="已酉"

       else

       e="戊申"

       fi

;;

10)

       if [ $ri -gt 8 ]

       then

       e="庚戌"

       else

       e="已酉"

       fi

;;

11)

       if [ $ri -gt 7 ]

       then

       e="辛亥"

       else

       e="庚戌"

       fi

;;

12)

       if  [ $ri -gt 7 ]

       then

       e="壬子"

       else

       e="辛亥"

       fi

;;

1)

       if  [ $ri -gt 6 ]

       then

       e="癸丑"

       else

       e="壬子"

fi

esac

;;

戊 | 癸 )

#如果现在的年天干 为 戊或癸。 则跟据节气交替日来判断这个月的干支

case $yue in

2)

if [ $ri -gt 4 ]

then

e="甲寅"

else

e="癸丑"

fi

;;

3)

       if [ $ri -gt 6 ]

       then

       e="乙卯"

       else

       e="甲寅"

       fi

;;

4)

       if [ $ri -gt 5 ]

       then

       e="丙辰"

       else

       e="乙卯"

       fi

;;

5)

       if [ $ri -gt 6 ]

       then

       e="丁巳"

       else

       e="丙辰"

       fi

;;

6)

       if [ $ri -gt 6 ]

       then

       e="戊午"

       else

       e="丁巳"

       fi

;;

7)

       if [ $ri -gt 7 ]

       then

       e="已未"

       else

       e="戊午"

       fi

;;

8)

       if [ $ri -gt 8 ]

       then

       e="庚申"

       else

       e="已未"

       fi

;;

9)

       if [ $ri -gt 8 ]

       then

       e="辛酉"

       else

       e="庚申"

       fi

;;

10)

       if [ $ri -gt 8 ]

       then

       e="壬戌"

       else

       e="辛酉"

       fi

;;

11)

       if [ $ri -gt 7 ]

       then

       e="癸亥"

       else

       e="壬戌"

       fi

;;

12)

       if  [ $ri -gt 7 ]

       then

       e="甲子"

       else

       e="癸亥"

       fi

;;

1)

       if  [ $ri -gt 6 ]

       then

       e="乙丑"

       else

       e="甲子"

     fi

esac

esac

echo "$e月"

#输出当前干支月

sh /tmp/jr

sh /tmp/js



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3