zoukankan      html  css  js  c++  java
  • 求时间段的交集

    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;
        }
    }

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

  • 相关阅读:
    ABP框架理论研究总结(典藏版)
    ABP系列文章总目录:
    使用NServiceBus开发分布式应用
    shell脚本进阶 详解及其实例(一)
    linux模拟实现主机跨路由通信
    网络管理之基础知识详解
    RAID RAID 大揭秘~
    磁盘管理(一)磁盘结构
    如何在centos7上安装源码包
    压缩与解压缩
  • 原文地址:https://www.cnblogs.com/guohu/p/5063935.html
Copyright © 2011-2022 走看看