fork(1) download
  1. #include <iostream>
  2. #include <math.h>
  3. /*
  4.  Prayers calculator start
  5. */
  6.  
  7. //convert Degree to Radian
  8. double degToRad(double degree)
  9. {
  10. return ((3.1415926 / 180) * degree);
  11. }
  12.  
  13. //convert Radian to Degree
  14. double radToDeg(double radian)
  15. {
  16. return (radian * (180/3.1415926));
  17. }
  18.  
  19. //make sure a value is between 0 and 360
  20. double moreLess360(double value)
  21. {
  22. while(value > 360 || value < 0)
  23. {
  24. if(value > 360)
  25. value -= 360;
  26. else if (value <0)
  27. value += 360;
  28. }
  29.  
  30. return value;
  31. }
  32.  
  33. //make sure a value is between 0 and 24
  34. double moreLess24(double value)
  35. {
  36. while(value > 24 || value < 0)
  37. {
  38. if(value > 24)
  39. value -= 24;
  40. else if (value <0)
  41. value += 24;
  42. }
  43.  
  44. return value;
  45. }
  46.  
  47. //convert the double number to Hours and Minutes
  48. void doubleToHrMin(double number, int &hours, int &minutes)
  49. {
  50. hours = floor(moreLess24(number));
  51. minutes = floor(moreLess24(number - hours) * 60);
  52. }
  53.  
  54. void calcPrayerTimes(int year, int month, int day,
  55. double longitude, double latitude, int timeZone,
  56. double fajrTwilight, double ishaTwilight,
  57. double &fajrTime, double &sunRiseTime, double &zuhrTime,
  58. double &asrTime, double &maghribTime, double &ishaTime)
  59. {
  60. double W1 = (367 * year);
  61. std::cout << "Julian Day (W1): " << W1 << std::endl;
  62. double W2 = ((year + (int)((month + 9) / 12)) * 7 / 4);
  63. std::cout << "Julian Day (W2): " << W2 << std::endl;
  64. double W3 = (367 * year) - ((year + (int)((month + 9) / 12)) * 7 / 4) + (((int)(275 * month / 9)) + day - 730531.5);
  65. std::cout << "Julian Day (W3): " << W3 << std::endl;
  66.  
  67.  
  68.  
  69.  
  70.  
  71. double D = (367 * year);
  72.  
  73. double L = 280.461 + 0.9856474 * D;
  74. L = moreLess360(L);
  75.  
  76. // Debugging L (Longitude)
  77. std::cout << "L: " << L << std::endl;
  78.  
  79. double M = 357.528 + (0.9856003) * D;
  80. M = moreLess360(M);
  81.  
  82. // Debugging M (Mean anomaly)
  83. std::cout << "M: " << M << std::endl;
  84.  
  85. double Lambda = L + 1.915 * sin(degToRad(M)) + 0.02 * sin(degToRad(2 * M));
  86. Lambda = moreLess360(Lambda);
  87.  
  88. // Debugging Lambda (Ecliptic longitude)
  89. std::cout << "Lambda: " << Lambda << std::endl;
  90.  
  91. double Obliquity = 23.439 - 0.0000004 * D;
  92. double Alpha = radToDeg(atan((cos(degToRad(Obliquity)) * tan(degToRad(Lambda)))));
  93. Alpha = moreLess360(Alpha);
  94.  
  95. // Debugging Alpha (Right Ascension)
  96. std::cout << "Alpha: " << Alpha << std::endl;
  97.  
  98. Alpha = Alpha - (360 * (int)(Alpha / 360));
  99. Alpha = Alpha + 90 * (floor(Lambda / 90) - floor(Alpha / 90));
  100.  
  101. double ST = 100.46 + 0.985647352 * D;
  102. double Dec = radToDeg(asin(sin(degToRad(Obliquity)) * sin(degToRad(Lambda))));
  103.  
  104. // Debugging Declination
  105. std::cout << "Declination (Dec): " << Dec << std::endl;
  106.  
  107. double Durinal_Arc = radToDeg(acos((sin(degToRad(-0.8333)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  108.  
  109. // Debugging Durinal Arc
  110. std::cout << "Durinal Arc: " << Durinal_Arc << std::endl;
  111.  
  112. double Noon = Alpha - ST;
  113. Noon = moreLess360(Noon);
  114.  
  115. // Debugging Noon (Solar Noon)
  116. std::cout << "Noon: " << Noon << std::endl;
  117.  
  118. double UT_Noon = Noon - longitude;
  119.  
  120. // Debugging UT Noon
  121. std::cout << "UT Noon: " << UT_Noon << std::endl;
  122.  
  123. ////////////////////////////////////////////
  124. // Calculating Prayer Times Arcs & Times //
  125. //////////////////////////////////////////
  126.  
  127. // 2) Zuhr Time [Local noon]
  128. zuhrTime = UT_Noon / 15 + timeZone;
  129.  
  130. // Debugging Zuhr Time
  131. std::cout << "Zuhr Time: " << zuhrTime << std::endl;
  132.  
  133. // Asr Hanafi
  134. double Asr_Alt = radToDeg(atan(1 + tan(degToRad(abs(latitude - Dec)))));
  135. double Asr_Arc = radToDeg(acos((sin(degToRad(90 - Asr_Alt)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  136. Asr_Arc = Asr_Arc / 15;
  137.  
  138. // Debugging Asr Time
  139. std::cout << "Asr Arc: " << Asr_Arc << std::endl;
  140.  
  141. // 3) Asr Time
  142. asrTime = zuhrTime + Asr_Arc;
  143.  
  144. // Debugging Asr Time
  145. std::cout << "Asr Time: " << asrTime << std::endl;
  146.  
  147. // 1) Shorouq Time
  148. sunRiseTime = zuhrTime - (Durinal_Arc / 15);
  149.  
  150. // Debugging Sunrise Time
  151. std::cout << "Sunrise Time: " << sunRiseTime << std::endl;
  152.  
  153. // 4) Maghrib Time
  154. maghribTime = zuhrTime + (Durinal_Arc / 15);
  155.  
  156. // Debugging Maghrib Time
  157. std::cout << "Maghrib Time: " << maghribTime << std::endl;
  158.  
  159. double Esha_Arc = radToDeg(acos((sin(degToRad(ishaTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  160. // 5) Isha Time
  161. ishaTime = zuhrTime + (Esha_Arc / 15);
  162.  
  163. // Debugging Isha Time
  164. std::cout << "Isha Time: " << ishaTime << std::endl;
  165.  
  166. // 0) Fajr Time
  167. double Fajr_Arc = radToDeg(acos((sin(degToRad(fajrTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  168. fajrTime = zuhrTime - (Fajr_Arc / 15);
  169.  
  170. // Debugging Fajr_Arc and Fajr Time
  171. std::cout << "Fajr Arc: " << Fajr_Arc << std::endl;
  172. std::cout << "Fajr Time: " << fajrTime << std::endl;
  173.  
  174. return;
  175. }
  176.  
  177. /*
  178.  Personal code. Calculating for Cairo.
  179.  
  180. Date: 18-1-2012
  181. Longitude: 30.2
  182. Latitude: 30
  183. Time Zone: +2
  184. Fajr Twilight: -19.5
  185. Esha Twilight: -17.5
  186. */
  187.  
  188. int main() {
  189. double fajr, sunRise, zuhr, asr, maghrib, isha;
  190. calcPrayerTimes(2025,3,9, 3.34, 36.6, 1, -18, -17,
  191. fajr, sunRise, zuhr, asr, maghrib, isha);
  192.  
  193. int hours, minutes;
  194.  
  195. doubleToHrMin(fajr, hours, minutes);
  196. std::cout << "Fajr: " << hours << ":" << minutes << std::endl;
  197.  
  198. doubleToHrMin(sunRise, hours, minutes);
  199. std::cout << "Sunrise: " << hours << ":" << minutes << std::endl;
  200.  
  201. doubleToHrMin(zuhr, hours, minutes);
  202. std::cout << "Zuhr: " << hours << ":" << minutes << std::endl;
  203.  
  204. doubleToHrMin(asr, hours, minutes);
  205. std::cout << "Asr: " << hours << ":" << minutes << std::endl;
  206.  
  207. doubleToHrMin(maghrib, hours, minutes);
  208. std::cout << "Maghrib: " << hours << ":" << minutes << std::endl;
  209.  
  210. doubleToHrMin(isha, hours, minutes);
  211. std::cout << "Isha: " << hours << ":" << minutes << std::endl;
  212. }
  213.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Julian Day (W1): 743175
Julian Day (W2): 3545
Julian Day (W3): 9198.5
L: 188.967
M: 231.031
Lambda: 187.498
Alpha: 6.9009
Declination (Dec): -2.93972
Durinal Arc: 88.8541
Noon: 177.97
UT Noon: 174.63
Zuhr Time: 12.642
Asr Arc: 3.36032
Asr Time: 16.0023
Sunrise Time: 6.7184
Maghrib Time: 18.5656
Isha Time: 19.9125
Fajr Arc: 110.321
Fajr Time: 5.28725
Fajr: 5:17
Sunrise: 6:43
Zuhr: 12:38
Asr: 16:0
Maghrib: 18:33
Isha: 19:54