public JsonResult Availabilities(int hotelId, IEnumerable <int> roomTypeIds, DateTime start, DateTime end, DateTime?startTime, DateTime?endTime, IEnumerable <Query> queries) { var startDT = start.MergeTime(startTime); var endDT = end.MergeTime(endTime); var manager = this.GetManagerFor <IRoomBookingManager>(); var results = manager.Search(hotelId, roomTypeIds, startDT, endDT).ToList(); var periods = new TimeRange(startDT, endDT, true); var collector = new CalendarPeriodCollector(new CalendarPeriodCollectorFilter(), periods); collector.CollectDays(); var days = collector.Periods.Select(d => d.Start).ToList(); days.Add(endDT); days.Insert(0, startDT); var tManager = this.GetManagerFor <IRoomTypeManager>(); var roomTypes = tManager.GetByIds(roomTypeIds).ToList(); var resultDTOs = results.Select(t => new RoomBookingDTO(queries, t)); var json = new AvailabilityResultDTO { DTSlots = days, RoomTypes = roomTypes.Select(t => new RoomTypeDTO { Id = t.Id, Name = t.Name, Rooms = t.Rooms.Where(r => r.HotelId == hotelId).Select(r => new RoomDTO(r)).ToList() }).ToList(), Bookings = resultDTOs }; return(Json(json)); }
public void CalendarPeriodCollectorSample() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); // only Januaries filter.WeekDays.Add( DayOfWeek.Friday ); // only Fridays filter.CollectingHours.Add( new HourRange( 8, 18 ) ); // working hours CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); Console.WriteLine( "Calendar period collector of period: " + testPeriod ); // > Calendar period collector of period: 01.01.2010 00:00:00 - 30.12.2011 23:59:59 | 728.23:59 CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectHours(); foreach ( ITimePeriod period in collector.Periods ) { Console.WriteLine( "Period: " + period ); } // > Period: 01.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 08.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 15.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 22.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 29.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 07.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 14.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 21.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 28.01.2011; 08:00 - 17:59 | 0.09:59 }
public void CollectDaysTest() { var filter = new CalendarPeriodCollectorFilter(); //! 1월의 금요일만 추출 filter.Months.Add(January); filter.WeekDays.Add(DayOfWeek.Friday); var testPeriod = new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31)); var collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectDays(); if(IsDebugEnabled) { foreach(var period in collector.Periods) log.Debug("CollectDays... Period=" + period); } collector.Periods.Count.Should().Be(9); collector.Periods[0].IsSamePeriod(new DayRange(new DateTime(2010, 1, 1))).Should().Be.True(); collector.Periods[1].IsSamePeriod(new DayRange(new DateTime(2010, 1, 8))).Should().Be.True(); collector.Periods[2].IsSamePeriod(new DayRange(new DateTime(2010, 1, 15))).Should().Be.True(); collector.Periods[3].IsSamePeriod(new DayRange(new DateTime(2010, 1, 22))).Should().Be.True(); collector.Periods[4].IsSamePeriod(new DayRange(new DateTime(2010, 1, 29))).Should().Be.True(); collector.Periods[5].IsSamePeriod(new DayRange(new DateTime(2011, 1, 7))).Should().Be.True(); collector.Periods[6].IsSamePeriod(new DayRange(new DateTime(2011, 1, 14))).Should().Be.True(); collector.Periods[7].IsSamePeriod(new DayRange(new DateTime(2011, 1, 21))).Should().Be.True(); collector.Periods[8].IsSamePeriod(new DayRange(new DateTime(2011, 1, 28))).Should().Be.True(); }
public void CollectAllDayHoursTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(YearMonth.January); filter.WeekDays.Add(DayOfWeek.Friday); filter.CollectingHours.Add(new HourRange(0, 24)); TimeRange testPeriod = new TimeRange(new DateTime(2010, 1, 1), new DateTime(2012, 1, 1)); TimeCalendar calendar = new TimeCalendar(new TimeCalendarConfig { EndOffset = TimeSpan.Zero }); CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod, SeekDirection.Forward, calendar); collector.CollectHours(); Assert.Equal(9, collector.Periods.Count); Assert.True(collector.Periods[0].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 01), new DateTime(2010, 1, 02)))); Assert.True(collector.Periods[1].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 08), new DateTime(2010, 1, 09)))); Assert.True(collector.Periods[2].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 15), new DateTime(2010, 1, 16)))); Assert.True(collector.Periods[3].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 22), new DateTime(2010, 1, 23)))); Assert.True(collector.Periods[4].IsSamePeriod(new TimeRange(new DateTime(2010, 1, 29), new DateTime(2010, 1, 30)))); Assert.True(collector.Periods[5].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 07), new DateTime(2011, 1, 08)))); Assert.True(collector.Periods[6].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 14), new DateTime(2011, 1, 15)))); Assert.True(collector.Periods[7].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 21), new DateTime(2011, 1, 22)))); Assert.True(collector.Periods[8].IsSamePeriod(new TimeRange(new DateTime(2011, 1, 28), new DateTime(2011, 1, 29)))); } // CollectAllDayHoursTest
public void CollectExcludePeriodTest() { const int workingDays2011 = 365 - 2 - ( 51 * 2 ) - 1; const int workingDaysMarch2011 = 31 - 8; // total days - weekend days Year year2011 = new Year( 2011 ); CalendarPeriodCollectorFilter filter1 = new CalendarPeriodCollectorFilter(); filter1.AddWorkingWeekDays(); CalendarPeriodCollector collector1 = new CalendarPeriodCollector( filter1, year2011 ); collector1.CollectDays(); Assert.AreEqual( collector1.Periods.Count, workingDays2011 ); // exclude month CalendarPeriodCollectorFilter filter2 = new CalendarPeriodCollectorFilter(); filter2.AddWorkingWeekDays(); filter2.ExcludePeriods.Add( new Month( 2011, YearMonth.March ) ); CalendarPeriodCollector collector2 = new CalendarPeriodCollector( filter2, year2011 ); collector2.CollectDays(); Assert.AreEqual( collector2.Periods.Count, workingDays2011 - workingDaysMarch2011 ); // exclude weeks (holidays) CalendarPeriodCollectorFilter filter3 = new CalendarPeriodCollectorFilter(); filter3.AddWorkingWeekDays(); filter3.ExcludePeriods.Add( new Month( 2011, YearMonth.March ) ); filter3.ExcludePeriods.Add( new Weeks( 2011, 26, 2 ) ); CalendarPeriodCollector collector3 = new CalendarPeriodCollector( filter3, year2011 ); collector3.CollectDays(); Assert.AreEqual( collector3.Periods.Count, workingDays2011 - workingDaysMarch2011 - 10 ); }
// ---------------------------------------------------------------------- public void CalendarPeriodCollectorSample() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(YearMonth.January); // only Januaries filter.WeekDays.Add(DayOfWeek.Friday); // only Fridays filter.CollectingHours.Add(new HourRange(8, 18)); // working hours CalendarTimeRange testPeriod = new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31)); Console.WriteLine("Calendar period collector of period: " + testPeriod); // > Calendar period collector of period: 01.01.2010 00:00:00 - 30.12.2011 23:59:59 | 728.23:59 CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectHours(); foreach (ITimePeriod period in collector.Periods) { Console.WriteLine("Period: " + period); } // > Period: 01.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 08.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 15.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 22.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 29.01.2010; 08:00 - 17:59 | 0.09:59 // > Period: 07.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 14.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 21.01.2011; 08:00 - 17:59 | 0.09:59 // > Period: 28.01.2011; 08:00 - 17:59 | 0.09:59 } // CalendarPeriodCollectorSample
public void CollectHoursTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(YearMonth.January); filter.WeekDays.Add(DayOfWeek.Friday); filter.CollectingHours.Add(new HourRange(8, 18)); CalendarTimeRange testPeriod = new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31)); CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectHours(); Assert.AreEqual(collector.Periods.Count, 9); Assert.IsTrue(collector.Periods[0].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 01, 8, 0, 0), new DateTime(2010, 1, 01, 18, 0, 0)))); Assert.IsTrue(collector.Periods[1].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 08, 8, 0, 0), new DateTime(2010, 1, 08, 18, 0, 0)))); Assert.IsTrue(collector.Periods[2].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 15, 8, 0, 0), new DateTime(2010, 1, 15, 18, 0, 0)))); Assert.IsTrue(collector.Periods[3].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 22, 8, 0, 0), new DateTime(2010, 1, 22, 18, 0, 0)))); Assert.IsTrue(collector.Periods[4].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 29, 8, 0, 0), new DateTime(2010, 1, 29, 18, 0, 0)))); Assert.IsTrue(collector.Periods[5].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 07, 8, 0, 0), new DateTime(2011, 1, 07, 18, 0, 0)))); Assert.IsTrue(collector.Periods[6].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 14, 8, 0, 0), new DateTime(2011, 1, 14, 18, 0, 0)))); Assert.IsTrue(collector.Periods[7].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 21, 8, 0, 0), new DateTime(2011, 1, 21, 18, 0, 0)))); Assert.IsTrue(collector.Periods[8].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 28, 8, 0, 0), new DateTime(2011, 1, 28, 18, 0, 0)))); } // CollectHoursTest
public JsonResult Availabilities(int hotelId, IEnumerable<int> roomTypeIds, DateTime start, DateTime end, DateTime? startTime, DateTime? endTime, IEnumerable<Query> queries) { var startDT = start.MergeTime(startTime); var endDT = end.MergeTime(endTime); var manager = this.GetManagerFor<IRoomBookingManager>(); var results = manager.Search(hotelId, roomTypeIds, startDT, endDT).ToList(); var periods = new TimeRange(startDT, endDT, true); var collector = new CalendarPeriodCollector(new CalendarPeriodCollectorFilter(), periods); collector.CollectDays(); var days = collector.Periods.Select(d => d.Start).ToList(); days.Add(endDT); days.Insert(0, startDT); var tManager = this.GetManagerFor<IRoomTypeManager>(); var roomTypes = tManager.GetByIds(roomTypeIds).ToList(); var resultDTOs = results.Select(t => new RoomBookingDTO(queries, t)); var json = new AvailabilityResultDTO { DTSlots = days, RoomTypes = roomTypes.Select(t => new RoomTypeDTO { Id = t.Id, Name = t.Name, Rooms = t.Rooms.Where(r => r.HotelId == hotelId).Select(r => new RoomDTO(r)).ToList() }).ToList(), Bookings = resultDTOs }; return Json(json); }
public void Excluded_slot() { var filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); filter.CollectingHours.Add(new HourRange(8, 12)); // working hours filter.CollectingHours.Add(new HourRange(14, 18)); // working hours filter.ExcludePeriods.Add(new CalendarTimeRange( DateTime.Parse("2014-07-15T09:30"), DateTime.Parse("2014-07-15T10:30") )); var testPeriod = new CalendarTimeRange(new DateTime(2014, 7, 14), new DateTime(2014, 7, 31)); Console.WriteLine("Calendar period collector of period: " + testPeriod); var collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectHours(); foreach (ITimePeriod period in collector.Periods) { Console.WriteLine("Period: " + period); } Assert.IsFalse(collector.Periods.Any(x => x.HasInside(DateTime.Parse("2014-07-15T10:00")))); Assert.IsTrue(collector.Periods.Any(x => x.HasInside(DateTime.Parse("2014-07-15T09:00")))); Assert.IsTrue(collector.Periods.Any(x => x.HasInside(DateTime.Parse("2014-07-15T11:00")))); }
public void CollectMonthsTest() { var filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(January); var testPeriod = new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31)); var collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectMonths(); if(IsDebugEnabled) log.Debug("CollectMonths... Period=" + collector.Periods.CollectionToString()); collector.Periods.Count.Should().Be(2); collector.Periods[0].IsSamePeriod(new MonthRange(2010, January)).Should().Be.True(); collector.Periods[1].IsSamePeriod(new MonthRange(2011, January)).Should().Be.True(); }
public void CollectMonthsTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(YearMonth.January); CalendarTimeRange testPeriod = new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31)); CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectMonths(); Assert.AreEqual(collector.Periods.Count, 2); Assert.IsTrue(collector.Periods[0].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2010, 2, 1)))); Assert.IsTrue(collector.Periods[1].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 1), new DateTime(2011, 2, 1)))); } // CollectMonthsTest
public void CollectHoursMissingLastPeriodTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(YearMonth.September); filter.WeekDays.Add(DayOfWeek.Monday); filter.WeekDays.Add(DayOfWeek.Tuesday); filter.WeekDays.Add(DayOfWeek.Wednesday); filter.WeekDays.Add(DayOfWeek.Thursday); filter.WeekDays.Add(DayOfWeek.Friday); filter.CollectingHours.Add(new HourRange(9, 17)); // working hours filter.ExcludePeriods.Add(new TimeBlock(new DateTime(2015, 9, 15, 00, 0, 0), new DateTime(2015, 9, 16, 0, 0, 0))); CalendarTimeRange testPeriod = new CalendarTimeRange(new DateTime(2015, 9, 14, 9, 0, 0), new DateTime(2015, 9, 17, 18, 0, 0)); CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectHours(); Assert.Equal(3, collector.Periods.Count); } // CollectHoursMissingLastPeriodTest
public void CollectYearsTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Years.Add(2006); filter.Years.Add(2007); filter.Years.Add(2012); CalendarTimeRange testPeriod = new CalendarTimeRange(new DateTime(2001, 1, 1), new DateTime(2019, 12, 31)); CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectYears(); Assert.AreEqual(collector.Periods.Count, 3); Assert.IsTrue(collector.Periods[0].IsSamePeriod(new CalendarTimeRange(new DateTime(2006, 1, 1), new DateTime(2007, 1, 1)))); Assert.IsTrue(collector.Periods[1].IsSamePeriod(new CalendarTimeRange(new DateTime(2007, 1, 1), new DateTime(2008, 1, 1)))); Assert.IsTrue(collector.Periods[2].IsSamePeriod(new CalendarTimeRange(new DateTime(2012, 1, 1), new DateTime(2013, 1, 1)))); } // CollectYearsTest
public void CollectYearsTest() { var filter = new CalendarPeriodCollectorFilter(); filter.Years.Add(2006); filter.Years.Add(2007); filter.Years.Add(2012); var testPeriod = new CalendarTimeRange(new DateTime(2001, 1, 1), new DateTime(2019, 12, 31)); var collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectYears(); if(IsDebugEnabled) log.Debug("CollectYears... Period=" + collector.Periods.CollectionToString()); collector.Periods.Count.Should().Be(filter.Years.Count); for(var i = 0; i < collector.Periods.Count; i++) collector.Periods[i].IsSamePeriod(new YearRange(filter.Years.ElementAt(i))); }
public static double NetworkDays(DateTime start, DateTime end, IEnumerable <DateTime> holidays = null, ITimePeriodCollection holidayPeriods = null) { var startDay = new Day(start < end ? start : end); var endDay = new Day(end > start ? end : start); if (startDay.Equals(endDay)) { return(0); } var filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); if (holidays != null) { foreach (DateTime holiday in holidays) { filter.ExcludePeriods.Add(new Day(holiday)); } } if (holidayPeriods != null) { filter.ExcludePeriods.AddAll(holidayPeriods); } var testPeriod = new CalendarTimeRange(start, end); var collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectDays(); var networkDays = 0.0d; foreach (ICalendarTimeRange period in collector.Periods) { networkDays += Math.Round(period.Duration.TotalDays, 2); } return(networkDays); }
/// <summary> /// The get remaining working time. /// </summary> /// <param name="start"> /// The start. /// </param> /// <param name="dueDate"> /// The due date. /// </param> /// <returns> /// The <see cref="TimeSpan"/>. /// </returns> public TimeSpan GetRemainingWorkingTime(DateTime start, DateTime dueDate) { var filter = new CalendarPeriodCollectorFilter(); foreach (var dayOfWeek in this.WorkDays) { filter.CollectingDayHours.Add(new DayHourRange(dayOfWeek.DayOfWeek, new Time(dayOfWeek.StartTime), new Time(dayOfWeek.EndTime))); } foreach (var holiday in this.Holidays) { filter.ExcludePeriods.Add(new TimeBlock(holiday.StartTime, holiday.EndTime)); } var range = new CalendarTimeRange(start, dueDate); var collector = new CalendarPeriodCollector(filter, range); collector.CollectHours(); var duration = collector.Periods.GetTotalDuration(new TimeZoneDurationProvider(TimeZoneInfo.FindSystemTimeZoneById("UTC"))); return duration; //var rounded = Math.Round(duration.TotalMinutes, MidpointRounding.AwayFromZero); //return TimeSpan.FromMinutes(rounded); }
public void CollectDaysTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); filter.WeekDays.Add( DayOfWeek.Friday ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectDays(); Assert.AreEqual( collector.Periods.Count, 9 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 01 ), new DateTime( 2010, 1, 02 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 08 ), new DateTime( 2010, 1, 09 ) ) ) ); Assert.IsTrue( collector.Periods[ 2 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 15 ), new DateTime( 2010, 1, 16 ) ) ) ); Assert.IsTrue( collector.Periods[ 3 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 22 ), new DateTime( 2010, 1, 23 ) ) ) ); Assert.IsTrue( collector.Periods[ 4 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 29 ), new DateTime( 2010, 1, 30 ) ) ) ); Assert.IsTrue( collector.Periods[ 5 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 07 ), new DateTime( 2011, 1, 08 ) ) ) ); Assert.IsTrue( collector.Periods[ 6 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 14 ), new DateTime( 2011, 1, 15 ) ) ) ); Assert.IsTrue( collector.Periods[ 7 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 21 ), new DateTime( 2011, 1, 22 ) ) ) ); Assert.IsTrue( collector.Periods[ 8 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 28 ), new DateTime( 2011, 1, 29 ) ) ) ); }
public void CollectExcludePeriodTest() { const int workingDays2011 = 365 - 2 - (51 * 2) - 1; const int workingDaysMarch2011 = 31 - 8; // total days - weekend days Year year2011 = new Year(2011); CalendarPeriodCollectorFilter filter1 = new CalendarPeriodCollectorFilter(); filter1.AddWorkingWeekDays(); CalendarPeriodCollector collector1 = new CalendarPeriodCollector(filter1, year2011); collector1.CollectDays(); Assert.AreEqual(collector1.Periods.Count, workingDays2011); // exclude month CalendarPeriodCollectorFilter filter2 = new CalendarPeriodCollectorFilter(); filter2.AddWorkingWeekDays(); filter2.ExcludePeriods.Add(new Month(2011, YearMonth.March)); CalendarPeriodCollector collector2 = new CalendarPeriodCollector(filter2, year2011); collector2.CollectDays(); Assert.AreEqual(collector2.Periods.Count, workingDays2011 - workingDaysMarch2011); // exclude weeks (holidays) CalendarPeriodCollectorFilter filter3 = new CalendarPeriodCollectorFilter(); filter3.AddWorkingWeekDays(); filter3.ExcludePeriods.Add(new Month(2011, YearMonth.March)); filter3.ExcludePeriods.Add(new Weeks(2011, 26, 2)); CalendarPeriodCollector collector3 = new CalendarPeriodCollector(filter3, year2011); collector3.CollectDays(); Assert.AreEqual(collector3.Periods.Count, workingDays2011 - workingDaysMarch2011 - 10); } // CollectExcludePeriodTest
public void CollectDaysTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(YearMonth.January); filter.WeekDays.Add(DayOfWeek.Friday); CalendarTimeRange testPeriod = new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31)); CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectDays(); Assert.AreEqual(collector.Periods.Count, 9); Assert.IsTrue(collector.Periods[0].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 01), new DateTime(2010, 1, 02)))); Assert.IsTrue(collector.Periods[1].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 08), new DateTime(2010, 1, 09)))); Assert.IsTrue(collector.Periods[2].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 15), new DateTime(2010, 1, 16)))); Assert.IsTrue(collector.Periods[3].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 22), new DateTime(2010, 1, 23)))); Assert.IsTrue(collector.Periods[4].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 29), new DateTime(2010, 1, 30)))); Assert.IsTrue(collector.Periods[5].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 07), new DateTime(2011, 1, 08)))); Assert.IsTrue(collector.Periods[6].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 14), new DateTime(2011, 1, 15)))); Assert.IsTrue(collector.Periods[7].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 21), new DateTime(2011, 1, 22)))); Assert.IsTrue(collector.Periods[8].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 28), new DateTime(2011, 1, 29)))); } // CollectDaysTest
// ---------------------------------------------------------------------- public TimeSpan CalcWorkingPeriod( DateTime start, DateTime end, ITimePeriodCollection excludePeriods = null) { if ( start.Equals( end ) ) { return TimeSpan.Zero; } // test range TimeRange testRange = new TimeRange( start, end ); // search range DateTime searchStart = new Day( testRange.Start ).Start; DateTime serachEnd = new Day( testRange.End ).GetNextDay().Start; TimeRange searchPeriod = new TimeRange( searchStart, serachEnd ); // search filter CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); // working days if ( excludePeriods != null ) { filter.ExcludePeriods.AddAll( excludePeriods ); } filter.CollectingHours.Add( new HourRange( 07, 12 ) ); // working hours filter.CollectingHours.Add( new HourRange( 13, 19 ) ); // working hours // collect working hours TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, searchPeriod, SeekDirection.Forward, calendar ); collector.CollectHours(); TimeSpan workingPeriod = new TimeSpan(); foreach ( ICalendarTimeRange period in collector.Periods ) { // get the intersection of the test-range and the day working-hours ITimePeriod intersection = testRange.GetIntersection( period ); if ( intersection == null ) { continue; } workingPeriod = workingPeriod.Add( intersection.Duration ); } return workingPeriod; }
// ---------------------------------------------------------------------- private double CalculateBusinessHours( ITimePeriod timePeriod ) { double businessHours = 0; // collect filter CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); filter.CollectingHours.Add( new HourRange( 8, 18 ) ); //filter.ExcludePeriods.Add( new Day( 2012, 4, 6 ) ); // mor exclude periods // collect the working hours TimeRange collectorTimeRange = new TimeRange( timePeriod.Start.Date, timePeriod.End.Date.AddDays( 1 ) ); TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, collectorTimeRange, SeekDirection.Forward, calendar ); collector.CollectHours(); if ( collector.Periods.Count > 0 ) { // calculate the business hour periods collector.Periods.Add( timePeriod ); // add source period to find the intersections TimePeriodIntersector<TimeRange> intersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection businessHourPeriods = intersector.IntersectPeriods( collector.Periods ); // collect the business hours foreach ( TimeRange businessHourPeriod in businessHourPeriods ) { businessHours += businessHourPeriod.Duration.TotalHours; } } return businessHours; }
// ---------------------------------------------------------------------- private IEnumerable<ITimePeriod> GetAvailableWeekPeriods( ITimePeriod period ) { if ( weekDays.Count == 0 && workingHours.Count == 0 && WorkingDayHours.Count == 0 ) { return new TimePeriodCollection { period }; } CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); // days foreach ( DayOfWeek weekDay in weekDays ) { filter.WeekDays.Add( weekDay ); } // hours foreach ( HourRange workingHour in workingHours ) { filter.CollectingHours.Add( workingHour ); } // day hours foreach ( DayHourRange workingDayHour in workingDayHours ) { filter.CollectingDayHours.Add( workingDayHour ); } CalendarPeriodCollector weekCollector = new CalendarPeriodCollector( filter, period, SeekDirection.Forward, calendar ); weekCollector.CollectHours(); return weekCollector.Periods; }
// ---------------------------------------------------------------------- public double NetworkDays( DateTime start, DateTime end, IEnumerable<DateTime> holidays = null, ITimePeriodCollection holidayPeriods = null ) { Day startDay = new Day( start < end ? start : end ); Day endDay = new Day( end > start ? end : start ); if ( startDay.Equals( endDay ) ) { return 0; } CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); // only working days if ( holidays != null ) { foreach ( DateTime holiday in holidays ) { filter.ExcludePeriods.Add( new Day( holiday ) ); } } if ( holidayPeriods != null ) { filter.ExcludePeriods.AddAll( holidayPeriods ); } CalendarTimeRange testPeriod = new CalendarTimeRange( start, end ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectDays(); double networkDays = 0.0; foreach ( ICalendarTimeRange period in collector.Periods ) { networkDays += Math.Round( period.Duration.TotalDays, 2 ); } return networkDays; }
public void CollectHoursTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); filter.WeekDays.Add( DayOfWeek.Friday ); filter.CollectingHours.Add( new HourRange( 8, 18 ) ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectHours(); Assert.AreEqual( collector.Periods.Count, 9 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 01, 8, 0, 0 ), new DateTime( 2010, 1, 01, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 08, 8, 0, 0 ), new DateTime( 2010, 1, 08, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 2 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 15, 8, 0, 0 ), new DateTime( 2010, 1, 15, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 3 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 22, 8, 0, 0 ), new DateTime( 2010, 1, 22, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 4 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 29, 8, 0, 0 ), new DateTime( 2010, 1, 29, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 5 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 07, 8, 0, 0 ), new DateTime( 2011, 1, 07, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 6 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 14, 8, 0, 0 ), new DateTime( 2011, 1, 14, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 7 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 21, 8, 0, 0 ), new DateTime( 2011, 1, 21, 18, 0, 0 ) ) ) ); Assert.IsTrue( collector.Periods[ 8 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 28, 8, 0, 0 ), new DateTime( 2011, 1, 28, 18, 0, 0 ) ) ) ); }
// ---------------------------------------------------------------------- public double CalculateBusinessHours( CalendarTimeRange testPeriod, ITimePeriodCollection holidays = null ) { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.CollectingMonths.Add( new MonthRange( YearMonth.January, YearMonth.January ) ); filter.CollectingDays.Add( new DayRange( 1, 1 ) ); filter.AddWorkingWeekDays(); // only working days filter.CollectingHours.Add( new HourRange( 8, 12 ) ); // opening hours morning filter.CollectingHours.Add( new HourRange( 13, 18 ) ); // opening hours afternoon if ( holidays != null ) { filter.ExcludePeriods.AddAll( holidays ); } CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectHours(); double businessHours = 0.0; foreach ( ICalendarTimeRange period in collector.Periods ) { businessHours += Math.Round( period.Duration.TotalHours, 2 ); } return businessHours; }
public void FindRemainigYearFridaysSample() { // filter: only Fridays CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.WeekDays.Add( DayOfWeek.Friday ); // the collecting period CalendarTimeRange collectPeriod = new CalendarTimeRange( DateTime.Now, new Year().End.Date ); // collect all Fridays CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, collectPeriod ); collector.CollectDays(); // show the results foreach ( ITimePeriod period in collector.Periods ) { Console.WriteLine( "Friday: " + period ); } }
public void CollectExcludePeriodTest() { const int workingDays2011 = 365 - 2 - (51 * 2) - 1; const int workingDaysMarch2011 = 31 - 8; // total days - weekend days var year2011 = new YearRange(2011); var filter1 = new CalendarPeriodCollectorFilter(); filter1.AddWorkingWeekDays(); var collector1 = new CalendarPeriodCollector(filter1, year2011); collector1.CollectDays(); collector1.Periods.Count.Should().Be(workingDays2011); // 3월 제외 var filter2 = new CalendarPeriodCollectorFilter(); filter2.AddWorkingWeekDays(); filter2.ExcludePeriods.Add(new MonthRange(2011, March)); var collector2 = new CalendarPeriodCollector(filter2, year2011); collector2.CollectDays(); collector2.Periods.Count.Should().Be(workingDays2011 - workingDaysMarch2011); // 2011년 26주차~27주차 (여름휴가가 2주야!!!) // var filter3 = new CalendarPeriodCollectorFilter(); filter3.AddWorkingWeekDays(); filter3.ExcludePeriods.Add(new MonthRange(2011, March)); filter3.ExcludePeriods.Add(new WeekRangeCollection(2011, 26, 2)); var collector3 = new CalendarPeriodCollector(filter3, year2011); collector3.CollectDays(); collector3.Periods.Count.Should().Be(workingDays2011 - workingDaysMarch2011 - 2 * TimeSpec.WeekDaysPerWeek); }
public void CollectYearsTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Years.Add( 2006 ); filter.Years.Add( 2007 ); filter.Years.Add( 2012 ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2001, 1, 1 ), new DateTime( 2019, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectYears(); Assert.AreEqual( collector.Periods.Count, 3 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2006, 1, 1 ), new DateTime( 2007, 1, 1 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2007, 1, 1 ), new DateTime( 2008, 1, 1 ) ) ) ); Assert.IsTrue( collector.Periods[ 2 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2012, 1, 1 ), new DateTime( 2013, 1, 1 ) ) ) ); }
public void CollectMonthsTest() { CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.Months.Add( YearMonth.January ); CalendarTimeRange testPeriod = new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, testPeriod ); collector.CollectMonths(); Assert.AreEqual( collector.Periods.Count, 2 ); Assert.IsTrue( collector.Periods[ 0 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2010, 2, 1 ) ) ) ); Assert.IsTrue( collector.Periods[ 1 ].IsSamePeriod( new CalendarTimeRange( new DateTime( 2011, 1, 1 ), new DateTime( 2011, 2, 1 ) ) ) ); }
public void WorkingHourSum() { DateTime start = new DateTime( 2011, 9, 11, 18, 0, 0 ); DateTime end = new DateTime( 2011, 9, 15, 9, 0, 0 ); // collect working hours CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Monday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Tuesday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Wednesday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Thursday, 8, 12 ) ); filter.CollectingDayHours.Add( new DayHourRange( DayOfWeek.Friday, 8, 12 ) ); filter.ExcludePeriods.Add( new Day( 2011, 9, 13 ) ); // exclude Tuesday CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, new TimeRange( start.Date, end.Date.AddDays( 1 ) ), SeekDirection.Forward, new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ) ); collector.CollectHours(); // add boundaries collector.Periods.Add( new TimeRange( start, end ) ); // intersect periods TimePeriodIntersector<TimeRange> periodIntersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection periods = periodIntersector.IntersectPeriods( collector.Periods ); // calculate working hours (sum period durations) TimeSpan workingHours = TimeSpan.Zero; foreach ( ITimePeriod period in periods ) { workingHours = workingHours.Add( period.Duration ); } Console.WriteLine( "Total working hours: " + workingHours ); // > Total working hours: 09:00:00 }
/// <summary> /// <paramref name="fromTime"/> ~ <paramref name="toTime"/> 기간의 WorkingTime 의 기간을 구합니다. /// </summary> /// <param name="fromTime"></param> /// <param name="toTime"></param> /// <returns></returns> public TimeSpan Difference(DateTime fromTime, DateTime toTime) { if(IsDebugEnabled) log.Debug("fromTime[{0}] ~ toTime[{1}] 의 WorkingTime 기간을 구합니다.", fromTime, toTime); if(Equals(fromTime, toTime)) return TimeSpan.Zero; var filterIsEmpty = _collectorFilter.WeekDays.Count == 0 && _collectorFilter.CollectingHours.Count == 0 && _collectorFilter.CollectingDayHours.Count == 0; if(filterIsEmpty) return new DateDiff(fromTime, toTime, _calendar.Culture.Calendar, _calendar.FirstDayOfWeek, _calendar.YearBaseMonth) .Difference; var differenceRange = new TimeRange(fromTime, toTime); var collector = new CalendarPeriodCollector(_collectorFilter, new TimeRange(differenceRange.Start.Date, differenceRange.End.Date.AddDays(1)), SeekDirection.Forward, _calendar); // Gap을 계산합니다. var gapCalculator = new TimeGapCalculator<TimeRange>(_calendar); var gaps = gapCalculator.GetGaps(collector.Periods, differenceRange); var difference = gaps.Aggregate(TimeSpan.Zero, (current, gap) => current.Add(gap.Duration)); if(IsDebugEnabled) log.Debug("fromTime[{0}] ~ toTime[{1}] 의 WorkingTime 기간은 [{2}] 입니다!!!", fromTime, toTime, difference); return (fromTime < toTime) ? difference : difference.Negate(); }
/// <summary> /// <paramref name="period"/> 기간 내에서 예외 기간등을 제외한 기간들을 HourRange 컬렉션으로 단위로 반환합니다. /// </summary> /// <param name="period"></param> /// <returns></returns> private IEnumerable<ITimePeriod> GetAvailableWeekPeriods(ITimePeriod period) { period.ShouldNotBeNull("period"); if(WeekDays.Count == 0 && WorkingHours.Count == 0 && WorkingDayHours.Count == 0) return new TimePeriodCollection { period }; // 필터에 필터링할 정보를 추가합니다. // var filter = new CalendarPeriodCollectorFilter(); WeekDays.RunEach(weekDay => filter.WeekDays.Add(weekDay)); WorkingHours.RunEach(workingHour => filter.CollectingHours.Add(workingHour)); WorkingDayHours.RunEach(workingDayHour => filter.CollectingDayHours.Add(workingDayHour)); var weekCollector = new CalendarPeriodCollector(filter, period, SeekDirection.Forward, TimeCalendar); weekCollector.CollectHours(); return weekCollector.Periods; }
public void CollectHoursTest() { //! 1월의 금요일의 8:00~18:00 까지 기간만을 계산한다. var filter = new CalendarPeriodCollectorFilter(); filter.Months.Add(January); filter.WeekDays.Add(DayOfWeek.Friday); filter.CollectingHours.Add(new HourRangeInDay(8, 18)); var testPeriod = new CalendarTimeRange(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31)); var collector = new CalendarPeriodCollector(filter, testPeriod); collector.CollectHours(); Assert.AreEqual(collector.Periods.Count, 9); Assert.IsTrue( collector.Periods[0].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 01, 8, 0, 0), new DateTime(2010, 1, 01, 18, 0, 0))), collector.Periods[0].ToString()); Assert.IsTrue( collector.Periods[1].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 08, 8, 0, 0), new DateTime(2010, 1, 08, 18, 0, 0))), collector.Periods[1].ToString()); Assert.IsTrue( collector.Periods[2].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 15, 8, 0, 0), new DateTime(2010, 1, 15, 18, 0, 0))), collector.Periods[2].ToString()); Assert.IsTrue( collector.Periods[3].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 22, 8, 0, 0), new DateTime(2010, 1, 22, 18, 0, 0))), collector.Periods[3].ToString()); Assert.IsTrue( collector.Periods[4].IsSamePeriod(new CalendarTimeRange(new DateTime(2010, 1, 29, 8, 0, 0), new DateTime(2010, 1, 29, 18, 0, 0))), collector.Periods[4].ToString()); Assert.IsTrue( collector.Periods[5].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 07, 8, 0, 0), new DateTime(2011, 1, 07, 18, 0, 0))), collector.Periods[5].ToString()); Assert.IsTrue( collector.Periods[6].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 14, 8, 0, 0), new DateTime(2011, 1, 14, 18, 0, 0))), collector.Periods[6].ToString()); Assert.IsTrue( collector.Periods[7].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 21, 8, 0, 0), new DateTime(2011, 1, 21, 18, 0, 0))), collector.Periods[7].ToString()); Assert.IsTrue( collector.Periods[8].IsSamePeriod(new CalendarTimeRange(new DateTime(2011, 1, 28, 8, 0, 0), new DateTime(2011, 1, 28, 18, 0, 0))), collector.Periods[8].ToString()); }
// ---------------------------------------------------------------------- public TimeSpan Difference( DateTime date1, DateTime date2 ) { if ( date1.Equals( date2 ) ) { return TimeSpan.Zero; } if ( collectorFilter.WeekDays.Count == 0 && collectorFilter.CollectingHours.Count == 0 && collectorFilter.CollectingDayHours.Count == 0 ) { return new DateDiff( date1, date2, calendar.Culture.Calendar, calendar.FirstDayOfWeek, calendar.YearBaseMonth ).Difference; } TimeRange differenceRange = new TimeRange( date1, date2 ); CalendarPeriodCollector collector = new CalendarPeriodCollector( collectorFilter, new TimeRange( differenceRange.Start.Date, differenceRange.End.Date.AddDays( 1 ) ), SeekDirection.Forward, calendar ); collector.CollectHours(); // calculate gaps TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<TimeRange>( calendar ); ITimePeriodCollection gaps = gapCalculator.GetGaps( collector.Periods, differenceRange ); // calculate difference (sum gap durations) TimeSpan difference = TimeSpan.Zero; foreach ( ITimePeriod gap in gaps ) { difference = difference.Add( gap.Duration ); } return date1 < date2 ? difference : difference.Negate(); }