Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions lunar_python/eightchar/DaYun.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ def __init__(self, yun, index: int):
self.__startAge = self.__startYear - birth_year + 1
self.__endYear = self.__startYear + 9
self.__endAge = self.__startAge + 9
# 预计算干支
if self.__index < 1:
self.__ganZhi = ""
else:
offset = LunarUtil.getJiaZiIndex(self.__lunar.getMonthInGanZhiExact())
offset += self.__index if self.__yun.isForward() else -self.__index
size = len(LunarUtil.JIA_ZI)
if offset >= size:
offset -= size
if offset < 0:
offset += size
self.__ganZhi = LunarUtil.JIA_ZI[offset]
# 预计算流年基准偏移量(只查一次节气表)
self.__liuNianBaseOffset = LunarUtil.getJiaZiIndex(
self.__lunar.getJieQiTable()["立春"].getLunar().getYearInGanZhiExact()
)
# 缓存
self.__liuNian = None
self.__xiaoYun = None

def getStartYear(self):
return self.__startYear
Expand All @@ -44,35 +63,30 @@ def getIndex(self):
def getLunar(self):
return self.__lunar

def getLiuNianBaseOffset(self):
"""获取流年基准偏移量(供 LiuNian 使用)"""
return self.__liuNianBaseOffset

def getGanZhi(self):
"""
获取干支
:return: 干支
"""
if self.__index < 1:
return ""
offset = LunarUtil.getJiaZiIndex(self.__lunar.getMonthInGanZhiExact())
offset += self.__index if self.__yun.isForward() else -self.__index
size = len(LunarUtil.JIA_ZI)
if offset >= size:
offset -= size
if offset < 0:
offset += size
return LunarUtil.JIA_ZI[offset]
return self.__ganZhi

def getXun(self):
"""
获取所在旬
:return: 旬
"""
return LunarUtil.getXun(self.getGanZhi())
return LunarUtil.getXun(self.__ganZhi)

def getXunKong(self):
"""
获取旬空(空亡)
:return: 旬空(空亡)
"""
return LunarUtil.getXunKong(self.getGanZhi())
return LunarUtil.getXunKong(self.__ganZhi)

def getLiuNian(self, n=10):
"""
Expand All @@ -82,10 +96,9 @@ def getLiuNian(self, n=10):
"""
if self.__index < 1:
n = self.__endYear - self.__startYear + 1
liu_nian = []
for i in range(0, n):
liu_nian.append(LiuNian(self, i))
return liu_nian
if self.__liuNian is None or len(self.__liuNian) != n:
self.__liuNian = [LiuNian(self, i) for i in range(n)]
return self.__liuNian

def getXiaoYun(self, n=10):
"""
Expand All @@ -95,7 +108,6 @@ def getXiaoYun(self, n=10):
"""
if self.__index < 1:
n = self.__endYear - self.__startYear + 1
xiao_yun = []
for i in range(0, n):
xiao_yun.append(XiaoYun(self, i, self.__yun.isForward()))
return xiao_yun
if self.__xiaoYun is None or len(self.__xiaoYun) != n:
self.__xiaoYun = [XiaoYun(self, i, self.__yun.isForward()) for i in range(n)]
return self.__xiaoYun
26 changes: 13 additions & 13 deletions lunar_python/eightchar/LiuNian.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ class LiuNian:

def __init__(self, da_yun, index):
self.__daYun = da_yun
self.__lunar = da_yun.getLunar()
self.__index = index
self.__year = da_yun.getStartYear() + index
self.__age = da_yun.getStartAge() + index
# 使用 DaYun 预计算的基准偏移量,避免重复查节气表
offset = da_yun.getLiuNianBaseOffset() + self.__index
if da_yun.getIndex() > 0:
offset += da_yun.getStartAge() - 1
offset %= len(LunarUtil.JIA_ZI)
self.__ganZhi = LunarUtil.JIA_ZI[offset]
self.__liuYue = None

