| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeInterval.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | using MyNet.Primitives.Intervals; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Temporal.Intervals; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Base class for intervals of <see cref="DateTime"/> (civil / UI time with explicit <see cref="DateTime.Kind"/>). |
| | | 15 | | /// Default boundary policy is half-open <c>[start, end)</c> when subclasses do not override inclusivity. |
| | | 16 | | /// Absolute (API / domain) instants should use <c>DateTimeOffset</c>-based interval types instead. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="TSelf">The concrete interval type.</typeparam> |
| | | 19 | | /// <param name="start">Optional start instant.</param> |
| | | 20 | | /// <param name="end">Optional end instant.</param> |
| | | 21 | | /// <param name="inclusiveStart">Whether the start bound is inclusive.</param> |
| | | 22 | | /// <param name="inclusiveEnd">Whether the end bound is inclusive.</param> |
| | | 23 | | public abstract class DateTimeInterval<TSelf>(DateTime? start, DateTime? end, bool inclusiveStart = true, bool inclusive |
| | 117 | 24 | | : Interval<DateTime, TSelf>(start.HasValue ? new IntervalBoundary<DateTime>(start.Value, inclusiveStart) : null, |
| | 117 | 25 | | end.HasValue ? new IntervalBoundary<DateTime>(end.Value, inclusiveEnd) : null), IDateTimeInterval |
| | | 26 | | where TSelf : DateTimeInterval<TSelf> |
| | | 27 | | { |
| | | 28 | | /// <inheritdoc /> |
| | 0 | 29 | | public TimeSpan? Duration => !HasStart || !HasEnd ? null : End!.Value.Value - Start!.Value.Value; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Returns whether <paramref name="now"/> lies inside this interval (respecting inclusivity). |
| | | 33 | | /// </summary> |
| | 3 | 34 | | public bool IsCurrent(DateTime now) => Contains(now); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Returns whether <paramref name="now"/> is strictly after this interval. |
| | | 38 | | /// </summary> |
| | | 39 | | public bool IsPast(DateTime now) |
| | | 40 | | { |
| | 12 | 41 | | if (!HasEnd) |
| | 0 | 42 | | return false; |
| | | 43 | | |
| | 12 | 44 | | var comparison = End!.Value.Value.CompareTo(now); |
| | | 45 | | |
| | 12 | 46 | | return comparison < 0 || (comparison == 0 && !End.Value.IsInclusive); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Returns whether <paramref name="now"/> is strictly before this interval. |
| | | 51 | | /// </summary> |
| | | 52 | | public bool IsFuture(DateTime now) |
| | | 53 | | { |
| | 9 | 54 | | if (!HasStart) |
| | 0 | 55 | | return false; |
| | | 56 | | |
| | 9 | 57 | | var comparison = Start!.Value.Value.CompareTo(now); |
| | | 58 | | |
| | 9 | 59 | | return comparison > 0 || (comparison == 0 && !Start.Value.IsInclusive); |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |