求时间段的交集

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<KeyValuePair<DateTime, DateTime>> timeSegments1 = new List<KeyValuePair<DateTime, DateTime>>();
        timeSegments1.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Parse("2015-11-21 8:00:00"), DateTime.Parse("2015-11-21 9:00:00")));
        timeSegments1.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Parse("2015-11-21 9:30:00"), DateTime.Parse("2015-11-21 10:00:00")));
        timeSegments1.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Parse("2015-11-21 10:30:00"), DateTime.Parse("2015-11-21 12:00:00")));

        List<KeyValuePair<DateTime, DateTime>> timeSegments2 = new List<KeyValuePair<DateTime, DateTime>>();
        timeSegments2.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Parse("2015-11-21 8:30:00"), DateTime.Parse("2015-11-21 9:20:00")));
        timeSegments2.Add(new KeyValuePair<DateTime, DateTime>(DateTime.Parse("2015-11-21 9:30:00"), DateTime.Parse("2015-11-21 10:40:00")));


        var objs = GetUnionTime(timeSegments1, timeSegments2);
        int count = 1;
        foreach (var item in objs)
        {
            Response.Write(string.Format("{2} 开始时间:{0} 结束时间:{1}<br/>", item.Key, item.Value, count));
            count++;
        }
    }


    private KeyValuePair<DateTime, DateTime>? GetUnioTimeSegment(
        DateTime fromTime1, DateTime toTime1, DateTime fromTime2, DateTime toTime2)
    {
        if (fromTime1 >= toTime1 || fromTime2 >= toTime2)
            throw new Exception("时间大小违反规则,出现了开始时间比结束时间大的情况!");

        KeyValuePair<DateTime, DateTime>? resultTimeSegment = null;

        //1包含2
        if (fromTime1 <= fromTime2 && toTime2 <= toTime1 )
        {
            resultTimeSegment = new KeyValuePair<DateTime, DateTime>(fromTime2, toTime2);
        }
        //2包含1
        if (fromTime2 < fromTime1 && toTime1 <= toTime2 )
        {
            resultTimeSegment = new KeyValuePair<DateTime, DateTime>(fromTime1, toTime1);
        }


        //12交叉,1左,2右
        if (fromTime1 <= fromTime2 && fromTime2 <= toTime1 && toTime1 <= toTime2 )
        {
            resultTimeSegment = new KeyValuePair<DateTime, DateTime>(fromTime2, toTime1);
        }

        //21交叉,2左,1右
        if (fromTime2 <= fromTime1 && fromTime1 <= toTime2 && toTime2 <= toTime1 )
        {
            resultTimeSegment = new KeyValuePair<DateTime, DateTime>(fromTime1, toTime2);
        }

        return resultTimeSegment;
    }

    private KeyValuePair<DateTime, DateTime>? GetUnioTimeSegment(
        KeyValuePair<DateTime, DateTime> timeSegment1, KeyValuePair<DateTime, DateTime> timeSegment2)
    {
        return GetUnioTimeSegment(timeSegment1.Key, timeSegment1.Value, timeSegment2.Key, timeSegment2.Value);
    }


    private List<KeyValuePair<DateTime, DateTime>> GetUnionTime(
        List<KeyValuePair<DateTime, DateTime>> timeSegments1, List<KeyValuePair<DateTime, DateTime>> timeSegments2)
    {
        List<KeyValuePair<DateTime, DateTime>> resultTimeSegments = new List<KeyValuePair<DateTime, DateTime>>();

        foreach (KeyValuePair<DateTime, DateTime> timeSegment1 in timeSegments1)
        {
            foreach (KeyValuePair<DateTime, DateTime> timeSegment2 in timeSegments2)
            {
                KeyValuePair<DateTime, DateTime>? timeSegment = GetUnioTimeSegment(timeSegment1, timeSegment2);
                if (timeSegment.HasValue)
                    resultTimeSegments.Add(timeSegment.Value);
            }
        }

        return resultTimeSegments;
    }
}

 程序员的基础教程:菜鸟程序员

原文地址:https://www.cnblogs.com/guohu/p/5063935.html