fork 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.  
  27. else if (value <0)
  28. value += 360;
  29. }
  30.  
  31. return value;
  32. }
  33.  
  34. //make sure a value is between 0 and 24
  35. double moreLess24(double value)
  36. {
  37. while(value > 24 || value < 0)
  38. {
  39. if(value > 24)
  40. value -= 24;
  41.  
  42. else if (value <0)
  43. value += 24;
  44. }
  45.  
  46. return value;
  47. }
  48.  
  49. //convert the double number to Hours and Minutes
  50. void doubleToHrMin(double number, int &hours, int &minutes)
  51. {
  52. hours = floor(moreLess24(number));
  53. minutes = floor(moreLess24(number - hours) * 60);
  54. }
  55.  
  56. void calcPrayerTimes(int year, int month, int day,
  57. double longitude, double latitude, int timeZone,
  58. double fajrTwilight, double ishaTwilight,
  59. double &fajrTime, double &sunRiseTime, double &zuhrTime,
  60. double &asrTime, double &maghribTime, double &ishaTime)
  61. {
  62. double D = (367 * year) - ((year + (int)((month + 9) / 12)) * 7 / 4) + (((int)(275 * month / 9)) + day - 730531.5);
  63.  
  64. double L = 280.461 + 0.9856474 * D;
  65. L = moreLess360(L);
  66.  
  67. double M = 357.528 + (0.9856003) * D;
  68. M = moreLess360(M);
  69.  
  70. double Lambda = L + 1.915 * sin(degToRad(M)) + 0.02 * sin(degToRad(2 * M));
  71. Lambda = moreLess360(Lambda);
  72.  
  73. double Obliquity = 23.439 - 0.0000004 * D;
  74. double Alpha = radToDeg(atan((cos(degToRad(Obliquity)) * tan(degToRad(Lambda)))));
  75. Alpha = moreLess360(Alpha);
  76.  
  77. Alpha = Alpha - (360 * (int)(Alpha / 360));
  78. Alpha = Alpha + 90 * (floor(Lambda / 90) - floor(Alpha / 90));
  79.  
  80. double ST = 100.46 + 0.985647352 * D;
  81. double Dec = radToDeg(asin(sin(degToRad(Obliquity)) * sin(degToRad(Lambda))));
  82. double Durinal_Arc = radToDeg(acos((sin(degToRad(-0.8333)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  83.  
  84. double Noon = Alpha - ST;
  85. Noon = moreLess360(Noon);
  86.  
  87.  
  88. double UT_Noon = Noon - longitude;
  89.  
  90.  
  91. ////////////////////////////////////////////
  92. // Calculating Prayer Times Arcs & Times //
  93. //////////////////////////////////////////
  94.  
  95. // Temkin değerleri
  96. double fajrTemkin = 0.0;
  97. double sunriseTemkin = -7.0 / 60.0;
  98. double zuhrTemkin = 4.0 / 60.0;
  99. double asrTemkin = 4.0 / 60.0;
  100. double maghribTemkin = 7.0 / 60.0;
  101. double ishaTemkin = 0.0;
  102.  
  103. // 2) Zuhr Time [Local noon]
  104. zuhrTime = (UT_Noon / 15 + timeZone) + zuhrTemkin;
  105.  
  106. // Asr Hanafi
  107. //double Asr_Alt =radToDeg(atan(2 + tan(degToRad(abs(latitude - Dec)))));
  108.  
  109. // Asr Shafii
  110. double Asr_Alt = radToDeg(atan(1 + tan(degToRad(abs(latitude - Dec)))));
  111. double Asr_Arc = radToDeg(acos((sin(degToRad(90 - Asr_Alt)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  112. Asr_Arc = Asr_Arc / 15;
  113. // 3) Asr Time
  114. asrTime = (zuhrTime + Asr_Arc) + asrTemkin;
  115.  
  116. // 1) Shorouq Time
  117. sunRiseTime = (zuhrTime - (Durinal_Arc / 15)) + sunriseTemkin;
  118.  
  119. // 4) Maghrib Time
  120. maghribTime = (zuhrTime + (Durinal_Arc / 15)) + maghribTemkin;
  121.  
  122.  
  123. double Esha_Arc = radToDeg(acos((sin(degToRad(ishaTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  124. // 5) Isha Time
  125. ishaTime = (zuhrTime + (Esha_Arc / 15)) + ishaTemkin;
  126.  
  127. // 0) Fajr Time
  128. double Fajr_Arc = radToDeg(acos((sin(degToRad(fajrTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
  129. fajrTime = (zuhrTime - (Fajr_Arc / 15)) + fajrTemkin;
  130.  
  131. return;
  132. }
  133.  
  134. /*
  135.  Prayers calculator end
  136. */
  137.  
  138.  
  139. /*
  140.  Personal code. Calculating for Ankara.
  141.  
  142. Date: 12-11-2024
  143. Longitude: 32.8597
  144. Latitude: 39.9334
  145. Time Zone: +3
  146. Fajr Twilight: -19.5
  147. Esha Twilight: -17.5
  148. */
  149.  
  150. int main() {
  151. double fajr, sunRise, zuhr, asr, maghrib, isha;
  152. calcPrayerTimes(2024,11,12, 32.8597, 39.9334, 3, -19, -17,
  153. fajr, sunRise, zuhr, asr, maghrib, isha);
  154.  
  155. int hours, minutes;
  156.  
  157. doubleToHrMin(fajr, hours, minutes);
  158. std::cout << "Fajr: " << hours << ":" << minutes << std::endl;
  159.  
  160. doubleToHrMin(sunRise, hours, minutes);
  161. std::cout << "Sunrise: " << hours << ":" << minutes << std::endl;
  162.  
  163. doubleToHrMin(zuhr, hours, minutes);
  164. std::cout << "Zuhr: " << hours << ":" << minutes << std::endl;
  165.  
  166. doubleToHrMin(asr, hours, minutes);
  167. std::cout << "Asr: " << hours << ":" << minutes << std::endl;
  168.  
  169. doubleToHrMin(maghrib, hours, minutes);
  170. std::cout << "Maghrib: " << hours << ":" << minutes << std::endl;
  171.  
  172. doubleToHrMin(isha, hours, minutes);
  173. std::cout << "Isha: " << hours << ":" << minutes << std::endl;
  174. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Fajr: 5:55
Sunrise: 7:27
Zuhr: 12:36
Asr: 15:21
Maghrib: 17:46
Isha: 19:7