-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrayerTimes.h
More file actions
652 lines (521 loc) · 18.4 KB
/
PrayerTimes.h
File metadata and controls
652 lines (521 loc) · 18.4 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#include <String.h>
/*-------------------------- In the name of God ----------------------------*\
/* Arduino class library to calculate Islamic prayer times */
/* This is ported by Shajeeb T Muhammad from the library published at PrayTimes.org */
/* Refer https://github.com/shajeebtm for usage details with example.
/*
-----------------------Original Copyright Block --------------------------------
Copyright (C) 2007-2010 PrayTimes.org
Developed By: Mohammad Ebrahim Mohammadi Panah <ebrahim at mohammadi dot ir>
Based on a JavaScript Code By: Hamid Zarrabi-Zadeh
License: GNU LGPL v3.0
TERMS OF USE:
Permission is granted to use this code, with or
without modification, in any website or application
provided that credit is given to the original work
with a link back to PrayTimes.org.
This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY.
PLEASE DO NOT REMOVE THIS COPYRIGHT BLOCK.
------------------------------------------------------------------------------
*/
class PrayerTimes
{
public:
enum
{
VERSION_MAJOR = 1,
VERSION_MINOR = 0,
};
/* --------------------- User Interface ----------------------- */
// Calculation Methods
enum CalculationMethod
{
Jafari, // Ithna Ashari
Karachi, // University of Islamic Sciences, Karachi
ISNA, // Islamic Society of North America (ISNA)
MWL, // Muslim World League (MWL)
Makkah, // Umm al-Qura, Makkah
Egypt, // Egyptian General Authority of Survey
Custom, // Custom Setting
CalculationMethodsCount
};
// Juristic Methods
enum JuristicMethod
{
Shafii, // Shafii (standard)
Hanafi, // Hanafi
};
// Adjusting Methods for Higher Latitudes
enum AdjustingMethod
{
None, // No adjustment
MidNight, // middle of night
OneSeventh, // 1/7th of night
AngleBased, // angle/60th of night
};
// Time IDs
enum TimeID
{
Fajr,
Sunrise,
Dhuhr,
Asr,
Sunset,
Maghrib,
Isha,
TimesCount
};
/* -------------------- Interface Functions -------------------- */
PrayerTimes(CalculationMethod calc_method = Jafari,
JuristicMethod asr_juristic = Shafii,
AdjustingMethod adjust_high_lats = None,
double dhuhr_minutes = 0)
: calc_method(calc_method)
, asr_juristic(asr_juristic)
, adjust_high_lats(adjust_high_lats)
, dhuhr_minutes(dhuhr_minutes)
{
method_params[Jafari] = MethodConfig(16.0, false, 4.0, false, 14.0); // Jafari
method_params[Karachi] = MethodConfig(18.0, true, 0.0, false, 18.0); // Karachi
method_params[ISNA] = MethodConfig(15.0, true, 0.0, false, 15.0); // ISNA
method_params[MWL] = MethodConfig(18.0, true, 0.0, false, 17.0); // MWL
method_params[Makkah] = MethodConfig(19.0, true, 0.0, true, 90.0); // Makkah
method_params[Egypt] = MethodConfig(19.5, true, 0.0, false, 17.5); // Egypt
method_params[Custom] = MethodConfig(18.0, true, 0.0, false, 17.0); // Custom
}
/* return prayer times for a given date */
void get_prayer_times(int year, int month, int day, double _latitude, double _longitude, double _timezone, bool _dst_on, String _calc_method, String _asr_method, String _my_high_lats_method, double times[])
{
latitude = _latitude;
longitude = _longitude;
timezone = _timezone;
if (_dst_on) {
timezone+=1;
}
// Fix calculation method
if (_calc_method.equalsIgnoreCase("ISNA")) {
set_calc_method(ISNA);
} else if (_calc_method.equalsIgnoreCase("Karachi")) {
set_calc_method(Karachi);
} else if (_calc_method.equalsIgnoreCase("MWL")) {
set_calc_method(MWL);
} else if (_calc_method.equalsIgnoreCase("Jafari")) {
set_calc_method(Jafari);
} else if (_calc_method.equalsIgnoreCase("Makkah")) {
set_calc_method(Makkah);
} else if (_calc_method.equalsIgnoreCase("Egypt")) {
set_calc_method(Egypt);
} else if (_calc_method.equalsIgnoreCase("Custom")) {
set_calc_method(Custom);
}
// Fix Asr calculation method
if (_asr_method.equalsIgnoreCase("Shafii")) {
set_asr_method(Shafii);
} else if (_asr_method.equalsIgnoreCase("Hanafi")) {
set_asr_method(Hanafi);
}
// Fix high latitude adjustment method
if (_my_high_lats_method.equalsIgnoreCase("None")) {
set_high_lats_adjust_method(None);
} else if (_my_high_lats_method.equalsIgnoreCase("MidNight")) {
set_high_lats_adjust_method(MidNight);
} else if (_my_high_lats_method.equalsIgnoreCase("OneSeventh")) {
set_high_lats_adjust_method(OneSeventh);
} else if (_my_high_lats_method.equalsIgnoreCase("AngleBased")) {
set_high_lats_adjust_method(AngleBased);
}
julian_date = get_julian_date(year, month, day) - longitude / (double) (15 * 24);
compute_day_times(times);
}
/* set the calculation method */
void set_calc_method(CalculationMethod method_id)
{
calc_method = method_id;
}
/* set the juristic method for Asr */
void set_asr_method(JuristicMethod method_id)
{
asr_juristic = method_id;
}
/* set adjusting method for higher latitudes */
void set_high_lats_adjust_method(AdjustingMethod method_id)
{
adjust_high_lats = method_id;
}
/* set the angle for calculating Fajr */
void set_fajr_angle(double angle)
{
method_params[Custom].fajr_angle = angle;
calc_method = Custom;
}
/* set the angle for calculating Maghrib */
void set_maghrib_angle(double angle)
{
method_params[Custom].maghrib_is_minutes = false;
method_params[Custom].maghrib_value = angle;
calc_method = Custom;
}
/* set the angle for calculating Isha */
void set_isha_angle(double angle)
{
method_params[Custom].isha_is_minutes = false;
method_params[Custom].isha_value = angle;
calc_method = Custom;
}
/* set the minutes after mid-day for calculating Dhuhr */
void set_dhuhr_minutes(double minutes)
{
dhuhr_minutes = minutes;
}
/* set the minutes after Sunset for calculating Maghrib */
void set_maghrib_minutes(double minutes)
{
method_params[Custom].maghrib_is_minutes = true;
method_params[Custom].maghrib_value = minutes;
calc_method = Custom;
}
/* set the minutes after Maghrib for calculating Isha */
void set_isha_minutes(double minutes)
{
method_params[Custom].isha_is_minutes = true;
method_params[Custom].isha_value = minutes;
calc_method = Custom;
}
/* to get the name of the prayer */
String get_prayer_name(int prayer)
{
return PrayerNames[prayer];
}
/* get hours and minutes parts of a float time */
static void get_float_time_parts(double time, int& hours, int& minutes)
{
time = fix_hour(time + 0.5 / 60); // add 0.5 minutes to round
hours = floor(time);
minutes = floor((time - hours) * 60);
}
/* convert float hours to 24h format */
static String float_time_to_time24(double time)
{
// if (isnan(time))
// return String;
int hours, minutes;
get_float_time_parts(time, hours, minutes);
return two_digits_format(hours) + ':' + two_digits_format(minutes);
}
/* convert float hours to 12h format */
/* with AM or PM suffix */
static String float_time_to_time12(double time, bool no_suffix = false)
{
//if (isnan(time))
//return std::string();
int hours, minutes;
get_float_time_parts(time, hours, minutes);
const char* suffix = hours >= 12 ? " PM" : " AM";
hours = (hours + 12 - 1) % 12 + 1;
return int_to_string(hours) + ':' + two_digits_format(minutes) + (no_suffix ? "" : suffix);
}
/* convert float hours to 12h format with no suffix */
/* without AM or PM suffix */
static String float_time_to_time12ns(double time)
{
return float_time_to_time12(time, true);
}
/* convert hh:mm 12hours format to hh.mm 24hr format, useful for time comparsion */
/* Added by by Shajeeb */
static float time12_to_float_time(int h, int m , bool pm)
{
float d;
if (pm) {
h += 12;
if (h == 24) { // afternoon 12'o clock means 12 hours
h -= 12;
}
} else {
if (h == 12 ) { // night 12'o clock means 00 hours
h = 0;
}
}
d = (float)m / 60;
d = d + (float)h;
return d;
}
private:
// Prayer Names
String PrayerNames[TimesCount] = {"Fajr", "Sunrise", "Dhuhr", "Asr", "Sunset", "Maghrib", "Isha"};
/* ------------------- Calc Method Parameters -------------------- */
struct MethodConfig
{
MethodConfig()
{
}
MethodConfig(double fajr_angle,
bool maghrib_is_minutes,
double maghrib_value,
bool isha_is_minutes,
double isha_value)
: fajr_angle(fajr_angle)
, maghrib_is_minutes(maghrib_is_minutes)
, maghrib_value(maghrib_value)
, isha_is_minutes(isha_is_minutes)
, isha_value(isha_value)
{
}
double fajr_angle;
bool maghrib_is_minutes;
double maghrib_value; // angle or minutes
bool isha_is_minutes;
double isha_value; // angle or minutes
};
/* ---------------------- Calculation Functions ----------------------- */
/* References: */
/* http://www.ummah.net/astronomy/saltime */
/* http://aa.usno.navy.mil/faq/docs/SunApprox.html */
// typedef std::pair<double, double> DoublePair;
/* compute declination angle of sun and equation of time */
void sun_position(double jd, double &a , double &b)
{
double d = jd - 2451545.0;
double g = fix_angle(357.529 + 0.98560028 * d);
double q = fix_angle(280.459 + 0.98564736 * d);
double l = fix_angle(q + 1.915 * dsin(g) + 0.020 * dsin(2 * g));
// double r = 1.00014 - 0.01671 * dcos(g) - 0.00014 * dcos(2 * g);
double e = 23.439 - 0.00000036 * d;
double dd = darcsin(dsin(e) * dsin(l));
double ra = darctan2(dcos(e) * dsin(l), dcos(l)) / 15.0;
ra = fix_hour(ra);
double eq_t = q / 15.0 - ra;
a = dd ;
b = eq_t;
// return DoublePair(dd, eq_t);
}
/* compute equation of time */
double equation_of_time(double jd)
{
double a , b;
sun_position (jd, a , b);
return b;
}
/* compute declination angle of sun */
double sun_declination(double jd)
{
double a , b;
sun_position (jd, a , b);
return a;
}
/* compute mid-day (Dhuhr, Zawal) time */
double compute_mid_day(double _t)
{
double t = equation_of_time(julian_date + _t);
double z = fix_hour(12 - t);
return z;
}
/* compute time for a given angle G */
double compute_time(double g, double t)
{
double d = sun_declination(julian_date + t);
double z = compute_mid_day(t);
double v = 1.0 / 15.0 * darccos((-dsin(g) - dsin(d) * dsin(latitude)) / (dcos(d) * dcos(latitude)));
return z + (g > 90.0 ? - v : v);
}
/* compute the time of Asr */
double compute_asr(int step, double t) // Shafii: step=1, Hanafi: step=2
{
double d = sun_declination(julian_date + t);
double g = -darccot(step + dtan(fabs(latitude - d)));
return compute_time(g, t);
}
/* ---------------------- Compute Prayer Times ----------------------- */
// array parameters must be at least of size TimesCount
/* compute prayer times at given julian date */
void compute_times(double times[])
{
day_portion(times);
times[Fajr] = compute_time(180.0 - method_params[calc_method].fajr_angle, times[Fajr]);
times[Sunrise] = compute_time(180.0 - 0.833, times[Sunrise]);
times[Dhuhr] = compute_mid_day(times[Dhuhr]);
times[Asr] = compute_asr(1 + asr_juristic, times[Asr]);
times[Sunset] = compute_time(0.833, times[Sunset]);
times[Maghrib] = compute_time(method_params[calc_method].maghrib_value, times[Maghrib]);
times[Isha] = compute_time(method_params[calc_method].isha_value, times[Isha]);
}
/* compute prayer times at given julian date */
void compute_day_times(double times[])
{
double default_times[] = { 5, 6, 12, 13, 18, 18, 18 }; // default times
for (int i = 0; i < TimesCount; ++i)
times[i] = default_times[i];
for (int i = 0; i < NUM_ITERATIONS; ++i)
compute_times(times);
adjust_times(times);
}
/* adjust times in a prayer time array */
void adjust_times(double times[])
{
for (int i = 0; i < TimesCount; ++i)
times[i] += timezone - longitude / 15.0;
times[Dhuhr] += dhuhr_minutes / 60.0; // Dhuhr
if (method_params[calc_method].maghrib_is_minutes) // Maghrib
times[Maghrib] = times[Sunset] + method_params[calc_method].maghrib_value / 60.0;
if (method_params[calc_method].isha_is_minutes) // Isha
times[Isha] = times[Maghrib] + method_params[calc_method].isha_value / 60.0;
if (adjust_high_lats != None)
adjust_high_lat_times(times);
}
/* adjust Fajr, Isha and Maghrib for locations in higher latitudes */
void adjust_high_lat_times(double times[])
{
double night_time = time_diff(times[Sunset], times[Sunrise]); // sunset to sunrise
// Adjust Fajr
double fajr_diff = night_portion(method_params[calc_method].fajr_angle) * night_time;
if (isnan(times[Fajr]) || time_diff(times[Fajr], times[Sunrise]) > fajr_diff)
times[Fajr] = times[Sunrise] - fajr_diff;
// Adjust Isha
double isha_angle = method_params[calc_method].isha_is_minutes ? 18.0 : method_params[calc_method].isha_value;
double isha_diff = night_portion(isha_angle) * night_time;
if (isnan(times[Isha]) || time_diff(times[Sunset], times[Isha]) > isha_diff)
times[Isha] = times[Sunset] + isha_diff;
// Adjust Maghrib
double maghrib_angle = method_params[calc_method].maghrib_is_minutes ? 4.0 : method_params[calc_method].maghrib_value;
double maghrib_diff = night_portion(maghrib_angle) * night_time;
if (isnan(times[Maghrib]) || time_diff(times[Sunset], times[Maghrib]) > maghrib_diff)
times[Maghrib] = times[Sunset] + maghrib_diff;
}
/* the night portion used for adjusting times in higher latitudes */
double night_portion(double angle)
{
switch (adjust_high_lats)
{
case AngleBased:
return angle / 60.0;
case MidNight:
return 1.0 / 2.0;
case OneSeventh:
return 1.0 / 7.0;
default:
// Just to return something!
// In original library nothing was returned
// Maybe I should throw an exception
// It must be impossible to reach here
return 0;
}
}
/* convert hours to day portions */
void day_portion(double times[])
{
for (int i = 0; i < TimesCount; ++i)
times[i] /= 24.0;
}
/* ---------------------- Misc Functions ----------------------- */
/* compute the difference between two times */
static double time_diff(double time1, double time2)
{
return fix_hour(time2 - time1);
}
static String int_to_string(int num)
{
char tmp[16];
tmp[0] = '\0';
sprintf(tmp, "%d", num);
return String(tmp);
}
// /* add a leading 0 if necessary */
static String two_digits_format(int num)
{
char tmp[16];
tmp[0] = '\0';
sprintf(tmp, "%2.2d", num);
return String(tmp);
}
/* ---------------------- Julian Date Functions ----------------------- */
/* calculate julian date from a calendar date */
double get_julian_date(int year, int month, int day)
{
if (month <= 2)
{
year -= 1;
month += 12;
}
double a = floor(year / 100.0);
double b = 2 - a + floor(a / 4.0);
return floor(365.25 * (year + 4716)) + floor(30.6001 * (month + 1)) + day + b - 1524.5;
}
/* ---------------------- Trigonometric Functions ----------------------- */
/* degree sin */
static double dsin(double d)
{
return sin(deg2rad(d));
}
/* degree cos */
static double dcos(double d)
{
return cos(deg2rad(d));
}
/* degree tan */
static double dtan(double d)
{
return tan(deg2rad(d));
}
/* degree arcsin */
static double darcsin(double x)
{
return rad2deg(asin(x));
}
/* degree arccos */
static double darccos(double x)
{
return rad2deg(acos(x));
}
/* degree arctan */
static double darctan(double x)
{
return rad2deg(atan(x));
}
/* degree arctan2 */
static double darctan2(double y, double x)
{
return rad2deg(atan2(y, x));
}
/* degree arccot */
static double darccot(double x)
{
return rad2deg(atan(1.0 / x));
}
/* degree to radian */
static double deg2rad(double d)
{
return d * M_PI / 180.0;
}
/* radian to degree */
static double rad2deg(double r)
{
return r * 180.0 / M_PI;
}
/* range reduce angle in degrees. */
static double fix_angle(double a)
{
a = a - 360.0 * floor(a / 360.0);
a = a < 0.0 ? a + 360.0 : a;
return a;
}
/* range reduce hours to 0..23 */
static double fix_hour(double a)
{
a = a - 24.0 * floor(a / 24.0);
a = a < 0.0 ? a + 24.0 : a;
return a;
}
private:
/* ---------------------- Private Variables -------------------- */
MethodConfig method_params[CalculationMethodsCount];
CalculationMethod calc_method; // caculation method
JuristicMethod asr_juristic; // Juristic method for Asr
AdjustingMethod adjust_high_lats; // adjusting method for higher latitudes
double dhuhr_minutes; // minutes after mid-day for Dhuhr
double latitude;
double longitude;
double timezone;
double julian_date;
/* --------------------- Technical Settings -------------------- */
static const int NUM_ITERATIONS = 1; // number of iterations needed to compute times
};