-
-
Save kojoru/5292247 to your computer and use it in GitHub Desktop.
A simple way to add UTC time zone check to ORMLite. You should initialize it with SqlServerOrmLiteDialectProvider.Instance = new UtcSqlServerOrmLiteDialectProvider(); somewhere in AppHost.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
using System; | |
using ServiceStack.OrmLite.SqlServer; | |
public class UtcSqlServerOrmLiteDialectProvider : SqlServerOrmLiteDialectProvider | |
{ | |
public override string GetQuotedValue(object value, Type fieldType) | |
{ | |
if (fieldType == typeof(DateTime) && value != null) | |
{ | |
var dateValue = (DateTime)value; | |
if (dateValue.Kind == DateTimeKind.Local) | |
dateValue = dateValue.ToUniversalTime(); | |
const string iso8601Format = "yyyyMMdd HH:mm:ss.fff"; | |
return base.GetQuotedValue(dateValue.ToString(iso8601Format), typeof(string)); | |
} | |
return base.GetQuotedValue(value, fieldType); | |
} | |
public override object ConvertDbValue(object value, Type type) | |
{ | |
if (type == typeof(DateTime)) | |
{ | |
var result = base.ConvertDbValue(value, type); | |
if(result is DateTime) | |
return DateTime.SpecifyKind((DateTime)result, DateTimeKind.Utc); | |
return result; | |
} | |
return base.ConvertDbValue(value, type); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment