-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbahai_obs.py
79 lines (61 loc) · 1.9 KB
/
bahai_obs.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
75
76
77
78
79
#!/usr/bin/python
from math import floor, ceil
from fractions import Fraction
from months import BAHAI_NORMAL, BAHAI_LEAP
from solun import trans
trop = 365 + Fraction(5,24) + Fraction(48,1440) + Fraction(45,86400) # tropical year
epoch = 2394281 + Fraction(572, 1440) # northward equinox of 0 BE
tz = Fraction(7,48) # Iranian standard time is UTC+3:30
def getnawruz(equinox):
'''Figure out the actual date of Nawruz'''
equinox = Fraction(equinox)
if equinox % 1 >= Fraction(18,24): # Bahá'í days start at sunset
nawruz = ceil(equinox)
else:
nawruz = floor(equinox)
return nawruz
def fromjd(jday):
'''Convert a Julian Day into a date in the new Bahá'í calendar'''
jday = int(jday)
day = 0
month = ""
year = (jday - floor(epoch)) // trop
equinox = trans((epoch + (year * trop)), 0.0, tz)
while getnawruz(equinox + trop) <= jday:
equinox += trop
while getnawruz(equinox) > jday:
equinox -= trop
nexteq = equinox + trop
nawruz = getnawruz(equinox)
next_nawruz = getnawruz(nexteq)
if next_nawruz - nawruz == 366:
MONTHS = BAHAI_LEAP
else:
MONTHS = BAHAI_NORMAL
monstar = nawruz
for m in MONTHS.keys():
if monstar + MONTHS[m] > jday:
break
else:
monstar += MONTHS[m]
month = m
day = jday - monstar + 1
return(day, month, year)
def tojd(day, month, year):
'''Convert a day in the new Bahá'í calendar into a Julian Day'''
day = int(day)
month = str(month)
day = int(day)
jday = epoch + (year * trop)
if (getnawruz(jday + trop) - getnawruz(jday)) == 366:
MONTHS = BAHAI_LEAP
else:
MONTHS = BAHAI_NORMAL
jday = getnawruz(jday)
for m in MONTHS.keys():
if m == month:
break
else:
jday += MONTHS[m]
jday = jday + day - 1
return(jday)