public Suntime Suntimes(Location location) { return SunTimes.Instance.CalculateSunRiseSetTimes(location, CurrentTime); }
/// <summary> /// Calculate sunrise and sunset times. Returns false if time zone and intitude are incompatible. /// </summary> /// <param name="lat">Latitude in decimal notation.</param> /// <param name="lon">intitude in decimal notation.</param> /// <param name="date">Date for which to calculate.</param> /// <param name="riseTime">Sunrise time (output)</param> /// <param name="setTime">Sunset time (output)</param> /// <param name="isSunrise">Whether or not the sun rises at that day</param> /// <param name="isSunset">Whether or not the sun sets at that day</param> public Suntime CalculateSunRiseSetTimes(Location location, DateTime date) { lock (mLock) // lock for thread safety { Suntime suntime = new Suntime(); double zone = -(int) Math.Round(TimeZone.CurrentTimeZone.GetUtcOffset(date).Seconds/3600); double jd = GetJulianDay(date) - 2451545; // Julian day relative to Jan 1.5, 2000 if ((Sign(zone) == Sign(location.Longitude)) && (zone != 0)) { Debug.Print("WARNING: time zone and intitude are incompatible!"); suntime.Valid = false; return suntime; } location.Longitude = location.Longitude / 360; double tz = zone/24; double ct = jd/36525 + 1; // centuries since 1900.0 double t0 = LocalSiderealTimeForTimeZone(location.Longitude, jd, tz); // local sidereal time // get sun position at start of day jd += tz; CalculateSunPosition(jd, ct); double ra0 = mSunPositionInSkyArr[0]; double dec0 = mSunPositionInSkyArr[1]; // get sun position at end of day jd += 1; CalculateSunPosition(jd, ct); double ra1 = mSunPositionInSkyArr[0]; double dec1 = mSunPositionInSkyArr[1]; // make continuous if (ra1 < ra0) ra1 += 2*Math.PI; // initialize mIsSunrise = false; mIsSunset = false; mRightAscentionArr[0] = ra0; mDecensionArr[0] = dec0; // check each hour of this day for (int k = 0; k < 24; k++) { mRightAscentionArr[2] = ra0 + (k + 1)*(ra1 - ra0)/24; mDecensionArr[2] = dec0 + (k + 1)*(dec1 - dec0)/24; mVHzArr[2] = TestHour(k, zone, t0, location.Latitude); // advance to next hour mRightAscentionArr[0] = mRightAscentionArr[2]; mDecensionArr[0] = mDecensionArr[2]; mVHzArr[0] = mVHzArr[2]; } suntime.Sunrise = new DateTime(date.Year, date.Month, date.Day, mRiseTimeArr[0], mRiseTimeArr[1], 0); suntime.Sunset = new DateTime(date.Year, date.Month, date.Day, mSetTimeArr[0], mSetTimeArr[1], 0); suntime.IsSunSet = true; suntime.IsSunRise = true; // neither sunrise nor sunset if ((!mIsSunrise) && (!mIsSunset)) { if (mVHzArr[2] < 0) suntime.IsSunRise = false; // Sun down all day else suntime.IsSunSet = false; // Sun up all day } // sunrise or sunset else { if (!mIsSunrise) // No sunrise this date suntime.IsSunRise = false; else if (!mIsSunset) // No sunset this date suntime.IsSunSet = false; } suntime.Valid = true; return suntime; } }