| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TemporalInterval.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Primitives.Intervals; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a temporal interval defined by optional start and end DateTime boundaries. This class extends the generic |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="start">The start DateTime of the interval.</param> |
| | | 15 | | /// <param name="end">The end DateTime of the interval.</param> |
| | | 16 | | /// <param name="inclusiveStart">Indicates whether the start boundary is inclusive.</param> |
| | | 17 | | /// <param name="inclusiveEnd">Indicates whether the end boundary is inclusive.</param> |
| | | 18 | | public abstract class TemporalInterval<TSelf>(DateTime? start, DateTime? end, bool inclusiveStart = true, bool inclusive |
| | 102 | 19 | | : Interval<DateTime, TSelf>(start.HasValue ? new IntervalBoundary<DateTime>(start.Value, inclusiveStart) : null, |
| | 102 | 20 | | end.HasValue ? new IntervalBoundary<DateTime>(end.Value, inclusiveEnd) : null), ITemporalInterval |
| | | 21 | | where TSelf : TemporalInterval<TSelf> |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the duration of the interval as a TimeSpan. If either the start or end boundary is not defined, this proper |
| | | 25 | | /// </summary> |
| | 0 | 26 | | public TimeSpan? Duration => !HasStart || !HasEnd ? null : End!.Value.Value - Start!.Value.Value; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the duration of the interval as a TimeSpan. If the duration cannot be calculated because either the start o |
| | | 30 | | /// </summary> |
| | 0 | 31 | | TimeSpan ITemporalInterval.Duration => Duration ?? TimeSpan.MaxValue; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Determines whether the specified DateTime value falls within the interval. This method checks if the value is gr |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="now">The DateTime value to check.</param> |
| | | 37 | | /// <returns>True if the value is within the interval; otherwise, false.</returns> |
| | 0 | 38 | | public bool IsCurrent(DateTime now) => Contains(now); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Determines whether the specified DateTime value is in the past relative to the interval. This method checks if t |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="now">The DateTime value to check.</param> |
| | | 44 | | /// <returns>True if the value is in the past relative to the interval; otherwise, false.</returns> |
| | | 45 | | public bool IsPast(DateTime now) |
| | | 46 | | { |
| | 12 | 47 | | if (!HasEnd) |
| | 0 | 48 | | return false; |
| | | 49 | | |
| | 12 | 50 | | var comparison = End!.Value.Value.CompareTo(now); |
| | | 51 | | |
| | 12 | 52 | | return comparison < 0 || (comparison == 0 && !End.Value.IsInclusive); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Determines whether the specified DateTime value is in the future relative to the interval. This method checks if |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="now">The DateTime value to check.</param> |
| | | 59 | | /// <returns>True if the value is in the future relative to the interval; otherwise, false.</returns> |
| | | 60 | | public bool IsFuture(DateTime now) |
| | | 61 | | { |
| | 9 | 62 | | if (!HasStart) |
| | 0 | 63 | | return false; |
| | | 64 | | |
| | 9 | 65 | | var comparison = Start!.Value.Value.CompareTo(now); |
| | | 66 | | |
| | 9 | 67 | | return comparison > 0 || (comparison == 0 && !Start.Value.IsInclusive); |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |