| | | 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 | | using MyNet.Primitives.Temporal; |
| | | 11 | | |
| | | 12 | | namespace 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> |
| | | 20 | | public sealed class TimeUnitRuleSelector : ITimeUnitRuleEngine |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets a new instance of the <see cref="TimeUnitRuleSelector"/> class. |
| | | 24 | | /// </summary> |
| | 6 | 25 | | 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 | | { |
| | 354 | 39 | | if (minUnit > maxUnit) |
| | 3 | 40 | | throw new ArgumentException($"{nameof(minUnit)} cannot be greater than {nameof(maxUnit)}."); |
| | | 41 | | |
| | 351 | 42 | | var absolute = span.Duration(); |
| | 351 | 43 | | var largest = minUnit; |
| | | 44 | | |
| | 2814 | 45 | | for (var unit = maxUnit; unit >= minUnit; unit--) |
| | | 46 | | { |
| | 1404 | 47 | | if (GetTotal(absolute, unit) >= 1d) |
| | | 48 | | { |
| | 348 | 49 | | largest = unit; |
| | 348 | 50 | | break; |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | 351 | 54 | | var result = new List<TimeUnit>(); |
| | 3054 | 55 | | for (var unit = largest; unit >= minUnit; unit--) |
| | | 56 | | { |
| | 1176 | 57 | | if (largest >= TimeUnit.Month && unit == TimeUnit.Week) |
| | | 58 | | continue; |
| | | 59 | | |
| | 1098 | 60 | | result.Add(unit); |
| | | 61 | | } |
| | | 62 | | |
| | 351 | 63 | | 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> |
| | 1404 | 72 | | private static double GetTotal(TimeSpan span, TimeUnit unit) => unit switch |
| | 1404 | 73 | | { |
| | 315 | 74 | | TimeUnit.Year => span.TotalDays / DateTimeHelper.DaysPerYear, |
| | 297 | 75 | | TimeUnit.Month => span.TotalDays / DateTimeHelper.DaysPerMonth, |
| | 234 | 76 | | TimeUnit.Week => span.TotalDays / DateTimeHelper.DaysPerWeek, |
| | 198 | 77 | | TimeUnit.Day => span.TotalDays, |
| | 192 | 78 | | TimeUnit.Hour => span.TotalHours, |
| | 123 | 79 | | TimeUnit.Minute => span.TotalMinutes, |
| | 45 | 80 | | TimeUnit.Second => span.TotalSeconds, |
| | 0 | 81 | | _ => span.TotalMilliseconds |
| | 1404 | 82 | | }; |
| | | 83 | | } |
| | | 84 | | |