mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-13 19:55:02 +00:00
feat: add DateTime extensions for add and wrap, like TimeSpan
This commit is contained in:
@@ -128,5 +128,92 @@ namespace ICD.Common.Utils.Extensions
|
||||
TimeSpan diff = extends.ToUniversalTime() - origin;
|
||||
return Math.Floor(diff.TotalSeconds);
|
||||
}
|
||||
|
||||
/// <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 DateTime AddYearsAndWrap(this DateTime extends, int years)
|
||||
{
|
||||
years += extends.Year;
|
||||
|
||||
// need to check days in month due to leap year
|
||||
int daysInMonth = DateTime.DaysInMonth(years, extends.Month);
|
||||
int day = daysInMonth < extends.Day ? daysInMonth : extends.Day;
|
||||
return new DateTime(years, extends.Month, day, extends.Hour, extends.Minute, extends.Second, extends.Millisecond);
|
||||
}
|
||||
|
||||
/// <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="months"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime AddMonthsAndWrap(this DateTime extends, int months)
|
||||
{
|
||||
months = MathUtils.Modulus(months + extends.Month - 1, 12) + 1;
|
||||
int daysInMonth = DateTime.DaysInMonth(extends.Year, months);
|
||||
int day = daysInMonth < extends.Day ? daysInMonth : extends.Day;
|
||||
|
||||
return new DateTime(extends.Year, months, day, extends.Hour, extends.Minute, extends.Second, extends.Millisecond);
|
||||
}
|
||||
|
||||
/// <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="days"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime AddDaysAndWrap(this DateTime extends, int days)
|
||||
{
|
||||
days = MathUtils.Modulus(days + extends.Day - 1, DateTime.DaysInMonth(extends.Year, extends.Month)) + 1;
|
||||
return new DateTime(extends.Year, extends.Month, days, extends.Hour, extends.Minute, extends.Second, extends.Millisecond);
|
||||
}
|
||||
|
||||
/// <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 DateTime AddHoursAndWrap(this DateTime extends, int hours)
|
||||
{
|
||||
hours = MathUtils.Modulus(hours + extends.Hour, 24);
|
||||
return new DateTime(extends.Year, extends.Month, extends.Day, hours, extends.Minute, extends.Second, extends.Millisecond);
|
||||
}
|
||||
|
||||
/// <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 DateTime AddHoursAndWrap12Hour(this DateTime extends, int hours)
|
||||
{
|
||||
int currentHour = extends.Hour;
|
||||
bool am = extends.Hour < 12;
|
||||
|
||||
int current12Hour = MathUtils.Modulus(currentHour, 12);
|
||||
int new12Hour = MathUtils.Modulus(current12Hour + hours, 12);
|
||||
|
||||
return am
|
||||
? new DateTime(extends.Year, extends.Month, extends.Day, new12Hour, extends.Minute, extends.Second, extends.Millisecond)
|
||||
: new DateTime(extends.Year, extends.Month, extends.Day, new12Hour + 12, extends.Minute, extends.Second, extends.Millisecond);
|
||||
}
|
||||
|
||||
/// <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 DateTime AddMinutesAndWrap(this DateTime extends, int minutes)
|
||||
{
|
||||
minutes = MathUtils.Modulus(minutes + extends.Minute, 60);
|
||||
return new DateTime(extends.Year, extends.Month, extends.Day, extends.Hour, minutes, extends.Second, extends.Millisecond);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user