目录
- 一、初始化
-
- 二、使用方法
-
- 1、引入本库
- 2、获取某天的信息
- 3、获取某日的前几天或者后几天的信息
- 4、获取一年中的闰月
- 5、获取一个农历月的天数
- 6、儒略日数与公历的互转
- 7、查找某日之前或者之后的节气
- 8、四柱反查
一、初始化
1、安装
pip install sxtwl==2.0.6
2、基础数据
Gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
Zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
ShX = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
numCn = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
jqmc = ["冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏",
"小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降",
"立冬", "小雪", "大雪"]
ymc = ["十一", "十二", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
rmc = ["初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十", "卅一"]
XiZ = ['摩羯', '水瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手']
WeekCn = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
二、使用方法
1、引入本库
import sxtwl
day = sxtwl.fromSolar(2022, 8, 12)
day = sxtwl.fromLunar(2020, 12, 1)
2、获取某天的信息
s = "公历:%d年%d月%d日" % (day.getSolarYear(), day.getSolarMonth(), day.getSolarDay())
print(s)
print(WeekCn[day.getWeek()])
print('该日属于这个月的第%d周' % (day.getWeekIndex()))
print("星座:", XiZ[day.getConstellation()])
s = "以春节为界的农历:%d年%s%d月%d日" % (day.getLunarYear(),
'闰' if day.isLunarLeap() else '', day.getLunarMonth(), day.getLunarDay())
print(s)
s = "以立春为界的农历:%d年%s%d月%d日" % (day.getLunarYear(False),
'闰' if day.isLunarLeap() else '', day.getLunarMonth(), day.getLunarDay())
print(s)
yTG = day.getYearGZ(True)
print("以春节为界的年干支", Gan[yTG.tg] + Zhi[yTG.dz])
print("以春节为界的生肖:", ShX[yTG.dz])
yTG = day.getYearGZ()
print("以立春为界的年干支", Gan[yTG.tg] + Zhi[yTG.dz])
print("以立春为界的生肖:", ShX[yTG.dz])
mTG = day.getMonthGZ()
print("月干支", Gan[mTG.tg] + Zhi[mTG.dz])
dTG = day.getDayGZ()
print("日干支", Gan[dTG.tg] + Zhi[dTG.dz])
hour = 18
sTG = day.getHourGZ(hour)
print("%d时的干支" % (hour,), Gan[sTG.tg] + Zhi[sTG.dz])
for hour in range(24):
hTG = sxtwl.getShiGz(dTG.tg, hour)
print("%d时天干地支:" % (hour), Gan[hTG.tg] + Zhi[hTG.dz])
if day.hasJieQi():
print('节气:%s' % jqmc[day.getJieQi()])
jd = day.getJieQiJD()
t = sxtwl.JD2DD(jd)
print("节气时间:%d-%d-%d %d:%d:%d" % (t.Y, t.M, t.D, t.h, t.m, round(t.s)))
else:
print("当天不是节气日")
3、获取某日的前几天或者后几天的信息
num = 1
day = day.after(num)
s = "公历:%d年%d月%d日" % (day.getSolarYear(), day.getSolarMonth(), day.getSolarDay())
print(s)
day = day.before(num)
s = "公历:%d年%d月%d日" % (day.getSolarYear(), day.getSolarMonth(), day.getSolarDay())
print(s)
4、获取一年中的闰月
year = 2020
month = sxtwl.getRunMonth(year)
if month > 0:
print("%d年的闰月是%d" % (year, month))
else:
print("没有闰月")
5、获取一个农历月的天数
year = 2020
month = 4
isRun = False
daynum = sxtwl.getLunarMonthNum(year, month, isRun)
print("农历%d年%s%d月的天数:" % (year, '闰' if isRun else '', month), daynum)
6、儒略日数与公历的互转
jd = sxtwl.J2000
t = sxtwl.JD2DD(jd)
print(t)
jd = sxtwl.toJD(t)
print(jd)
7、查找某日之前或者之后的节气
while True:
day = day.after(1)
if day.hasJieQi():
print('节气:%s' % jqmc[day.getJieQi()])
jd = day.getJieQiJD()
t = sxtwl.JD2DD(jd)
print("节气时间:%d-%d-%d %d:%d:%d" % (t.Y, t.M, t.D, t.h, t.m, round(t.s)))
break
8、四柱反查
def getGZ(gzStr):
tg = -1
dz = -1
for i, v in enumerate(Gan):
if gzStr[0] == v:
tg = i
break
for i, v in enumerate(Zhi):
if gzStr[1] == v:
dz = i
break
return sxtwl.GZ(tg, dz)
jds = sxtwl.siZhu2Year(getGZ('辛丑'), getGZ('辛丑'), getGZ('丙戌'), getGZ('癸巳'), 2003, 2029)
for jd in jds:
t = sxtwl.JD2DD(jd )
print("符合条件的时间:%d-%d-%d %d:%d:%d"%(t.Y, t.M, t.D, t.h, t.m, round(t.s)))