-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateTimeOffset converter for Postgres.
- Loading branch information
Showing
3 changed files
with
172 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using System; | ||
using System.Globalization; | ||
using System.Linq.Expressions; | ||
|
||
namespace Remotely.Server.Converters; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Postgres can only handle DateTimeOffset with an offset of 0. This converter | ||
/// will allow us to use DateTimeOffset in entities without any manual conversion. | ||
/// </para> | ||
/// <para> | ||
/// Docs: https://www.npgsql.org/efcore/release-notes/6.0.html?tabs=annotations#detailed-notes | ||
/// </para> | ||
/// </summary> | ||
public class PostgresDateTimeOffsetConverter : ValueConverter<DateTimeOffset, DateTimeOffset> | ||
{ | ||
public PostgresDateTimeOffsetConverter() | ||
: base(ToUtc(), ToLocalTime(), null) | ||
{ | ||
|
||
} | ||
|
||
public PostgresDateTimeOffsetConverter( | ||
Expression<Func<DateTimeOffset, DateTimeOffset>> convertToProviderExpression, | ||
Expression<Func<DateTimeOffset, DateTimeOffset>> convertFromProviderExpression, | ||
ConverterMappingHints? mappingHints = null) | ||
: base(convertToProviderExpression, convertFromProviderExpression, mappingHints) | ||
{ | ||
} | ||
protected static Expression<Func<DateTimeOffset, DateTimeOffset>> ToUtc() | ||
=> v => v.ToUniversalTime(); | ||
|
||
protected static Expression<Func<DateTimeOffset, DateTimeOffset>> ToLocalTime() | ||
=> v => v.ToLocalTime(); | ||
} |
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