| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeRange.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 closed interval of time, defined by a start and an end time. The interval includes both the start and e |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="start">The start time of the interval.</param> |
| | | 15 | | /// <param name="end">The end time of the interval.</param> |
| | 21 | 16 | | public sealed class TimeRange(TimeOnly start, TimeOnly end) : Interval<TimeOnly, TimeRange>(new IntervalBoundary<TimeOnl |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the duration of the time range as a TimeSpan value, calculated as the difference between the end time and t |
| | | 20 | | /// </summary> |
| | | 21 | | public TimeSpan Duration => |
| | 18 | 22 | | !CrossesMidnight |
| | 18 | 23 | | ? End!.Value.Value - Start!.Value.Value |
| | 18 | 24 | | : TimeSpan.FromDays(1) - Start!.Value.Value.ToTimeSpan() + End!.Value.Value.ToTimeSpan(); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets a value indicating whether the time range crosses midnight. A time range is considered to cross midnight if |
| | | 28 | | /// </summary> |
| | 30 | 29 | | public bool CrossesMidnight => End!.Value.Value < Start!.Value.Value; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Determines whether a given TimeOnly value falls within the time range. If the time range does not cross midnight |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="value">The TimeOnly value to check.</param> |
| | | 35 | | /// <returns><c>true</c> if the value falls within the time range; otherwise, <c>false</c>.</returns> |
| | | 36 | | public override bool Contains(TimeOnly value) |
| | 9 | 37 | | => !CrossesMidnight ? base.Contains(value) : value >= Start!.Value.Value || value <= End!.Value.Value; |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 0 | 40 | | protected override TimeRange Create(IntervalBoundary<TimeOnly>? start, IntervalBoundary<TimeOnly>? end) => !start.Ha |
| | 0 | 41 | | ? throw new InvalidOperationException("TimeRange must be bounded.") |
| | 0 | 42 | | : !start.Value.IsInclusive || !end.Value.IsInclusive |
| | 0 | 43 | | ? throw new InvalidOperationException("TimeRange only supports inclusive boundaries.") |
| | 0 | 44 | | : new(start.Value.Value, end.Value.Value); |
| | | 45 | | } |
| | | 46 | | |