From 1f7819e53607afa2dd6ea004966ae5d84c2fa40c Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Thu, 12 Jul 2018 14:03:15 -0400 Subject: [PATCH] feat: add DateTimeExtensions.PreviousLatestTime --- .../Extensions/DateTimeExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs index 77f0b14..8a95078 100644 --- a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs +++ b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs @@ -46,5 +46,22 @@ namespace ICD.Common.Utils.Extensions var time = orderedTimes.FirstOrDefault(dt => target < dt); return time == default(DateTime) ? (DateTime?) null : time; } + + /// + /// Returns the closest DateTime to the target time that is less than the target time + /// + /// + /// + /// + /// + public static DateTime? PreviousLatestTime(this DateTime target, params DateTime[] times) + { + if (times.Length == 0) + return null; + + DateTime[] orderedTimes = times.OrderByDescending(dt => dt).ToArray(); + var time = orderedTimes.FirstOrDefault(dt => target > dt); + return time == default(DateTime) ? (DateTime?) null : time; + } } }