< Summary

Information
Line coverage
87%
Covered lines: 21
Uncovered lines: 3
Coverable lines: 24
Total lines: 99
Line coverage: 87.5%
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%210%
IsPast(...)100%210%
IsFuture(...)100%210%
EnumerateHours()100%22100%
EnumerateDays()100%22100%
TimeGap(...)75%44100%
get_StartTimeOfDay(...)100%11100%
EndTimeOfDay(...)50%66100%
get_SpansMidnight(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Intervals/Extensions/DateTimePeriodExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DateTimePeriodExtensions.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;
 10using MyNet.Temporal.Intervals;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Temporal;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16/// <summary>
 17/// Fluent helpers for <see cref="DateTimePeriod"/>. Status helpers require an explicit <c>now</c>;
 18/// they do not call <see cref="DateTime.Now"/>.
 19/// </summary>
 20public static class DateTimePeriodExtensions
 21{
 22    extension(DateTimePeriod left)
 23    {
 24        /// <summary>
 25        /// Returns whether <paramref name="now"/> lies inside the period.
 26        /// </summary>
 027        public bool IsCurrent(DateTime now) => left.Contains(now);
 28
 29        /// <summary>
 30        /// Returns whether the period is entirely before <paramref name="now"/>.
 31        /// </summary>
 032        public bool IsPast(DateTime now) => left.IsPast(now);
 33
 34        /// <summary>
 35        /// Returns whether the period is entirely after <paramref name="now"/>.
 36        /// </summary>
 037        public bool IsFuture(DateTime now) => left.IsFuture(now);
 38
 39        /// <summary>
 40        /// Enumerates hour ticks from start (inclusive) while strictly before end.
 41        /// </summary>
 42        public IEnumerable<DateTime> EnumerateHours()
 43        {
 644            for (var current = left.Start!.Value.Value;
 1845                 current < left.End!.Value.Value;
 1246                 current = current.AddHours(1))
 47            {
 1248                yield return current;
 49            }
 650        }
 51
 52        /// <summary>
 53        /// Enumerates calendar-day midnights from start.Date while strictly before end.
 54        /// </summary>
 55        public IEnumerable<DateTime> EnumerateDays()
 56        {
 357            for (var current = left.Start!.Value.Value.Date;
 1558                 current < left.End!.Value.Value;
 1259                 current = current.AddDays(1))
 60            {
 1261                yield return current;
 62            }
 363        }
 64
 65        /// <summary>
 66        /// Fixed-duration gap between two non-overlapping periods; <see cref="TimeSpan.Zero"/> if they overlap.
 67        /// For the gap as an interval, use the generic interval <c>Gap</c> method.
 68        /// </summary>
 69        public TimeSpan TimeGap(DateTimePeriod right) =>
 670            left.Overlaps(right)
 671                ? TimeSpan.Zero
 672                : left.End!.Value.Value < right.Start!.Value.Value
 673                    ? right.Start.Value.Value - left.End.Value.Value
 674                    : left.Start!.Value.Value - right.End!.Value.Value;
 75
 76        /// <summary>
 77        /// Gets the start time-of-day component.
 78        /// </summary>
 679        public TimeSpan StartTimeOfDay => left.Start!.Value.Value.TimeOfDay;
 80
 81        /// <summary>
 82        /// Gets the end time-of-day, collapsing sub-minute intervals to the start time when below <paramref name="minim
 83        /// </summary>
 84        public TimeSpan EndTimeOfDay(TimeSpan minimumDuration = default)
 85        {
 386            var minimum = minimumDuration == TimeSpan.Zero ? TimeSpan.FromMinutes(1) : minimumDuration;
 387            var start = left.StartTimeOfDay;
 388            var end = left.End!.Value.Value.TimeOfDay;
 89
 390            return end > start && end - start < minimum ? start : end;
 91        }
 92
 93        /// <summary>
 94        /// Gets a value indicating whether the exclusive end falls on a later calendar day than the start.
 95        /// </summary>
 396        public bool SpansMidnight => left.End!.Value.Value.Date > left.Start!.Value.Value.Date;
 97    }
 98}
 99