private void CalculateDayLight() { //Read the stored Open Weather location json GeographicLocation location; string jsonPath = "%OneDrive%\\Etc\\DemonOpenWeather.json".TranslatePath(); using (StreamReader stream = File.OpenText(jsonPath)) { string json = stream.ReadToEnd(); location = JsonConvert.DeserializeObject <GeographicLocation>(json); } DaylightHours daylight = DaylightHours.Calculate(DateTime.Now.Date.AddHours(2), location); SunriseTime = daylight.SunriseUtc.Value.LocalDateTime.ToLocalTime(); SunsetTime = daylight.SunsetUtc.Value.LocalDateTime.ToLocalTime(); }
private void AddDaylight() { //Temperature graph area Rectangle area = new Rectangle() { Width = graph.Width - MarginLeft, Height = graph.Height - MarginTop - MarginBottom, Fill = Brushes.LightSlateGray, }; graph.Children.Add(area); Canvas.SetLeft(area, MarginLeft); Canvas.SetTop(area, MarginTop); //Read the stored Open Weather location json GeographicLocation location; string jsonPath = "%OneDrive%\\Etc\\DemonOpenWeather.json".TranslatePath(); using (StreamReader stream = File.OpenText(jsonPath)) { string json = stream.ReadToEnd(); location = JsonConvert.DeserializeObject <GeographicLocation>(json); } DateTime date = DateStr.Date.AddHours(2); //Calculate the daylight times do { DaylightHours daylight = DaylightHours.Calculate(date, location); DateTime sunriseTime = daylight.SunriseUtc.Value.LocalDateTime.ToLocalTime(); DateTime sunsetTime = daylight.SunsetUtc.Value.LocalDateTime.ToLocalTime(); if (sunsetTime > DateStr) { if (sunriseTime < DateStr) { sunriseTime = DateStr; } } if (sunsetTime > DateFin) { sunsetTime = DateFin; } if (sunriseTime < sunsetTime) { area = new Rectangle() { Width = ((sunsetTime - sunriseTime).TotalMinutes * TimeLineScale), Height = TempLineStr - TempLineFin, Fill = Brushes.LightCyan, }; graph.Children.Add(area); Canvas.SetLeft(area, TimeLineStr + (sunriseTime - DateStr).TotalMinutes * TimeLineScale); Canvas.SetTop(area, MarginTop); } date = date.AddDays(1); } while (date.Date == Weather.Max(x => x.Time).Date); }