-
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.
Merge pull request #76 from NerosoftDev/develop
Use DateTime.Now as default value. Add UniversalTimeConverter.
- Loading branch information
Showing
4 changed files
with
58 additions
and
19 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
39 changes: 39 additions & 0 deletions
39
Source/Euonia.Repository.EfCore/ValueConversion/UniversalTimeConverter.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 @@ | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
namespace Nerosoft.Euonia.Repository.EfCore; | ||
|
||
/// <summary> | ||
/// Defines conversion for <see cref="DateTime"/> values to universal time. | ||
/// </summary> | ||
public class UniversalTimeConverter : ValueConverter<DateTime, DateTime> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UniversalTimeConverter"/> class. | ||
/// </summary> | ||
public UniversalTimeConverter() | ||
: base(t => ConvertToUniversalTime(t), t => ConvertToLocalTime(t)) | ||
{ | ||
} | ||
|
||
private static DateTime ConvertToUniversalTime(DateTime time) | ||
{ | ||
return time.Kind switch | ||
{ | ||
DateTimeKind.Unspecified => DateTime.SpecifyKind(time, DateTimeKind.Local).ToUniversalTime(), | ||
DateTimeKind.Local => time.ToUniversalTime(), | ||
DateTimeKind.Utc => time, | ||
_ => time | ||
}; | ||
} | ||
|
||
private static DateTime ConvertToLocalTime(DateTime time) | ||
{ | ||
return time.Kind switch | ||
{ | ||
DateTimeKind.Unspecified => DateTime.SpecifyKind(time, DateTimeKind.Utc).ToLocalTime(), | ||
DateTimeKind.Utc => time.ToLocalTime(), | ||
DateTimeKind.Local => time, | ||
_ => time | ||
}; | ||
} | ||
} |
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