< Summary

Information
Class: MyNet.Temporal.Decomposition.TimeUnitRuleSelector
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Decomposition/TimeUnitRuleSelector.cs
Tag: 342_31701152804
Line coverage
96%
Covered lines: 24
Uncovered lines: 1
Coverable lines: 25
Total lines: 81
Line coverage: 96%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Select(...)100%1212100%
GetTotal(...)87.5%8890.9%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Decomposition/TimeUnitRuleSelector.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TimeUnitRuleSelector.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;
 10
 11namespace MyNet.Temporal.Decomposition;
 12
 13/// <summary>
 14/// Selects time units for decomposing a TimeSpan based on its magnitude and specified minimum and maximum units.
 15/// </summary>
 16/// <remarks>
 17/// Strict behavior: this selector chooses units only, without any fuzzy approximation.
 18/// </remarks>
 19public sealed class TimeUnitRuleSelector : ITimeUnitRuleEngine
 20{
 21    /// <summary>
 22    /// Gets a new instance of the <see cref="TimeUnitRuleSelector"/> class.
 23    /// </summary>
 624    public static TimeUnitRuleSelector Default { get; } = new();
 25
 26    /// <summary>
 27    /// Selects the TimeUnits to use for a given TimeSpan, based on the provided minimum and maximum TimeUnits.
 28    /// </summary>
 29    /// <param name="span">The TimeSpan to decompose.</param>
 30    /// <param name="minUnit">The minimum TimeUnit to include in the decomposition.</param>
 31    /// <param name="maxUnit">The maximum TimeUnit to include in the decomposition.</param>
 32    /// <returns>A list of TimeUnits to use for the decomposition.</returns>
 33    /// <example>
 34    /// 63 minutes with min=Minute and max=Hour returns [Hour, Minute].
 35    /// </example>
 36    public IReadOnlyList<TimeUnit> Select(TimeSpan span, TimeUnit minUnit, TimeUnit maxUnit)
 37    {
 35438        if (minUnit > maxUnit)
 339            throw new ArgumentException($"{nameof(minUnit)} cannot be greater than {nameof(maxUnit)}.");
 40
 35141        var absolute = span.Duration();
 35142        var largest = minUnit;
 43
 281444        for (var unit = maxUnit; unit >= minUnit; unit--)
 45        {
 140446            if (!(GetTotal(absolute, unit) >= 1d)) continue;
 34847            largest = unit;
 34848            break;
 49        }
 50
 35151        var result = new List<TimeUnit>();
 305452        for (var unit = largest; unit >= minUnit; unit--)
 53        {
 117654            if (largest >= TimeUnit.Month && unit == TimeUnit.Week)
 55                continue;
 56
 109857            result.Add(unit);
 58        }
 59
 35160        return result;
 61    }
 62
 63    /// <summary>
 64    /// Calculates the total amount of the specified TimeUnit in the given TimeSpan.
 65    /// </summary>
 66    /// <param name="span">The TimeSpan to evaluate.</param>
 67    /// <param name="unit">The TimeUnit to calculate the total for.</param>
 68    /// <returns>The total amount of the specified TimeUnit in the given TimeSpan.</returns>
 140469    private static double GetTotal(TimeSpan span, TimeUnit unit) => unit switch
 140470    {
 31571        TimeUnit.Year => span.TotalDays / DateTimeHelper.DaysPerYear,
 29772        TimeUnit.Month => span.TotalDays / DateTimeHelper.DaysPerMonth,
 23473        TimeUnit.Week => span.TotalDays / DateTimeHelper.DaysPerWeek,
 19874        TimeUnit.Day => span.TotalDays,
 19275        TimeUnit.Hour => span.TotalHours,
 12376        TimeUnit.Minute => span.TotalMinutes,
 4577        TimeUnit.Second => span.TotalSeconds,
 078        _ => span.TotalMilliseconds
 140479    };
 80}
 81