>  기사  >  백엔드 개발  >  C# 기간이 교차하는지 확인

C# 기간이 교차하는지 확인

黄舟
黄舟원래의
2017-03-01 10:42:252891검색

1. 두 시작 시간과 종료 시간이 교차하는지 확인:

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


그 중 From과 To는 두 개의 시간 속성입니다.

위는 시간의 교차 여부를 판단하는 C#의 내용입니다. PHP 중국어 홈페이지(www.php.cn)!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.