public string GetTimeZoneId(Location location) { var middayToday = DateTime.UtcNow.Date.AddHours(12); var url = string.Format( "https://maps.googleapis.com/maps/api/timezone/json?location={0},{1}×tamp={2}", location.Latitude, location.Longitude, ToUnixTimeStamp(middayToday)); using (var client = new WebClient()) { var responseJson = client.DownloadString(url); var response = JsonConvert.DeserializeObject<TimeZoneResponse>(responseJson); if (response.Status != "OK") { var message = response.Status == "ZERO_RESULTS" ? "Sorry, but the time zone for your location could not be obtained. Please make sure your location is on land." : string.Format("Sorry, but the time zone for your location could not be obtained ({0}: {1}).", response.Status, response.ErrorMessage); throw new UserDisplayableException(message); } var id = TimeZones.OlsonTimeZoneToTimeZoneInfo(response.TimeZoneId); if (id == null) { throw new UserDisplayableException(string.Format("Sorry, but the time zone information for your location could not be found ({0}).", response.TimeZoneId)); } return id; } }
public DaylightInfo GetDaylight(Location location, string timeZoneId, DateTime commuteStart, DateTime commuteEnd) { var sunCalculator = new SunCalculator(location.Longitude, location.Latitude); var daylightInfo = CalculateDaylightInfo(commuteStart, commuteEnd, sunCalculator, timeZoneId); return daylightInfo; }
public ActionResult Index(CommuteInfoModel model) { if (model == null || model.HasDefaultValues()) { model = new CommuteInfoModel { h = "0800", w = "1830", j = 30, d = WorkingDays.Monday | WorkingDays.Tuesday | WorkingDays.Wednesday | WorkingDays.Thursday | WorkingDays.Friday }; SetLocation(model); ModelState.Clear(); return View(model); } if (!model.y.HasValue || !model.x.HasValue) // shouldn't happen, but could { SetLocation(model); } DateTime outboundCommuteStart; DateTime returnCommuteStart; model.Validate(ModelState, out outboundCommuteStart, out returnCommuteStart); if (ModelState.IsValid) { try { var daylightHunter = new DaylightHunter(); var outboundCommuteEnd = outboundCommuteStart.AddMinutes(model.j); var returnCommuteEnd = returnCommuteStart.AddMinutes(model.j); var location = new Location { Latitude = model.y.Value, Longitude = model.x.Value }; model.TimeZoneId = _geoService.GetTimeZoneId(location); var toWorkDaylightInfo = daylightHunter.GetDaylight(location, model.TimeZoneId, outboundCommuteStart, outboundCommuteEnd); var fromWorkDaylightInfo = daylightHunter.GetDaylight(location, model.TimeZoneId, returnCommuteStart, returnCommuteEnd); model.ToWorkDaylights = Builders.BuildDaylightInfoModel(DateTime.Now.Date, toWorkDaylightInfo, Commute.ToWork, model.d); model.FromWorkDaylights = Builders.BuildDaylightInfoModel(DateTime.Now.Date, fromWorkDaylightInfo, Commute.FromWork, model.d); } catch (UserDisplayableException ex) { ModelState.AddModelError("UserDisplayable", ex.Message); } } return View(model); }
public static Location? Parse(XDocument response) { Location? location = null; XNamespace ns = "http://www.opengis.net/gml"; var coordinates = GetNestedElementValue(response.Root, new[] { ns + "featureMember", "Hostip", "ipLocation", ns + "pointProperty", ns + "Point", ns + "coordinates" }); if (!string.IsNullOrEmpty(coordinates)) { var coordParts = coordinates.Split(','); var longitude = double.Parse(coordParts[0]); var latitude = double.Parse(coordParts[1]); location = new Location { Latitude = latitude, Longitude = longitude }; } return location; }