| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateRangeExtensions.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 DateRangeExtensions |
| | | 16 | | { |
| | | 17 | | extension(DateRange left) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Determines whether this date range is adjacent to another date range. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="right">The other date range to check adjacency with.</param> |
| | | 23 | | /// <returns><c>true</c> if the date ranges are adjacent; otherwise, <c>false</c>.</returns> |
| | 0 | 24 | | public bool IsAdjacentTo(DateRange right) => left.End!.Value.Value.AddDays(1) == right.Start!.Value.Value || rig |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Splits the date range into multiple date ranges, each representing a single month. The resulting date ranges |
| | | 28 | | /// </summary> |
| | | 29 | | /// <returns>An enumerable of date ranges, each representing a single month.</returns> |
| | | 30 | | public IEnumerable<DateRange> SplitByMonth() |
| | | 31 | | { |
| | 3 | 32 | | var current = new DateOnly(left.Start!.Value.Value.Year, left.Start.Value.Value.Month, 1); |
| | | 33 | | |
| | 9 | 34 | | while (current <= left.End!.Value.Value) |
| | | 35 | | { |
| | 6 | 36 | | var monthStart = current < left.Start.Value.Value ? left.Start.Value.Value : current; |
| | | 37 | | |
| | 6 | 38 | | var monthEnd = |
| | 6 | 39 | | current.AddMonths(1).AddDays(-1); |
| | | 40 | | |
| | 6 | 41 | | if (monthEnd > left.End.Value.Value) |
| | | 42 | | { |
| | 3 | 43 | | monthEnd = left.End.Value.Value; |
| | | 44 | | } |
| | | 45 | | |
| | 6 | 46 | | yield return new(monthStart, monthEnd); |
| | | 47 | | |
| | 6 | 48 | | current = current.AddMonths(1); |
| | | 49 | | } |
| | 3 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Splits the date range into multiple date ranges, each representing a single week. The resulting date ranges |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="firstDayOfWeek">The first day of the week to use when splitting the date range.</param> |
| | | 56 | | /// <returns>An enumerable of date ranges, each representing a single week.</returns> |
| | | 57 | | public IEnumerable<DateRange> SplitByWeek(DayOfWeek firstDayOfWeek = DayOfWeek.Monday) |
| | | 58 | | { |
| | 3 | 59 | | var current = left.Start!.Value.Value; |
| | | 60 | | |
| | 3 | 61 | | while (current <= left.End!.Value.Value) |
| | | 62 | | { |
| | 3 | 63 | | var offset = ((int)current.DayOfWeek - (int)firstDayOfWeek + 7) % 7; |
| | | 64 | | |
| | 3 | 65 | | var weekStart = current.AddDays(-offset); |
| | | 66 | | |
| | 3 | 67 | | if (weekStart < left.Start.Value.Value) |
| | | 68 | | { |
| | 0 | 69 | | weekStart = left.Start.Value.Value; |
| | | 70 | | } |
| | | 71 | | |
| | 3 | 72 | | var weekEnd = weekStart.AddDays(6); |
| | | 73 | | |
| | 3 | 74 | | if (weekEnd > left.End.Value.Value) |
| | | 75 | | { |
| | 0 | 76 | | weekEnd = left.End.Value.Value; |
| | | 77 | | } |
| | | 78 | | |
| | 3 | 79 | | yield return new(weekStart, weekEnd); |
| | | 80 | | |
| | 0 | 81 | | current = weekEnd.AddDays(1); |
| | | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Expands the date range by a specified number of days on both sides. The resulting date range will start the |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="days">The number of days to expand the date range by on each side.</param> |
| | | 89 | | /// <returns>A new date range expanded by the specified number of days on both sides.</returns> |
| | | 90 | | public DateRange Expand(int days) |
| | 3 | 91 | | => new(left.Start!.Value.Value.AddDays(-days), left.End!.Value.Value.AddDays(days)); |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |