コード例 #1
0
        private PrayerTimes(Coordinates coordinates, DateTime date, CalculationParameters parameters)
        {
            DateTime?tempFajr    = null;
            DateTime?tempSunrise = null;
            DateTime?tempDhuhr   = null;
            DateTime?tempAsr     = null;
            DateTime?tempMaghrib = null;
            DateTime?tempIsha    = null;

            //DateTime calendar = date.ToUniversalTime();
            int year      = date.Year;
            int dayOfYear = date.DayOfYear;

            SolarTime solarTime = new SolarTime(date, coordinates);

            TimeComponents timeComponents = TimeComponents.FromDouble(solarTime.Transit);
            DateTime?      transit        = timeComponents?.DateComponents(date);

            timeComponents = TimeComponents.FromDouble(solarTime.Sunrise);
            DateTime?sunriseComponents = timeComponents?.DateComponents(date);

            timeComponents = TimeComponents.FromDouble(solarTime.Sunset);
            DateTime?sunsetComponents = timeComponents?.DateComponents(date);

            bool error = transit == null || sunriseComponents == null || sunsetComponents == null;

            if (!error)
            {
                tempDhuhr   = transit;
                tempSunrise = sunriseComponents;
                tempMaghrib = sunsetComponents;

                timeComponents = TimeComponents.FromDouble(
                    solarTime.Afternoon(parameters.Madhab.GetShadowLength()));

                if (timeComponents != null)
                {
                    tempAsr = timeComponents.DateComponents(date);
                }

                // get night length
                DateTime tomorrowSunrise = sunriseComponents.Value.AddDays(1);
                double   night           = tomorrowSunrise.GetTime() - sunsetComponents.Value.GetTime();

                timeComponents = TimeComponents
                                 .FromDouble(solarTime.HourAngle(-parameters.FajrAngle, false));

                if (timeComponents != null)
                {
                    tempFajr = timeComponents.DateComponents(date);
                }

                if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE &&
                    coordinates.Latitude >= 55)
                {
                    tempFajr = sunriseComponents.Value.AddSeconds(-1 * (int)(night / 7000));
                }

                NightPortions nightPortions = parameters.NightPortions();

                DateTime safeFajr;
                if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE)
                {
                    safeFajr = SeasonAdjustedMorningTwilight(coordinates.Latitude, dayOfYear, year, sunriseComponents.Value);
                }
                else
                {
                    double portion       = nightPortions.Fajr;
                    long   nightFraction = (long)(portion * night / 1000);
                    safeFajr = sunriseComponents.Value.AddSeconds(-1 * (int)nightFraction);
                }

                if (tempFajr == null || tempFajr.Value.Before(safeFajr))
                {
                    tempFajr = safeFajr;
                }

                // Isha calculation with check against safe value
                if (parameters.IshaInterval > 0)
                {
                    tempIsha = tempMaghrib.Value.AddSeconds(parameters.IshaInterval * 60);
                }
                else
                {
                    timeComponents = TimeComponents.FromDouble(
                        solarTime.HourAngle(-parameters.IshaAngle, true));

                    if (timeComponents != null)
                    {
                        tempIsha = timeComponents.DateComponents(date);
                    }

                    if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE &&
                        coordinates.Latitude >= 55)
                    {
                        long nightFraction = (long)night / 7000;
                        tempIsha = sunsetComponents.Value.AddSeconds(nightFraction);
                    }

                    DateTime safeIsha;
                    if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE)
                    {
                        safeIsha = PrayerTimes.SeasonAdjustedEveningTwilight(
                            coordinates.Latitude, dayOfYear, year, sunsetComponents.Value);
                    }
                    else
                    {
                        double portion       = nightPortions.Isha;
                        long   nightFraction = (long)(portion * night / 1000);
                        safeIsha = sunsetComponents.Value.AddSeconds(nightFraction);
                    }

                    if (tempIsha == null || (tempIsha.Value.After(safeIsha)))
                    {
                        tempIsha = safeIsha;
                    }
                }
            }

            if (error || tempAsr == null)
            {
                // if we don't have all prayer times then initialization failed
                this.Fajr    = DateTime.MinValue;
                this.Sunrise = DateTime.MinValue;
                this.Dhuhr   = DateTime.MinValue;
                this.Asr     = DateTime.MinValue;
                this.Maghrib = DateTime.MinValue;
                this.Isha    = DateTime.MinValue;
            }
            else
            {
                // Assign final times to public struct members with all offsets
                this.Fajr    = CalendarUtil.RoundedMinute(tempFajr.Value.AddMinutes(parameters.Adjustments.Fajr + parameters.MethodAdjustments.Fajr));
                this.Sunrise = CalendarUtil.RoundedMinute(tempSunrise.Value.AddMinutes(parameters.Adjustments.Sunrise + parameters.MethodAdjustments.Sunrise));
                this.Dhuhr   = CalendarUtil.RoundedMinute(tempDhuhr.Value.AddMinutes(parameters.Adjustments.Dhuhr + parameters.MethodAdjustments.Dhuhr));
                this.Asr     = CalendarUtil.RoundedMinute(tempAsr.Value.AddMinutes(parameters.Adjustments.Asr + parameters.MethodAdjustments.Asr));
                this.Maghrib = CalendarUtil.RoundedMinute(tempMaghrib.Value.AddMinutes(parameters.Adjustments.Maghrib + parameters.MethodAdjustments.Maghrib));
                this.Isha    = CalendarUtil.RoundedMinute(tempIsha.Value.AddMinutes(parameters.Adjustments.Isha + parameters.MethodAdjustments.Isha));
            }
        }
コード例 #2
0
 /// <summary>
 /// Calculate PrayerTimes
 /// </summary>
 /// <param name="coordinates">the coordinates of the location</param>
 /// <param name="date">the date components for that location</param>
 /// <param name="params">he parameters for the calculation</param>
 public PrayerTimes(Coordinates coordinates, DateComponents date, CalculationParameters parameters)
     : this(coordinates, CalendarUtil.ResolveTime(date), parameters)
 {
 }