From 4695a805752d95477c4a39a096afb6b1c7d06249 Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Thu, 12 Dec 2019 14:58:00 -0500 Subject: [PATCH] feat: add DateTime extensions for add and wrap, like TimeSpan --- .../Extensions/DateTimeExtensions.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs index 34b5eb7..f813ce5 100644 --- a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs +++ b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs @@ -128,5 +128,92 @@ namespace ICD.Common.Utils.Extensions TimeSpan diff = extends.ToUniversalTime() - origin; return Math.Floor(diff.TotalSeconds); } + + /// + /// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day. + /// + /// + /// + /// + 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); + } + + /// + /// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day. + /// + /// + /// + /// + 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); + } + + /// + /// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day. + /// + /// + /// + /// + 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); + } + + /// + /// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day. + /// + /// + /// + /// + 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); + } + + /// + /// Adds the given number of hours to the time, wrapping within the current 12 hour span, without modifying the day. + /// + /// + /// + /// + 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); + } + + /// + /// Adds the given number of minutes to the time, wrapping every 60 minutes without modifying the hour. + /// + /// + /// + /// + 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); + } + } }