-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchinese_solar_qin.py
74 lines (58 loc) · 1.95 KB
/
chinese_solar_qin.py
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
#!/usr/bin/python3
# Convert between the old Chinese calendar (Huangdi era) and Julian Day.
from fractions import Fraction
from math import floor
from months import JIEQI, SOLAR_TERMS
from solun import truesun
solar_year = 365 + Fraction(5,24) + Fraction(49,1440) + Fraction(328,864000)
solar_term = solar_year / 24
solstice = Fraction(708780574297, 432000) + 1 # instant of the southern solstice preceding the first year of the reign of Emperor Qin Shi Huangdi, GMT.
epoch = solstice + (3 * solar_term)
timezone = Fraction(8,24)
def tojd(day, st, year):
day = int(day)
st = str(st)
year = int(year)
if year > 0:
# positive years
jday = epoch + (solar_year * (year - 1))
else:
# negative years
jday = epoch + (year * solar_year)
for s in JIEQI.keys():
if s == st:
jday = truesun(jday, JIEQI[s], timezone) + day - 1
break
else:
jday += solar_term
return jday
def fromjd(jday):
jday = int(jday)
day = 0
st = ""
year = 0
if jday >= truesun(epoch + (3 * solar_term), 315, timezone):
# positive dates
orbits = ((jday - epoch) // solar_year)
dongzhi = solstice + (orbits * solar_year)
year = orbits + 1
xin = dongzhi + (3 * solar_term)
while jday >= truesun(xin + solar_year, 315, timezone):
year += 1
xin += solar_year
else:
# negative dates
year = 0 - ((epoch - jday) // solar_year)
dongzhi = solstice + (year * solar_year)
xin = dongzhi + (3 * solar_term)
while jday < truesun(xin, 315, timezone):
year -= 1
xin -= solar_year
term = xin
angle = 315
while truesun((term + solar_term), ((angle + 15) % 360), timezone) <= jday:
term += solar_term
angle = (angle + 15) % 360
st = SOLAR_TERMS[angle]
day = jday - truesun(term, angle, timezone) + 1
return(day, st, year)