TimeSpan中一天的非活动间隔

时间:2018-11-05 10:05:29

标签: c# intervals timespan

我有一个名为Slot的c#类,该类的所有0:00:00属性仅接受从1.00:00:00TimeSpan的值。

public class Slot
{
    public TimeSpan StartTime { get; set; }

    public TimeSpan EndTime { get; set; }

}

我将这些值作为活动间隔不互相重叠:

var activeIntervals= new List<Slot>
{
    new Slot
    {
        StartTime = new TimeSpan(10,0,0),
        EndTime = new TimeSpan(12,0,0),
    },
    new Slot
    {
        StartTime = new TimeSpan(12,0,0),
        EndTime = new TimeSpan(13,0,0),
    },
    new Slot
    {
        StartTime = new TimeSpan(16,0,0),
        EndTime = new TimeSpan(22,0,0),
    }
};

是否有任何逻辑可以将一天中的其他时间间隔设置为inactiveIntervals?

在这种情况下,我想要这个结果:  0:00:0010:00:0013:00:0016:00:0022:00:001.00:00:00

3 个答案:

答案 0 :(得分:2)

尝试以下逻辑。它将为您工作。

您可以test it here

var activeIntervals = new List<Slot>
{
    new Slot
    {
        StartTime = new TimeSpan(10,0,0),
        EndTime = new TimeSpan(12,0,0),
    },
    new Slot
    {
        StartTime = new TimeSpan(12,0,0),
        EndTime = new TimeSpan(13,0,0),
    },
    new Slot
    {
        StartTime = new TimeSpan(16,0,0),
        EndTime = new TimeSpan(22,0,0),
    }
};


activeIntervals = activeIntervals.OrderBy(x => x.StartTime).ToList();

var inActiveIntervals = new List<Slot>();
TimeSpan start =  new TimeSpan(0,0,0);
TimeSpan end =  new TimeSpan(24,0,0);

for (int i = 0; i < activeIntervals.Count(); i++)
{
    if (start < activeIntervals[i].StartTime) 
    {
        inActiveIntervals.Add(new Slot { StartTime = start, EndTime = activeIntervals[i].StartTime });
    }
    start = activeIntervals[i].EndTime;
}            
if (start < end) 
{
    inActiveIntervals.Add(new Slot { StartTime = start, EndTime = end });
    start = end;
}
for (int i = 0; i < inActiveIntervals.Count(); i++)
{
     Console.WriteLine(inActiveIntervals[i].StartTime.ToString() + " - " + inActiveIntervals[i].EndTime.ToString());
}

答案 1 :(得分:1)

linq的一个主意:

var inactiveIntervals = activeIntervals
    .Zip(activeIntervals.Skip(1), (first, second) => {
        if (second.StartTime - first.EndTime != TimeSpan.Zero) // Get rid of regions that don't have 'gap', e.g. 12:00:00 to 12:00:00
            return new Slot { StartTime = first.EndTime, EndTime = second.StartTime };
        return null;
    }).Where(a => a != null).ToList(); // Filter out nulls

// Insert range from start to first date
inactiveIntervals.Insert(0, new Slot { StartTime = TimeSpan.Zero, EndTime = activeIntervals[0].StartTime });
// Insert range from last date to end
inactiveIntervals.Add(new Slot { StartTime = activeIntervals.Last().EndTime, EndTime = new TimeSpan(1, 0, 0, 0) });

这使用了.Zip的技巧,您可以获取一个列表,然后将其与移动1的相同列表一起压缩。然后,您可以将一个元素的EndTime与下一个StartTime进行比较元素。

过滤没有间隙的区域有点难看。您必须检查极端情况。

答案 2 :(得分:1)

您需要创建List<Slot>的新实例,然后使用列表元素的迭代添加activeIntervals列表中不存在的一天间隔。

public static class SlotExtensions
{
    public static List<Slot> GetInactive(this List<Slot> slots)
    {
        Slot day = new Slot
        {
            StartTime = new TimeSpan(0,0,0),
            EndTime = new TimeSpan(24,0,0)
        }
        List<Slot> inactive = new List<Slot>();
        Slot tmp;
        foreach(Slot slot in slots)
        {
            if(day.StartTime < slot.StartTime)
            {
                tmp = new Slot
                {
                    StartTime = day.StartTime;
                    EndTime = slot.StartTime;
                };
                inactive.Add(tmp);
            };
            day.StartTime = slot.EndTime;
        }
        if(day.StartTime < day.EndTime)
        {
            inactive.Add(day);
        }
        return inactive;
    }
}

扩展方法使用day,这是Slot类的实例,可以全天使用。然后,我们将day和迭代槽的开始之间的差异添加到新列表中,然后将day的起始位置移到迭代槽的末尾。当循环结束时,方法会将剩余间隔添加到列表中。