// ---------------------------------------------------------------------- public DateTime AddBusinessDays2( DateTime startDate, int noOfBusinessDays ) { CalendarDateAdd dateAdd = new CalendarDateAdd(); dateAdd.AddWorkingWeekDays(); DateTime? targetDate = dateAdd.Add( startDate.Date.AddTicks( 1 ), new TimeSpan( noOfBusinessDays, 0, 0, 0, 0 ) ); return targetDate.HasValue ? targetDate.Value.Date : startDate.Date; }
// ---------------------------------------------------------------------- public DateTime AddBusinessDays3( DateTime startDate, int noOfBusinessDays ) { TimeCalendar timeCalendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ); CalendarDateAdd dateAdd = new CalendarDateAdd( timeCalendar ); dateAdd.AddWorkingWeekDays(); // Exclude holidays (simplified from the actual production code, here reduced to one hardcoded day) dateAdd.ExcludePeriods.Add( new Day( 2012, 6, 22, timeCalendar ) ); DateTime? targetDate = dateAdd.Add( startDate.Date.AddTicks( 1 ), new TimeSpan( noOfBusinessDays, 0, 0, 0, 0 ) ); return targetDate.HasValue ? targetDate.Value.Date : startDate.Date; }
public void CalendarDateAdd3Test() { CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.AddWorkingWeekDays(); calendarDateAdd.ExcludePeriods.Add( new Day( 2011, 4, 4, calendarDateAdd.Calendar ) ); calendarDateAdd.WorkingHours.Add( new HourRange( new Time( 8, 30 ), new Time( 12 ) ) ); calendarDateAdd.WorkingHours.Add( new HourRange( new Time( 13, 30 ), new Time( 18 ) ) ); DateTime start = new DateTime( 2011, 4, 1, 9, 0, 0 ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 03, 0, 0 ) ), new DateTime( 2011, 4, 1, 13, 30, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 04, 0, 0 ) ), new DateTime( 2011, 4, 1, 14, 30, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 08, 0, 0 ) ), new DateTime( 2011, 4, 5, 09, 00, 0 ) ); }
public void CalendarDateAdd1Test() { TimeCalendar timeCalendar = new TimeCalendar( new TimeCalendarConfig { Culture = new CultureInfo( "en-AU" ), EndOffset = TimeSpan.Zero } ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd( timeCalendar ); calendarDateAdd.AddWorkingWeekDays(); calendarDateAdd.ExcludePeriods.Add( new Day( 2011, 4, 4, calendarDateAdd.Calendar ) ); calendarDateAdd.WorkingHours.Add( new HourRange( 8, 18 ) ); DateTime start = new DateTime( 2011, 4, 1, 9, 0, 0 ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 22, 0, 0 ) ), new DateTime( 2011, 4, 6, 11, 0, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 29, 0, 0 ) ), new DateTime( 2011, 4, 7, 8, 0, 0 ) ); }
public void CalendarDateAddSample() { CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); // weekdays calendarDateAdd.AddWorkingWeekDays(); // holidays calendarDateAdd.ExcludePeriods.Add( new Day( 2011, 4, 5, calendarDateAdd.Calendar ) ); // working hours calendarDateAdd.WorkingHours.Add( new HourRange( new Time( 08, 30 ), new Time( 12 ) ) ); calendarDateAdd.WorkingHours.Add( new HourRange( new Time( 13, 30 ), new Time( 18 ) ) ); DateTime start = new DateTime( 2011, 4, 1, 9, 0, 0 ); TimeSpan offset = new TimeSpan( 22, 0, 0 ); // 22 hours DateTime? end = calendarDateAdd.Add( start, offset ); Console.WriteLine( "start: {0}", start ); // > start: 01.04.2011 09:00:00 Console.WriteLine( "offset: {0}", offset ); // > offset: 22:00:00 Console.WriteLine( "end: {0}", end ); // > end: 06.04.2011 16:30:00 }
public void WorkingDayHoursTest() { CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.AddWorkingWeekDays(); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Monday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Tuesday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Wednesday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Thursday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Friday, 09, 13 ) ); DateTime start = new DateTime( 2011, 08, 15 ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 00, 0, 0 ) ), new DateTime( 2011, 8, 15, 09, 0, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 07, 0, 0 ) ), new DateTime( 2011, 8, 16, 09, 0, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 28, 0, 0 ) ), new DateTime( 2011, 8, 19, 09, 0, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 31, 0, 0 ) ), new DateTime( 2011, 8, 19, 12, 0, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 32, 0, 0 ) ), new DateTime( 2011, 8, 22, 09, 0, 0 ) ); }
public void PeriodLimitsSubtractTest() { DateTime test = new DateTime( 2011, 4, 30 ); TimeRange timeRange1 = new TimeRange( new DateTime( 2011, 4, 20 ), new DateTime( 2011, 4, 25 ) ); TimeRange timeRange2 = new TimeRange( DateTime.MinValue, new DateTime( 2011, 4, 10 ) ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.ExcludePeriods.Add( timeRange1 ); calendarDateAdd.ExcludePeriods.Add( timeRange2 ); Assert.AreEqual( calendarDateAdd.Subtract( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange1.Start ); Assert.IsNull( calendarDateAdd.Subtract( test, new TimeSpan( 20, 0, 0, 0 ) ) ); }
public void PeriodLimitsAddTest() { DateTime test = new DateTime( 2011, 4, 12 ); TimeRange timeRange1 = new TimeRange( new DateTime( 2011, 4, 20 ), new DateTime( 2011, 4, 25 ) ); TimeRange timeRange2 = new TimeRange( new DateTime( 2011, 4, 30 ), DateTime.MaxValue ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.ExcludePeriods.Add( timeRange1 ); calendarDateAdd.ExcludePeriods.Add( timeRange2 ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 8, 0, 0, 0 ) ), timeRange1.End ); Assert.IsNull( calendarDateAdd.Add( test, new TimeSpan( 20, 0, 0, 0 ) ) ); }
public void NoPeriodsTest() { DateTime test = new DateTime( 2011, 4, 12 ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); Assert.AreEqual( calendarDateAdd.Add( test, TimeSpan.Zero ), test ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 1, 0, 0, 0 ) ), test.Add( new TimeSpan( 1, 0, 0, 0 ) ) ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( -1, 0, 0, 0 ) ), test.Add( new TimeSpan( -1, 0, 0, 0 ) ) ); Assert.AreEqual( calendarDateAdd.Subtract( test, new TimeSpan( 1, 0, 0, 0 ) ), test.Subtract( new TimeSpan( 1, 0, 0, 0 ) ) ); Assert.AreEqual( calendarDateAdd.Subtract( test, new TimeSpan( -1, 0, 0, 0 ) ), test.Subtract( new TimeSpan( -1, 0, 0, 0 ) ) ); }
public void ExcludeTest() { DateTime test = new DateTime( 2011, 4, 12 ); TimeRange timeRange = new TimeRange( new DateTime( 2011, 4, 15 ), new DateTime( 2011, 4, 20 ) ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.ExcludePeriods.Add( timeRange ); Assert.AreEqual( calendarDateAdd.Add( test, TimeSpan.Zero ), test ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 2, 0, 0, 0 ) ), test.Add( new TimeSpan( 2, 0, 0, 0 ) ) ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 3, 0, 0, 0 ) ), timeRange.End ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 3, 0, 0, 0, 1 ) ), timeRange.End.Add( new TimeSpan( 0, 0, 0, 0, 1 ) ) ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange.End.Add( new TimeSpan( 2, 0, 0, 0 ) ) ); }
public void ExcludeSplitTest() { DateTime test = new DateTime( 2011, 4, 12 ); TimeRange timeRange1 = new TimeRange( new DateTime( 2011, 4, 15 ), new DateTime( 2011, 4, 20 ) ); TimeRange timeRange2 = new TimeRange( new DateTime( 2011, 4, 22 ), new DateTime( 2011, 4, 25 ) ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.ExcludePeriods.Add( timeRange1 ); calendarDateAdd.ExcludePeriods.Add( timeRange2 ); Assert.AreEqual( calendarDateAdd.Add( test, TimeSpan.Zero ), test ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 2, 0, 0, 0 ) ), test.Add( new TimeSpan( 2, 0, 0, 0 ) ) ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 3, 0, 0, 0 ) ), timeRange1.End ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 4, 0, 0, 0 ) ), timeRange1.End.Add( new TimeSpan( 1, 0, 0, 0 ) ) ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 5, 0, 0, 0 ) ), timeRange2.End ); Assert.AreEqual( calendarDateAdd.Add( test, new TimeSpan( 7, 0, 0, 0 ) ), timeRange2.End.Add( new TimeSpan( 2, 0, 0, 0 ) ) ); }
public void EmptyStartWeekTest() { CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); // weekdays calendarDateAdd.AddWorkingWeekDays(); //Start on a Saturday DateTime start = new DateTime( 2011, 4, 2, 13, 0, 0 ); TimeSpan offset = new TimeSpan( 20, 0, 0 ); // 20 hours Assert.AreEqual( calendarDateAdd.Add( start, offset ), new DateTime( 2011, 4, 4, 20, 00, 0 ) ); }
/// <summary> /// The get due date. /// </summary> /// <param name="start"> /// The start. /// </param> /// <param name="workHours"> /// The work hours. /// </param> /// <returns> /// The <see cref="DateTime"/>. /// </returns> public DateTime GetDueDate(DateTime start, TimeSpan workHours) { var calendar = new CalendarDateAdd(); foreach (var dayOfWeek in this.WorkDays) { calendar.WeekDays.Add(dayOfWeek.DayOfWeek); calendar.WorkingHours.Add(new DayHourRange(dayOfWeek.DayOfWeek, new Time(dayOfWeek.StartTime), new Time(dayOfWeek.EndTime))); } foreach (var holiday in this.Holidays) { calendar.ExcludePeriods.Add(new TimeBlock(holiday.StartTime, holiday.EndTime)); } return calendar.Add(start, workHours) ?? DateTime.MinValue; }
public void AddWeekDaySample() { CalendarDateAdd calendarDateAdd = new CalendarDateAdd(); calendarDateAdd.AddWorkingWeekDays(); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Monday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Tuesday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Wednesday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Thursday, 09, 16 ) ); calendarDateAdd.WorkingDayHours.Add( new DayHourRange( DayOfWeek.Friday, 09, 13 ) ); DateTime start = new DateTime( 2011, 08, 15 ); Console.WriteLine( "AddWeekDaySample : {0}", calendarDateAdd.Add( start, new TimeSpan( 00, 0, 0 ) ) ); // > AddWeekDaySample : 15.08.2011 09:00:00 Console.WriteLine( "AddWeekDaySample : {0}", calendarDateAdd.Add( start, new TimeSpan( 07, 0, 0 ) ) ); // > AddWeekDaySample : 16.08.2011 09:00:00 Console.WriteLine( "AddWeekDaySample : {0}", calendarDateAdd.Add( start, new TimeSpan( 28, 0, 0 ) ) ); // > AddWeekDaySample : 19.08.2011 09:00:00 Console.WriteLine( "AddWeekDaySample : {0}", calendarDateAdd.Add( start, new TimeSpan( 31, 0, 0 ) ) ); // > AddWeekDaySample : 19.08.2011 12:00:00 Console.WriteLine( "AddWeekDaySample : {0}", calendarDateAdd.Add( start, new TimeSpan( 32, 0, 0 ) ) ); // > AddWeekDaySample : 22.08.2011 09:00:00 }