Skip to content

Instantly share code, notes, and snippets.

@kojoru
Forked from anonymous/gist:5291992
Last active December 15, 2015 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kojoru/5292247 to your computer and use it in GitHub Desktop.
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
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