-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
25 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/TableStorage.Abstracts/Extensions/DateOnlyExtensions.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,39 @@ | ||
namespace TableStorage.Abstracts.Extensions; | ||
|
||
#if NET6_0_OR_GREATER | ||
/// <summary> | ||
/// <see cref="DateOnly" /> extension methods | ||
/// </summary> | ||
public static class DateOnlyExtensions | ||
{ | ||
/// <summary> | ||
/// Converts the <see cref="DateOnly"/> to a <see cref="DateTimeOffset"/> in the specified timezone. | ||
/// </summary> | ||
/// <param name="dateOnly">The date to convert.</param> | ||
/// <param name="zone">The time zone the date is in.</param> | ||
/// <returns>The converted <see cref="DateTimeOffset"/></returns> | ||
public static DateTimeOffset ToDateTimeOffset(this DateOnly dateOnly, TimeZoneInfo? zone = null) | ||
{ | ||
zone ??= TimeZoneInfo.Local; | ||
|
||
var dateTime = dateOnly.ToDateTime(TimeOnly.MinValue); | ||
var offset = zone.GetUtcOffset(dateTime); | ||
|
||
return new DateTimeOffset(dateTime, offset); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the <see cref="DateTimeOffset"/> to a <see cref="DateOnly"/> in the specified timezone. | ||
/// </summary> | ||
/// <param name="dateTime">The <see cref="DateTimeOffset"/> to convert.</param> | ||
/// <param name="zone">The time zone the date is in.</param> | ||
/// <returns>The converted <see cref="DateOnly"/></returns> | ||
public static DateOnly ToDateOnly(this DateTimeOffset dateTime, TimeZoneInfo? zone = null) | ||
{ | ||
zone ??= TimeZoneInfo.Local; | ||
|
||
var targetZone = TimeZoneInfo.ConvertTime(dateTime, zone); | ||
return DateOnly.FromDateTime(targetZone.Date); | ||
} | ||
} | ||
#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