Skip to content

Commit

Permalink
修复立春年柱的逻辑判断错误
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba3 committed Feb 28, 2024
1 parent df42dfe commit 80b5862
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cnlunar/demo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import datetime, cnlunar
# print('a = cnlunar.Lunar(datetime.datetime(2021, 2, 7, 10, 30), godType=\'8char\')')
print('\n八字月柱与八字日柱算神煞版本')

# 常规算法 demo 应输出 壬寅
a = cnlunar.Lunar(datetime.datetime(2022, 2, 3, 10, 30), godType='8char')
# 八字立春切换算法 1986, 11, 1, 7, 0 demo 应输出 丙寅
# a = cnlunar.Lunar(datetime.datetime(1986, 11, 1, 7, 0), godType='8char')
# a = cnlunar.Lunar(datetime.datetime(1986, 11, 1, 7, 0), godType='8char', year8Char='beginningOfSpring')
# 八字立春切换算法 demo 应输出 辛丑
# a = cnlunar.Lunar(datetime.datetime(2022, 2, 3, 10, 30), godType='8char', year8Char='beginningOfSpring')
# 常规算法 (2024, 2, 4, 10, 30) demo 应输出 癸卯
Expand Down
33 changes: 30 additions & 3 deletions cnlunar/lunar.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, date, godType='8char', year8Char='year'):
# 立春干支参数

self._x = self.getBeginningOfSpringX()
# print('x=',int(self._x))
# print('x=', int(self._x))

(self.year8Char, self.month8Char, self.day8Char) = self.get_the8char()
self.get_earthNum(), self.get_heavenNum(), self.get_season()
Expand All @@ -54,9 +54,34 @@ def __init__(self, date, godType='8char', year8Char='year'):

def getBeginningOfSpringX(self):
# print(self.nextSolarTermYear, self.lunarYear, self.nextSolarNum, self.lunarYear - self.nextSolarTermYear)
isBeforBeginningOfSpring = self.nextSolarNum < 3
# print('是否在立春前', isBeforBeginningOfSpring, self.nextSolarNum)
# print('spanDays', self.spanDays)
isBeforLunarYear = self.spanDays < 0
# print('是过农历年', isBeforLunarYear)
_x = 0
if self.year8Char != 'beginningOfSpring':
return 0
return (self.nextSolarNum < 3) - (self.lunarMonth > 6)
return _x
# 现在节气在立春之前 且 已过完农历年(农历小于3月作为测试判断),年柱需要减1
if isBeforLunarYear:
# print('还没过农历年')
if not isBeforBeginningOfSpring:
# print('立春前')
# _x = 0
# else:
# print('立春后')
_x = -1
else:
# print('过农历年了')
if isBeforBeginningOfSpring:
# print('立春前')
_x = 1
# else:
# print('立春后')
# _x = 0
# print(_x)
return _x



def get_lunarYearCN(self):
Expand Down Expand Up @@ -156,8 +181,10 @@ def get_lunarDateNum(self):
"""
self.lunarYear, self.lunarMonth, self.lunarDay = self.date.year, 1, 1
_code_year = lunarNewYearList[self.lunarYear - START_YEAR]

""" 获取当前日期与当年春节的差日 """
_span_days = (self.date - datetime(self.lunarYear, ((_code_year >> 5) & 0x3), ((_code_year >> 0) & 0x1f))).days
self.spanDays = _span_days
if (_span_days >= 0):
""" 新年后推算日期,差日依序减月份天数,直到不足一个月,剪的次数为月数,剩余部分为日数 """
""" 先获取闰月 """
Expand Down

0 comments on commit 80b5862

Please sign in to comment.