< Summary

Information
Class: MyNet.Temporal.Decomposition.TimeSpanDecomposer
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Decomposition/TimeSpanDecomposer.cs
Tag: 342_31701152804
Line coverage
95%
Covered lines: 59
Uncovered lines: 3
Coverable lines: 62
Total lines: 181
Line coverage: 95.1%
Branch coverage
82%
Covered branches: 28
Total branches: 34
Branch coverage: 82.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Decompose(...)66.66%9990%
DecomposeHierarchical(...)100%66100%
DecomposeFlat(...)100%11100%
DecomposeLargestOnly(...)100%11100%
DecomposeSmallestOnly(...)100%11100%
ApplyPostProcessing(...)75%88100%
GetAbsoluteTicks(...)50%22100%
GetUnitTicks(...)88.88%9991.66%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TimeSpanDecomposer.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 System.Linq;
 10using MyNet.Primitives;
 11
 12namespace MyNet.Temporal.Decomposition;
 13
 14/// <summary>
 15/// Decomposes a <see cref="TimeSpan"/> into ordered unit values using strict arithmetic.
 16/// </summary>
 17public sealed class TimeSpanDecomposer : ITimeSpanDecomposer
 18{
 19    /// <summary>
 20    /// Gets the default decomposer instance.
 21    /// </summary>
 622    public static TimeSpanDecomposer Default { get; } = new();
 23
 24    /// <inheritdoc />
 25    public IReadOnlyList<TimeUnitValue> Decompose(TimeSpan timeSpan, TimeSpanDecompositionOptions options)
 26    {
 34227        ArgumentNullException.ThrowIfNull(options);
 34228        ArgumentNullException.ThrowIfNull(options.RuleEngine);
 29
 34230        var ticks = GetAbsoluteTicks(timeSpan);
 31
 34232        var units = options.RuleEngine
 34233            .Select(timeSpan, options.MinUnit, options.MaxUnit)
 34234            .Distinct()
 34235            .OrderByDescending(GetUnitTicks)
 34236            .ToList();
 37
 34238        if (units.Count == 0)
 039            return [new TimeUnitValue(0, options.MinUnit)];
 40
 34241        var result = options.Mode switch
 34242        {
 32143            TimeSpanDecompositionMode.Hierarchical => DecomposeHierarchical(ticks, units),
 344            TimeSpanDecompositionMode.Flat => DecomposeFlat(ticks, units),
 1245            TimeSpanDecompositionMode.LargestUnitOnly => DecomposeLargestOnly(ticks, units),
 646            TimeSpanDecompositionMode.SmallestUnitOnly => DecomposeSmallestOnly(ticks, units),
 047            _ => DecomposeHierarchical(ticks, units)
 34248        };
 49
 34250        result = ApplyPostProcessing(result, options);
 51
 34252        return result.Count == 0 ? [new TimeUnitValue(0, options.MinUnit)] : result;
 53    }
 54
 55    // --------------------------------------------------
 56    // Core decomposition strategies
 57    // --------------------------------------------------
 58
 59    /// <summary>
 60    /// Decomposes the given ticks into unit values for all specified units using a hierarchical subtraction approach. E
 61    /// </summary>
 62    /// <param name="ticks">The number of ticks to decompose.</param>
 63    /// <param name="units">The list of time units to consider.</param>
 64    /// <returns>A list of TimeUnitValue representing the timespan in each specified unit.</returns>
 65    private static List<TimeUnitValue> DecomposeHierarchical(long ticks, List<TimeUnit> units)
 66    {
 32167        var remaining = ticks;
 32168        var result = new List<TimeUnitValue>(units.Count);
 69
 171670        foreach (var unit in units)
 71        {
 69372            var unitTicks = GetUnitTicks(unit);
 69373            if (unitTicks <= 0)
 74                continue;
 75
 69376            var value = (int)(remaining / unitTicks);
 69377            result.Add(new TimeUnitValue(value, unit));
 78
 69379            remaining -= value * unitTicks;
 69380            if (remaining <= 0)
 31281                break;
 82        }
 83
 32184        return result;
 85    }
 86
 87    /// <summary>
 88    /// Decomposes the given ticks into unit values for all specified units without performing hierarchical subtraction.
 89    /// </summary>
 90    /// <param name="ticks">The number of ticks to decompose.</param>
 91    /// <param name="units">The list of time units to consider.</param>
 92    /// <returns>A list of TimeUnitValue representing the timespan in each specified unit.</returns>
 93    private static List<TimeUnitValue> DecomposeFlat(long ticks, List<TimeUnit> units)
 94    {
 395        var result = new List<TimeUnitValue>(units.Count);
 396        result.AddRange(from unit in units let unitTicks = GetUnitTicks(unit) where unitTicks > 0 let value = (int)(tick
 97
 398        return result;
 99    }
 100
 101    /// <summary>
 102    /// Decomposes the given ticks into a single unit value based on the largest unit in the provided list. This strateg
 103    /// </summary>
 104    /// <param name="ticks">The number of ticks to decompose.</param>
 105    /// <param name="units">The list of time units to consider.</param>
 106    /// <returns>A list containing a single TimeUnitValue representing the entire timespan in the largest unit.</returns
 107    private static List<TimeUnitValue> DecomposeLargestOnly(long ticks, List<TimeUnit> units)
 108    {
 12109        var unit = units[0];
 12110        var value = (int)(ticks / GetUnitTicks(unit));
 111
 12112        return [new TimeUnitValue(value, unit)];
 113    }
 114
 115    /// <summary>
 116    /// Decomposes the given ticks into a single unit value based on the smallest unit in the provided list. This strate
 117    /// </summary>
 118    /// <param name="ticks">The number of ticks to decompose.</param>
 119    /// <param name="units">The list of time units to consider.</param>
 120    /// <returns>A list containing a single TimeUnitValue representing the entire timespan in the smallest unit.</return
 121    private static List<TimeUnitValue> DecomposeSmallestOnly(long ticks, List<TimeUnit> units)
 122    {
 6123        var unit = units[^1];
 6124        var value = (int)(ticks / GetUnitTicks(unit));
 125
 6126        return [new TimeUnitValue(value, unit)];
 127    }
 128
 129    /// <summary>
 130    /// Applies post-processing steps such as quantization, zero-unit filtering, and component limiting to the decompose
 131    /// </summary>
 132    /// <param name="input">The list of decomposed time unit values.</param>
 133    /// <param name="options">The decomposition options to apply.</param>
 134    /// <returns>The post-processed list of time unit values.</returns>
 135    private static List<TimeUnitValue> ApplyPostProcessing(List<TimeUnitValue> input, TimeSpanDecompositionOptions optio
 136    {
 342137        var result = input;
 138
 342139        if (options.Quantizer is not null)
 9140            result = [.. options.Quantizer.Quantize(result)];
 141
 342142        if (!options.IncludeZeroUnits)
 333143            result = [.. result.Where(x => x.Value != 0)];
 144
 342145        if (options.MaxComponents is > 0)
 318146            result = [.. result.Take(options.MaxComponents.Value)];
 147
 342148        return result;
 149    }
 150
 151    // --------------------------------------------------
 152    // Helpers
 153    // --------------------------------------------------
 154
 155    /// <summary>
 156    /// Gets the absolute value of ticks, treating long.MinValue as long.MaxValue to avoid overflow issues.
 157    /// </summary>
 158    /// <param name="timeSpan">The TimeSpan to get the absolute ticks from.</param>
 159    /// <returns>The absolute value of ticks.</returns>
 342160    private static long GetAbsoluteTicks(TimeSpan timeSpan) => timeSpan.Ticks == long.MinValue ? long.MaxValue : Math.Ab
 161
 162    /// <summary>
 163    /// Gets the number of ticks corresponding to a given time unit, using fixed average values for variable-length unit
 164    /// </summary>
 165    /// <param name="unit">The time unit.</param>
 166    /// <returns>The number of ticks for the specified time unit.</returns>
 167    private static long GetUnitTicks(TimeUnit unit)
 1743168        => unit switch
 1743169        {
 36170            TimeUnit.Year => (long)(DateTimeHelper.DaysPerYear * TimeSpan.TicksPerDay),
 153171            TimeUnit.Month => (long)(DateTimeHelper.DaysPerMonth * TimeSpan.TicksPerDay),
 72172            TimeUnit.Week => TimeSpan.TicksPerDay * DateTimeHelper.DaysPerWeek,
 273173            TimeUnit.Day => TimeSpan.TicksPerDay,
 354174            TimeUnit.Hour => TimeSpan.TicksPerHour,
 450175            TimeUnit.Minute => TimeSpan.TicksPerMinute,
 390176            TimeUnit.Second => TimeSpan.TicksPerSecond,
 15177            TimeUnit.Millisecond => TimeSpan.TicksPerMillisecond,
 0178            _ => 1L
 1743179        };
 180}
 181