forked from 6tail/lunar-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLunarTime.py
More file actions
171 lines (131 loc) · 4.99 KB
/
LunarTime.py
File metadata and controls
171 lines (131 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
from . import NineStar
from .util import LunarUtil
class LunarTime:
"""
时辰
"""
def __init__(self, lunar_year, lunar_month, lunar_day, hour, minute, second):
from . import Lunar
self.__lunar = Lunar.fromYmdHms(lunar_year, lunar_month, lunar_day, hour, minute, second)
self.__zhiIndex = LunarUtil.getTimeZhiIndex("%02d:%02d" % (hour, minute))
self.__ganIndex = (self.__lunar.getDayGanIndexExact() % 5 * 2 + self.__zhiIndex) % 10
@staticmethod
def fromYmdHms(lunar_year, lunar_month, lunar_day, hour, minute, second):
return LunarTime(lunar_year, lunar_month, lunar_day, hour, minute, second)
def getGan(self):
return LunarUtil.GAN[self.__ganIndex + 1]
def getZhi(self):
return LunarUtil.ZHI[self.__zhiIndex + 1]
def getGanZhi(self):
return "%s%s" % (self.getGan(), self.getZhi())
def getShengXiao(self):
return LunarUtil.SHENGXIAO[self.__zhiIndex + 1]
def getPositionXi(self):
return LunarUtil.POSITION_XI[self.__ganIndex + 1]
def getPositionXiDesc(self):
return LunarUtil.POSITION_DESC[self.getPositionXi()]
def getPositionYangGui(self):
return LunarUtil.POSITION_YANG_GUI[self.__ganIndex + 1]
def getPositionYangGuiDesc(self):
return LunarUtil.POSITION_DESC[self.getPositionYangGui()]
def getPositionYinGui(self):
return LunarUtil.POSITION_YIN_GUI[self.__ganIndex + 1]
def getPositionYinGuiDesc(self):
return LunarUtil.POSITION_DESC[self.getPositionYinGui()]
def getPositionFu(self, sect=2):
return (LunarUtil.POSITION_FU if 1 == sect else LunarUtil.POSITION_FU_2)[self.__ganIndex + 1]
def getPositionFuDesc(self, sect=2):
return LunarUtil.POSITION_DESC[self.getPositionFu(sect)]
def getPositionCai(self):
return LunarUtil.POSITION_CAI[self.__ganIndex + 1]
def getPositionCaiDesc(self):
return LunarUtil.POSITION_DESC[self.getPositionCai()]
def getChong(self):
return LunarUtil.CHONG[self.__zhiIndex]
def getChongGan(self):
return LunarUtil.CHONG_GAN[self.__ganIndex]
def getChongGanTie(self):
return LunarUtil.CHONG_GAN_TIE[self.__ganIndex]
def getChongShengXiao(self):
chong = self.getChong()
for i in range(0, len(LunarUtil.ZHI)):
if LunarUtil.ZHI[i] == chong:
return LunarUtil.SHENGXIAO[i]
return ""
def getChongDesc(self):
return "(" + self.getChongGan() + self.getChong() + ")" + self.getChongShengXiao()
def getSha(self):
return LunarUtil.SHA[self.getZhi()]
def getNaYin(self):
return LunarUtil.NAYIN[self.getGanZhi()]
def getTianShen(self):
return LunarUtil.TIAN_SHEN[(self.__zhiIndex + LunarUtil.ZHI_TIAN_SHEN_OFFSET[self.__lunar.getDayZhiExact()]) % 12 + 1]
def getTianShenType(self):
return LunarUtil.TIAN_SHEN_TYPE[self.getTianShen()]
def getTianShenLuck(self):
return LunarUtil.TIAN_SHEN_TYPE_LUCK[self.getTianShenType()]
def getYi(self):
"""
获取时宜
:return: 宜
"""
return LunarUtil.getTimeYi(self.__lunar.getDayInGanZhiExact(), self.getGanZhi())
def getJi(self):
"""
获取时忌
:return: 忌
"""
return LunarUtil.getTimeJi(self.__lunar.getDayInGanZhiExact(), self.getGanZhi())
def getNineStar(self):
solar_ymd = self.__lunar.getSolar().toYmd()
jie_qi = self.__lunar.getJieQiTable()
asc = False
if jie_qi["冬至"] <= solar_ymd < jie_qi["夏至"]:
asc = True
start = 7 if asc else 3
day_zhi = self.__lunar.getDayZhi()
if day_zhi in "子午卯酉":
start = 1 if asc else 9
elif day_zhi in "辰戌丑未":
start = 4 if asc else 6
index = start + self.__zhiIndex - 1 if asc else start - self.__zhiIndex - 1
if index > 8:
index -= 9
if index < 0:
index += 9
return NineStar.fromIndex(index)
def getGanIndex(self):
return self.__ganIndex
def getZhiIndex(self):
return self.__zhiIndex
def __str__(self):
return self.toString()
def toString(self):
return self.getGanZhi()
def getXun(self):
"""
获取时辰所在旬
:return: 旬
"""
return LunarUtil.getXun(self.getGanZhi())
def getXunKong(self):
"""
获取值时空亡
:return: 空亡(旬空)
"""
return LunarUtil.getXunKong(self.getGanZhi())
def getMinHm(self):
hour = self.__lunar.getHour()
if hour < 1:
return "00:00"
elif hour > 22:
return "23:00"
return "%02d:00" % (hour - 1 if hour % 2 == 0 else hour)
def getMaxHm(self):
hour = self.__lunar.getHour()
if hour < 1:
return "00:59"
elif hour > 22:
return "23:59"
return "%02d:59" % (hour + 1 if hour % 2 != 0 else hour)