コード例 #1
0
        public void SolarTime()
        {
            /*
             * Comparison values generated from
             * http://aa.usno.navy.mil/rstt/onedaytable?form=1&ID=AA&year=2015&month=7&day=12&state=NC&place=raleigh
             */

            Coordinates coordinates = new Coordinates(35 + 47.0 / 60.0, -78 - 39.0 / 60.0);
            SolarTime   solar       = new SolarTime(TestUtils.MakeDate(2015, 7, 12), coordinates);

            double transit       = solar.Transit;
            double sunrise       = solar.Sunrise;
            double sunset        = solar.Sunset;
            double twilightStart = solar.HourAngle(-6, /* afterTransit */ false);
            double twilightEnd   = solar.HourAngle(-6, /* afterTransit */ true);
            double invalid       = solar.HourAngle(-36, /* afterTransit */ true);

            Assert.IsTrue(TimeString(twilightStart) == "9:38");
            Assert.IsTrue(TimeString(sunrise) == "10:08");
            Assert.IsTrue(TimeString(transit) == "17:20");
            Assert.IsTrue(TimeString(sunset) == "24:32");
            Assert.IsTrue(TimeString(twilightEnd) == "25:02");
            Assert.IsTrue(TimeString(invalid) == "");
        }
コード例 #2
0
ファイル: PrayerTimes.cs プロジェクト: zendbit/Kaffah.Adzan
        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));
            }
        }