| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeUnitRuleSelector.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using MyNet.Primitives; |
| | | 10 | | |
| | | 11 | | namespace 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> |
| | | 19 | | public sealed class TimeUnitRuleSelector : ITimeUnitRuleEngine |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets a new instance of the <see cref="TimeUnitRuleSelector"/> class. |
| | | 23 | | /// </summary> |
| | 6 | 24 | | 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 | | { |
| | 354 | 38 | | if (minUnit > maxUnit) |
| | 3 | 39 | | throw new ArgumentException($"{nameof(minUnit)} cannot be greater than {nameof(maxUnit)}."); |
| | | 40 | | |
| | 351 | 41 | | var absolute = span.Duration(); |
| | 351 | 42 | | var largest = minUnit; |
| | | 43 | | |
| | 2814 | 44 | | for (var unit = maxUnit; unit >= minUnit; unit--) |
| | | 45 | | { |
| | 1404 | 46 | | if (!(GetTotal(absolute, unit) >= 1d)) continue; |
| | 348 | 47 | | largest = unit; |
| | 348 | 48 | | break; |
| | | 49 | | } |
| | | 50 | | |
| | 351 | 51 | | var result = new List<TimeUnit>(); |
| | 3054 | 52 | | for (var unit = largest; unit >= minUnit; unit--) |
| | | 53 | | { |
| | 1176 | 54 | | if (largest >= TimeUnit.Month && unit == TimeUnit.Week) |
| | | 55 | | continue; |
| | | 56 | | |
| | 1098 | 57 | | result.Add(unit); |
| | | 58 | | } |
| | | 59 | | |
| | 351 | 60 | | 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> |
| | 1404 | 69 | | private static double GetTotal(TimeSpan span, TimeUnit unit) => unit switch |
| | 1404 | 70 | | { |
| | 315 | 71 | | TimeUnit.Year => span.TotalDays / DateTimeHelper.DaysPerYear, |
| | 297 | 72 | | TimeUnit.Month => span.TotalDays / DateTimeHelper.DaysPerMonth, |
| | 234 | 73 | | TimeUnit.Week => span.TotalDays / DateTimeHelper.DaysPerWeek, |
| | 198 | 74 | | TimeUnit.Day => span.TotalDays, |
| | 192 | 75 | | TimeUnit.Hour => span.TotalHours, |
| | 123 | 76 | | TimeUnit.Minute => span.TotalMinutes, |
| | 45 | 77 | | TimeUnit.Second => span.TotalSeconds, |
| | 0 | 78 | | _ => span.TotalMilliseconds |
| | 1404 | 79 | | }; |
| | | 80 | | } |
| | | 81 | | |