csharp 测试一段时间是否重叠

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 测试一段时间是否重叠相关的知识,希望对你有一定的参考价值。

<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Tests if two given periods overlap each other.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;BS&quot;&gt;Base period start&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;BE&quot;&gt;Base period end&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;TS&quot;&gt;Test period start&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;TE&quot;&gt;Test period end&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;</span>
<span style="color: #008080; font-style: italic;">/// 	&lt;c&gt;true&lt;/c&gt; if the periods overlap; otherwise, &lt;c&gt;false&lt;/c&gt;.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/returns&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> TimePeriodOverlap<span style="color: #000000;">&#40;</span>DateTime BS, DateTime BE, DateTime TS, DateTime TE<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// More simple?</span>
    <span style="color: #008080; font-style: italic;">// return !((TS &lt; BS &amp;&amp; TE &lt; BS) || (TS &gt; BE &amp;&amp; TE &gt; BE));</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// The version below, without comments </span>
    <span style="color: #008080; font-style: italic;">/*
    return (
        (TS &gt;= BS &amp;&amp; TS &lt; BE) || (TE &lt;= BE &amp;&amp; TE &gt; BS) || (TS &lt;= BS &amp;&amp; TE &gt;= BE)
    );
    */</span>
&nbsp;
    <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>
        <span style="color: #008080; font-style: italic;">// 1. Case:</span>
        <span style="color: #008080; font-style: italic;">//</span>
        <span style="color: #008080; font-style: italic;">//       TS-------TE</span>
        <span style="color: #008080; font-style: italic;">//    BS------BE </span>
        <span style="color: #008080; font-style: italic;">//</span>
        <span style="color: #008080; font-style: italic;">// TS is after BS but before BE</span>
        <span style="color: #000000;">&#40;</span>TS &gt;= BS &amp;&amp; TS &lt; BE<span style="color: #000000;">&#41;</span>
        || <span style="color: #008080; font-style: italic;">// or</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// 2. Case</span>
        <span style="color: #008080; font-style: italic;">//</span>
        <span style="color: #008080; font-style: italic;">//    TS-------TE</span>
        <span style="color: #008080; font-style: italic;">//        BS---------BE</span>
        <span style="color: #008080; font-style: italic;">//</span>
        <span style="color: #008080; font-style: italic;">// TE is before BE but after BS</span>
        <span style="color: #000000;">&#40;</span>TE &lt;= BE &amp;&amp; TE &gt; BS<span style="color: #000000;">&#41;</span>
        || <span style="color: #008080; font-style: italic;">// or</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// 3. Case</span>
        <span style="color: #008080; font-style: italic;">//</span>
        <span style="color: #008080; font-style: italic;">//  TS----------TE</span>
        <span style="color: #008080; font-style: italic;">//     BS----BE</span>
        <span style="color: #008080; font-style: italic;">//</span>
        <span style="color: #008080; font-style: italic;">// TS is before BS and TE is after BE</span>
        <span style="color: #000000;">&#40;</span>TS &lt;= BS &amp;&amp; TE &gt;= BE<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>

以上是关于csharp 测试一段时间是否重叠的主要内容,如果未能解决你的问题,请参考以下文章

SQL判断两个时间段是否重叠

csharp 测试字符串是否只是十六进制数

csharp 此控制台应用程序测试是否在SharePoint中使用内容类型

csharp C#转换为HTML和HTML的基本HTML Helper类,提供Extensions以测试String是否包含HTML。

csharp 对于那些需要在一段时间内创建DropDownList的时候,您可以通过“Enumerable.Range”轻松完成。样本c

Oracle判断两个时间段是否有重叠