From 5c60b2bde052cf1c9d1c6440300c05f83d47ff43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20ANDRE?= Date: Fri, 21 Jun 2024 12:18:25 +0200 Subject: [PATCH] feat: add extension to simplify timespan --- .../Extensions/TimeSpanExtensionsTests.cs | 33 +++++++++++++++++++ .../Extensions/TimeSpanExtensions.cs | 23 +++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/MyNet.Utilities.UnitTests/Extensions/TimeSpanExtensionsTests.cs diff --git a/src/MyNet.Utilities.UnitTests/Extensions/TimeSpanExtensionsTests.cs b/src/MyNet.Utilities.UnitTests/Extensions/TimeSpanExtensionsTests.cs new file mode 100644 index 0000000..156f685 --- /dev/null +++ b/src/MyNet.Utilities.UnitTests/Extensions/TimeSpanExtensionsTests.cs @@ -0,0 +1,33 @@ +// Copyright (c) Stéphane ANDRE. All Right Reserved. +// See the LICENSE file in the project root for more information. + +using System; +using MyNet.Utilities.Units; +using Xunit; + +namespace MyNet.Utilities.UnitTests.Extensions +{ + public class TimeSpanExtensionsTests + { + [Theory] + [InlineData(0, 0, 30, 30, TimeUnit.Minute)] + [InlineData(0, 5, 0, 5, TimeUnit.Hour)] + [InlineData(2, 0, 0, 2, TimeUnit.Day)] + [InlineData(30, 0, 0, 1, TimeUnit.Month)] + [InlineData(365, 0, 0, 1, TimeUnit.Year)] + [InlineData(0, 0, 0, 0, TimeUnit.Millisecond)] + [InlineData(14, 0, 0, 2, TimeUnit.Week)] + [InlineData(60, 0, 0, 2, TimeUnit.Month)] + [InlineData(730, 0, 0, 2, TimeUnit.Year)] + [InlineData(22, 0, 0, 22, TimeUnit.Day)] + [InlineData(0, 1, 23, 83, TimeUnit.Minute)] + [InlineData(1, 0, 42, 1482, TimeUnit.Minute)] + public void Simplify_ShouldReturnExpectedResult(int days, int hours, int minutes, int expectedValue, TimeUnit expectedUnit) + { + var (value, unit) = new TimeSpan(days, hours, minutes, 0).Simplify(); + + Assert.Equal(expectedValue, value); + Assert.Equal(expectedUnit, unit); + } + } +} diff --git a/src/MyNet.Utilities/Extensions/TimeSpanExtensions.cs b/src/MyNet.Utilities/Extensions/TimeSpanExtensions.cs index 99a9be6..99e71ce 100644 --- a/src/MyNet.Utilities/Extensions/TimeSpanExtensions.cs +++ b/src/MyNet.Utilities/Extensions/TimeSpanExtensions.cs @@ -2,6 +2,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using MyNet.Utilities.DateTimes; using MyNet.Utilities.Units; @@ -172,5 +173,27 @@ public static TimeSpan Round(this TimeSpan timeSpan, RoundTo rt) TimeUnit.Year => new TimeSpan((int)Math.Round(value * DaysInAYear), 0, 0, 0, 0), _ => new TimeSpan(), }; + + public static (int value, TimeUnit unit) Simplify(this TimeSpan time) + { + var dictionary = new Dictionary checkValue, Func getTotal)> + { + { TimeUnit.Second, (new Func(x => x.Seconds != 0), new Func(x => x.TotalSeconds)) }, + { TimeUnit.Minute, (new Func(x => x.Minutes != 0), new Func(x => x.TotalMinutes)) }, + { TimeUnit.Hour, (new Func(x => x.Hours != 0), new Func(x => x.TotalHours)) }, + { TimeUnit.Year, (new Func(x => x.Days > 0 && x.Days % (int)DaysInAYear == 0), new Func(x => x.TotalDays / (int)DaysInAYear)) }, + { TimeUnit.Month, (new Func(x => x.Days > 0 && x.Days % (int)DaysInAMonth == 0), new Func(x => x.TotalDays / (int)DaysInAMonth)) }, + { TimeUnit.Week, (new Func(x => x.Days > 0 && x.Days % DaysInAWeek == 0), new Func(x => x.TotalDays / DaysInAWeek)) }, + { TimeUnit.Day, (new Func(x => x.Days != 0), new Func(x => x.TotalDays)) }, + }; + + foreach (var (unit, value) in dictionary) + { + if (value.checkValue.Invoke(time)) + return ((int)value.getTotal.Invoke(time), unit); + } + + return (0, TimeUnit.Millisecond); + } } }