Finding the intersection of time periods

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classdefault7:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {List<keyvaluepair<datetime, datetime>> timeSegments1 =NewList<keyvaluepair<datetime, datetime>>(); Timesegments1.add (NewKeyvaluepair<datetime, datetime> (DateTime.Parse ("2015-11-21 8:00:00"), DateTime.Parse ("2015-11-21 9:00:00"))); Timesegments1.add (NewKeyvaluepair<datetime, datetime> (DateTime.Parse ("2015-11-21 9:30:00"), DateTime.Parse ("2015-11-21 10:00:00"))); Timesegments1.add (NewKeyvaluepair<datetime, datetime> (DateTime.Parse ("2015-11-21 10:30:00"), DateTime.Parse ("2015-11-21 12:00:00"))); List<keyvaluepair<datetime, datetime>> timeSegments2 =NewList<keyvaluepair<datetime, datetime>>(); Timesegments2.add (NewKeyvaluepair<datetime, datetime> (DateTime.Parse ("2015-11-21 8:30:00"), DateTime.Parse ("2015-11-21 9:20:00"))); Timesegments2.add (NewKeyvaluepair<datetime, datetime> (DateTime.Parse ("2015-11-21 9:30:00"), DateTime.Parse ("2015-11-21 10:40:00"))); varOBJS =getuniontime (TimeSegments1, timeSegments2); intCount =1; foreach(varIteminchObjs) {Response.Write (string. Format ("{2} start time: {0} end time:{1}<br/>", item. Key, item.            Value, Count)); Count++; }    }    PrivateKeyvaluepair<datetime, Datetime>?getuniotimesegment (DateTime fromTime1, DateTime toTime1, DateTime fromTime2, datetime toTime2) {
    if(fromTime1 >= toTime1 | | fromTime2 >=toTime2)Throw NewException ("The time size violates the rule, and there is a situation where the start time is greater than the end time! "); KeyValuePair<datetime, Datetime>? Resulttimesegment =NULL; //1 contains 2        if(fromTime1 <= fromTime2 && toTime2 <=toTime1) {resulttimesegment=NewKeyvaluepair<datetime, datetime>(fromTime2, toTime2); }        //2 contains 1        if(FromTime2 < fromTime1 && toTime1 <=toTime2) {resulttimesegment=NewKeyvaluepair<datetime, datetime>(fromTime1, toTime1); }        //12 Crossover, 1 left, 2 right        if(fromTime1 <= fromTime2 && fromTime2 <= toTime1 && toTime1 <=toTime2) {resulttimesegment=NewKeyvaluepair<datetime, datetime>(fromTime2, toTime1); }        //21 Crossover, 2 left, 1 right        if(fromTime2 <= fromTime1 && fromTime1 <= toTime2 && toTime2 <=toTime1) {resulttimesegment=NewKeyvaluepair<datetime, datetime>(fromTime1, toTime2); }        returnresulttimesegment; }    PrivateKeyvaluepair<datetime, Datetime>?getuniotimesegment (KeyValuePair<datetime, datetime> timeSegment1, Keyvaluepair<datetime, datetime>TimeSegment2) {        returngetuniotimesegment (Timesegment1.key, Timesegment1.value, Timesegment2.key, Timesegment2.value); }    PrivateList<keyvaluepair<datetime, datetime>>Getuniontime (List<keyvaluepair<datetime, datetime>> timeSegments1, List<keyvaluepair<datetime, DateTime>>timeSegments2) {List<keyvaluepair<datetime, datetime>> resulttimesegments =NewList<keyvaluepair<datetime, datetime>>(); foreach(Keyvaluepair<datetime, Datetime> timeSegment1inchtimeSegments1) {            foreach(Keyvaluepair<datetime, Datetime> TimeSegment2inchtimeSegments2) {KeyValuePair<datetime, Datetime>? Timesegment =getuniotimesegment (TimeSegment1, TimeSegment2); if(Timesegment.hasvalue) Resulttimesegments.add (Timesegment.value); }        }        returnresulttimesegments; }}

Finding the intersection of time periods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.