1
0

Added Collection Extensions, Updated DateTimeExtensions

This commit is contained in:
2024-02-25 09:49:43 +01:00
parent cf425da609
commit 8e31601d75
8 changed files with 242 additions and 27 deletions

View File

@@ -0,0 +1,55 @@
namespace System.Collections.Generic
{
/// <summary>
/// Provides extension methods for generic <see cref="ICollection{T}"/> implementations.
/// </summary>
public static class CollectionExtensions
{
/// <summary>
/// Add the <paramref name="item"/> to the <paramref name="collection"/> if it is not <see langword="null"/>.
/// </summary>
/// <typeparam name="T">The type of the elements in the collection</typeparam>
/// <param name="collection">The collection.</param>
/// <param name="item">The item to add if not <see langword="null"/>.</param>
public static void AddIfNotNull<T>(this ICollection<T> collection, T item)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(collection);
#else
if (collection == null)
throw new ArgumentNullException(nameof(collection));
#endif
if (item == null)
return;
collection.Add(item);
}
/// <summary>
/// Adds a <see cref="AddRange{T}(ICollection{T}, IEnumerable{T})"/> functionallity to <see cref="ICollection{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the elements in the collection.</typeparam>
/// <param name="collection">The collection.</param>
/// <param name="items">The items to add.</param>
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(items);
#else
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (items == null)
throw new ArgumentNullException(nameof(items));
#endif
if (collection == items)
return;
foreach (var item in items)
collection.Add(item);
}
}
}

View File

@@ -61,20 +61,27 @@ namespace System
public static TimeSpan GetAlignedIntervalLocal(this TimeSpan timeSpan, TimeSpan offset = default)
=> timeSpan.GetAlignedInterval(DateTime.Now, offset);
internal static TimeSpan GetAlignedInterval(this TimeSpan timeSpan, DateTime now, TimeSpan offset = default)
/// <summary>
/// Aligns the <see cref="TimeSpan"/> to the specified time.
/// </summary>
/// <param name="timeSpan">The timespan to align.</param>
/// <param name="now">A timestamp to align with.</param>
/// <param name="offset">A specific offset to the timespan.</param>
/// <returns>The timespan until the aligned time.</returns>
public static TimeSpan GetAlignedInterval(this TimeSpan timeSpan, DateTime now, TimeSpan offset = default)
{
var nowWithOffset = new DateTimeOffset(now);
var dtOffsetNow = new DateTimeOffset(now);
var nextTime = new DateTime(nowWithOffset.Ticks / timeSpan.Ticks * timeSpan.Ticks, now.Kind).Add(offset);
var nextTimeWithOffset = new DateTimeOffset(nextTime);
var nextTime = new DateTime(dtOffsetNow.Ticks / timeSpan.Ticks * timeSpan.Ticks, now.Kind).Add(offset);
var dtOffsetNext = new DateTimeOffset(nextTime);
if (nextTimeWithOffset <= nowWithOffset)
nextTimeWithOffset = nextTimeWithOffset.Add(timeSpan);
if (dtOffsetNext <= dtOffsetNow)
dtOffsetNext = dtOffsetNext.Add(timeSpan);
if (now.Kind == DateTimeKind.Local)
return nextTimeWithOffset.LocalDateTime - nowWithOffset.LocalDateTime;
return dtOffsetNext.LocalDateTime - dtOffsetNow.LocalDateTime;
return nextTimeWithOffset - nowWithOffset;
return dtOffsetNext - dtOffsetNow;
}
#endregion Aligned Interval