def getIndex(self):
return self.__index
Expand All @@ -29,33 +35,27 @@ def getGanZhi(self):
获取干支
:return: 干支
"""
offset = LunarUtil.getJiaZiIndex(self.__lunar.getJieQiTable()["立春"].getLunar().getYearInGanZhiExact()) + self.__index
if self.__daYun.getIndex() > 0:
offset += self.__daYun.getStartAge() - 1
offset %= len(LunarUtil.JIA_ZI)
return LunarUtil.JIA_ZI[offset]
return self.__ganZhi

def getXun(self):
"""
获取所在旬
:return: 旬
"""
return LunarUtil.getXun(self.getGanZhi())
return LunarUtil.getXun(self.__ganZhi)

def getXunKong(self):
"""
获取旬空(空亡)
:return: 旬空(空亡)
"""
return LunarUtil.getXunKong(self.getGanZhi())
return LunarUtil.getXunKong(self.__ganZhi)

def getLiuYue(self):
"""
获取流月
:return: 流月
"""
n = 12
liu_yue = []
for i in range(0, n):
liu_yue.append(LiuYue(self, i))
return liu_yue
if self.__liuYue is None:
self.__liuYue = [LiuYue(self, i) for i in range(12)]
return self.__liuYue
42 changes: 18 additions & 24 deletions lunar_python/eightchar/LiuYue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ class LiuYue:
def __init__(self, liu_nian, index):
self.__liuNian = liu_nian
self.__index = index
# 预计算干支
year_gan = liu_nian.getGanZhi()[:1]
if "甲" == year_gan or "己" == year_gan:
offset = 2
elif "乙" == year_gan or "庚" == year_gan:
offset = 4
elif "丙" == year_gan or "辛" == year_gan:
offset = 6
elif "丁" == year_gan or "壬" == year_gan:
offset = 8
else:
offset = 0
gan = LunarUtil.GAN[(index + offset) % 10 + 1]
zhi = LunarUtil.ZHI[(index + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12 + 1]
self.__ganZhi = gan + zhi

def getIndex(self):
return self.__index
Expand All @@ -25,41 +40,20 @@ def getMonthInChinese(self):
def getGanZhi(self):
"""
获取干支
<p>
《五虎遁》
甲己之年丙作首,
乙庚之年戊为头,
丙辛之年寻庚上,
丁壬壬寅顺水流,
若问戊癸何处走,
甲寅之上好追求。
:return: 干支
"""
offset = 0
year_gan_zhi = self.__liuNian.getGanZhi()
year_gan = year_gan_zhi[:1]
if "甲" == year_gan or "己" == year_gan:
offset = 2
elif "乙" == year_gan or "庚" == year_gan:
offset = 4
elif "丙" == year_gan or "辛" == year_gan:
offset = 6
elif "丁" == year_gan or "壬" == year_gan:
offset = 8
gan = LunarUtil.GAN[(self.__index + offset) % 10 + 1]
zhi = LunarUtil.ZHI[(self.__index + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12 + 1]
return gan + zhi
return self.__ganZhi

def getXun(self):
"""
获取所在旬
:return: 旬
"""
return LunarUtil.getXun(self.getGanZhi())
return LunarUtil.getXun(self.__ganZhi)

def getXunKong(self):
"""
获取旬空(空亡)
:return: 旬空(空亡)
"""
return LunarUtil.getXunKong(self.getGanZhi())
return LunarUtil.getXunKong(self.__ganZhi)
26 changes: 14 additions & 12 deletions lunar_python/eightchar/XiaoYun.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def __init__(self, da_yun, index, forward):
self.__year = da_yun.getStartYear() + index
self.__age = da_yun.getStartAge() + index
self.__forward = forward
# 预计算干支
offset = LunarUtil.getJiaZiIndex(self.__lunar.getTimeInGanZhi())
add = self.__index + 1
if da_yun.getIndex() > 0:
add += da_yun.getStartAge() - 1
offset += add if self.__forward else -add
size = len(LunarUtil.JIA_ZI)
while offset < 0:
offset += size
offset %= size
self.__ganZhi = LunarUtil.JIA_ZI[offset]

def getIndex(self):
return self.__index
Expand All @@ -29,27 +40,18 @@ def getGanZhi(self):
获取干支
:return: 干支
"""
offset = LunarUtil.getJiaZiIndex(self.__lunar.getTimeInGanZhi())
add = self.__index + 1
if self.__daYun.getIndex() > 0:
add += self.__daYun.getStartAge() - 1
offset += add if self.__forward else -add
size = len(LunarUtil.JIA_ZI)
while offset < 0:
offset += size
offset %= size
return LunarUtil.JIA_ZI[offset]
return self.__ganZhi

def getXun(self):
"""
获取所在旬
:return: 旬
"""
return LunarUtil.getXun(self.getGanZhi())
return LunarUtil.getXun(self.__ganZhi)

def getXunKong(self):
"""
获取旬空(空亡)
:return: 旬空(空亡)
"""
return LunarUtil.getXunKong(self.getGanZhi())
return LunarUtil.getXunKong(self.__ganZhi)