< Summary

Information
Class: MyNet.Temporal.DateRangeExtensions
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Extensions/DateRangeExtensions.cs
Tag: 342_31701152804
Line coverage
79%
Covered lines: 19
Uncovered lines: 5
Coverable lines: 24
Total lines: 98
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
IsConsecutiveWith(...)0%620%
SplitByMonth()100%66100%
SplitByWeek()50%7666.66%
Expand(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.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.Temporal.Intervals;
 10
 11#pragma warning disable IDE0130 // Namespace does not match folder structure
 12namespace MyNet.Temporal;
 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>
 24        /// <summary>
 25        /// Returns whether the ranges are consecutive calendar days (end+1 day equals the other start).
 26        /// For shared-bound adjacency of interval algebra, use the generic interval <c>IsAdjacentTo</c> method.
 27        /// </summary>
 028        public bool IsConsecutiveWith(DateRange right) => left.End!.Value.Value.AddDays(1) == right.Start!.Value.Value |
 29
 30        /// <summary>
 31        /// Splits the date range into multiple date ranges, each representing a single month. The resulting date ranges
 32        /// </summary>
 33        /// <returns>An enumerable of date ranges, each representing a single month.</returns>
 34        public IEnumerable<DateRange> SplitByMonth()
 35        {
 336            var current = new DateOnly(left.Start!.Value.Value.Year, left.Start.Value.Value.Month, 1);
 37
 938            while (current <= left.End!.Value.Value)
 39            {
 640                var monthStart = current < left.Start.Value.Value ? left.Start.Value.Value : current;
 41
 642                var monthEnd =
 643                    current.AddMonths(1).AddDays(-1);
 44
 645                if (monthEnd > left.End.Value.Value)
 46                {
 347                    monthEnd = left.End.Value.Value;
 48                }
 49
 650                yield return new DateRange(monthStart, monthEnd);
 51
 652                current = current.AddMonths(1);
 53            }
 354        }
 55
 56        /// <summary>
 57        /// Splits the date range into multiple date ranges, each representing a single week. The resulting date ranges 
 58        /// </summary>
 59        /// <param name="firstDayOfWeek">The first day of the week to use when splitting the date range.</param>
 60        /// <returns>An enumerable of date ranges, each representing a single week.</returns>
 61        public IEnumerable<DateRange> SplitByWeek(DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
 62        {
 363            var current = left.Start!.Value.Value;
 64
 365            while (current <= left.End!.Value.Value)
 66            {
 367                var offset = ((int)current.DayOfWeek - (int)firstDayOfWeek + 7) % 7;
 68
 369                var weekStart = current.AddDays(-offset);
 70
 371                if (weekStart < left.Start.Value.Value)
 72                {
 073                    weekStart = left.Start.Value.Value;
 74                }
 75
 376                var weekEnd = weekStart.AddDays(6);
 77
 378                if (weekEnd > left.End.Value.Value)
 79                {
 080                    weekEnd = left.End.Value.Value;
 81                }
 82
 383                yield return new DateRange(weekStart, weekEnd);
 84
 085                current = weekEnd.AddDays(1);
 86            }
 087        }
 88
 89        /// <summary>
 90        /// Expands the date range by a specified number of days on both sides. The resulting date range will start the 
 91        /// </summary>
 92        /// <param name="days">The number of days to expand the date range by on each side.</param>
 93        /// <returns>A new date range expanded by the specified number of days on both sides.</returns>
 94        public DateRange Expand(int days)
 395            => new(left.Start!.Value.Value.AddDays(-days), left.End!.Value.Value.AddDays(days));
 96    }
 97}
 98