/// <summary> /// Recalculates the dates of the task based on the start dates of its /// dependencies. The earliest date that the task will use as its start date /// (in the situation where it has no dependencies, for example) is the value /// passed as earliestDate. /// </summary> /// <param name="earliestDate">The earliest date that the task can use as its start /// date.</param> /// <param name="week">The working week to base calculations on.</param> public void RecalculateDates(DateTime earliestDate, Week week) { var latestDate = earliestDate; // Get a list of all dependencies that affect this task. // That includes all dependencies that this task has and // that its parents have. var allDependencies = new List <IDependency>(); ITask task = this; while (task != null) { foreach (var dependency in task.Dependencies) { allDependencies.Add(dependency); } task = task.Parent; } foreach (var dependency in allDependencies) { var dependencyDate = dependency.StartDate(week); // Only update the date if the dependency starts later than the // currently calculated date if (dependencyDate > latestDate) { latestDate = dependencyDate; } } // Ensure that the start date falls within a working shift mStartDate = week.AscendingShifts(latestDate, DateTime.MaxValue).ElementAt(0).StartTime; // Work out end date using working week. We subtract 1 from the duration // to ensure that, should the end date be at the end of a working shift, // we retrieve the correct shift from the working week later mEndDate = week.DateAdd(mStartDate, mDuration.Subtract(new TimeSpan(0, 0, 0, 0, 1))); // Ensure that the end date falls within a working shift mEndDate = week.AscendingShifts(mEndDate, DateTime.MaxValue).ElementAt(0).StartTime; // Add the millisecond back on to the date that we subtracted earlier mEndDate = mEndDate.Add(new TimeSpan(0, 0, 0, 0, 1)); }
static void Main(string[] args) { Week week = new Week(); week.AddShift(DayOfWeek.Monday, 9, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Monday, 12, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Monday, 15, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Tuesday, 9, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Tuesday, 12, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Tuesday, 15, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Wednesday, 9, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Wednesday, 12, 30, 0, 0, TimeSpan.FromHours(2)); week.AddShift(DayOfWeek.Wednesday, 15, 30, 0, 0, TimeSpan.FromHours(2)); week.RemoveShift(DayOfWeek.Tuesday, 12, 30, 0, 0); foreach (Shift shift in week.AscendingShifts(new DateTime(2010, 6, 28, 0, 0, 0), new DateTime(2010, 6, 30, 0, 0, 0))) { System.Diagnostics.Debug.WriteLine(shift.StartTime); } foreach (Shift shift in week.DescendingShifts(new DateTime(2010, 6, 30, 0, 0, 0), new DateTime(2010, 6, 28, 0, 0, 0))) { System.Diagnostics.Debug.WriteLine(shift.StartTime); } System.Diagnostics.Debug.WriteLine(""); System.Diagnostics.Debug.WriteLine(week.DateAdd(new DateTime(2010, 6, 30, 0, 0, 0), new TimeSpan(29, 2, 3, 0))); System.Diagnostics.Debug.WriteLine(""); System.Diagnostics.Debug.WriteLine(week.DateAdd(new DateTime(2010, 6, 30, 0, 0, 0), new TimeSpan(-29, 2, 0, 0))); System.Diagnostics.Debug.WriteLine(""); System.Diagnostics.Debug.WriteLine(week.DateDiff(new DateTime(2010, 6, 30, 0, 0, 0), new DateTime(2010, 7, 1, 0, 0, 0))); System.Diagnostics.Debug.WriteLine(""); System.Diagnostics.Debug.WriteLine(week.DateDiff(new DateTime(2010, 7, 1, 0, 0, 0), new DateTime(2010, 6, 30, 0, 0, 0))); System.Diagnostics.Debug.WriteLine(""); System.Diagnostics.Debug.WriteLine(week.Duration); }