diff --git a/CHANGELOG.md b/CHANGELOG.md index e92d8e0..8bdeedc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + - Added shim methods for finding closest DateTimes from a sequence + ## [10.1.0] - 2019-11-18 ### Added - Added PathUtils methods for getting ProgramData paths diff --git a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs index 6fde0be..34b5eb7 100644 --- a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs +++ b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.Linq; using ICD.Common.Properties; @@ -38,14 +39,29 @@ namespace ICD.Common.Utils.Extensions /// Whether or not to include times equal to the target time /// /// - public static DateTime? NextEarliestTime(this DateTime target, bool inclusive, params DateTime[] times) + public static DateTime? NextEarliestTime(this DateTime target, bool inclusive, [NotNull] params DateTime[] times) + { + if (times == null) + throw new ArgumentNullException("times"); + + return target.NextEarliestTime(inclusive, (IEnumerable)times); + } + + /// + /// Returns the closest DateTime to the target time that is greater than the target time + /// + /// + /// Whether or not to include times equal to the target time + /// + /// + public static DateTime? NextEarliestTime(this DateTime target, bool inclusive, [NotNull] IEnumerable times) { if (times == null) throw new ArgumentNullException("times"); DateTime earliestTime; bool success = times.OrderBy(dt => dt).TryFirst(dt => inclusive ? target <= dt : target < dt, out earliestTime); - return success ? earliestTime : (DateTime?) null; + return success ? earliestTime : (DateTime?)null; } /// @@ -55,14 +71,29 @@ namespace ICD.Common.Utils.Extensions /// Whether or not to include times equal to the target time /// /// - public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, params DateTime[] times) + public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, [NotNull] params DateTime[] times) + { + if (times == null) + throw new ArgumentNullException("times"); + + return target.PreviousLatestTime(inclusive, (IEnumerable)times); + } + + /// + /// Returns the closest DateTime to the target time that is less than the target time + /// + /// + /// Whether or not to include times equal to the target time + /// + /// + public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, [NotNull] IEnumerable times) { if (times == null) throw new ArgumentNullException("times"); DateTime latestTime; bool success = times.OrderByDescending(dt => dt).TryFirst(dt => inclusive ? target >= dt : target > dt, out latestTime); - return success ? latestTime : (DateTime?) null; + return success ? latestTime : (DateTime?)null; } /// @@ -85,6 +116,11 @@ namespace ICD.Common.Utils.Extensions return extends.StartOfDay() + new TimeSpan(24, 0, 0); } + /// + /// Returns the given date in unix timestamp format. + /// + /// + /// [PublicAPI] public static double ToUnixTimestamp(this DateTime extends) {