Skip to content

Instantly share code, notes, and snippets.

Created April 2, 2013 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5291992 to your computer and use it in GitHub Desktop.
Save anonymous/5291992 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
public class UtcSqlServerOrmLiteDialectProvider : SqlServerOrmLiteDialectProvider
{
public override string GetQuotedValue(object value, Type fieldType)
{
if (fieldType == typeof(DateTime))
{
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 = (DateTime)base.ConvertDbValue(value, type);
return DateTime.SpecifyKind(result, DateTimeKind.Utc);
}
return base.ConvertDbValue(value, type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment