Python实现四柱八字排盘

时间:2021-03-15 06:17:07 生肖

其实八字排盘的程序时间再简单不过了,不过是年月日时分别除以60取余就好。不过Python的时间戳只支持到1970年,这也意味着1970年以前的八字是无法用python完成排盘的。

一、思路

首先,我们先把所有的二十四节气中的节以列表形式列出来。把时间输入进去后八字排盘四柱,放到列表里,根据输入时间在列表的位置,我们来确定年月日时的天干地支。

二、确定年月日时

__Jie = [19700105, 19700204, 19700306, 19700405, 19700506, 19700606, 19700707, 19700808, 19700908, 19701009, 19701108, 19701207, 19710106, 19710204, 19710306, 19710405, 19710506, 19710606, 19710708, 19710808,……]

这是所有节形成的列表八字排盘四柱,我们比如说1970年10月28,那么就是19701028放在列表中,求出这个值的index。这里是10,除以12(因为12个节是一年),所以1970年的天干地支就可以求出来了。

def Year(self):
        if self.list_num in self.__Jie:
            self.index_num = self.index_num + 1
        self.__Jie.append(self.list_num)
        self.__Jie.sort()
        self.index_num = self.__Jie.index(self.list_num)
        self.year_num = self.index_num // 12 + 1
        self.year_tg = (self.year_num + 6) % 10
        self.year_dz = (self.year_num + 8) % 12
        self.tiangan_year = self.__Tian_Gan[self.year_tg]
        self.dizhi_year = self.__Di_Zhi[self.year_dz]
        return self.tiangan_year, self.dizhi_year

二、月的算法

月是同样的道理,只不过要除以60:

八字四柱十神算命排盘_八字排盘四柱_四柱神煞排盘

def Month(self):
        self.month_num = self.index_num % 60
        self.month_tg = (self.month_num + 3) % 10
        self.month_dz = (self.month_num + 11) % 12
        self.tiangan_month = self.__Tian_Gan[self.month_tg]
        self.dizhi_month = self.__Di_Zhi[self.month_dz]
        return self.tiangan_month, self.dizhi_month

三、日的算法

日最为简单,不需要查表。因为日就是60天一个循环,我们只需要把时间戳除以60,取余数就好。

def Day(self):
        self.date_num = int((datetime.datetime(self.year, self.month, self.day) - self.stardard_day).days)
        self.day_num = self.date_num % 60
        self.day_tg = (self.day_num + 3) % 10
        self.day_dz = (self.day_num + 3) % 12
        self.tiangan_day = self.__Tian_Gan[self.day_tg]
        self.dizhi_day = self.__Di_Zhi[self.day_dz]
        self.hour_num = (datetime.datetime(self.year, self.month, self.day, self.hour) - self.stardard_day).seconds
        if self.hour_num == 0:
            self.tiangan_day = self.__Tian_Gan[self.day_tg + 1]
            self.dizhi_day = self.__Di_Zhi[self.day_dz + 1]
        return self.tiangan_day , self.dizhi_day

八字四柱十神算命排盘_八字排盘四柱_四柱神煞排盘

四、求时间的算法

由日推时有一个口诀,甲己还加甲 乙庚丙作初 丙辛从戊起 丁壬庚子居 戊癸何方发 壬子是真途,这说明,甲和己开头的日,那么这一日的子时是甲子时,后面以此类推。

这时候我们推时:

def Hour(self):
        self.hour_num = int((datetime.datetime(self.year, self.month, self.day, self.hour) - self.stardard_day).seconds / 3600)
        if self.hour_num == 0:
            self.hour_dz = 1
            self.day_tg = (self.day_tg + 1) % 10
            if self.day_tg == 1 or self.day_tg == 6:
                self.hour_tg = 1
            elif self.day_tg == 2 or self.day_tg == 7:
                self.hour_tg = 3
            elif self.day_tg== 3 or self.day_tg == 8:
                self.hour_tg = 5
            elif self.day_tg == 4 or self.day_tg == 9:
                self.hour_tg = 7
            elif self.day_tg == 5 or self.day_tg == 0:
                self.hour_tg = 9
        else:
            if self.hour_num % 2 == 0:
                self.hour_dz = self.hour_num / 2 + 1
                if self.day_tg == 1 or self.day_tg == 6:
                    self.hour_tg = (1 + self.hour_dz - 1) % 10
                elif self.day_tg == 2 or self.day_tg == 7:
                    self.hour_tg = (3 + self.hour_dz - 1) % 10
                elif self.day_tg == 3 or self.day_tg == 8:
                    self.hour_tg = (5 + self.hour_dz - 1) % 10
                elif self.day_tg == 4 or self.day_tg == 9:
                    self.hour_tg = (7 + self.hour_dz - 1) % 10
                elif self.day_tg == 5 or self.day_tg == 0:
                    self.hour_tg = (9 + self.hour_dz - 1) % 10
            elif self.hour_num % 2 == 1:
                self.hour_dz = (self.hour_num + 1) / 2
                if self.day_tg == 1 or self.day_tg == 6:
                    self.hour_tg = (1 + self.hour_dz - 1) % 10
                elif self.day_tg == 2 or self.day_tg == 7:
                    self.hour_tg = (3 + self.hour_dz - 1) % 10
                elif self.day_tg == 3 or self.day_tg == 8:
                    self.hour_tg = (5 + self.hour_dz - 1) % 10
                elif self.day_tg == 4 or self.day_tg == 9:
                    self.hour_tg = (7 + self.hour_dz - 1) % 10
                elif self.day_tg == 5 or self.day_tg == 0:
                    self.hour_tg = (9 + self.hour_dz - 1) % 10
        self.tiangan_hour = self.__Tian_Gan[self.hour_tg]
        self.dizhi_hour = self.__Hour_Dz[self.hour_dz]
        return self.tiangan_hour, self.dizhi_hour

四柱神煞排盘_八字四柱十神算命排盘_八字排盘四柱

五、求起运时间:

起运时间需要查表,根据阳男阴女顺排,阴男阳女逆排来和列表中上一个或者下一个值相减:

def QiYun(self):  #  sex:男为1,女为0
        if (self.sex == 1 and self.year_tg % 2 == 1) or (self.sex == 0 and self.year_tg % 2 == 0):
            self.jie_year = int(self.__Jie[self.index_num + 1] // 10000)
            self.jie_month = int((self.__Jie[self.index_num + 1] - self.jie_year * 10000) // 100)
            self.jie_day = int(self.__Jie[self.index_num + 1] % 10)
            self.jie_date = datetime.datetime(self.jie_year, self.jie_month, self.jie_day)
            self.jie_day_num = int((self.jie_date - self.input_day).days / 3)
            if self.jie_day_num == 0:
                self.jie_day_num = self.jie_day_num + 1
            self.qiyun_year = self.year + self.jie_day_num
            print(self.qiyun_year)
        elif (self.sex == 1 and self.year_tg % 2 == 0) or (self.sex == 0 and self.year_tg % 2 == 1):
            self.jie_year = int(self.__Jie[self.index_num - 1] // 10000)
            self.jie_month = int((self.__Jie[self.index_num - 1] - self.jie_year * 10000) // 100)
            self.jie_day = int(self.__Jie[self.index_num - 1] % 10)
            self.jie_date = datetime.datetime(self.jie_year, self.jie_month, self.jie_day)
            self.jie_day_num = int((self.input_day - self.jie_date).days / 3)
            if self.jie_day_num == 0:
                self.jie_day_num = self.jie_day_num + 1
            self.qiyun_year = self.year + self.jie_day_num
            return self.qiyun_year, self.__Sex[self.sex]

六、求空亡的算法

八字排盘四柱_八字四柱十神算命排盘_四柱神煞排盘

太简单了,我在我的八字课就这么教:地支数减去天干数的值和这个值减去1的值,就是空亡的地支代表的数字,如果小于0,结果加上12,以此求出空亡即可。比如壬午,壬9午7,7-9+12=10,所以空9和10,申和酉。

def KongWang(self):
        if self.day_tg == 0:
            self.day_tg = self.day_tg + 10
        self.kongwang = self.day_dz + 2 - self.day_tg
        if self.kongwang <= 0:
            self.kongwang = self.kongwang + 12
        self.kongwang1 = self.__Hour_Dz[self.kongwang - 1]
        self.kongwang2 = self.__Hour_Dz[self.kongwang]
        return self.kongwang1, self.kongwang2   #  空亡

七、大运的求法:

其余年份有了,大运就是从月柱衍生就好了:

def DaYun(self):
        if (self.sex == 1 and self.year_tg % 2 == 1) or (self.sex == 0 and self.year_tg % 2 == 0):
            i = 1
            while i <= 7:
                self.dayun_tg = self.month_tg + i
                if self.dayun_tg >= 10:
                    self.dayun_tg = self.dayun_tg - 10
                self.dayun_dz = self.month_dz + i
                if self.dayun_dz >= 12:
                    self.dayun_dz = self.dayun_dz - 12
                self.qiyun_y = self.qiyun_year + i * 10
                i += 1
                self.tiangan_dayun = self.__Tian_Gan[self.dayun_tg]
                self.dizhi_dayun = self.__Di_Zhi[self.dayun_dz]
        elif (self.sex == 1 and self.year_tg % 2 == 0) or (self.sex == 0 and self.year_tg % 2 == 1):
            i = 1
            while i <= 7:
                self.dayun_tg = self.month_tg - i
                if self.dayun_tg < 0:
                    self.dayun_tg = self.dayun_tg + 10
                self.dayun_dz = self.month_dz - i
                if self.dayun_dz < 12:
                    self.dayun_dz = (self.dayun_dz + 12) % 12
                self.qiyun_y = self.qiyun_year + (i - 1) * 10
                i += 1
                self.tiangan_dayun = self.__Tian_Gan[self.dayun_tg]
                self.dizhi_dayun = self.__Di_Zhi[self.dayun_dz]
        return self.tiangan_dayun, self.dizhi_dayun, self.qiyun_y