| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateRange.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 dates, defined by a start and an end date. The interval includes both the start and |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="start">The start date of the interval.</param> |
| | | 16 | | /// <param name="end">The end date of the interval.</param> |
| | 39 | 17 | | public sealed class DateRange(DateOnly start, DateOnly end) : Interval<DateOnly, DateRange>(new IntervalBoundary<DateOnl |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the number of days in the date range, including both the start and end dates. |
| | | 21 | | /// </summary> |
| | 3 | 22 | | public int DayCount => End!.Value.Value.DayNumber - Start!.Value.Value.DayNumber + 1; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | protected override DateRange Create(IntervalBoundary<DateOnly>? start, IntervalBoundary<DateOnly>? end) => |
| | 0 | 26 | | !start.HasValue || !end.HasValue |
| | 0 | 27 | | ? throw new InvalidOperationException("DateRange must be bounded.") |
| | 0 | 28 | | : !start.Value.IsInclusive || !end.Value.IsInclusive |
| | 0 | 29 | | ? throw new InvalidOperationException("DateRange only supports inclusive boundaries.") |
| | 0 | 30 | | : new(start.Value.Value, end.Value.Value); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Enumerates all the dates in the date range, starting from the start date and ending with the end date. Each date |
| | | 34 | | /// </summary> |
| | | 35 | | /// <returns>An enumerable collection of DateOnly values representing each day in the date range.</returns> |
| | | 36 | | public IEnumerable<DateOnly> EnumerateDays() |
| | | 37 | | { |
| | 3 | 38 | | for (var date = Start!.Value.Value; |
| | 12 | 39 | | date <= End!.Value.Value; |
| | 9 | 40 | | date = date.AddDays(1)) |
| | | 41 | | { |
| | 9 | 42 | | yield return date; |
| | | 43 | | } |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the start and end boundaries of the date range as <see cref="IntervalBoundary{DateOnly}"/> values. The star |
| | | 48 | | /// </summary> |
| | 3 | 49 | | IntervalBoundary<DateOnly> IBoundedInterval<DateOnly>.Start => Start!.Value; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the start and end boundaries of the date range as <see cref="IntervalBoundary{DateOnly}"/> values. The star |
| | | 53 | | /// </summary> |
| | 0 | 54 | | IntervalBoundary<DateOnly> IBoundedInterval<DateOnly>.End => End!.Value; |
| | | 55 | | } |
| | | 56 | | |