feat: Added TimeSpan extension methods for cycling hours and minutes without modifying the day

This commit is contained in:
Chris Cameron
2019-09-09 10:06:51 -04:00
parent 45ee3e70b0
commit 9a6f197aa0
4 changed files with 88 additions and 1 deletions

View File

@@ -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 converting 24 hour to 12 hour format
- Added a method for determining if a culture uses 24 hour format - Added a method for determining if a culture uses 24 hour format
- Added math util method for mod - 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 ## [9.8.0] - 2019-09-03
### Added ### Added

View File

@@ -9,7 +9,7 @@ namespace ICD.Common.Utils.Tests
[TestCase(0, 12)] [TestCase(0, 12)]
[TestCase(12, 12)] [TestCase(12, 12)]
[TestCase(23, 11)] [TestCase(23, 11)]
public void Get12Hour(int hour, int expected) public void To12HourTest(int hour, int expected)
{ {
Assert.AreEqual(expected, DateTimeUtils.To12Hour(hour)); Assert.AreEqual(expected, DateTimeUtils.To12Hour(hour));
} }

View File

@@ -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);
}
}
}

View File

@@ -25,5 +25,48 @@ namespace ICD.Common.Utils.Extensions
return builder.ToString(); return builder.ToString();
} }
/// <summary>
/// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day.
/// </summary>
/// <param name="extends"></param>
/// <param name="hours"></param>
/// <returns></returns>
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);
}
/// <summary>
/// Adds the given number of hours to the time, wrapping within the current 12 hour span, without modifying the day.
/// </summary>
/// <param name="extends"></param>
/// <param name="hours"></param>
/// <returns></returns>
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);
}
/// <summary>
/// Adds the given number of minutes to the time, wrapping every 60 minutes without modifying the hour.
/// </summary>
/// <param name="extends"></param>
/// <param name="minutes"></param>
/// <returns></returns>
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);
}
} }
} }