< Summary

Information
Class: MyNet.Temporal.DateTimeOffsetPeriodExtensions
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Intervals/Extensions/DateTimeOffsetPeriodExtensions.cs
Tag: 342_31701152804
Line coverage
0%
Covered lines: 0
Uncovered lines: 25
Coverable lines: 25
Total lines: 100
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
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()0%620%
EnumerateDays()0%620%
TimeGap(...)0%2040%
get_StartTimeOfDay(...)100%210%
EndTimeOfDay(...)0%4260%
get_SpansMidnight(...)100%210%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DateTimeOffsetPeriodExtensions.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="DateTimeOffsetPeriod"/>. Status helpers require an explicit <c>now</c>;
 18/// they do not call <see cref="DateTimeOffset.Now"/>.
 19/// </summary>
 20public static class DateTimeOffsetPeriodExtensions
 21{
 22    extension(DateTimeOffsetPeriod left)
 23    {
 24        /// <summary>
 25        /// Returns whether <paramref name="now"/> lies inside the period.
 26        /// </summary>
 027        public bool IsCurrent(DateTimeOffset now) => left.Contains(now);
 28
 29        /// <summary>
 30        /// Returns whether the period is entirely before <paramref name="now"/>.
 31        /// </summary>
 032        public bool IsPast(DateTimeOffset now) => left.IsPast(now);
 33
 34        /// <summary>
 35        /// Returns whether the period is entirely after <paramref name="now"/>.
 36        /// </summary>
 037        public bool IsFuture(DateTimeOffset now) => left.IsFuture(now);
 38
 39        /// <summary>
 40        /// Enumerates hour ticks from start (inclusive) while strictly before end.
 41        /// </summary>
 42        public IEnumerable<DateTimeOffset> EnumerateHours()
 43        {
 044            for (var current = left.Start!.Value.Value;
 045                 current < left.End!.Value.Value;
 046                 current = current.AddHours(1))
 47            {
 048                yield return current;
 49            }
 050        }
 51
 52        /// <summary>
 53        /// Enumerates calendar-day midnights (in the start offset) from the start date while strictly before end.
 54        /// </summary>
 55        public IEnumerable<DateTimeOffset> EnumerateDays()
 56        {
 057            var start = left.Start!.Value.Value;
 058            for (var current = new DateTimeOffset(start.Year, start.Month, start.Day, 0, 0, 0, start.Offset);
 059                 current < left.End!.Value.Value;
 060                 current = current.AddDays(1))
 61            {
 062                yield return current;
 63            }
 064        }
 65
 66        /// <summary>
 67        /// Fixed-duration gap between two non-overlapping periods; <see cref="TimeSpan.Zero"/> if they overlap.
 68        /// For the gap as an interval, use the generic interval <c>Gap</c> method.
 69        /// </summary>
 70        public TimeSpan TimeGap(DateTimeOffsetPeriod right) =>
 071            left.Overlaps(right)
 072                ? TimeSpan.Zero
 073                : left.End!.Value.Value < right.Start!.Value.Value
 074                    ? right.Start.Value.Value - left.End.Value.Value
 075                    : left.Start!.Value.Value - right.End!.Value.Value;
 76
 77        /// <summary>
 78        /// Gets the start time-of-day component.
 79        /// </summary>
 080        public TimeSpan StartTimeOfDay => left.Start!.Value.Value.TimeOfDay;
 81
 82        /// <summary>
 83        /// End time-of-day, collapsing sub-minute intervals to the start time when below <paramref name="minimumDuratio
 84        /// </summary>
 85        public TimeSpan EndTimeOfDay(TimeSpan minimumDuration = default)
 86        {
 087            var minimum = minimumDuration == TimeSpan.Zero ? TimeSpan.FromMinutes(1) : minimumDuration;
 088            var start = left.StartTimeOfDay;
 089            var end = left.End!.Value.Value.TimeOfDay;
 90
 091            return end > start && end - start < minimum ? start : end;
 92        }
 93
 94        /// <summary>
 95        /// Gets a value indicating whether the exclusive end falls on a later calendar day than the start (using each b
 96        /// </summary>
 097        public bool SpansMidnight => left.End!.Value.Value.Date > left.Start!.Value.Value.Date;
 98    }
 99}
 100