1950-2050的天干地支

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39942341/article/details/85337097

 适用范围1950-2050,月建和时辰交叉的时间可能也不准

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# 天干
heavenly_stems = ['癸', '甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬']
# 地支
earthly_branches = ['亥', '子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌']
# 年数表
year_table = {
    1950: 31, 1960: 24, 1970: 16, 1980: 9, 1990: 1, 1951: 36, 1961: 29, 1971: 21, 1981: 14, 1991: 6, 1952: 42, 1962: 34,
    1972: 27, 1982: 19, 1992: 12,
    1953: 47, 1963: 39, 1973: 32, 1983: 24, 1993: 17, 1954: 52, 1964: 45, 1974: 37, 1984: 30, 1994: 22, 1955: 57,
    1965: 50, 1975: 42, 1985: 35,
    1995: 27, 1956: 3, 1966: 55, 1976: 48, 1986: 40, 1996: 33, 1957: 8, 1967: 0, 1977: 53, 1987: 45, 1997: 38, 1958: 13,
    1968: 6, 1978: 58, 1988: 51,
    1998: 43, 1959: 18, 1969: 11, 1979: 3, 1989: 56, 1999: 48, 2000: 54, 2010: 46, 2020: 39, 2030: 31, 2040: 24,
    2001: 59, 2011: 51, 2021: 44, 2031: 36,
    2041: 29, 2002: 4, 2012: 57, 2022: 49, 2032: 42, 2042: 34, 2003: 9, 2013: 2, 2023: 54, 2033: 47, 2043: 39, 2004: 15,
    2014: 7, 2024: 0, 2034: 52,
    2044: 45, 2005: 20, 2015: 12, 2025: 5, 2035: 57, 2045: 50, 2006: 25, 2016: 18, 2026: 10, 2036: 3, 2046: 55,
    2007: 30, 2017: 23, 2027: 15, 2037: 8,
    2047: 0, 2008: 36, 2018: 28, 2028: 21, 2038: 13, 2048: 6, 2009: 41, 2019: 33, 2029: 26, 2039: 18, 2049: 11
}
# 月数表
month_table = {3: 0, 4: 31, 5: 1, 6: 32, 7: 2, 8: 33, 9: 4, 10: 34, 11: 5, 12: 35, 1: 6, 2: 37}


def change(temp, flag):
    if flag:
        return heavenly_stems[temp]
    return earthly_branches[temp]


def get_all(y, m, d, h):
    y_heavenly_stems = (y - 3) % 10
    y_earthly_branches = (y - 3) % 12
    # 月地支
    if m == 2 and d >= 4 or m == 3 and d < 4:
        m_earthly_branches = 3
    elif m == 3 and d >= 4 or m == 4 and d < 4:
        m_earthly_branches = 4
    elif m == 4 and d >= 4 or m == 5 and d < 5:
        m_earthly_branches = 5
    elif m == 5 and d >= 5 or m == 6 and d < 5:
        m_earthly_branches = 6
    elif m == 6 and d >= 5 or m == 7 and d < 7:
        m_earthly_branches = 7
    elif m == 7 and d >= 7 or m == 8 and d < 7:
        m_earthly_branches = 8
    elif m == 8 and d >= 7 or m == 9 and d < 7:
        m_earthly_branches = 9
    elif m == 9 and d >= 7 or m == 10 and d < 7:
        m_earthly_branches = 10
    elif m == 10 and d >= 7 or m == 11 and d < 7:
        m_earthly_branches = 11
    elif m == 11 and d >= 7 or m == 12 and d < 6:
        m_earthly_branches = 0
    elif m == 12 and d >= 6 or m == 1 and d < 4:
        m_earthly_branches = 1
    else:
        m_earthly_branches = 2
    # 月天干
    if y_heavenly_stems % 5 == 1:
        m_heavenly_stems = 3
    elif y_heavenly_stems % 5 == 2:
        m_heavenly_stems = 5
    elif y_heavenly_stems % 5 == 3:
        m_heavenly_stems = 7
    elif y_heavenly_stems % 5 == 4:
        m_heavenly_stems = 9
    else:
        m_heavenly_stems = 1
    m_heavenly_stems += (m_earthly_branches - 3 + 12) % 12
    m_heavenly_stems %= 10
    # 月天干地支转换
    # m_earthly_branches = earthly_branches[m_earthly_branches]
    # m_heavenly_stems = heavenly_stems[m_heavenly_stems]
    temp_d = (year_table[y] + month_table[m] + d) % 60
    d_heavenly_stems = temp_d % 10
    d_earthly_branches = temp_d % 12
    # 时间地支
    if h >= 23 or h < 1:
        t_earthly_branches = 1
    elif h < 3:
        t_earthly_branches = 2
    elif h < 5:
        t_earthly_branches = 3
    elif h < 7:
        t_earthly_branches = 4
    elif h < 9:
        t_earthly_branches = 5
    elif h < 11:
        t_earthly_branches = 6
    elif h < 13:
        t_earthly_branches = 7
    elif h < 15:
        t_earthly_branches = 8
    elif h < 17:
        t_earthly_branches = 9
    elif h < 19:
        t_earthly_branches = 10
    elif h < 21:
        t_earthly_branches = 11
    else:
        t_earthly_branches = 0
    # 时间天干
    if d_heavenly_stems % 5 == 1:
        t_heavenly_stems = 1
    elif d_heavenly_stems % 5 == 2:
        t_heavenly_stems = 3
    elif d_heavenly_stems % 5 == 3:
        t_heavenly_stems = 5
    elif d_heavenly_stems % 5 == 4:
        t_heavenly_stems = 7
    else:
        t_heavenly_stems = 9
    t_heavenly_stems += (t_earthly_branches - 1 + 12) % 12
    t_heavenly_stems %= 10
    return [change(y_heavenly_stems, True), change(y_earthly_branches, False),
            change(m_heavenly_stems, True), change(m_earthly_branches, False),
            change(d_heavenly_stems, True), change(d_earthly_branches, False),
            change(t_heavenly_stems, True), change(t_earthly_branches, False)]


y, m, d, t = input().split()
y = int(y)
m = int(m)
d = int(d)
h, mi = map(int, t.split(':'))
result = get_all(y, m, d, h)
print('年:%s%s,月:%s%s,日:%s%s,时:%s%s' % (
    result[0], result[1],
    result[2], result[3],
    result[4], result[5],
    result[6], result[7]))

测试案例

1972 11 29 00:45
1998 03 21 05:25
1966 06 08 04:10
1958 07 23 20:10
1981 09 12 18:15
1981 09 13 10:15
2015 03 29 12:15
2000 09 13 20:15
1963 03 26 03:45
1958 07 11 02:15
1958 07 20 05:15
1958 07 24 11:25
1968 11 28 15:15
1958 12 01 14:25
1972 11 22 15:25
2002 06 17 10:15
1990 06 30 09:35
1987 06 30 10:05
2016 10 05 20:10
1966 12 17 17:30
1998 03 21 05:25
1958 07 11 02:15
2000 09 13 10:15
1987 05 21 10:05
1991 04 26 09:35
1998 10 27 06:10
1963 03 26 04:15
1966 06 10 02:10
1991 04 26 21:20
1963 03 26 18:15
1987 05 21 16:05
1998 10 27 11:45
1974 06 26 21:25
1968 12 12 12:15
1995 03 17 10:15
1995 05 22 17:15

答案

年:壬子,月:辛亥,日:甲子,时:甲子
年:戊寅,月:乙卯,日:丁卯,时:癸卯
年:丙午,月:甲午,日:戊戌,时:甲寅
年:戊戌,月:己未,日:辛丑,时:戊戌
年:辛酉,月:丁酉,日:癸巳,时:辛酉
年:辛酉,月:丁酉,日:甲午,时:己巳
年:乙未,月:己卯,日:甲辰,时:庚午
年:庚辰,月:乙酉,日:甲戌,时:甲戌
年:癸卯,月:乙卯,日:戊辰,时:甲寅
年:戊戌,月:己未,日:己丑,时:乙丑
年:戊戌,月:己未,日:戊戌,时:乙卯
年:戊戌,月:己未,日:壬寅,时:丙午
年:戊申,月:癸亥,日:壬寅,时:戊申
年:戊戌,月:癸亥,日:壬子,时:丁未
年:壬子,月:辛亥,日:丁巳,时:戊申
年:壬午,月:丙午,日:丙辰,时:癸巳
年:庚午,月:壬午,日:丙寅,时:癸巳
年:丁卯,月:丙午,日:庚戌,时:辛巳
年:丙申,月:丁酉,日:庚申,时:丙戌
年:丙午,月:庚子,日:庚戌,时:乙酉
年:戊寅,月:乙卯,日:丁卯,时:癸卯
年:戊戌,月:己未,日:己丑,时:乙丑
年:庚辰,月:乙酉,日:甲戌,时:己巳
年:丁卯,月:乙巳,日:庚午,时:辛巳
年:辛未,月:壬辰,日:丙寅,时:癸巳
年:戊寅,月:壬戌,日:丁未,时:癸卯
年:癸卯,月:乙卯,日:戊辰,时:甲寅
年:丙午,月:甲午,日:庚子,时:丁丑
年:辛未,月:壬辰,日:丙寅,时:己亥
年:癸卯,月:乙卯,日:戊辰,时:辛酉
年:丁卯,月:乙巳,日:庚午,时:甲申
年:戊寅,月:壬戌,日:丁未,时:丙午
年:甲寅,月:庚午,日:戊戌,时:癸亥
年:戊申,月:甲子,日:丙辰,时:甲午
年:乙亥,月:己卯,日:丁未,时:乙巳
年:乙亥,月:辛巳,日:癸丑,时:辛酉

猜你喜欢

转载自blog.csdn.net/qq_39942341/article/details/85337097
今日推荐