本文整理汇总了C#中Itenso.TimePeriod.TimeRange.GetIntersection方法的典型用法代码示例。如果您正苦于以下问题:C# TimeRange.GetIntersection方法的具体用法?C# TimeRange.GetIntersection怎么用?C# TimeRange.GetIntersection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Itenso.TimePeriod.TimeRange
的用法示例。
在下文中一共展示了TimeRange.GetIntersection方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TimeRangeSample
public void TimeRangeSample()
{
// --- time range 1 ---
TimeRange timeRange1 = new TimeRange(
new DateTime( 2011, 2, 22, 14, 0, 0 ),
new DateTime( 2011, 2, 22, 18, 0, 0 ) );
Console.WriteLine( "TimeRange1: " + timeRange1 );
// > TimeRange1: 22.02.2011 14:00:00 - 18:00:00 | 04:00:00
// --- time range 2 ---
TimeRange timeRange2 = new TimeRange(
new DateTime( 2011, 2, 22, 15, 0, 0 ),
new TimeSpan( 2, 0, 0 ) );
Console.WriteLine( "TimeRange2: " + timeRange2 );
// > TimeRange2: 22.02.2011 15:00:00 - 17:00:00 | 02:00:00
// --- time range 3 ---
TimeRange timeRange3 = new TimeRange(
new DateTime( 2011, 2, 22, 16, 0, 0 ),
new DateTime( 2011, 2, 22, 21, 0, 0 ) );
Console.WriteLine( "TimeRange3: " + timeRange3 );
// > TimeRange3: 22.02.2011 16:00:00 - 21:00:00 | 05:00:00
// --- relation ---
Console.WriteLine( "TimeRange1.GetRelation( TimeRange2 ): " + timeRange1.GetRelation( timeRange2 ) );
// > TimeRange1.GetRelation( TimeRange2 ): Enclosing
Console.WriteLine( "TimeRange1.GetRelation( TimeRange3 ): " + timeRange1.GetRelation( timeRange3 ) );
// > TimeRange1.GetRelation( TimeRange3 ): EndInside
Console.WriteLine( "TimeRange3.GetRelation( TimeRange2 ): " + timeRange3.GetRelation( timeRange2 ) );
// > TimeRange3.GetRelation( TimeRange2 ): StartInside
// --- intersection ---
Console.WriteLine( "TimeRange1.GetIntersection( TimeRange2 ): " + timeRange1.GetIntersection( timeRange2 ) );
// > TimeRange1.GetIntersection( TimeRange2 ): 22.02.2011 15:00:00 - 17:00:00 | 02:00:00
Console.WriteLine( "TimeRange1.GetIntersection( TimeRange3 ): " + timeRange1.GetIntersection( timeRange3 ) );
// > TimeRange1.GetIntersection( TimeRange3 ): 22.02.2011 16:00:00 - 18:00:00 | 02:00:00
Console.WriteLine( "TimeRange3.GetIntersection( TimeRange2 ): " + timeRange3.GetIntersection( timeRange2 ) );
// > TimeRange3.GetIntersection( TimeRange2 ): 22.02.2011 16:00:00 - 17:00:00 | 01:00:00
}
示例2: CalcWorkingPeriod
// ----------------------------------------------------------------------
public TimeSpan CalcWorkingPeriod( DateTime start, DateTime end,
ITimePeriodCollection excludePeriods = null)
{
if ( start.Equals( end ) )
{
return TimeSpan.Zero;
}
// test range
TimeRange testRange = new TimeRange( start, end );
// search range
DateTime searchStart = new Day( testRange.Start ).Start;
DateTime serachEnd = new Day( testRange.End ).GetNextDay().Start;
TimeRange searchPeriod = new TimeRange( searchStart, serachEnd );
// search filter
CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
filter.AddWorkingWeekDays(); // working days
if ( excludePeriods != null )
{
filter.ExcludePeriods.AddAll( excludePeriods );
}
filter.CollectingHours.Add( new HourRange( 07, 12 ) ); // working hours
filter.CollectingHours.Add( new HourRange( 13, 19 ) ); // working hours
// collect working hours
TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } );
CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, searchPeriod, SeekDirection.Forward, calendar );
collector.CollectHours();
TimeSpan workingPeriod = new TimeSpan();
foreach ( ICalendarTimeRange period in collector.Periods )
{
// get the intersection of the test-range and the day working-hours
ITimePeriod intersection = testRange.GetIntersection( period );
if ( intersection == null )
{
continue;
}
workingPeriod = workingPeriod.Add( intersection.Duration );
}
return workingPeriod;
}
示例3: GetIntersectionTest
public void GetIntersectionTest()
{
TimeRange readOnlyTimeRange = new TimeRange( start, end );
Assert.AreEqual( readOnlyTimeRange.GetIntersection( readOnlyTimeRange ), new TimeRange( readOnlyTimeRange ) );
TimeRange timeRange = new TimeRange( start, end );
// before
ITimeRange before1 = timeRange.GetIntersection( new TimeRange( start.AddHours( -2 ), start.AddHours( -1 ) ) );
Assert.AreEqual( before1, null );
ITimeRange before2 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( -1 ), start ) );
Assert.AreEqual( before2, new TimeRange( start ) );
ITimeRange before3 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( -1 ), start.AddMilliseconds( 1 ) ) );
Assert.AreEqual( before3, new TimeRange( start, start.AddMilliseconds( 1 ) ) );
// after
ITimeRange after1 = timeRange.GetIntersection( new TimeRange( end.AddHours( 1 ), end.AddHours( 2 ) ) );
Assert.AreEqual( after1, null );
ITimeRange after2 = timeRange.GetIntersection( new TimeRange( end, end.AddMilliseconds( 1 ) ) );
Assert.AreEqual( after2, new TimeRange( end ) );
ITimeRange after3 = timeRange.GetIntersection( new TimeRange( end.AddMilliseconds( -1 ), end.AddMilliseconds( 1 ) ) );
Assert.AreEqual( after3, new TimeRange( end.AddMilliseconds( -1 ), end ) );
// intersect
Assert.AreEqual( timeRange.GetIntersection( timeRange ), timeRange );
ITimeRange itersect1 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( -1 ), end.AddMilliseconds( 1 ) ) );
Assert.AreEqual( itersect1, timeRange );
ITimeRange itersect2 = timeRange.GetIntersection( new TimeRange( start.AddMilliseconds( 1 ), end.AddMilliseconds( -1 ) ) );
Assert.AreEqual( itersect2, new TimeRange( start.AddMilliseconds( 1 ), end.AddMilliseconds( -1 ) ) );
}
示例4: GetCommonIntersection
// ----------------------------------------------------------------------
private ITimePeriod GetCommonIntersection( ITimePeriodCollection periods )
{
if ( periods.Count == 0 )
{
return null;
}
ITimeRange timeRange = new TimeRange( periods[ 0 ] );
if ( periods.Count > 1 )
{
for ( int i = 1; i < periods.Count; i++ )
{
timeRange = timeRange.GetIntersection( periods[ i ] );
if ( timeRange == null )
{
return null;
}
}
}
return timeRange;
}