在计划中找到漏洞

时间:2017-03-15 08:29:11

标签: c# schedule

我正在寻找一种在计划中找到漏洞的方法,这是没有预订的时间。

我在C#中有一个简单的类,看起来像:

DateTime StartTime { get; set; }

Datetime EndTime { get; set; }

public int User_ID { get; set; }

同样的班级用于预订。 我们假设我有这些对象:

Schedule: StartTime "2017-03-14 08:00" - EndTime "2017-03-14 16:00" (8 hours)
Booking: StartTime "2017-03-14 09:00" - Endtime "2017-03-14 10:00" (1 hour)

我的最终结果是2个代表“空闲时间”的对象:

Free: StartTime "2017-03-14 08:00" EndTime: "2017-03-14 09:00" (1 hour)
Free: StartTime "2017-03-14 10:00" EndTime: "2017-03-14 16:00"(6 hour)

我如何在C#中查看?

我正在考虑循环计划并在每次预订的开始/结束时拆分它们,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

比我想象的要容易......请注意,此代码未经过优化,此算法可能不太可优化:

public class TimeSegment
{
    public readonly DateTime StartTime;

    public readonly DateTime EndTime;

    public TimeSegment(DateTime startTime, DateTime endTime)
    {
        StartTime = startTime;
        EndTime = endTime;
    }

    public TimeSegment[] Subtract(TimeSegment other)
    {
        // 8-10 Subtract 10-11 = 8-10
        if (StartTime > other.EndTime || other.StartTime > EndTime)
        {
            // If there is no intersection, we return { this }
            // (no subtraction)
            return new[] { this };
        }

        if (StartTime >= other.StartTime)
        {
            // 8-10 Subtract 8-10 = (nothing)
            // 8-10 Subtract 7-11 = (nothing)
            if (EndTime <= other.EndTime)
            {
                // Total subtraction, nothing remains!
                return new TimeSegment[0];
            }
            else
            {
                // 8-10 Subtract 7-9 = 9-10
                return new[] { new TimeSegment(other.EndTime, EndTime) };
            }
        }

        // 8-12 Subtract 9-13 = 8-9
        if (EndTime <= other.EndTime)
        {
            return new[] { new TimeSegment(StartTime, other.EndTime) };
        }

        // 8-12 Subtract 9-11 = 8-9, 11-12
        // Complete case: two TimeSegments returned
        return new[] { new TimeSegment(StartTime, other.StartTime), new TimeSegment(other.EndTime, EndTime) };
    }

    public override string ToString()
    {
        return string.Format("{0}-{1}", StartTime, EndTime);
    }
}

然后:

var schedules = new List<TimeSegment> { new TimeSegment(new DateTime(2017, 03, 14, 08, 00, 00), new DateTime(2017, 03, 14, 16, 00, 00)) };

var bookings = new List<TimeSegment> 
{
    new TimeSegment(new DateTime(2017, 03, 14, 09, 00, 00), new DateTime(2017, 03, 14, 10, 00, 00)),
    new TimeSegment(new DateTime(2017, 03, 14, 12, 00, 00), new DateTime(2017, 03, 14, 14, 00, 00)),
    new TimeSegment(new DateTime(2017, 03, 14, 13, 00, 00), new DateTime(2017, 03, 14, 15, 00, 00)),
};

foreach (TimeSegment booking in bookings)
{
    var schedulesNew = new List<TimeSegment>();

    foreach (TimeSegment schedule in schedules)
    {
        var diff = schedule.Subtract(booking);
        schedulesNew.AddRange(diff);
    }

    schedules = schedulesNew;
}

这个“核心”是一个Subtract函数,它从另一个TimeSegment中减去TimeSegment个减法,返回0,1或2 TimeSegment个...然后我们迭代地从我们之前预订中生成的TimeSegment中减去所有预订。