diff --git a/CHANGELOG.md b/CHANGELOG.md index 618f1de..92f2ac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added a method for converting 24 hour to 12 hour format - Added a method for determining if a culture uses 24 hour format - Added math util method for mod + - Added TimeSpan extension methods for cycling hours and minutes without modifying the day ## [9.8.0] - 2019-09-03 ### Added diff --git a/ICD.Common.Utils.Tests/DateTimeUtilsTest.cs b/ICD.Common.Utils.Tests/DateTimeUtilsTest.cs index a3957ee..8347322 100644 --- a/ICD.Common.Utils.Tests/DateTimeUtilsTest.cs +++ b/ICD.Common.Utils.Tests/DateTimeUtilsTest.cs @@ -9,7 +9,7 @@ namespace ICD.Common.Utils.Tests [TestCase(0, 12)] [TestCase(12, 12)] [TestCase(23, 11)] - public void Get12Hour(int hour, int expected) + public void To12HourTest(int hour, int expected) { Assert.AreEqual(expected, DateTimeUtils.To12Hour(hour)); } diff --git a/ICD.Common.Utils.Tests/Extensions/TimeSpanExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/TimeSpanExtensionsTest.cs new file mode 100644 index 0000000..740756f --- /dev/null +++ b/ICD.Common.Utils.Tests/Extensions/TimeSpanExtensionsTest.cs @@ -0,0 +1,43 @@ +using System; +using ICD.Common.Utils.Extensions; +using NUnit.Framework; + +namespace ICD.Common.Utils.Tests.Extensions +{ + [TestFixture] + public sealed class TimeSpanExtensionsTest + { + [Test] + public void ToReadableStringTest() + { + Assert.Inconclusive(); + } + + [TestCase(0, 0, 0)] + [TestCase(12, 1, 13)] + [TestCase(23, 1, 0)] + [TestCase(6, -12, 18)] + public void AddHoursAndWrapTest(int hours, int addHours, int expectedHours) + { + Assert.AreEqual(expectedHours, new TimeSpan(hours, 0, 0).AddHoursAndWrap(addHours).Hours); + } + + [TestCase(0, 0, 0)] + [TestCase(12, 1, 13)] + [TestCase(23, 1, 12)] + [TestCase(6, -12, 6)] + public void AddHoursAndWrap12Hour(int hours, int addHours, int expectedHours) + { + Assert.AreEqual(expectedHours, new TimeSpan(hours, 0, 0).AddHoursAndWrap12Hour(addHours).Hours); + } + + [TestCase(0, 0, 0)] + [TestCase(30, 1, 31)] + [TestCase(59, 1, 0)] + [TestCase(30, -60, 30)] + public void AddMinutesAndWrap(int minutes, int addMinutes, int expectedMinutes) + { + Assert.AreEqual(expectedMinutes, new TimeSpan(0, minutes, 0).AddMinutesAndWrap(addMinutes).Minutes); + } + } +} diff --git a/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs b/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs index e1e0b28..6cb9e00 100644 --- a/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs +++ b/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs @@ -25,5 +25,48 @@ namespace ICD.Common.Utils.Extensions return builder.ToString(); } + + /// + /// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day. + /// + /// + /// + /// + public static TimeSpan AddHoursAndWrap(this TimeSpan extends, int hours) + { + hours = MathUtils.Mod(hours + extends.Hours, 24); + return new TimeSpan(extends.Days, hours, extends.Minutes, extends.Seconds, extends.Milliseconds); + } + + /// + /// Adds the given number of hours to the time, wrapping within the current 12 hour span, without modifying the day. + /// + /// + /// + /// + public static TimeSpan AddHoursAndWrap12Hour(this TimeSpan extends, int hours) + { + int currentHour = extends.Hours; + bool am = extends.Hours < 12; + + int current12Hour = MathUtils.Mod(currentHour, 12); + int new12Hour = MathUtils.Mod(current12Hour + hours, 12); + + return am + ? new TimeSpan(extends.Days, new12Hour, extends.Minutes, extends.Seconds, extends.Milliseconds) + : new TimeSpan(extends.Days, new12Hour + 12, extends.Minutes, extends.Seconds, extends.Milliseconds); + } + + /// + /// Adds the given number of minutes to the time, wrapping every 60 minutes without modifying the hour. + /// + /// + /// + /// + public static TimeSpan AddMinutesAndWrap(this TimeSpan extends, int minutes) + { + minutes = MathUtils.Mod(minutes + extends.Minutes, 60); + return new TimeSpan(extends.Days, extends.Hours, minutes, extends.Seconds, extends.Milliseconds); + } } }