/// <summary>
        /// Converts the given <see cref="DateTimeInterval"/> to a <see cref="DateInterval"/> covering the same dates.
        /// </summary>
        /// <param name="dateTimeInterval">A date time interval</param>
        /// <returns>The <see cref="DateInterval"/> that covers the same dates as <paramref name="dateTimeInterval"/>.</returns>
        public static DateInterval ToDateInterval(this DateTimeInterval dateTimeInterval)
        {
            var startDate = dateTimeInterval.Start.Date;
            var endDate   = dateTimeInterval.End.TimeOfDay == LocalTime.Midnight && !dateTimeInterval.IsSinglePointInterval()
                ? dateTimeInterval.End.Date.PreviousDay()
                : dateTimeInterval.End.Date;

            return(new DateInterval(startDate, endDate));
        }
Esempio n. 2
0
        public void IsSinglePointInterval_Null()
        {
            // Arrange
            DateTimeInterval interval = null;

            // Act
            Action act = () => interval.IsSinglePointInterval();

            // Assert
            act.Should().Throw <ArgumentNullException>().WithMessage("Value cannot be null.\r\nParameter name: dateTimeInterval");
        }
Esempio n. 3
0
        public void IsSinglePointInterval_SinglePoint()
        {
            // Arrange
            var localDateTime = Fixture.Create <LocalDateTime>();
            var interval      = new DateTimeInterval(localDateTime, localDateTime);

            // Act
            var isSinglePointInterval = interval.IsSinglePointInterval();

            // Assert
            isSinglePointInterval.Should().BeTrue();
        }
Esempio n. 4
0
        public void IsSinglePointInterval_NonSinglePoint()
        {
            // Arrange
            var(start, end) = Fixture.Create <LocalDateTime>((x, y) => x < y);
            var interval = new DateTimeInterval(start, end);

            // Act
            var isSinglePointInterval = interval.IsSinglePointInterval();

            // Assert
            isSinglePointInterval.Should().BeFalse();
        }
Esempio n. 5
0
        /// <summary>
        /// Returns the date time intervals where <paramref name="timeInterval"/> overlaps <paramref name="dateTimeInterval"/>.
        /// </summary>
        /// <param name="timeInterval">The time interval.</param>
        /// <param name="dateTimeInterval">The date time interval.</param>
        /// <returns>Empty result, if the two intervals don't overlap; otherwise, each date time interval where they overlap.</returns>
        /// <exception cref="System.ArgumentNullException">If <paramref name="timeInterval"/> or <paramref name="dateTimeInterval"/> is <c>null</c>.</exception>
        public static IEnumerable <DateTimeInterval> GetOverlapsWith(this TimeInterval timeInterval, DateTimeInterval dateTimeInterval)
        {
            if (timeInterval == null)
            {
                throw new System.ArgumentNullException(nameof(timeInterval));
            }

            if (dateTimeInterval == null)
            {
                throw new System.ArgumentNullException(nameof(dateTimeInterval));
            }

            if (!timeInterval.Overlaps(dateTimeInterval))
            {
                yield break;
            }

            if (dateTimeInterval.IsSinglePointInterval())
            {
                yield return(dateTimeInterval);

                yield break;
            }

            var start = timeInterval.Contains(dateTimeInterval.Start.TimeOfDay)
                ? dateTimeInterval.Start
                : dateTimeInterval.Start.NextOrSame(timeInterval.Start);

            var end = start.Next(timeInterval.End);

            while (start < dateTimeInterval.End)
            {
                if (dateTimeInterval.End < end)
                {
                    end = dateTimeInterval.End;
                }

                yield return(new DateTimeInterval(start, end));

                start = start.Next(timeInterval.Start);
                end   = start.Next(timeInterval.End);
            }
        }
        /// <summary>
        /// Converts the given <see cref="DateTimeInterval"/> to a <see cref="TimeInterval"/> covering the same local time.
        /// </summary>
        /// <param name="interval">A date time interval.</param>
        /// <returns>The time interval that covers the same times as <paramref name="interval"/>.</returns>
        /// <remarks>
        /// This only works for date time intervals with a duration of a day or less.
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">If <paramref name="interval"/> is <c>null</c>.</exception>
        /// <exception cref="System.NotSupportedException">If <paramref name="interval"/> is empty or longer than a day.</exception>
        public static TimeInterval ToTimeInterval(this DateTimeInterval interval)
        {
            if (interval == null)
            {
                throw new System.ArgumentNullException(nameof(interval));
            }

            if (interval.IsSinglePointInterval())
            {
                throw new System.NotSupportedException("Empty time intervals are not supported.");
            }

            var maxEnd = interval.Start + Period.FromDays(1);

            if (maxEnd < interval.End)
            {
                throw new System.NotSupportedException("The interval can at most be one day long.");
            }

            return(new TimeInterval(interval.Start.TimeOfDay, interval.End.TimeOfDay));
        }
Esempio n. 7
0
        /// <summary>
        /// Determines whether the time interval overlaps the date time interval.
        /// </summary>
        /// <param name="timeInterval">The time interval.</param>
        /// <param name="dateTimeInterval">The date time interval.</param>
        /// <returns><c>true</c> if the time interval overlaps the date time interval; <c>false</c>, otherwise.</returns>
        /// <remarks>
        /// This will always be <c>true</c> for date time intervals that are longer than a day.
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">If <paramref name="timeInterval"/> or <paramref name="dateTimeInterval"/> is <c>null</c>.</exception>
        public static bool Overlaps(this TimeInterval timeInterval, DateTimeInterval dateTimeInterval)
        {
            if (timeInterval == null)
            {
                throw new System.ArgumentNullException(nameof(timeInterval));
            }

            if (dateTimeInterval == null)
            {
                throw new System.ArgumentNullException(nameof(dateTimeInterval));
            }

            if (dateTimeInterval.IsSinglePointInterval())
            {
                return(timeInterval.Contains(dateTimeInterval.Start.TimeOfDay));
            }

            var maxEnd = dateTimeInterval.Start + Period.FromDays(1);

            return(maxEnd < dateTimeInterval.End || dateTimeInterval.ToTimeInterval().Overlaps(timeInterval));
        }
 public static bool IsEmptyInterval(this DateTimeInterval dateTimeInterval) => dateTimeInterval.IsSinglePointInterval();