< Summary

Information
Class: MyNet.Primitives.DateRangeExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Temporal/Extensions/DateRangeExtensions.cs
Tag: 323_28699572109
Line coverage
79%
Covered lines: 19
Uncovered lines: 5
Coverable lines: 24
Total lines: 94
Line coverage: 79.1%
Branch coverage
64%
Covered branches: 9
Total branches: 14
Branch coverage: 64.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsAdjacentTo(...)0%620%
SplitByMonth()100%66100%
SplitByWeek()50%7666.66%
Expand(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Temporal/Extensions/DateRangeExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DateRangeExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using MyNet.Primitives.Intervals;
 10
 11#pragma warning disable IDE0130 // Namespace does not match folder structure
 12namespace MyNet.Primitives;
 13#pragma warning restore IDE0130 // Namespace does not match folder structure
 14
 15public 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>
 024        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        {
 332            var current = new DateOnly(left.Start!.Value.Value.Year, left.Start.Value.Value.Month, 1);
 33
 934            while (current <= left.End!.Value.Value)
 35            {
 636                var monthStart = current < left.Start.Value.Value ? left.Start.Value.Value : current;
 37
 638                var monthEnd =
 639                    current.AddMonths(1).AddDays(-1);
 40
 641                if (monthEnd > left.End.Value.Value)
 42                {
 343                    monthEnd = left.End.Value.Value;
 44                }
 45
 646                yield return new(monthStart, monthEnd);
 47
 648                current = current.AddMonths(1);
 49            }
 350        }
 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        {
 359            var current = left.Start!.Value.Value;
 60
 361            while (current <= left.End!.Value.Value)
 62            {
 363                var offset = ((int)current.DayOfWeek - (int)firstDayOfWeek + 7) % 7;
 64
 365                var weekStart = current.AddDays(-offset);
 66
 367                if (weekStart < left.Start.Value.Value)
 68                {
 069                    weekStart = left.Start.Value.Value;
 70                }
 71
 372                var weekEnd = weekStart.AddDays(6);
 73
 374                if (weekEnd > left.End.Value.Value)
 75                {
 076                    weekEnd = left.End.Value.Value;
 77                }
 78
 379                yield return new(weekStart, weekEnd);
 80
 081                current = weekEnd.AddDays(1);
 82            }
 083        }
 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)
 391            => new(left.Start!.Value.Value.AddDays(-days), left.End!.Value.Value.AddDays(days));
 92    }
 93}
 94