ホームページ  >  記事  >  バックエンド開発  >  C# 期間が交差しているかどうかを判断する

C# 期間が交差しているかどうかを判断する

黄舟
黄舟オリジナル
2017-03-01 10:42:252891ブラウズ

1. 2 つの開始時間と終了時間が交差するかどうかを判断します:

public static bool IsTimeBetween(TimeSpan input, TimeSpan start, TimeSpan end, bool fromInclusice, bool toInclusive)
        {
            //http://www.php.cn/
            // see if start comes before end
            if (end < start)
            {
                return
                    ((toInclusive && (input <= end)) || (!toInclusive && (input < end)))
                    ||
                    ((fromInclusice && (input >= start)) || (!fromInclusice && (input > start)));
            }
            else
            {
                return
                    ((fromInclusice && (input >= start)) || (!fromInclusice && (input > start)))
                    &&
                    ((toInclusive && (input <= end)) || (!toInclusive && (input < end)));
            }


        }

2. 開始時間と終了時間の式を渡し、既知の期間との交差を判断し、Mongo クエリを生成します:

public IMongoQuery GetMongoQueryIntersectWith<TCollection>(
            Expression<Func<TCollection, DateTime>> fromExp, 
            Expression<Func<TCollection, DateTime>> toExp)
        {
            var rangeTo = Query.And(Query<TCollection>.GTE(toExp, To), Query<TCollection>.LTE(fromExp, To));
            var rangeFrom = Query.And(Query<TCollection>.GTE(toExp, From), Query<TCollection>.LTE(fromExp, From));

            var rangeQuery = Query.Or(rangeTo, rangeFrom, 
                Query.And(Query<TCollection>.GTE(fromExp, From),Query<TCollection>.LTE(toExp, To)));
            return rangeQuery;
        }


where Fromと To は 2 つの時間属性です

上記は、期間が交差するかどうかを判断するための C# の内容です。さらに関連する内容については、PHP 中国語 Web サイト (www.php.cn) に注目してください。


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。