| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeRangeExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using MyNet.Primitives.Intervals; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Primitives; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class TimeRangeExtensions |
| | | 16 | | { |
| | | 17 | | extension(TimeRange left) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Shifts the time range by a specified offset, effectively moving the entire range forward or backward in time |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="offset">The time span to shift the time range by.</param> |
| | | 23 | | /// <returns>A new time range shifted by the specified offset.</returns> |
| | 3 | 24 | | public TimeRange Shift(TimeSpan offset) => new(left.Start!.Value.Value.Add(offset), left.End!.Value.Value.Add(of |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Expands the time range by a specified duration, effectively increasing the length of the range by adding the |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="duration">The duration to expand the time range by.</param> |
| | | 30 | | /// <returns>A new time range expanded by the specified duration.</returns> |
| | 3 | 31 | | public TimeRange Expand(TimeSpan duration) => new(left.Start!.Value.Value.Add(-duration), left.End!.Value.Value. |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Enumerates the time values within the time range at specified intervals defined by the step parameter. This |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="step">The time span to use as the interval between each enumerated time value.</param> |
| | | 37 | | /// <returns>An enumerable sequence of TimeOnly values within the time range.</returns> |
| | | 38 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the step is less than or equal to zero.</exception |
| | | 39 | | public IEnumerable<TimeOnly> Enumerate(TimeSpan step) |
| | | 40 | | { |
| | 6 | 41 | | ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(step, TimeSpan.Zero); |
| | | 42 | | |
| | 3 | 43 | | var current = left.Start!.Value.Value; |
| | 3 | 44 | | var elapsed = TimeSpan.Zero; |
| | | 45 | | |
| | 3 | 46 | | yield return current; |
| | | 47 | | |
| | 15 | 48 | | while (elapsed + step <= left.Duration) |
| | | 49 | | { |
| | 12 | 50 | | current = current.Add(step); |
| | 12 | 51 | | elapsed += step; |
| | 12 | 52 | | yield return current; |
| | | 53 | | } |
| | 3 | 54 | | } |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |