Skip to content

Commit

Permalink
Added "ToEssentialsDate" and "ToEssentialsTime" extension methods for…
Browse files Browse the repository at this point in the history
… both "DateTime" and "DateTimeOffset"

The extension methods offer the same functionality as the "EssentialsDate" and "EssentialsTime" constructors, but may be used in a method chain which is not possible with a constructor.
  • Loading branch information
abjerner committed Apr 21, 2024
1 parent b350aba commit d556dc9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Skybrud.Essentials/Time/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Newtonsoft.Json;
using Skybrud.Essentials.Time.Iso8601;

namespace Skybrud.Essentials.Time.Extensions {
Expand Down Expand Up @@ -344,6 +345,40 @@ public static string ToIso8601(this DateTime value) {
return value is null ? null : Iso8601Utils.ToString(value.Value);
}

/// <summary>
/// Returns a new <see cref="EssentialsDate"/> instance based on the specified <paramref name="time"/>, or
/// <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.
/// </summary>
/// <param name="time">The date and time the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <returns>An instance of <see cref="EssentialsDate"/>, or <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.</returns>
[return: NotNullIfNotNull(nameof(time))]
public static EssentialsDate? ToEssentialsDate(this DateTime? time) {
return time is null ? null : new EssentialsDate(time.Value);
}

/// <summary>
/// Returns a new <see cref="EssentialsDate"/> instance based on the specified <paramref name="time"/>, or
/// <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.
/// </summary>
/// <param name="time">The date and time the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <returns>An instance of <see cref="EssentialsDate"/>, or <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.</returns>
[return: NotNullIfNotNull(nameof(time))]
public static EssentialsTime? ToEssentialsTime(this DateTime? time) {
return time is null ? null : new EssentialsTime(time.Value);
}

/// <summary>
/// Returns a new <see cref="EssentialsDate"/> instance based on the specified <paramref name="time"/>, or
/// <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.
/// </summary>
/// <param name="time">The date and time the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <param name="timeZone">The time zone the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <returns>An instance of <see cref="EssentialsDate"/>, or <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.</returns>
[return: NotNullIfNotNull(nameof(time))]
public static EssentialsTime? ToEssentialsTime(this DateTime? time, TimeZoneInfo timeZone) {
return time is null ? null : new EssentialsTime(time.Value, timeZone);
}

}

}
34 changes: 34 additions & 0 deletions src/Skybrud.Essentials/Time/Extensions/DateTimeOffsetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,40 @@ public static string ToIso8601(this DateTimeOffset value) {
return value is null ? null : Iso8601Utils.ToString(value.Value);
}

/// <summary>
/// Returns a new <see cref="EssentialsDate"/> instance based on the specified <paramref name="time"/>, or
/// <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.
/// </summary>
/// <param name="time">The date and time the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <returns>An instance of <see cref="EssentialsDate"/>, or <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.</returns>
[return: NotNullIfNotNull(nameof(time))]
public static EssentialsDate? ToEssentialsDate(this DateTimeOffset? time) {
return time is null ? null : new EssentialsDate(time.Value);
}

/// <summary>
/// Returns a new <see cref="EssentialsDate"/> instance based on the specified <paramref name="time"/>, or
/// <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.
/// </summary>
/// <param name="time">The date and time the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <returns>An instance of <see cref="EssentialsDate"/>, or <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.</returns>
[return: NotNullIfNotNull(nameof(time))]
public static EssentialsTime? ToEssentialsTime(this DateTimeOffset? time) {
return time is null ? null : new EssentialsTime(time.Value);
}

/// <summary>
/// Returns a new <see cref="EssentialsDate"/> instance based on the specified <paramref name="time"/>, or
/// <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.
/// </summary>
/// <param name="time">The date and time the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <param name="timeZone">The time zone the new <see cref="EssentialsDate"/> instance should be based on.</param>
/// <returns>An instance of <see cref="EssentialsDate"/>, or <see langword="null"/> if <paramref name="time"/> is <see langword="null"/>.</returns>
[return: NotNullIfNotNull(nameof(time))]
public static EssentialsTime? ToEssentialsTime(this DateTimeOffset? time, TimeZoneInfo timeZone) {
return time is null ? null : new EssentialsTime(time.Value, timeZone);
}

}

}

0 comments on commit d556dc9

Please sign in to comment.