-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
fcc5409
commit a656d4b
Showing
6 changed files
with
65 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
|
||
namespace Daybreak.Utils; | ||
|
||
public static class DateTimeExtensions | ||
{ | ||
/// <summary> | ||
/// Converts <see cref="DateTime"/> to <see cref="DateTimeOffset"/> safely and clamps the values between <see cref="DateTimeOffset.MinValue"/> and <see cref="DateTimeOffset.MaxValue"/> | ||
/// </summary> | ||
public static DateTimeOffset ToSafeDateTimeOffset(this DateTime dateTime) | ||
{ | ||
var utcDateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); | ||
return utcDateTime.ToUniversalTime() <= DateTimeOffset.MinValue.UtcDateTime | ||
? DateTimeOffset.MinValue | ||
: utcDateTime.ToUniversalTime() >= DateTimeOffset.MaxValue.UtcDateTime | ||
? DateTimeOffset.MaxValue | ||
: new DateTimeOffset(utcDateTime); | ||
} | ||
|
||
/// <summary> | ||
/// Converts <see cref="DateTime"/> to <see cref="DateTimeOffset"/> safely and clamps the values between <see cref="DateTimeOffset.MinValue"/> and <see cref="DateTimeOffset.MaxValue"/> | ||
/// </summary> | ||
public static DateTimeOffset? ToSafeDateTimeOffset(this DateTime? dateTime) | ||
{ | ||
if (!dateTime.HasValue) | ||
{ | ||
return default; | ||
} | ||
|
||
return dateTime.Value.ToSafeDateTimeOffset(); | ||
} | ||
} |