-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprayertime.py
275 lines (200 loc) · 7.95 KB
/
prayertime.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
__author__ = "Ahmed Youssef"
__all__ = ['Season', 'Calendar', 'Prayertime', 'Mazhab',
'as_pytime', 'as_pydatetime']
from math import degrees, radians, atan, asin, acos, cos, sin, tan, atan2
from datetime import date, datetime, time
from time import strptime
def remove_duplication(var):
return var % 360
class Season(object):
Winter, Summer = 0, 1
class TimeFormat(object):
H12, H24 = 0, 1
class Calendar(object):
UmmAlQuraUniv, \
EgyptianGeneralAuthorityOfSurvey,\
UnivOfIslamicSciencesKarachi,\
IslamicSocietyOfNorthAmerica,\
MuslimWorldLeague = range(5)
class Mazhab(object):
Default, Hanafi = 0, 1
def to_hrtime(var, timeformat):
hours = int(var)
minutes = int((var*60) % 60)
seconds = int((var*3600) % 60)
time2 = time(hours, minutes, seconds)
if timeformat == TimeFormat.H12:
return time2.strftime("%I:%M:%S %p")
else:
return time2.strftime("%H:%M:%S")
def as_pytime(string_to_parse, fmt="%I:%M:%S %p"):
"""returns time.tm_struct by parsing string_to_parse."""
return strptime(string_to_parse, fmt)
def as_pydatetime(d, ts):
"""returns a datetime object.
d: date object
ts: tm_struct
"""
return datetime(year=d.year, month=d.month, day=d.day,
hour=ts.tm_hour, minute=ts.tm_min, second=ts.tm_sec)
class Coordinate(object):
def __init__(self, longitude, latitude, zone):
"""
Describe a place by its longitude, latitude, and zone.
"""
self.longitude = longitude
self.latitude = latitude
self.zone = zone
class Prayertime(object):
def __init__(self, longitude, latitude, zone,
year, month, day,
cal=Calendar.UmmAlQuraUniv, mazhab=Mazhab.Default, season=Season.Winter, timeformat=TimeFormat.H12):
self.coordinate = Coordinate(longitude, latitude, zone)
self.date = date(year, month, day)
self.calendar = cal
self.mazhab = mazhab
self.season = season
self.timeformat = timeformat
self._shrouk = None
self._fajr = None
self._zuhr = None
self._asr = None
self._maghrib = None
self._isha = None
self.dec = 0
def shrouk_time(self):
"""Gets the time of shrouk."""
fmt = to_hrtime(self._shrouk, self.timeformat)
return fmt
def fajr_time(self):
"""Gets the time of fajr."""
fmt = to_hrtime(self._fajr, self.timeformat)
return fmt
def zuhr_time(self):
"""Gets the time of zuhr."""
fmt = to_hrtime(self._zuhr, self.timeformat)
return fmt
def asr_time(self):
"""Gets the time of asr."""
fmt = to_hrtime(self._asr, self.timeformat)
return fmt
def maghrib_time(self):
"""Gets the time of maghrib"""
fmt = to_hrtime(self._maghrib, self.timeformat)
return fmt
def isha_time(self):
"""Gets the time of isha"""
fmt = to_hrtime(self._isha, self.timeformat)
return fmt
def calculate(self):
"""Calculations of prayertimes."""
year = self.date.year
month = self.date.month
day = self.date.day
longitude = self.coordinate.longitude
latitude = self.coordinate.latitude
zone = self.coordinate.zone
julian_day = (367 * year) - int(((year + int((month + 9) / 12))
* 7) / 4) + int(275 * month / 9) + day - 730531.5
sun_length = 280.461 + 0.9856474 * julian_day
sun_length = remove_duplication(sun_length)
middle_sun = 357.528 + 0.9856003 * julian_day
middle_sun = remove_duplication(middle_sun)
lamda = sun_length + 1.915 * \
sin(radians(middle_sun)) + 0.02 * sin(radians(2 * middle_sun))
lamda = remove_duplication(lamda)
obliquity = 23.439 - 0.0000004 * julian_day
alpha = degrees(atan(cos(radians(obliquity)) * tan(radians(lamda))))
if 90 < lamda < 180:
alpha += 180
elif 100 < lamda < 360:
alpha += 360
ST = 100.46 + 0.985647352 * julian_day
ST = remove_duplication(ST)
self.dec = degrees(asin(sin(radians(obliquity)) * sin(radians(lamda))))
noon = alpha - ST
if noon < 0:
noon += 360
UTNoon = noon - longitude
local_noon = (UTNoon / 15) + zone
zuhr = local_noon # Zuhr Time.
maghrib = local_noon + self._equation(-0.8333) / 15 # Maghrib Time
shrouk = local_noon - self._equation(-0.8333) / 15 # Shrouk Time
fajr_alt = 0
isha_alt = 0
if self.calendar == Calendar.UmmAlQuraUniv:
fajr_alt = -19
elif self.calendar == Calendar.EgyptianGeneralAuthorityOfSurvey:
fajr_alt = -19.5
isha_alt = -17.5
elif self.calendar == Calendar.MuslimWorldLeague:
fajr_alt = -18
isha_alt = -17
elif self.calendar == Calendar.IslamicSocietyOfNorthAmerica:
fajr_alt = isha_alt = -15
elif self.calendar == Calendar.UnivOfIslamicSciencesKarachi:
fajr_alt = isha_alt = -18
fajr = local_noon - self._equation(fajr_alt) / 15 # Fajr Time
isha = local_noon + self._equation(isha_alt) / 15 # Isha Time
if self.calendar == Calendar.UmmAlQuraUniv:
isha = maghrib + 1.5
asr_alt = 0
if self.mazhab == Mazhab.Hanafi:
asr_alt = 90 - degrees(atan(2 + tan(radians(abs(latitude - self.dec)))))
else:
asr_alt = 90 - degrees(atan(1 + tan(radians(abs(latitude - self.dec)))))
asr = local_noon + self._equation(asr_alt) / 15 # Asr Time.
# Add one hour to all times if the season is Summmer.
if self.season == Season.Summer:
fajr += 1
shrouk += 1
zuhr += 1
asr += 1
maghrib += 1
isha += 1
self._shrouk = shrouk
self._fajr = fajr
self._zuhr = zuhr
self._asr = asr
self._maghrib = maghrib
self._isha = isha
def _equation(self, alt):
# return
# RadToDeg*acos((sin(alt*DegToRad)-sin(self.dec*DegToRad)*sin(self.coordinate.latitude*DegToRad))/(cos(self.dec*DegToRad)*cos(self.coordinate.latitude*DegToRad)))
return degrees(acos((sin(radians(alt)) - sin(radians(self.dec)) * sin(radians(self.coordinate.latitude))) / (cos(radians(self.dec)) * cos(radians(self.coordinate.latitude)))))
def report(self):
"""Simple report of all prayertimes."""
print(self.fajr_time())
print(self.shrouk_time())
print(self.zuhr_time())
print(self.asr_time())
print(self.maghrib_time())
print(self.isha_time())
# Fixes taken from rbprayertime --> Thanks abom :)
def get_qibla(self):
"""Returns the angle of the Qibla from north."""
k_lat = radians(21.423333)
k_lon = radians(39.823333)
longitude = radians(self.coordinate.longitude)
latitude = radians(self.coordinate.latitude)
numerator = sin(k_lon - longitude)
denominator = (cos(latitude) * tan(k_lat)) - \
(sin(latitude) * cos(k_lon - longitude))
q = atan2(numerator, denominator)
q = degrees(q)
return q
def get_qibla_distance(self):
"""Returns the distance to Kaaba in Kilometers."""
k_lat = radians(21.423333)
k_lon = radians(39.823333)
longitude = radians(self.coordinate.longitude)
latitude = radians(self.coordinate.latitude)
r = 6378.7 # kilometers
return acos(sin(k_lat) * sin(latitude) + cos(k_lat) * cos(latitude) * cos(longitude - k_lon)) * r
if __name__ == "__main__":
pt = Prayertime(31.2599, 30.0599, 2, 2010, 8, 6,
Calendar.EgyptianGeneralAuthorityOfSurvey, Mazhab.Default, Season.Summer, TimeFormat.H24)
print(pt.get_qibla())
pt.calculate()
pt.report()