)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Skip Navigation LinksHome » Code Library » PrayerTime

Public general use code classes and xml files that we've compiled and used over the years:

Prayer times support class.

    1: //--------------------- Copyright Block ----------------------
    2: /* 
    3: 
    4: PrayTime.cs: Prayer Times Calculator (ver 1.2)
    5: Copyright (C) 2007-2010 PrayTimes.org
    6: 
    7: C# Code By: Jandost Khoso
    8: Original JS Code By: Hamid Zarrabi-Zadeh
    9: 
   10: License: GNU General Public License, ver 3
   11: 
   12: TERMS OF USE:
   13:     Permission is granted to use this code, with or 
   14:     without modification, in any website or application 
   15:     provided that credit is given to the original work 
   16:     with a link back to PrayTimes.org.
   17: 
   18: This program is distributed in the hope that it will 
   19: be useful, but WITHOUT ANY WARRANTY. 
   20: 
   21: PLEASE DO NOT REMOVE THIS COPYRIGHT BLOCK.
   22: 
   23: */
   24:  
   25: using System;
   26:  
   27: namespace Ia.Cl.Model
   28: {
   29:     /// <summary publish="true">
   30:     /// Prayer times support class.
   31:     /// </summary>
   32:     /// <remarks> 
   33:     /// Copyright � 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   34:     ///
   35:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   36:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   37:     ///
   38:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   39:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   40:     /// 
   41:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   42:     /// 
   43:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   44:     /// </remarks>
   45:     public class PrayerTime
   46:     {
   47:         //------------------------ Constants --------------------------
   48:  
   49:         // Calculation Methods
   50:         /// <summary/>
   51:         public static int Jafari = 0;    // Ithna Ashari
   52:  
   53:         /// <summary/>
   54:         public static int Karachi = 1;    // University of Islamic Sciences, Karachi
   55:  
   56:         /// <summary/>
   57:         public static int ISNA = 2;    // Islamic Society of North America (ISNA)
   58:  
   59:         /// <summary/>
   60:         public static int MWL = 3;    // Muslim World League (MWL)
   61:  
   62:         /// <summary/>
   63:         public static int Makkah = 4;    // Umm al-Qura, Makkah
   64:  
   65:         /// <summary/>
   66:         public static int Egypt = 5;    // Egyptian General Authority of Survey
   67:  
   68:         /// <summary/>
   69:         public static int Custom = 6;    // Custom Setting
   70:  
   71:         /// <summary/>
   72:         public static int Tehran = 7;    // Institute of Geophysics, University of Tehran
   73:  
   74:         // Juristic Methods
   75:         /// <summary/>
   76:         public static int Shafii = 0;    // Shafii (standard)
   77:  
   78:         /// <summary/>
   79:         public static int Hanafi = 1;    // Hanafi
   80:  
   81:         // Adjusting Methods for Higher Latitudes
   82:         /// <summary/>
   83:         public static int None = 0;    // No adjustment
   84:  
   85:         /// <summary/>
   86:         public static int MidNight = 1;    // middle of night
   87:         /// <summary/>
   88:  
   89:         public static int OneSeventh = 2;    // 1/7th of night
   90:         /// <summary/>
   91:         public static int AngleBased = 3;    // angle/60th of night
   92:  
   93:  
   94:         // Time Formats
   95:         /// <summary/>
   96:         public static int Time24 = 0;    // 24-hour format
   97:  
   98:         /// <summary/>
   99:         public static int Time12 = 1;    // 12-hour format
  100:  
  101:         /// <summary/>
  102:         public static int Time12NS = 2;    // 12-hour format with no suffix
  103:  
  104:         /// <summary/>
  105:         public static int Floating = 3;    // floating point number
  106:  
  107:         // Time Names
  108:         /// <summary/>
  109:         public static string[] timeNames = { "Fajr", "Sunrise", "Dhuhr", "Asr", "Sunset", "Maghrib", "Isha" };
  110:         static string InvalidTime = "----";     // The string used for inv
  111:  
  112:  
  113:  
  114:  
  115:         //---------------------- Global Variables --------------------
  116:  
  117:  
  118:         private int calcMethod = 3;        // caculation method
  119:         private int asrJuristic;        // Juristic method for Asr
  120:         private int dhuhrMinutes = 0;        // minutes after mid-day for Dhuhr
  121:         private int adjustHighLats = 1;    // adjusting method for higher latitudes
  122:  
  123:         private int timeFormat = 0;        // time format
  124:  
  125:         private double lat;        // latitude
  126:         private double lng;        // longitude
  127:         private int timeZone;   // time-zone
  128:         private double JDate;      // Julian date
  129:  
  130:         private int[] times;
  131:  
  132:  
  133:         //--------------------- Technical Settings --------------------
  134:  
  135:  
  136:         private int numIterations = 1;        // number of iterations needed to compute times
  137:  
  138:  
  139:  
  140:         //------------------- Calc Method Parameters --------------------
  141:  
  142:         private double[][] methodParams;
  143:  
  144:         /// <summary/>
  145:         public PrayerTime()
  146:         {
  147:             times = new int[7];
  148:             methodParams = new double[8][];
  149:             this.methodParams[Jafari] = new double[] { 16, 0, 4, 0, 14 };
  150:             this.methodParams[Karachi] = new double[] { 18, 1, 0, 0, 18 };
  151:             this.methodParams[ISNA] = new double[] { 15, 1, 0, 0, 15 };
  152:             this.methodParams[MWL] = new double[] { 18, 1, 0, 0, 17 };
  153:             this.methodParams[Makkah] = new double[] { 18.5, 1, 0, 1, 90 };
  154:             this.methodParams[Egypt] = new double[] { 19.5, 1, 0, 0, 17.5 };
  155:             this.methodParams[Tehran] = new double[] { 17.7, 0, 4.5, 0, 14 };
  156:             this.methodParams[Custom] = new double[] { 18, 1, 0, 0, 17 };
  157:         }
  158:  
  159:         // return prayer times for a given date
  160:         /// <summary/>
  161:         public string[] GetPrayerTimes(int year, int month, int day, double latitude, double longitude, int timeZone)
  162:         {
  163:             return this.GetDatePrayerTimes(year, month + 1, day, latitude, longitude, timeZone);
  164:         }
  165:  
  166:         // set the calculation method
  167:         /// <summary/>
  168:         public void SetCalcMethod(int methodID)
  169:         {
  170:             this.calcMethod = methodID;
  171:         }
  172:  
  173:         // set the juristic method for Asr
  174:         /// <summary/>
  175:         public void SetAsrMethod(int methodID)
  176:         {
  177:             if (methodID < 0 || methodID > 1) return;
  178:             this.asrJuristic = methodID;
  179:         }
  180:  
  181:         // set the angle for calculating Fajr
  182:         /// <summary/>
  183:         public void SetFajrAngle(double angle)
  184:         {
  185:             this.SetCustomParams(new int[] { (int)angle, -1, -1, -1, -1 });
  186:         }
  187:  
  188:         // set the angle for calculating Maghrib
  189:         /// <summary/>
  190:         public void SetMaghribAngle(double angle)
  191:         {
  192:             this.SetCustomParams(new int[] { -1, 0, (int)angle, -1, -1 });
  193:         }
  194:  
  195:         // set the angle for calculating Isha
  196:         /// <summary/>
  197:         public void SetIshaAngle(double angle)
  198:         {
  199:             this.SetCustomParams(new int[] { -1, -1, -1, 0, (int)angle });
  200:         }
  201:  
  202:         // set the minutes after mid-day for calculating Dhuhr
  203:         /// <summary/>
  204:         public void SetDhuhrMinutes(int minutes)
  205:         {
  206:             this.dhuhrMinutes = minutes;
  207:         }
  208:  
  209:         // set the minutes after Sunset for calculating Maghrib
  210:         /// <summary/>
  211:         public void SetMaghribMinutes(int minutes)
  212:         {
  213:             this.SetCustomParams(new int[] { -1, 1, minutes, -1, -1 });
  214:         }
  215:  
  216:         // set the minutes after Maghrib for calculating Isha
  217:         /// <summary/>
  218:         public void SetIshaMinutes(int minutes)
  219:         {
  220:             this.SetCustomParams(new int[] { -1, -1, -1, 1, minutes });
  221:         }
  222:  
  223:         // set custom values for calculation parameters
  224:         /// <summary/>
  225:         public void SetCustomParams(int[] param)
  226:         {
  227:             for (int i = 0; i < 5; i++)
  228:             {
  229:                 if (param[i] == -1) this.methodParams[Custom][i] = this.methodParams[this.calcMethod][i];
  230:                 else this.methodParams[Custom][i] = param[i];
  231:             }
  232:  
  233:             this.calcMethod = Custom;
  234:         }
  235:  
  236:         // set adjusting method for higher latitudes
  237:         /// <summary/>
  238:         public void SetHighLatsMethod(int methodID)
  239:         {
  240:             this.adjustHighLats = methodID;
  241:         }
  242:  
  243:         // set the time format
  244:         /// <summary/>
  245:         public void SetTimeFormat(int timeFormat)
  246:         {
  247:             this.timeFormat = timeFormat;
  248:         }
  249:  
  250:         // convert float hours to 24h format
  251:         /// <summary/>
  252:         public string FloatToTime24(double time)
  253:         {
  254:             if (time < 0) return InvalidTime;
  255:             time = this.FixHour(time + 0.5 / 60);  // add 0.5 minutes to round
  256:             double hours = Math.Floor(time);
  257:             double minutes = Math.Floor((time - hours) * 60);
  258:             return this.TwoDigitsFormat((int)hours) + ":" + this.TwoDigitsFormat((int)minutes);
  259:         }
  260:  
  261:         // convert float hours to 12h format
  262:         /// <summary/>
  263:         public string FloatToTime12(double time, bool noSuffix)
  264:         {
  265:             if (time < 0) return InvalidTime;
  266:             time = this.FixHour(time + 0.5 / 60);  // add 0.5 minutes to round
  267:             double hours = Math.Floor(time);
  268:             double minutes = Math.Floor((time - hours) * 60);
  269:             string suffix = hours >= 12 ? " pm" : " am";
  270:             hours = (hours + 12 - 1) % 12 + 1;
  271:             return ((int)hours) + ":" + this.TwoDigitsFormat((int)minutes) + (noSuffix ? "" : suffix);
  272:         }
  273:  
  274:         // convert float hours to 12h format with no suffix
  275:         /// <summary/>
  276:         public string FloatToTime12NS(double time)
  277:         {
  278:             return this.FloatToTime12(time, true);
  279:         }
  280:  
  281:         //---------------------- Compute Prayer Times -----------------------
  282:  
  283:  
  284:         // return prayer times for a given date
  285:         /// <summary/>
  286:         public string[] GetDatePrayerTimes(int year, int month, int day, double latitude, double longitude, int timeZone)
  287:         {
  288:             this.lat = latitude;
  289:             this.lng = longitude;
  290:             this.timeZone = timeZone;
  291:             this.JDate = this.JulianDate(year, month, day) - longitude / (15 * 24);
  292:  
  293:             return this.ComputeDayTimes();
  294:         }
  295:  
  296:         // compute declination angle of sun and equation of time
  297:         /// <summary/>
  298:         public double[] SunPosition(double jd)
  299:         {
  300:             double D = jd - 2451545.0;
  301:             double g = this.FixAngle(357.529 + 0.98560028 * D);
  302:             double q = this.FixAngle(280.459 + 0.98564736 * D);
  303:             double L = this.FixAngle(q + 1.915 * this.Dsin(g) + 0.020 * this.Dsin(2 * g));
  304:  
  305:             double R = 1.00014 - 0.01671 * this.Dcos(g) - 0.00014 * this.Dcos(2 * g);
  306:             double e = 23.439 - 0.00000036 * D;
  307:  
  308:             double d = this.Darcsin(this.Dsin(e) * this.Dsin(L));
  309:             double RA = this.Darctan2(this.Dcos(e) * this.Dsin(L), this.Dcos(L)) / 15;
  310:             RA = this.FixHour(RA);
  311:             double EqT = q / 15 - RA;
  312:  
  313:             return new double[] { d, EqT };
  314:         }
  315:  
  316:         // compute equation of time
  317:         /// <summary/>
  318:         public double EquationOfTime(double jd)
  319:         {
  320:             return this.SunPosition(jd)[1];
  321:         }
  322:  
  323:         // compute declination angle of sun
  324:         /// <summary/>
  325:         public double SunDeclination(double jd)
  326:         {
  327:             return this.SunPosition(jd)[0];
  328:         }
  329:  
  330:         // compute mid-day (Dhuhr, Zawal) time
  331:         /// <summary/>
  332:         public double ComputeMidDay(double t)
  333:         {
  334:             double T = this.EquationOfTime(this.JDate + t);
  335:             double Z = this.FixHour(12 - T);
  336:             return Z;
  337:         }
  338:  
  339:         // compute time for a given angle G
  340:         /// <summary/>
  341:         public double ComputeTime(double G, double t)
  342:         {
  343:             //System.out.println("G: "+G);
  344:  
  345:             double D = this.SunDeclination(this.JDate + t);
  346:             double Z = this.ComputeMidDay(t);
  347:             double V = ((double)1 / 15) * this.Darccos((-this.Dsin(G) - this.Dsin(D) * this.Dsin(this.lat)) /
  348:                     (this.Dcos(D) * this.Dcos(this.lat)));
  349:             return Z + (G > 90 ? -V : V);
  350:         }
  351:  
  352:         // compute the time of Asr
  353:         /// <summary/>
  354:         public double ComputeAsr(int step, double t)  // Shafii: step=1, Hanafi: step=2
  355:         {
  356:             double D = this.SunDeclination(this.JDate + t);
  357:             double G = -this.Darccot(step + this.Dtan(Math.Abs(this.lat - D)));
  358:             return this.ComputeTime(G, t);
  359:         }
  360:  
  361:         //---------------------- Compute Prayer Times -----------------------
  362:  
  363:         // compute prayer times at given julian date
  364:         /// <summary/>
  365:         public double[] ComputeTimes(double[] times)
  366:         {
  367:             double[] t = this.DayPortion(times);
  368:  
  369:  
  370:             double Fajr = this.ComputeTime(180 - this.methodParams[this.calcMethod][0], t[0]);
  371:             double Sunrise = this.ComputeTime(180 - 0.833, t[1]);
  372:             double Dhuhr = this.ComputeMidDay(t[2]);
  373:             double Asr = this.ComputeAsr(1 + this.asrJuristic, t[3]);
  374:             double Sunset = this.ComputeTime(0.833, t[4]); ;
  375:             double Maghrib = this.ComputeTime(this.methodParams[this.calcMethod][2], t[5]);
  376:             double Isha = this.ComputeTime(this.methodParams[this.calcMethod][4], t[6]);
  377:  
  378:             return new double[] { Fajr, Sunrise, Dhuhr, Asr, Sunset, Maghrib, Isha };
  379:         }
  380:  
  381:         // adjust Fajr, Isha and Maghrib for locations in higher latitudes
  382:         /// <summary/>
  383:         public double[] AdjustHighLatTimes(double[] times)
  384:         {
  385:             double nightTime = this.GetTimeDifference(times[4], times[1]); // sunset to sunrise
  386:  
  387:             // Adjust Fajr
  388:             double FajrDiff = this.NightPortion(this.methodParams[this.calcMethod][0]) * nightTime;
  389:             if (this.GetTimeDifference(times[0], times[1]) > FajrDiff)
  390:                 times[0] = times[1] - FajrDiff;
  391:  
  392:             // Adjust Isha
  393:             double IshaAngle = (this.methodParams[this.calcMethod][3] == 0) ? this.methodParams
  394:  
  395:         [this.calcMethod][4] : 18;
  396:             double IshaDiff = this.NightPortion(IshaAngle) * nightTime;
  397:             if (this.GetTimeDifference(times[4], times[6]) > IshaDiff)
  398:                 times[6] = times[4] + IshaDiff;
  399:  
  400:             // Adjust Maghrib
  401:             double MaghribAngle = (methodParams[this.calcMethod][1] == 0) ? this.methodParams
  402:  
  403:         [this.calcMethod][2] : 4;
  404:             double MaghribDiff = this.NightPortion(MaghribAngle) * nightTime;
  405:             if (this.GetTimeDifference(times[4], times[5]) > MaghribDiff)
  406:                 times[5] = times[4] + MaghribDiff;
  407:  
  408:             return times;
  409:         }
  410:  
  411:         // the night portion used for adjusting times in higher latitudes
  412:         /// <summary/>
  413:         public double NightPortion(double angle)
  414:         {
  415:             double val = 0;
  416:             if (this.adjustHighLats == AngleBased)
  417:                 val = 1.0 / 60.0 * angle;
  418:             if (this.adjustHighLats == MidNight)
  419:                 val = 1.0 / 2.0;
  420:             if (this.adjustHighLats == OneSeventh)
  421:                 val = 1.0 / 7.0;
  422:  
  423:             return val;
  424:         }
  425:  
  426:         /// <summary/>
  427:         public double[] DayPortion(double[] times)
  428:         {
  429:             for (int i = 0; i < times.Length; i++)
  430:             {
  431:                 times[i] /= 24;
  432:             }
  433:             return times;
  434:         }
  435:  
  436:         // compute prayer times at given julian date
  437:         /// <summary/>
  438:         public string[] ComputeDayTimes()
  439:         {
  440:             double[] times = { 5, 6, 12, 13, 18, 18, 18 }; //default times
  441:  
  442:             for (int i = 0; i < this.numIterations; i++)
  443:             {
  444:                 times = this.ComputeTimes(times);
  445:             }
  446:  
  447:             times = this.AdjustTimes(times);
  448:             return this.AdjustTimesFormat(times);
  449:         }
  450:  
  451:  
  452:         // adjust times in a prayer time array
  453:         /// <summary/>
  454:         public double[] AdjustTimes(double[] times)
  455:         {
  456:             for (int i = 0; i < 7; i++)
  457:             {
  458:                 times[i] += this.timeZone - this.lng / 15;
  459:             }
  460:             times[2] += this.dhuhrMinutes / 60; //Dhuhr
  461:             if (this.methodParams[this.calcMethod][1] == 1) // Maghrib
  462:                 times[5] = times[4] + this.methodParams[this.calcMethod][2] / 60.0;
  463:             if (this.methodParams[this.calcMethod][3] == 1) // Isha
  464:                 times[6] = times[5] + this.methodParams[this.calcMethod][4] / 60.0;
  465:  
  466:             if (this.adjustHighLats != None)
  467:             {
  468:                 times = this.AdjustHighLatTimes(times);
  469:             }
  470:  
  471:             return times;
  472:         }
  473:  
  474:         /// <summary/>
  475:         public string[] AdjustTimesFormat(double[] times)
  476:         {
  477:             string[] formatted = new string[times.Length];
  478:  
  479:             if (this.timeFormat == Floating)
  480:             {
  481:                 for (int i = 0; i < times.Length; ++i)
  482:                 {
  483:                     formatted[i] = times[i] + "";
  484:                 }
  485:                 return formatted;
  486:             }
  487:  
  488:             for (int i = 0; i < 7; i++)
  489:             {
  490:                 if (this.timeFormat == Time12)
  491:                     formatted[i] = this.FloatToTime12(times[i], true);
  492:                 else if (this.timeFormat == Time12NS)
  493:                     formatted[i] = this.FloatToTime12NS(times[i]);
  494:                 else
  495:                     formatted[i] = this.FloatToTime24(times[i]);
  496:             }
  497:             return formatted;
  498:         }
  499:  
  500:         //---------------------- Misc Functions -----------------------
  501:  
  502:         // compute the difference between two times
  503:         /// <summary/>
  504:         public double GetTimeDifference(double c1, double c2)
  505:         {
  506:             double diff = this.FixHour(c2 - c1); ;
  507:             return diff;
  508:         }
  509:  
  510:         // add a leading 0 if necessary
  511:         /// <summary/>
  512:         public string TwoDigitsFormat(int num)
  513:         {
  514:  
  515:             return (num < 10) ? "0" + num : num + "";
  516:         }
  517:  
  518:         //---------------------- Julian Date Functions -----------------------
  519:  
  520:         // calculate julian date from a calendar date
  521:         /// <summary/>
  522:         public double JulianDate(int year, int month, int day)
  523:         {
  524:             if (month <= 2)
  525:             {
  526:                 year -= 1;
  527:                 month += 12;
  528:             }
  529:  
  530:             double A = (double)Math.Floor(year / 100.0);
  531:             double B = 2 - A + Math.Floor(A / 4);
  532:  
  533:             double JD = Math.Floor(365.25 * (year + 4716)) + Math.Floor(30.6001 * (month + 1)) + day + B - 1524.5;
  534:             return JD;
  535:         }
  536:  
  537:  
  538:         //---------------------- Time-Zone Functions -----------------------
  539:  
  540:  
  541:         // detect daylight saving in a given date
  542:         /// <summary/>
  543:         public bool UseDayLightaving(int year, int month, int day)
  544:         {
  545:             return System.TimeZoneInfo.Local.IsDaylightSavingTime(new DateTime(year, month, day));
  546:             // TimeZone . CurrentTimeZone . IsDaylightSavingTime ( new DateTime ( year , month , day ) ); 
  547:         }
  548:  
  549:         // ---------------------- Trigonometric Functions -----------------------
  550:  
  551:         // degree sin
  552:         /// <summary/>
  553:         public double Dsin(double d)
  554:         {
  555:             return Math.Sin(this.DegreeToRadian(d));
  556:         }
  557:  
  558:         // degree cos
  559:         /// <summary/>
  560:         public double Dcos(double d)
  561:         {
  562:             return Math.Cos(this.DegreeToRadian(d));
  563:         }
  564:  
  565:         // degree tan
  566:         /// <summary/>
  567:         public double Dtan(double d)
  568:         {
  569:             return Math.Tan(this.DegreeToRadian(d));
  570:         }
  571:  
  572:         // degree arcsin
  573:         /// <summary/>
  574:         public double Darcsin(double x)
  575:         {
  576:             return this.RadianToDegree(Math.Asin(x));
  577:         }
  578:  
  579:         // degree arccos
  580:         /// <summary/>
  581:         public double Darccos(double x)
  582:         {
  583:             return this.RadianToDegree(Math.Acos(x));
  584:         }
  585:  
  586:         // degree arctan
  587:         /// <summary/>
  588:         public double Darctan(double x)
  589:         {
  590:             return this.RadianToDegree(Math.Atan(x));
  591:         }
  592:  
  593:         // degree arctan2
  594:         /// <summary/>
  595:         public double Darctan2(double y, double x)
  596:         {
  597:             return this.RadianToDegree(Math.Atan2(y, x));
  598:         }
  599:  
  600:         // degree arccot
  601:         /// <summary/>
  602:         public double Darccot(double x)
  603:         {
  604:             return this.RadianToDegree(Math.Atan(1 / x));
  605:         }
  606:  
  607:  
  608:         // Radian to Degree
  609:         /// <summary/>
  610:         public double RadianToDegree(double radian)
  611:         {
  612:             return (radian * 180.0) / Math.PI;
  613:         }
  614:  
  615:         // degree to radian
  616:         /// <summary/>
  617:         public double DegreeToRadian(double degree)
  618:         {
  619:             return (degree * Math.PI) / 180.0;
  620:         }
  621:  
  622:         /// <summary/>
  623:         public double FixAngle(double angel)
  624:         {
  625:             angel = angel - 360.0 * (Math.Floor(angel / 360.0));
  626:             angel = angel < 0 ? angel + 360.0 : angel;
  627:             return angel;
  628:         }
  629:  
  630:         // range reduce hours to 0..23
  631:         /// <summary/>
  632:         public double FixHour(double hour)
  633:         {
  634:             hour = hour - 24.0 * (Math.Floor(hour / 24.0));
  635:             hour = hour < 0 ? hour + 24.0 : hour;
  636:             return hour;
  637:         }
  638:     }
  639: }