예제 #1
0
        /// <summary>
        /// Returns the sunrise or sunset datetime for given latitude and longitude coordates
        /// </summary>
        /// <param name="getSunrise"></param>
        /// <param name="LatLng"></param>
        /// <returns></returns>
        private DateTime?GetSunriseSunset(bool getSunrise, string LatLng)
        {
            USNavySunData sunriseSunsetData = GetSunriseSunsetDataFromNavy(LatLng);

            // Get today's sunrise and sunset from USNavySunData object
            if (sunriseSunsetData != null)
            {
                DateTime?sunrise = null, sunset = null;
                //var timeZone = GetTimeZoneInfo(LatLng);
                var tz = GetTimeZoneData(LatLng);

                foreach (var item in sunriseSunsetData.sundata)
                {
                    if (item.phen == "R")
                    {
                        sunrise = DateTime.Parse(item.time).AddSeconds(tz.rawOffset + tz.dstOffset);
                    }

                    if (item.phen == "S")
                    {
                        sunset = DateTime.Parse(item.time).AddSeconds(tz.rawOffset + tz.dstOffset);
                    }
                }

                if (sunrise != null && sunset != null)
                {
                    /* Due to summer tiems and UTC offset, the sunset was coming out after midnight UTC, but was being set to the same date
                     * so sunset was showing as before sunrise. Adding a day to account for this anomoly
                     */
                    if (sunrise > sunset)
                    {
                        sunset = sunset.Value.AddDays(1);
                    }

                    if (getSunrise)
                    {
                        return(sunrise);
                    }

                    return(sunset);
                }
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Get the Sun data JSON object from the US Navy web service
        /// </summary>
        /// <param name="Address"></param>
        /// <returns></returns>
        private USNavySunData GetSunriseSunsetDataFromNavy(string latLng)
        {
            // Get the Sunrise/Sunset data from the US Navy web service api
            string        cacheKey          = FormatCacheKey("USNavySunriseSunset", latLng);
            USNavySunData sunriseSunsetData = Cache.Get <USNavySunData>(cacheKey);

            if (sunriseSunsetData == null)
            {
                var url = string.Format("http://api.usno.navy.mil/rstt/oneday?date={0}&coords={1}", DateTime.Now.ToString("MM/dd/yyyy"), latLng);
                sunriseSunsetData = GetAsyncResult <USNavySunData>(url);

                if (sunriseSunsetData != null)
                {
                    Cache.Set(cacheKey, sunriseSunsetData, 43200);
                }
            }

            return(sunriseSunsetData);
        }