-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateOnly and DateTimeOffset extension methods (#194)
- Loading branch information
Showing
13 changed files
with
152 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace TinyHelpers.Extensions; | ||
|
||
#if NET6_0_OR_GREATER | ||
/// <summary> | ||
/// Contains extension methods for the <see cref="DateOnly"/> type. | ||
/// </summary> | ||
/// <seealso cref="DateOnly"/> | ||
public static class DateOnlyExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a <see cref="DateOnly"/> value to a <see cref="DateTimeOffset"/> value. | ||
/// </summary> | ||
/// <param name="dateOnly">The <see cref="DateOnly"/> value to convert.</param> | ||
/// <param name="zone">The optional <see cref="TimeZoneInfo"/> to apply to the resulting <see cref="DateTimeOffset"/> value.</param> | ||
/// <returns>The converted <see cref="DateTimeOffset"/> value.</returns> | ||
/// <seealso cref="DateOnly"/> | ||
/// <seealso cref="DateTimeOffset"/> | ||
/// <seealso cref="TimeZoneInfo"/> | ||
public static DateTimeOffset ToDateTimeOffset(this DateOnly dateOnly, TimeZoneInfo? zone = null) | ||
{ | ||
var dateTime = dateOnly.ToDateTime(TimeOnly.MinValue); | ||
return new DateTimeOffset(dateTime, zone?.GetUtcOffset(dateTime) ?? TimeSpan.Zero); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace TinyHelpers.Extensions; | ||
|
||
/// <summary> | ||
/// Contains extension methods for the <see cref="DateTimeOffset"/> type. | ||
/// </summary> | ||
/// <seealso cref="DateTimeOffset"/> | ||
public static class DateTimeOffsetExtensions | ||
{ | ||
#if NET6_0_OR_GREATER | ||
/// <summary> | ||
/// Constructs a <see cref="DateOnly"/> object that is set to the date part of the specified <see cref="DateTimeOffset"/>. | ||
/// </summary> | ||
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> object to extract the date part from.</param> | ||
/// <param name="zone">The optional <see cref="TimeZoneInfo"/> to apply to the resulting <see cref="DateTimeOffset"/> value.</param> | ||
/// <returns>A <see cref="DateOnly"/> object representing date of the day specified in the <paramref name="dateTimeOffset"/> object.</returns> | ||
/// <seealso cref="DateTimeOffset"/> | ||
/// <seealso cref="DateOnly"/> | ||
/// <seealso cref="TimeZoneInfo"/> | ||
public static DateOnly ToDateOnly(this DateTimeOffset dateTimeOffset, TimeZoneInfo? zone = null) | ||
{ | ||
var inTargetZone = TimeZoneInfo.ConvertTime(dateTimeOffset, zone ?? TimeZoneInfo.Utc); | ||
return DateOnly.FromDateTime(inTargetZone.Date); | ||
} | ||
|
||
/// <summary> | ||
/// Constructs a <see cref="TimeOnly"/> object from a <see cref="DateTimeOffset"/> representing the time of the day in this <see cref="DateTimeOffset"/> object. | ||
/// </summary> | ||
/// <param name="dateTimeOffset">The <see cref="DateTimeOffset"/> object to extract the time of the day from.</param> | ||
/// <param name="zone">The optional <see cref="TimeZoneInfo"/> to apply to the resulting <see cref="DateTimeOffset"/> value.</param> | ||
/// <returns>A <see cref="TimeOnly"/> object representing time of the day specified in the <paramref name="dateTimeOffset"/> object.</returns> | ||
/// <seealso cref="DateTimeOffset"/> | ||
/// <seealso cref="TimeOnly"/> | ||
/// <seealso cref="TimeZoneInfo"/> | ||
public static TimeOnly ToTimeOnly(this DateTimeOffset dateTimeOffset, TimeZoneInfo? zone = null) | ||
{ | ||
var inTargetZone = TimeZoneInfo.ConvertTime(dateTimeOffset, zone ?? TimeZoneInfo.Utc); | ||
return TimeOnly.FromDateTime(dateTimeOffset.DateTime); | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace TinyHelpers.Extensions; | ||
|
||
/// <summary> | ||
/// Contains extension methods for the <see cref="TimeSpan"/> type. | ||
/// </summary> | ||
/// <seealso cref="TimeSpan"/> | ||
public static class TimeSpanExtensions | ||
{ | ||
#if NET6_0_OR_GREATER | ||
/// <summary> | ||
/// Constructs a <see cref="TimeOnly"/> object from a <see cref="TimeSpan"/> representing the time elapsed since midnight. | ||
/// </summary> | ||
/// <param name="timeSpan">The time interval measured since midnight. This value has to be positive and not exceeding the time of the day.</param> | ||
/// <returns>A <see cref="TimeOnly"/> object representing the time elapsed since midnight using the <paramref name="timeSpan"/> value.</returns> | ||
/// <seealso cref="DateTime"/> | ||
/// <seealso cref="TimeOnly"/> | ||
public static TimeOnly ToTimeOnly(this TimeSpan timeSpan) | ||
=> TimeOnly.FromTimeSpan(timeSpan); | ||
#endif | ||
} |
49 changes: 49 additions & 0 deletions
49
tests/TinyHelpers.Tests/Extensions/DateOnlyExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using FluentAssertions; | ||
using TinyHelpers.Extensions; | ||
|
||
namespace TinyHelpers.Tests.Extensions; | ||
|
||
public class DateOnlyExtensionsTests | ||
{ | ||
[Fact] | ||
public void Should_Return_True1() | ||
{ | ||
// Arrange | ||
var input = new DateOnly(2020, 1, 1); | ||
var result = new DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.Zero); | ||
|
||
// Act | ||
var output = input.ToDateTimeOffset(); | ||
|
||
// Assert | ||
output.Should().Be(result); | ||
} | ||
|
||
[Fact] | ||
public void Should_Return_True2() | ||
{ | ||
// Arrange | ||
var input = new DateOnly(2023, 6, 12); | ||
var result = new DateTimeOffset(2023, 6, 12, 0, 0, 0, TimeSpan.Zero); | ||
|
||
// Act | ||
var output = input.ToDateTimeOffset(); | ||
|
||
// Assert | ||
output.Should().Be(result); | ||
} | ||
|
||
[Fact] | ||
public void Should_Return_False() | ||
{ | ||
// Arrange | ||
var input = new DateOnly(2020, 1, 1); | ||
var result = new DateTimeOffset(2020, 1, 1, 4, 0, 0, TimeSpan.Zero); | ||
|
||
// Act | ||
var output = input.ToDateTimeOffset(); | ||
|
||
// Assert | ||
output.Should().NotBe(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters