-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathathan.cpp
More file actions
494 lines (422 loc) · 12.5 KB
/
Copy pathathan.cpp
File metadata and controls
494 lines (422 loc) · 12.5 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
/*
Developer : Ammar
Email : armmarov@gmail.com
Version : 1.0
*/
#include <ctime>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include "athan.h"
using namespace std;
Athan* Athan::m_pInstance = NULL;
Athan* Athan::GetInstance(void)
{
if(m_pInstance == NULL) {
m_pInstance = new Athan;
}
return m_pInstance;
}
Athan::Athan(void)
{
m_offset_time = 0;
m_latitude = 0;
m_longitude = 0;
}
Athan::~Athan(void)
{
}
/*
Function: To set offset for athan time
Argument: float
Return: -
*/
void Athan::Set_Offset(float offset)
{
m_offset_time = offset;
}
/*
Function: To get offset for athan time
Argument: -
Return: float
*/
float Athan::Get_Offset()
{
return m_offset_time;
}
/*
Function: To set latitude
Argument: double
Return: -
*/
void Athan::Set_Latitude(double latitude)
{
m_latitude = latitude;
}
/*
Function: To get latitude
Argument: -
Return: double
*/
double Athan::Get_Latitude()
{
return m_latitude;
}
/*
Function: To set longitude
Argument: double
Return: -
*/
void Athan::Set_Longitude(double longitude)
{
m_longitude = longitude;
}
/*
Function: To get longitude
Argument: -
Return: double
*/
double Athan::Get_Longitude()
{
return m_longitude;
}
/*
Function: To get athan time
Argument: EN_ATHAN_TIME
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Get_Athan_Time(EN_ATHAN_TIME athan_time)
{
switch(athan_time) {
case EN_ATHAN_TIME_SUBUH:
return Get_Athan_Subuh();
case EN_ATHAN_TIME_SUNRISE:
return Get_Sunrise();
case EN_ATHAN_TIME_ZUHUR:
return Get_Athan_Zuhur();
case EN_ATHAN_TIME_ASAR:
return Get_Athan_Asar();
case EN_ATHAN_TIME_MAGHRIB:
return Get_Athan_Maghrib();
case EN_ATHAN_TIME_ISYAK:
return Get_Athan_Isyak();
default:
break;
}
ST_ATHAN_TIME temp;
return temp;
}
/*
Function: To get current year
Argument: -
Return: int
*/
int Athan::Get_Current_Year()
{
time_t now = time(0);
tm *ltm = localtime(&now);
return 1900 + ltm->tm_year;
}
/*
Function: To get current day in year
Argument: -
Return: int
*/
int Athan::Get_Current_Day_In_Year()
{
time_t now = time(0);
int cur_month = 0;
int cur_day = 0;
int ttl_day = 0;
tm *ltm = localtime(&now);
cur_month = 1 + ltm->tm_mon;
cur_day = ltm->tm_mday;
switch(cur_month) {
case int(EN_MONTH_JAN):
ttl_day = cur_day;
break;
case int(EN_MONTH_FEB):
ttl_day = cur_day + TOTAL_DAYS_IN_JAN;
break;
case int(EN_MONTH_MAR):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB_LEAP;
else
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB;
break;
case int(EN_MONTH_APR):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB_LEAP + TOTAL_DAYS_IN_MAR;
else
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB + TOTAL_DAYS_IN_MAR;
break;
case int(EN_MONTH_MAY):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB_LEAP + TOTAL_DAYS_IN_MAR + TOTAL_DAYS_IN_APR;
else
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB + TOTAL_DAYS_IN_MAR + TOTAL_DAYS_IN_APR;
break;
case int(EN_MONTH_JUN):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB_LEAP + TOTAL_DAYS_IN_MAR + TOTAL_DAYS_IN_APR + TOTAL_DAYS_IN_MAY;
else
ttl_day = cur_day + TOTAL_DAYS_IN_JAN + TOTAL_DAYS_IN_FEB + TOTAL_DAYS_IN_MAR + TOTAL_DAYS_IN_APR + TOTAL_DAYS_IN_MAY;
break;
case int(EN_MONTH_JUL):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS_LEAP;
else
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS;
break;
case int(EN_MONTH_AUG):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS_LEAP + TOTAL_DAYS_IN_JUL;
else
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS + TOTAL_DAYS_IN_JUL;
break;
case int(EN_MONTH_SEP):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS_LEAP + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG;
else
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG;
break;
case int(EN_MONTH_OCT):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS_LEAP + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG + TOTAL_DAYS_IN_SEP;
else
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG + TOTAL_DAYS_IN_SEP;
break;
case int(EN_MONTH_NOV):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS_LEAP + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG + TOTAL_DAYS_IN_SEP + TOTAL_DAYS_IN_OCT;
else
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG + TOTAL_DAYS_IN_OCT;
break;
case int(EN_MONTH_DEC):
if(Get_Current_Year()%4==0) // Case for leap year
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS_LEAP + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG + TOTAL_DAYS_IN_SEP + TOTAL_DAYS_IN_OCT + TOTAL_DAYS_IN_NOV;
else
ttl_day = cur_day + TOTAL_DAYS_IN_1ST_HALF_YEARS + TOTAL_DAYS_IN_JUL + TOTAL_DAYS_IN_AUG + TOTAL_DAYS_IN_SEP + TOTAL_DAYS_IN_OCT + TOTAL_DAYS_IN_NOV;
break;
default:
break;
}
return ttl_day;
}
/*
Function: To calculate time different for case Subuh, Sunrise, Maghrib and Isyak
Argument: float
Return: double
*/
double Athan::Calculate_Time_Diff(float alpha)
{
double d1 = -sin(alpha * DEG_TO_RAD);
double d2 = sin(m_latitude * DEG_TO_RAD)*sin(Calculate_Declination_of_Sun() * DEG_TO_RAD);
double d3 = cos(m_latitude * DEG_TO_RAD)*cos(Calculate_Declination_of_Sun() * DEG_TO_RAD);
return acos((d1-d2)/d3) * RAD_TO_DEG / 15;
}
/*
Function: To calculate time different for case Asar
Argument: float
Return: double
*/
double Athan::Calculate_Time_Diff_Asr(float time)
{
double d0 = tan(m_latitude * DEG_TO_RAD - Calculate_Declination_of_Sun() * DEG_TO_RAD);
double d1 = arccot(time + d0);
double d2 = sin(m_latitude * DEG_TO_RAD)*sin(Calculate_Declination_of_Sun() * DEG_TO_RAD);
double d3 = cos(m_latitude * DEG_TO_RAD)*cos(Calculate_Declination_of_Sun() * DEG_TO_RAD);
return acos((sin(d1)-d2)/d3) * RAD_TO_DEG / 15;
}
double Athan::arccot(double x)
{
return atan(1/x);
}
/*
Function: To calculate time update
Argument: ST_ATHAN_TIME, ST_ATHAN_TIME, bool
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Calculate_Time_Update(ST_ATHAN_TIME t1, ST_ATHAN_TIME t2, bool isUpward) {
ST_ATHAN_TIME ret;
if(isUpward) {
ret.hour = t1.hour + t2.hour;
ret.min = t1.min + t2.min;
} else {
if(t1.min < t2.min) {
ret.hour = t1.hour - t2.hour - 1;
ret.min = (60 + t1.min - t2.min);
} else {
ret.hour = t1.hour - t2.hour;
ret.min = (t1.min - t2.min);
}
}
if(ret.min >= 60) {
ret.min = (ret.min - 60);
ret.hour = ret.hour + 1;
}
return ret;
}
/*
Function: To calculate Subuh time
Argument: -
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Get_Athan_Subuh()
{
double hour = 0;
double min = 0;
double time = Calculate_Time_Diff(SUN_ANGLE_SUBUH);
min = modf (time , &hour);
ST_ATHAN_TIME athan_time;
athan_time.hour = int(hour);
athan_time.min = int(min*60);
athan_time.sec = 0;
return Calculate_Time_Update(Get_Athan_Zuhur(), athan_time, false);
}
/*
Function: To calculate Sunrise
Argument: -
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Get_Sunrise()
{
double hour = 0;
double min = 0;
double time = Calculate_Time_Diff(SUN_ANGLE_SUNRISE);
min = modf (time , &hour);
ST_ATHAN_TIME athan_time;
athan_time.hour = int(hour);
athan_time.min = int(min*60);
athan_time.sec = 0;
return Calculate_Time_Update(Get_Athan_Zuhur(), athan_time, false);
}
/*
Function: To calculate Zuhur
Argument: -
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Get_Athan_Zuhur()
{
double hour = 0;
double min = 0;
double time = 12 + TIMEZONE_MALAYSIA - m_longitude/15;
min = modf (time , &hour);
ST_ATHAN_TIME athan_time;
athan_time.hour = int(hour);
athan_time.min = int(min*60) - Calculate_Equation_of_Time() + m_offset_time;
athan_time.sec = 0;
return athan_time;
}
/*
Function: To calculate Asar
Argument: -
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Get_Athan_Asar()
{
double hour = 0;
double min = 0;
double time = Calculate_Time_Diff_Asr(T_ASAR);
min = modf (time , &hour);
ST_ATHAN_TIME athan_time;
athan_time.hour = int(hour);
athan_time.min = int(min*60);
athan_time.sec = 0;
return Calculate_Time_Update(Get_Athan_Zuhur(), athan_time, true);
}
/*
Function: To calculate Maghrib
Argument: -
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Get_Athan_Maghrib()
{
double hour = 0;
double min = 0;
double time = Calculate_Time_Diff(SUN_ANGLE_MAGHRIB);
min = modf (time , &hour);
ST_ATHAN_TIME athan_time;
athan_time.hour = int(hour);
athan_time.min = int(min*60);
athan_time.sec = 0;
return Calculate_Time_Update(Get_Athan_Zuhur(), athan_time, true);
}
/*
Function: To calculate Isyak
Argument: -
Return: ST_ATHAN_TIME
*/
ST_ATHAN_TIME Athan::Get_Athan_Isyak()
{
double hour = 0;
double min = 0;
double time = Calculate_Time_Diff(SUN_ANGLE_ISYAK);
min = modf (time , &hour);
ST_ATHAN_TIME athan_time;
athan_time.hour = int(hour);
athan_time.min = int(min*60);
athan_time.sec = 0;
return Calculate_Time_Update(Get_Athan_Zuhur(), athan_time, true);
}
/*
Function: To calculate declination of sun
Argument: -
Return: double
*/
double Athan::Calculate_Declination_of_Sun()
{
// Link : https://sciencing.com/calculate-suns-declination-6904335.html
if(Get_Current_Year()%4==0)
return EARTH_TILT * cos((DAYS/TOTAL_DAYS_IN_LEAP_YEARS) * (Get_Current_Day_In_Year()+10) * DEG_TO_RAD);
else
return EARTH_TILT * cos((DAYS/TOTAL_DAYS_IN_YEARS) * (Get_Current_Day_In_Year()+10) * DEG_TO_RAD);
}
/*
Function: To calculate equation of time
Argument: -
Return: double
*/
double Athan::Calculate_Equation_of_Time()
{
// Link : https://www.intmath.com/blog/mathematics/the-equation-of-time-5039
double d1 = -7.655 * sin(Get_Current_Day_In_Year() * DEG_TO_RAD);
double d2 = 9.873 * sin(2 * (Get_Current_Day_In_Year() * DEG_TO_RAD) + 3.588);
return d1 + d2;
}
#ifdef UNIT_TEST
int main() {
float offset = 0;
double latitude = 1.65;
double longitude = 103.2;
ST_ATHAN_TIME temp_subuh;
ST_ATHAN_TIME temp_sunrise;
ST_ATHAN_TIME temp_zuhur;
ST_ATHAN_TIME temp_asar;
ST_ATHAN_TIME temp_maghrib;
ST_ATHAN_TIME temp_isyak;
Athan::GetInstance()->Set_Offset(offset);
cout << "Offset is : " << Athan::GetInstance()->Get_Offset() << endl;
Athan::GetInstance()->Set_Latitude(latitude);
cout << "Latitude is : " << Athan::GetInstance()->Get_Latitude() << endl;
Athan::GetInstance()->Set_Longitude(longitude);
cout << "Longitude is : " << Athan::GetInstance()->Get_Longitude() << endl;
temp_subuh = Athan::GetInstance()->Get_Athan_Time(EN_ATHAN_TIME_SUBUH);
temp_sunrise = Athan::GetInstance()->Get_Athan_Time(EN_ATHAN_TIME_SUNRISE);
temp_zuhur = Athan::GetInstance()->Get_Athan_Time(EN_ATHAN_TIME_ZUHUR);
temp_asar = Athan::GetInstance()->Get_Athan_Time(EN_ATHAN_TIME_ASAR);
temp_maghrib = Athan::GetInstance()->Get_Athan_Time(EN_ATHAN_TIME_MAGHRIB);
temp_isyak = Athan::GetInstance()->Get_Athan_Time(EN_ATHAN_TIME_ISYAK);
cout << endl;
cout << "########################" << endl;
cout << "Subuh is at " << temp_subuh.hour << ":" << setfill('0') << setw(2) << temp_subuh.min << endl;
cout << "Sunrise is at " << temp_sunrise.hour << ":" << setfill('0') << setw(2) << temp_sunrise.min << endl;
cout << "Zuhur is at " << temp_zuhur.hour << ":" << setfill('0') << setw(2) << temp_zuhur.min << endl;
cout << "Asar is at " << temp_asar.hour << ":" << setfill('0') << setw(2) << temp_asar.min << endl;
cout << "Maghrib is at " << temp_maghrib.hour << ":" << setfill('0') << setw(2) << temp_maghrib.min << endl;
cout << "Isyak is at " << temp_isyak.hour << ":" << setfill('0') << setw(2) << temp_isyak.min << endl;
cout << "########################" << endl;
return 0;
}
#endif