private void UpdateData() { using (var context = new ApplicationDbContext()) { var todayTicks = DateTime.UtcNow.Date.Ticks; var today = context.Days.SingleOrDefault(x => x.DateTicks == todayTicks); if (today == null) { today = new Models.Day { DateTicks = DateTime.UtcNow.Date.Ticks, StartTime = DateTime.UtcNow.TimeOfDay }; context.Days.Add(today); context.SaveChanges(); } else if (today.StartTime == null) { today.StartTime = DateTime.UtcNow.TimeOfDay; context.SaveChanges(); } var dayOfWeek = new DateTime(today.DateTicks).DayOfWeek; // mon = 1 var monday = DateTime.UtcNow.Date.AddDays(1 - (int)dayOfWeek); var sunday = monday.AddDays(6); var thisWeekDays = context.Days.Where(x => x.DateTicks >= monday.Ticks && x.DateTicks <= sunday.Ticks).ToList(); TimeSpan weekTime = TimeSpan.FromSeconds(0); foreach (var day in thisWeekDays) { weekTime += day.WorkTime(); } ((MainWindow)MainWindow).UpdateData(weekTime, today); } }
public void OnShutdown(object sender, SessionEndingEventArgs e) { // Register the shutdown time using (var context = new ApplicationDbContext()) { var todayTicks = DateTime.UtcNow.Date.Ticks; var today = context.Days.SingleOrDefault(x => x.DateTicks == todayTicks); if (today != null) { today.EndTime = DateTime.UtcNow.TimeOfDay; context.SaveChanges(); } else { today = new Models.Day { DateTicks = DateTime.UtcNow.Date.Ticks, EndTime = DateTime.UtcNow.TimeOfDay }; context.Days.Add(today); context.SaveChanges(); } } }