| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeRange.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 | | |
| | | 10 | | namespace MyNet.Primitives.Intervals; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents a closed interval of date and time, defined by a start and an end DateTime. The interval can be configure |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="start">The start DateTime of the interval.</param> |
| | | 16 | | /// <param name="end">The end DateTime of the interval.</param> |
| | | 17 | | /// <param name="inclusiveStart">Indicates whether the start DateTime is inclusive.</param> |
| | | 18 | | /// <param name="inclusiveEnd">Indicates whether the end DateTime is inclusive.</param> |
| | 21 | 19 | | public sealed class DateTimeRange(DateTime start, DateTime end, bool inclusiveStart = true, bool inclusiveEnd = false) : |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Enumerates the DateTime values within the range at regular intervals defined by the specified step. The method t |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="step">The interval between each enumerated DateTime value.</param> |
| | | 25 | | /// <returns>An enumerable collection of DateTime values within the range.</returns> |
| | | 26 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the step is less than or equal to zero.</exception> |
| | | 27 | | public IEnumerable<DateTime> Enumerate(TimeSpan step) |
| | | 28 | | { |
| | 3 | 29 | | ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(step, TimeSpan.Zero); |
| | | 30 | | |
| | 3 | 31 | | var current = Start!.Value.Value; |
| | 3 | 32 | | if (!Start.Value.IsInclusive) |
| | | 33 | | { |
| | 3 | 34 | | current += step; |
| | | 35 | | } |
| | | 36 | | |
| | 9 | 37 | | while (current < End!.Value.Value || (End.Value.IsInclusive && current == End.Value.Value)) |
| | | 38 | | { |
| | 6 | 39 | | yield return current; |
| | 6 | 40 | | current += step; |
| | | 41 | | } |
| | 3 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | protected override DateTimeRange Create(IntervalBoundary<DateTime>? start, IntervalBoundary<DateTime>? end) |
| | 3 | 46 | | => new(start!.Value.Value, end!.Value.Value, start.Value.IsInclusive, end.Value.IsInclusive); |
| | | 47 | | } |
| | | 48 | | |