rbprayertime 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/prayertime.rb +315 -0
- metadata +67 -0
data/lib/prayertime.rb
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
# prayertimes implementation for ruby - just a port of pyprayertime
|
|
4
|
+
# Author: Abdelrahman ghanem <abom.jdev@gmail.com>
|
|
5
|
+
#
|
|
6
|
+
# Algorithm is taken from http://qasweb.org
|
|
7
|
+
# Thanks to:
|
|
8
|
+
# Ahmed Essam <suda.nix@hotmail.com>: cpp implementation (first one)
|
|
9
|
+
# Ahmed Youssef <xmonader@gmail.com>: pyprayertime (python implementation, mine :D)
|
|
10
|
+
# Ahmed Atalla <a.atalla@linuxac.org>: support and azan!
|
|
11
|
+
# Amine Roukh <amineroukh@gmail.com>: qibla & qibla distance cpp implementation
|
|
12
|
+
#
|
|
13
|
+
# This program is free software; you can redistribute it and/or modify
|
|
14
|
+
# it under the terms of the GNU General Public License as published by
|
|
15
|
+
# the Free Software Foundation; either version 2 of the License, or
|
|
16
|
+
# (at your option) any later version.
|
|
17
|
+
#
|
|
18
|
+
# This program is distributed in the hope that it will be useful,
|
|
19
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21
|
+
# GNU General Public License for more details.
|
|
22
|
+
|
|
23
|
+
# You should have received a copy of the GNU General Public License along
|
|
24
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
25
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
26
|
+
require 'date'
|
|
27
|
+
include Math
|
|
28
|
+
|
|
29
|
+
def degrees(rad)
|
|
30
|
+
rad * (180 / PI)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def radians(deg)
|
|
34
|
+
deg * (PI / 180)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def remove_duplication(var)
|
|
38
|
+
##BETTER WAY?
|
|
39
|
+
if var > 360
|
|
40
|
+
var /= 360
|
|
41
|
+
var -= var.to_i
|
|
42
|
+
var *= 360
|
|
43
|
+
end
|
|
44
|
+
var
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class Season
|
|
48
|
+
Winter, Summer = 0, 1
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class Calendar
|
|
52
|
+
UmmAlQuraUniv,
|
|
53
|
+
EgyptianGeneralAuthorityOfSurvey,
|
|
54
|
+
UnivOfIslamicSciencesKarachi,
|
|
55
|
+
IslamicSocietyOfNorthAmerica,
|
|
56
|
+
MuslimWorldLeague = (1..5).to_a
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class Mazhab
|
|
60
|
+
Default, Hanafi = 0, 1
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def to_hrtime(var, isAM = false)
|
|
64
|
+
# var: double -> human readable string of format "%I:%M:%S %p"
|
|
65
|
+
|
|
66
|
+
time = ''
|
|
67
|
+
intvar = var.to_i #var is double.
|
|
68
|
+
if isAM
|
|
69
|
+
if intvar % 12 and intvar % 12 < 12
|
|
70
|
+
zone = "AM"
|
|
71
|
+
else
|
|
72
|
+
zone = "PM"
|
|
73
|
+
end
|
|
74
|
+
else
|
|
75
|
+
zone = "PM"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
if intvar > 12
|
|
79
|
+
time += (intvar%12).to_s
|
|
80
|
+
elsif intvar % 12 == 12
|
|
81
|
+
time += var.to_s
|
|
82
|
+
else
|
|
83
|
+
time += var.to_s
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
time += ":"
|
|
87
|
+
var -= intvar
|
|
88
|
+
var *= 60
|
|
89
|
+
minute = var.to_i
|
|
90
|
+
time += minute.to_s
|
|
91
|
+
|
|
92
|
+
time += ":"
|
|
93
|
+
|
|
94
|
+
var -= var.to_i
|
|
95
|
+
var *= 60
|
|
96
|
+
sec = var.to_i
|
|
97
|
+
time += sec.to_s
|
|
98
|
+
time += " "
|
|
99
|
+
|
|
100
|
+
time += zone
|
|
101
|
+
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
class Coordinate
|
|
105
|
+
|
|
106
|
+
attr :longitude, true
|
|
107
|
+
attr :latitude, true
|
|
108
|
+
attr :zone, true
|
|
109
|
+
|
|
110
|
+
def initialize(longitude, latitude, zone)
|
|
111
|
+
@longitude = longitude
|
|
112
|
+
@latitude = latitude
|
|
113
|
+
@zone = zone
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
class Prayertime
|
|
119
|
+
|
|
120
|
+
def initialize(longitude, latitude, zone,
|
|
121
|
+
year, month, day,
|
|
122
|
+
cal=Calendar::UmmAlQuraUniv, mazhab=Mazhab::Default, season=Season::Winter)
|
|
123
|
+
|
|
124
|
+
@coordinate = Coordinate.new(longitude, latitude, zone)
|
|
125
|
+
@date = Date.new(year, month, day)
|
|
126
|
+
@calendar = cal
|
|
127
|
+
@mazhab = mazhab
|
|
128
|
+
@season = season
|
|
129
|
+
@shrouk = nil
|
|
130
|
+
@fajr = nil
|
|
131
|
+
@zuhr = nil
|
|
132
|
+
@asr = nil
|
|
133
|
+
@maghrib = nil
|
|
134
|
+
@isha = nil
|
|
135
|
+
@dec = 0
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def shrouk_time
|
|
139
|
+
# Gets the time of fajr.
|
|
140
|
+
to_hrtime(@shrouk, true)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def fajr_time
|
|
144
|
+
# Gets the time of fajr.
|
|
145
|
+
to_hrtime(@fajr, true)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def zuhr_time
|
|
149
|
+
# Gets the time of zuhr.
|
|
150
|
+
to_hrtime(@zuhr, true)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def asr_time
|
|
154
|
+
# Gets the time of asr.
|
|
155
|
+
to_hrtime(@asr)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def maghrib_time
|
|
159
|
+
# Gets the time of maghrib.
|
|
160
|
+
to_hrtime(@maghrib)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def isha_time
|
|
164
|
+
# Gets the time of isha.
|
|
165
|
+
to_hrtime(@isha)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def calculate
|
|
169
|
+
# Calculations of prayertimes.
|
|
170
|
+
|
|
171
|
+
year = @date.year
|
|
172
|
+
month = @date.month
|
|
173
|
+
day = @date.day
|
|
174
|
+
longitude = @coordinate.longitude
|
|
175
|
+
latitude = @coordinate.latitude
|
|
176
|
+
zone = @coordinate.zone
|
|
177
|
+
julian_day = (367*year)-(((year+((month+9)/12).to_i)*7)/4).to_i+(275*month/9).to_i+day-730531.5
|
|
178
|
+
sun_length = 280.461+0.9856474*julian_day
|
|
179
|
+
sun_length = remove_duplication(sun_length)
|
|
180
|
+
middle_sun = 357.528+0.9856003*julian_day
|
|
181
|
+
middle_sun = remove_duplication(middle_sun)
|
|
182
|
+
|
|
183
|
+
lamda = sun_length+1.915*sin(radians(middle_sun))+0.02*sin(radians(2*middle_sun))
|
|
184
|
+
lamda = remove_duplication(lamda)
|
|
185
|
+
|
|
186
|
+
obliquity = 23.439-0.0000004*julian_day
|
|
187
|
+
|
|
188
|
+
alpha = degrees(atan(cos(radians(obliquity))*tan(radians(lamda))))
|
|
189
|
+
|
|
190
|
+
if 90 < lamda and lamda < 180
|
|
191
|
+
alpha += 180
|
|
192
|
+
elsif 100 < lamda and lamda < 360
|
|
193
|
+
alpha += 360
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
st = 100.46+0.985647352*julian_day
|
|
197
|
+
|
|
198
|
+
st = remove_duplication(st)
|
|
199
|
+
|
|
200
|
+
@dec = degrees(asin(sin(radians(obliquity))*sin(radians(lamda))))
|
|
201
|
+
|
|
202
|
+
noon = alpha-st
|
|
203
|
+
|
|
204
|
+
noon += 360 if noon < 0
|
|
205
|
+
|
|
206
|
+
unt_noon = noon-longitude
|
|
207
|
+
local_noon = (unt_noon/15)+zone
|
|
208
|
+
zuhr = local_noon #Zuhr Time.
|
|
209
|
+
maghrib = local_noon+equation(-0.8333)/15 # Maghrib Time
|
|
210
|
+
shrouk = local_noon-equation(-0.8333)/15 # Shrouk Time
|
|
211
|
+
|
|
212
|
+
fajr_alt = 0
|
|
213
|
+
isha_alt = 0
|
|
214
|
+
|
|
215
|
+
case @calendar
|
|
216
|
+
when Calendar::UmmAlQuraUniv
|
|
217
|
+
fajr_alt = -19
|
|
218
|
+
when Calendar::EgyptianGeneralAuthorityOfSurvey
|
|
219
|
+
fajr_alt = -19.5
|
|
220
|
+
isha_alt = -17.5
|
|
221
|
+
when Calendar::MuslimWorldLeague
|
|
222
|
+
fajr_alt = -18
|
|
223
|
+
isha_alt = -17
|
|
224
|
+
when Calendar::IslamicSocietyOfNorthAmerica
|
|
225
|
+
fajr_alt = isha_alt = -15
|
|
226
|
+
when Calendar::UnivOfIslamicSciencesKarachi
|
|
227
|
+
fajr_alt = isha_alt = -18
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
fajr = local_noon-equation(fajr_alt)/15 # Fajr Time
|
|
231
|
+
isha = local_noon+equation(isha_alt)/15 # Isha Time
|
|
232
|
+
|
|
233
|
+
isha = (maghrib+1.5) if @calendar == Calendar::UmmAlQuraUniv
|
|
234
|
+
|
|
235
|
+
asr_alt = 0
|
|
236
|
+
|
|
237
|
+
if @mazhab == Mazhab::Hanafi
|
|
238
|
+
asr_alt = 90 - degrees(atan(2+tan(radians((latitude - @dec).abs))))
|
|
239
|
+
else
|
|
240
|
+
asr_alt = 90 - degrees(atan(1 + tan(radians((latitude - @dec).abs))))
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
asr = local_noon+equation(asr_alt)/15 # Asr Time.
|
|
244
|
+
|
|
245
|
+
#Add one hour to all times if the season is Summmer.
|
|
246
|
+
if @season == Season::Summer
|
|
247
|
+
fajr += 1
|
|
248
|
+
shrouk += 1
|
|
249
|
+
zuhr += 1
|
|
250
|
+
asr += 1
|
|
251
|
+
maghrib += 1
|
|
252
|
+
isha += 1
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
@shrouk = shrouk
|
|
256
|
+
@fajr = fajr
|
|
257
|
+
@zuhr = zuhr
|
|
258
|
+
@asr = asr
|
|
259
|
+
@maghrib = maghrib
|
|
260
|
+
@isha = isha
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def equation(alt)
|
|
264
|
+
#return RadToDeg*acos((sin(alt*DegToRad)-sin(self.dec*DegToRad)*sin(self.coordinate.latitude*DegToRad))/(cos(self.dec*DegToRad)*cos(self.coordinate.latitude*DegToRad)))
|
|
265
|
+
degrees( acos( (sin(radians(alt)) - sin(radians(@dec)) * sin(radians(@coordinate.latitude)))/(cos(radians(@dec))*cos(radians(@coordinate.latitude)))))
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def qibla
|
|
269
|
+
|
|
270
|
+
k_lat = radians(21.423333);
|
|
271
|
+
k_lon = radians(39.823333);
|
|
272
|
+
|
|
273
|
+
longitude = radians(@coordinate.longitude)
|
|
274
|
+
latitude = radians(@coordinate.latitude)
|
|
275
|
+
|
|
276
|
+
numerator = sin(k_lon - longitude)
|
|
277
|
+
denominator = (cos(latitude) * tan(k_lat)) - (sin(latitude) * cos(k_lon - longitude))
|
|
278
|
+
q = atan2(numerator,denominator)
|
|
279
|
+
|
|
280
|
+
q = degrees(q)
|
|
281
|
+
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def qibla_distance
|
|
285
|
+
|
|
286
|
+
k_lat = radians(21.423333);
|
|
287
|
+
k_lon = radians(39.823333);
|
|
288
|
+
|
|
289
|
+
longitude = radians(@coordinate.longitude)
|
|
290
|
+
latitude = radians(@coordinate.latitude)
|
|
291
|
+
|
|
292
|
+
r = 6378.7 #kilometers
|
|
293
|
+
|
|
294
|
+
acos(sin(k_lat) * sin(latitude) + cos(k_lat) * cos(latitude) * cos(longitude-k_lon)) * r
|
|
295
|
+
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def report
|
|
299
|
+
# Simple report of all prayertimes.
|
|
300
|
+
puts "Fajr Time is #{fajr_time}"
|
|
301
|
+
puts "Shrouk Time is #{shrouk_time}"
|
|
302
|
+
puts "Zuhr Time is #{zuhr_time}"
|
|
303
|
+
puts "Asr Time is #{asr_time}"
|
|
304
|
+
puts "Maghrib Time is #{maghrib_time}"
|
|
305
|
+
puts "Ishaa Time is #{isha_time}"
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
if $0 == __FILE__
|
|
310
|
+
pt=Prayertime.new(31.3101, 29.7768, 2, 2010, 12, 6, Calendar::EgyptianGeneralAuthorityOfSurvey, Mazhab::Default, Season::Winter)
|
|
311
|
+
pt.calculate()
|
|
312
|
+
pt.report()
|
|
313
|
+
puts pt.qibla.to_s + "° N"
|
|
314
|
+
puts pt.qibla_distance.to_s + " km"
|
|
315
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rbprayertime
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 27
|
|
5
|
+
prerelease: false
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 1
|
|
9
|
+
- 0
|
|
10
|
+
version: 0.1.0
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Abdelrahmn Ghanem
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2010-12-06 00:00:00 +02:00
|
|
19
|
+
default_executable:
|
|
20
|
+
dependencies: []
|
|
21
|
+
|
|
22
|
+
description: A ruby port of pyprayertime (prayertime algorithm implementation in python).
|
|
23
|
+
email: abom.jdev@gmail.com
|
|
24
|
+
executables: []
|
|
25
|
+
|
|
26
|
+
extensions: []
|
|
27
|
+
|
|
28
|
+
extra_rdoc_files: []
|
|
29
|
+
|
|
30
|
+
files:
|
|
31
|
+
- lib/prayertime.rb
|
|
32
|
+
has_rdoc: true
|
|
33
|
+
homepage: http://bitbucket.org/abom/rbprayertime
|
|
34
|
+
licenses: []
|
|
35
|
+
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
none: false
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
hash: 3
|
|
47
|
+
segments:
|
|
48
|
+
- 0
|
|
49
|
+
version: "0"
|
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
|
+
none: false
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
hash: 3
|
|
56
|
+
segments:
|
|
57
|
+
- 0
|
|
58
|
+
version: "0"
|
|
59
|
+
requirements: []
|
|
60
|
+
|
|
61
|
+
rubyforge_project: rbprayertime
|
|
62
|
+
rubygems_version: 1.3.7
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 3
|
|
65
|
+
summary: A ruby port of pyprayertime.
|
|
66
|
+
test_files: []
|
|
67
|
+
|