public SortedDictionary <long, ScheduleBlock> GetScheduleBlockList(DateTime start, DateTime end, List <ScheduleBlock> existingBlockList) { DateTime endDate = this.RuleEndDate ?? DateTime.Now.AddMonths(20); endDate = endDate > end ? end : endDate; DateTime startDate = this.RuleStartDate > start ? this.RuleStartDate.Date : start.Date; // Step 1. Build SortedDictionary <long, ScheduleBlock> directory = new SortedDictionary <long, ScheduleBlock>(); DateTime date = startDate.Date.AddHours(this.StartHour ?? 0).AddMinutes(this.StartMinute ?? 0); while (date <= endDate) { if (this.IsInstance(date)) { var sb = new ScheduleBlock() { IsAllDay = false, EmployeeId = this.EmployeeId, StartYear = date.Year, StartMonth = date.Month, StartDay = date.Day, StartHour = date.Hour, StartMinute = date.Minute, EndYear = date.Year, EndMonth = date.Month, EndDay = date.Day, EndHour = this.EndHour ?? 0, EndMinute = this.EndMinute ?? 0, ScheduleRuleId = this.Id }; directory.Add(sb.StartKey, sb); } date = date.AddDays(1); } // Step 2. Replace w/ existing if (existingBlockList == null) { existingBlockList = new List <ScheduleBlock>(); } foreach (var item in existingBlockList) { if (this.IsInstance(new DateTime(item.StartYear, item.StartMonth, item.StartDay, item.StartHour, item.StartMinute, 0))) { directory[item.StartKey] = item; } } return(directory); }
public ScheduleBlock CreateScheduleBlock(DateTime date) { DateTime startTime = this.StartTime.Value; ScheduleBlock scheduleBlock = new ScheduleBlock() { IsAllDay = this.IsAllDay, Start = new DateTime(date.Year, date.Month, date.Day, startTime.Hour, startTime.Minute, 0), End = startTime.AddMinutes(this.Duration.Value), ProviderId = this.ProviderId, ScheduleRuleId = this.Id, Saved = null }; this.ScheduleBlockCollection.Add(scheduleBlock); return(scheduleBlock); }
public bool TryGetDate(DateTime date, out ScheduleBlock scheduleBlock) { List <ScheduleBlock> existingList = this.ScheduleBlockCollection.ToList(); scheduleBlock = existingList.Find(x => x.Start.Date == date); if (scheduleBlock != null) { return(true); } if (!this.ValidDate(date)) { return(false); } scheduleBlock = this.CreateScheduleBlock(date); return(true); }