rbprayertime 0.1.0 → 0.1.5

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.
Files changed (2) hide show
  1. data/lib/prayertime.rb +76 -32
  2. metadata +5 -5
data/lib/prayertime.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
  # coding: utf-8
3
- # prayertimes implementation for ruby - just a port of pyprayertime
4
- # Author: Abdelrahman ghanem <abom.jdev@gmail.com>
5
3
  #
6
- # Algorithm is taken from http://qasweb.org
4
+ # Prayertimes implementation for ruby - just a port of pyprayertime.
5
+ #
6
+ # Author: Abdelrahman ghanem <abom.jdev@gmail.com>.
7
+ #
8
+ # Algorithm is taken from http://qasweb.org.
7
9
  # Thanks to:
8
10
  # Ahmed Essam <suda.nix@hotmail.com>: cpp implementation (first one)
9
11
  # Ahmed Youssef <xmonader@gmail.com>: pyprayertime (python implementation, mine :D)
@@ -26,28 +28,25 @@
26
28
  require 'date'
27
29
  include Math
28
30
 
31
+ # Radians to Degrees
29
32
  def degrees(rad)
30
33
  rad * (180 / PI)
31
34
  end
32
-
35
+ # Degrees to Radians
33
36
  def radians(deg)
34
37
  deg * (PI / 180)
35
38
  end
36
39
 
37
40
  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
41
+ var % 360
45
42
  end
46
43
 
44
+ # A class represents seasons for day light saving time -dst-.
47
45
  class Season
48
46
  Winter, Summer = 0, 1
49
47
  end
50
48
 
49
+ # A class represents calculation calendars.
51
50
  class Calendar
52
51
  UmmAlQuraUniv,
53
52
  EgyptianGeneralAuthorityOfSurvey,
@@ -56,17 +55,23 @@ class Calendar
56
55
  MuslimWorldLeague = (1..5).to_a
57
56
  end
58
57
 
58
+ # A class represents mazhab -asr time difference only-.
59
59
  class Mazhab
60
60
  Default, Hanafi = 0, 1
61
61
  end
62
62
 
63
+ # A String formatted time: %I:%M:%S %p.
63
64
  def to_hrtime(var, isAM = false)
64
65
  # var: double -> human readable string of format "%I:%M:%S %p"
65
-
66
66
  time = ''
67
67
  intvar = var.to_i #var is double.
68
68
  if isAM
69
- if intvar % 12 and intvar % 12 < 12
69
+ # because 0, 1 arn't equal false, true in ruby
70
+ # in python 0 => False, 1 => True
71
+ # in ruby every thing except false and nil is true
72
+ a = true
73
+ a = false if intvar % 12 == 0
74
+ if a and intvar % 12 < 12
70
75
  zone = "AM"
71
76
  else
72
77
  zone = "PM"
@@ -78,9 +83,9 @@ def to_hrtime(var, isAM = false)
78
83
  if intvar > 12
79
84
  time += (intvar%12).to_s
80
85
  elsif intvar % 12 == 12
81
- time += var.to_s
86
+ time += intvar.to_s
82
87
  else
83
- time += var.to_s
88
+ time += intvar.to_s
84
89
  end
85
90
 
86
91
  time += ":"
@@ -95,12 +100,23 @@ def to_hrtime(var, isAM = false)
95
100
  var *= 60
96
101
  sec = var.to_i
97
102
  time += sec.to_s
103
+ # fill => zero padded
104
+ fill = lambda {|t| t.split(':').map{|i| ('0'*(2-i.length)) + i}.join(':') }
105
+ time = fill[time]
98
106
  time += " "
99
-
107
+
108
+ # meridian indicator
100
109
  time += zone
101
110
 
102
111
  end
103
112
 
113
+ # The same as to_hrtime() uses Time.
114
+ def as_rbtime(var)
115
+ # needs testing
116
+ (Time.local('00:00:00 AM') + var*60*60).strftime("%I:%M:%S %p")
117
+ end
118
+
119
+ # A class represents coordinates (latitude, longitude) and zone.
104
120
  class Coordinate
105
121
 
106
122
  attr :longitude, true
@@ -115,11 +131,33 @@ class Coordinate
115
131
 
116
132
  end
117
133
 
134
+ # Prayertime class, containing calculations, prayer times...etc.
118
135
  class Prayertime
119
136
 
137
+ # Initialize a new prayertime object.
138
+ #
139
+ # Usage example:
140
+ #
141
+ # #cairo > lat,lon => 31.3101, 29.7768
142
+ # pt=Prayertime.new(31.3101, 29.7768, 2, 2011, 2, 7, Calendar::EgyptianGeneralAuthorityOfSurvey, Mazhab::Default, Season::Winter)
143
+ # pt.report()
144
+ # puts pt.qibla.to_s + "° N"
145
+ # puts pt.qibla_distance.to_s + " km"
146
+ # produces:
147
+ # Fajr Time is 05:13:09 AM
148
+ # Shrouk Time is 06:41:17 AM
149
+ # Zuhr Time is 12:08:50 PM
150
+ # Asr Time is 03:13:56 PM
151
+ # Maghrib Time is 05:36:24 PM
152
+ # Ishaa Time is 06:55:13 PM
153
+ # 135.489731310865° N
154
+ # 1262.14097579674 km
155
+
120
156
  def initialize(longitude, latitude, zone,
121
157
  year, month, day,
122
- cal=Calendar::UmmAlQuraUniv, mazhab=Mazhab::Default, season=Season::Winter)
158
+ cal=Calendar::UmmAlQuraUniv,
159
+ mazhab=Mazhab::Default,
160
+ season=Season::Winter)
123
161
 
124
162
  @coordinate = Coordinate.new(longitude, latitude, zone)
125
163
  @date = Date.new(year, month, day)
@@ -133,41 +171,42 @@ class Prayertime
133
171
  @maghrib = nil
134
172
  @isha = nil
135
173
  @dec = 0
174
+
175
+ calculate()
136
176
  end
137
177
 
178
+ # Gets the time of shrouk.
138
179
  def shrouk_time
139
- # Gets the time of fajr.
140
180
  to_hrtime(@shrouk, true)
141
181
  end
142
-
182
+
183
+ # Gets the time of fajr.
143
184
  def fajr_time
144
- # Gets the time of fajr.
145
185
  to_hrtime(@fajr, true)
146
186
  end
147
187
 
188
+ # Gets the time of zuhr.
148
189
  def zuhr_time
149
- # Gets the time of zuhr.
150
190
  to_hrtime(@zuhr, true)
151
191
  end
152
192
 
193
+ # Gets the time of asr.
153
194
  def asr_time
154
- # Gets the time of asr.
155
195
  to_hrtime(@asr)
156
196
  end
157
197
 
198
+ # Gets the time of maghrib.
158
199
  def maghrib_time
159
- # Gets the time of maghrib.
160
200
  to_hrtime(@maghrib)
161
201
  end
162
202
 
203
+ # Gets the time of isha.
163
204
  def isha_time
164
- # Gets the time of isha.
165
205
  to_hrtime(@isha)
166
206
  end
167
-
207
+
208
+ # Calculations of prayertimes.
168
209
  def calculate
169
- # Calculations of prayertimes.
170
-
171
210
  year = @date.year
172
211
  month = @date.month
173
212
  day = @date.day
@@ -259,12 +298,14 @@ class Prayertime
259
298
  @maghrib = maghrib
260
299
  @isha = isha
261
300
  end
262
-
301
+
302
+ # The main equation used in calculations.
263
303
  def equation(alt)
264
304
  #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
305
  degrees( acos( (sin(radians(alt)) - sin(radians(@dec)) * sin(radians(@coordinate.latitude)))/(cos(radians(@dec))*cos(radians(@coordinate.latitude)))))
266
306
  end
267
-
307
+
308
+ # Return qibla angle from north (in degrees).
268
309
  def qibla
269
310
 
270
311
  k_lat = radians(21.423333);
@@ -281,6 +322,7 @@ class Prayertime
281
322
 
282
323
  end
283
324
 
325
+ # The distance to kaaba in kilometers.
284
326
  def qibla_distance
285
327
 
286
328
  k_lat = radians(21.423333);
@@ -295,8 +337,8 @@ class Prayertime
295
337
 
296
338
  end
297
339
 
340
+ # A simple report of all prayertimes.
298
341
  def report
299
- # Simple report of all prayertimes.
300
342
  puts "Fajr Time is #{fajr_time}"
301
343
  puts "Shrouk Time is #{shrouk_time}"
302
344
  puts "Zuhr Time is #{zuhr_time}"
@@ -304,11 +346,13 @@ class Prayertime
304
346
  puts "Maghrib Time is #{maghrib_time}"
305
347
  puts "Ishaa Time is #{isha_time}"
306
348
  end
349
+
307
350
  end
308
351
 
309
352
  if $0 == __FILE__
310
- pt=Prayertime.new(31.3101, 29.7768, 2, 2010, 12, 6, Calendar::EgyptianGeneralAuthorityOfSurvey, Mazhab::Default, Season::Winter)
311
- pt.calculate()
353
+ #cairo > lat,lon => 31.3101, 29.7768
354
+ pt=Prayertime.new(31.3101, 29.7768, 2, 2011, 2, 7, Calendar::EgyptianGeneralAuthorityOfSurvey, Mazhab::Default, Season::Winter)
355
+ #pt.calculate()
312
356
  pt.report()
313
357
  puts pt.qibla.to_s + "° N"
314
358
  puts pt.qibla_distance.to_s + " km"
metadata CHANGED
@@ -1,21 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbprayertime
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
- - Abdelrahmn Ghanem
13
+ - Abdelrahman Ghanem
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-06 00:00:00 +02:00
18
+ date: 2011-02-07 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21