feat: Added shim methods for finding closest DateTimes from a sequence

This commit is contained in:
Chris Cameron
2019-11-19 17:08:21 -05:00
parent fb8abe2aa4
commit eb52b164bf
2 changed files with 43 additions and 4 deletions

View File

@@ -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

View File

@@ -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
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
/// <param name="times"></param>
/// <returns></returns>
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<DateTime>)times);
}
/// <summary>
/// Returns the closest DateTime to the target time that is greater than the target time
/// </summary>
/// <param name="target"></param>
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
/// <param name="times"></param>
/// <returns></returns>
public static DateTime? NextEarliestTime(this DateTime target, bool inclusive, [NotNull] IEnumerable<DateTime> 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;
}
/// <summary>
@@ -55,14 +71,29 @@ namespace ICD.Common.Utils.Extensions
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
/// <param name="times"></param>
/// <returns></returns>
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<DateTime>)times);
}
/// <summary>
/// Returns the closest DateTime to the target time that is less than the target time
/// </summary>
/// <param name="target"></param>
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
/// <param name="times"></param>
/// <returns></returns>
public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, [NotNull] IEnumerable<DateTime> 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;
}
/// <summary>
@@ -85,6 +116,11 @@ namespace ICD.Common.Utils.Extensions
return extends.StartOfDay() + new TimeSpan(24, 0, 0);
}
/// <summary>
/// Returns the given date in unix timestamp format.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static double ToUnixTimestamp(this DateTime extends)
{