| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeOffsetRange.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 | | using MyNet.Primitives.Intervals; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Temporal.Intervals; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Configurable absolute <see cref="DateTimeOffset"/> interval. Defaults to half-open <c>[start, end)</c>. |
| | | 16 | | /// Use <see cref="DateTimeOffsetPeriod"/> when the half-open contract must be enforced; use this type when |
| | | 17 | | /// inclusivity is caller-defined. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="start">Start bound.</param> |
| | | 20 | | /// <param name="end">End bound.</param> |
| | | 21 | | /// <param name="inclusiveStart">Whether start is inclusive (default true).</param> |
| | | 22 | | /// <param name="inclusiveEnd">Whether end is inclusive (default false).</param> |
| | | 23 | | public sealed class DateTimeOffsetRange(DateTimeOffset start, DateTimeOffset end, bool inclusiveStart = true, bool inclu |
| | 0 | 24 | | : DateTimeOffsetInterval<DateTimeOffsetRange>(start, end, inclusiveStart, inclusiveEnd) |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Enumerates values from start to end using a fixed <paramref name="step"/>. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="step"/> is not positive.</exception> |
| | | 30 | | public IEnumerable<DateTimeOffset> Enumerate(TimeSpan step) |
| | | 31 | | { |
| | 0 | 32 | | ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(step, TimeSpan.Zero); |
| | | 33 | | |
| | 0 | 34 | | var current = Start!.Value.Value; |
| | 0 | 35 | | if (!Start.Value.IsInclusive) |
| | | 36 | | { |
| | 0 | 37 | | current += step; |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | while (current < End!.Value.Value || (End.Value.IsInclusive && current == End.Value.Value)) |
| | | 41 | | { |
| | 0 | 42 | | yield return current; |
| | 0 | 43 | | current += step; |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | protected override DateTimeOffsetRange Create(IntervalBoundary<DateTimeOffset>? start, IntervalBoundary<DateTimeOffs |
| | 0 | 49 | | !start.HasValue || !end.HasValue |
| | 0 | 50 | | ? throw new InvalidOperationException("DateTimeOffsetRange must be bounded.") |
| | 0 | 51 | | : new DateTimeOffsetRange(start.Value.Value, end.Value.Value, start.Value.IsInclusive, end.Value.IsInclusive |
| | | 52 | | } |
| | | 53 | | |