< Summary

Information
Line coverage
92%
Covered lines: 24
Uncovered lines: 2
Coverable lines: 26
Total lines: 115
Line coverage: 92.3%
Branch coverage
71%
Covered branches: 10
Total branches: 14
Branch coverage: 71.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsCurrent(...)100%11100%
IsPast(...)100%11100%
IsFuture(...)100%11100%
Shift(...)100%210%
Extend(...)100%210%
EnumerateHours()100%22100%
EnumerateDays()100%22100%
Gap(...)75%44100%
get_StartTimeOfDay(...)100%11100%
EndTimeOfDay(...)50%66100%
get_SpansMidnight(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Intervals/Extensions/PeriodExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="PeriodExtensions.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 PeriodExtensions
 16{
 17    extension(Period left)
 18    {
 19        /// <summary>
 20        /// Determines whether the current period is current, meaning that the current date and time falls within the pe
 21        /// </summary>
 22        /// <returns>True if the current date and time falls within the period; otherwise, false.</returns>
 323        public bool IsCurrent() => left.Contains(DateTime.Now);
 24
 25        /// <summary>
 26        /// Determines whether the current period is in the past, meaning that the end of the period is before the curre
 27        /// </summary>
 28        /// <returns>True if the period is in the past; otherwise, false.</returns>
 629        public bool IsPast() => left.IsPast(DateTime.Now);
 30
 31        /// <summary>
 32        /// Determines whether the current period is in the future, meaning that the start of the period is after the cu
 33        /// </summary>
 34        /// <returns>True if the period is in the future; otherwise, false.</returns>
 635        public bool IsFuture() => left.IsFuture(DateTime.Now);
 36
 37        /// <summary>
 38        /// Shifts the current period by a specified offset. This method creates a new period with the start and end tim
 39        /// </summary>
 40        /// <param name="offset">The time span to shift the period by.</param>
 41        /// <returns>A new period shifted by the specified offset.</returns>
 042        public Period Shift(TimeSpan offset) => new(left.Start!.Value.Value + offset, left.End!.Value.Value + offset);
 43
 44        /// <summary>
 45        /// Extends the current period by a specified duration. This method creates a new period with the end time exten
 46        /// </summary>
 47        /// <param name="duration">The duration to extend the period by.</param>
 48        /// <returns>A new period extended by the specified duration.</returns>
 049        public Period Extend(TimeSpan duration) => new(left.Start!.Value.Value, left.End!.Value.Value + duration);
 50
 51        /// <summary>
 52        /// Enumerates the hours within the current period. This method generates a sequence of DateTime values represen
 53        /// </summary>
 54        /// <returns>An enumerable of DateTime values for each hour within the period.</returns>
 55        public IEnumerable<DateTime> EnumerateHours()
 56        {
 657            for (var current = left.Start!.Value.Value;
 1858                 current < left.End!.Value.Value;
 1259                 current = current.AddHours(1))
 60            {
 1261                yield return current;
 62            }
 663        }
 64
 65        /// <summary>
 66        /// Enumerates the days within the current period. This method generates a sequence of DateTime values represent
 67        /// </summary>
 68        /// <returns>An enumerable of DateTime values for each day within the period.</returns>
 69        public IEnumerable<DateTime> EnumerateDays()
 70        {
 371            for (var current = left.Start!.Value.Value.Date;
 1272                 current < left.End!.Value.Value;
 973                 current = current.AddDays(1))
 74            {
 975                yield return current;
 76            }
 377        }
 78
 79        /// <summary>
 80        /// Calculates the gap between the current period and another period. This method returns the time span between 
 81        /// </summary>
 82        /// <param name="right">The other period to calculate the gap with.</param>
 83        /// <returns>The time span representing the gap between the two periods, or TimeSpan.Zero if they overlap.</retu
 84        public TimeSpan Gap(Period right) =>
 685            left.Overlaps(right)
 686                ? TimeSpan.Zero
 687                : left.End!.Value.Value < right.Start!.Value.Value
 688                    ? right.Start.Value.Value - left.End.Value.Value
 689                    : left.Start!.Value.Value - right.End!.Value.Value;
 90
 91        /// <summary>
 92        /// Gets the start time-of-day component of the period.
 93        /// </summary>
 694        public TimeSpan StartTimeOfDay => left.Start!.Value.Value.TimeOfDay;
 95
 96        /// <summary>
 97        /// Gets the end time-of-day component, collapsing sub-minute intervals to the start time.
 98        /// </summary>
 99        /// <param name="minimumDuration">Minimum duration before end time is returned; defaults to one minute.</param>
 100        public TimeSpan EndTimeOfDay(TimeSpan minimumDuration = default)
 101        {
 3102            var minimum = minimumDuration == TimeSpan.Zero ? TimeSpan.FromMinutes(1) : minimumDuration;
 3103            var start = left.StartTimeOfDay;
 3104            var end = left.End!.Value.Value.TimeOfDay;
 105
 3106            return end > start && end - start < minimum ? start : end;
 107        }
 108
 109        /// <summary>
 110        /// Gets a value indicating whether the period spans more than one calendar day.
 111        /// </summary>
 3112        public bool SpansMidnight => left.End!.Value.Value.Date > left.Start!.Value.Value.Date;
 113    }
 114}
 115