private static void OptimizeOnePeriod(Period period, List <Period> newPeriods, List <Period> zones) { if (period.Duration().Hours > 23) { newPeriods.Add(period); return; } Period zone = zones.First(x => x.Distance(period).Equals(zones.Min(g => g.Distance(period)))); //zone = zones[0]; if (period.Duration() < zone.Duration()) { period.MoveTo(zone.Start); } else { period.MoveTo(zone.Start.Add(zone.Duration() - period.Duration())); } int offset = 5; while (newPeriods.Exists(x => x.IsIntersect(period))) { period.MoveTo(period.Start.AddMinutes(offset)); if (period.Start.Day != period.End.Day) { if (offset < 0) { break; } offset = -5; } } newPeriods.Add(period); if (Period.ToTimeSpan(zone.Start) > Period.ToTimeSpan(period.End)) { period.MoveTo(zone.Start); } //else if (Period.ToTimeSpan(zone.End) < Period.ToTimeSpan(period.Start)) // period.MoveTo(zone.Start); }