< Summary

Information
Class: MyNet.Temporal.Decomposition.TimeUnitRuleSelector
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Temporal/Decomposition/TimeUnitRuleSelector.cs
Tag: 323_28699572109
Line coverage
96%
Covered lines: 24
Uncovered lines: 1
Coverable lines: 25
Total lines: 84
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/85372080fe102cd9ee155ceab49ae000e7f66103/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;
 10using MyNet.Primitives.Temporal;
 11
 12namespace MyNet.Temporal.Decomposition;
 13
 14/// <summary>
 15/// Selects time units for decomposing a TimeSpan based on its magnitude and specified minimum and maximum units.
 16/// </summary>
 17/// <remarks>
 18/// Strict behavior: this selector chooses units only, without any fuzzy approximation.
 19/// </remarks>
 20public sealed class TimeUnitRuleSelector : ITimeUnitRuleEngine
 21{
 22    /// <summary>
 23    /// Gets a new instance of the <see cref="TimeUnitRuleSelector"/> class.
 24    /// </summary>
 625    public static TimeUnitRuleSelector Default { get; } = new();
 26
 27    /// <summary>
 28    /// Selects the TimeUnits to use for a given TimeSpan, based on the provided minimum and maximum TimeUnits.
 29    /// </summary>
 30    /// <param name="span">The TimeSpan to decompose.</param>
 31    /// <param name="minUnit">The minimum TimeUnit to include in the decomposition.</param>
 32    /// <param name="maxUnit">The maximum TimeUnit to include in the decomposition.</param>
 33    /// <returns>A list of TimeUnits to use for the decomposition.</returns>
 34    /// <example>
 35    /// 63 minutes with min=Minute and max=Hour returns [Hour, Minute].
 36    /// </example>
 37    public IReadOnlyList<TimeUnit> Select(TimeSpan span, TimeUnit minUnit, TimeUnit maxUnit)
 38    {
 35439        if (minUnit > maxUnit)
 340            throw new ArgumentException($"{nameof(minUnit)} cannot be greater than {nameof(maxUnit)}.");
 41
 35142        var absolute = span.Duration();
 35143        var largest = minUnit;
 44
 281445        for (var unit = maxUnit; unit >= minUnit; unit--)
 46        {
 140447            if (GetTotal(absolute, unit) >= 1d)
 48            {
 34849                largest = unit;
 34850                break;
 51            }
 52        }
 53
 35154        var result = new List<TimeUnit>();
 305455        for (var unit = largest; unit >= minUnit; unit--)
 56        {
 117657            if (largest >= TimeUnit.Month && unit == TimeUnit.Week)
 58                continue;
 59
 109860            result.Add(unit);
 61        }
 62
 35163        return result;
 64    }
 65
 66    /// <summary>
 67    /// Calculates the total amount of the specified TimeUnit in the given TimeSpan.
 68    /// </summary>
 69    /// <param name="span">The TimeSpan to evaluate.</param>
 70    /// <param name="unit">The TimeUnit to calculate the total for.</param>
 71    /// <returns>The total amount of the specified TimeUnit in the given TimeSpan.</returns>
 140472    private static double GetTotal(TimeSpan span, TimeUnit unit) => unit switch
 140473    {
 31574        TimeUnit.Year => span.TotalDays / DateTimeHelper.DaysPerYear,
 29775        TimeUnit.Month => span.TotalDays / DateTimeHelper.DaysPerMonth,
 23476        TimeUnit.Week => span.TotalDays / DateTimeHelper.DaysPerWeek,
 19877        TimeUnit.Day => span.TotalDays,
 19278        TimeUnit.Hour => span.TotalHours,
 12379        TimeUnit.Minute => span.TotalMinutes,
 4580        TimeUnit.Second => span.TotalSeconds,
 081        _ => span.TotalMilliseconds
 140482    };
 83}
 84