Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an OrmLite option for DateTime.SpecifyKind(DateTimeKind.Utc)?

Is there a way to specify that I want all of the DateTimes that OrmLite materializes to be set to UTC kind?

I store a lot of DateTimes in my database via stored procedures when a row is inserted:

insert [Comment] (
  Body
, CreatedOn
) values (
  @Body
, getutcdate()
);

When I retrieve the values via a select statement in ormlite, the datetimes come out in Unspecified kind (which is interpreted as the local timezone, I believe):

var comments = db.SqlList<Comment>("select * from [Comment] where ... ");

I would prefer not to set each DateTime object individually:

foreach (var comment in comments) {
    comment.CreatedOn = DateTime.SpecifyKind(comment.CreatedOn, DateTimeKind.Utc);
}

I found this question, but I don't think it's quite what I'm asking for:
servicestack ormlite sqlite DateTime getting TimeZone adjustment on insert

Also found this pull request, but setting SqlServerOrmLiteDialectProvider.EnsureUtc(true) doesn't seem to do it either.

like image 377
Zachary Yates Avatar asked Sep 02 '13 21:09

Zachary Yates


People also ask

What is DateTime SpecifyKind?

The DateTime. SpecifyKind() method in C# is used to create a new DateTime object that has the same number of ticks as the specified DateTime but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value.

How do you specify the kind in DateTime?

The SpecifyKind method creates a new DateTime object using the specified kind parameter and the original time value. The returned DateTime value does not represent the same instant in time as the value parameter, and SpecifyKind is not a time zone conversion method.


1 Answers

SqlServerOrmLiteDialectProvider.EnsureUtc(true) does work, there was something else going on with my test case that led me to believe that it didn't. Hopefully this will help someone else.

Here's some sample code:

model.cs

public class DateTimeTest {
    [AutoIncrement]
    public int Id { get; set; }
    public DateTime CreatedOn { get; set; }
}

test.cs

var connectionString = "server=dblcl;database=flak;trusted_connection=true;";
var provider = new SqlServerOrmLiteDialectProvider();
provider.EnsureUtc(true);
var factory = new OrmLiteConnectionFactory(connectionString, provider);
var connection = factory.Open();

connection.CreateTable(true, typeof(DateTimeTest));
connection.ExecuteSql("insert DateTimeTest (CreatedOn) values (getutcdate())");
var results = connection.SqlList<DateTimeTest>("select * from DateTimeTest");

foreach(var result in results) {
    Console.WriteLine("{0},{1},{2},{3},{4}", result.Id, result.CreatedOn, result.CreatedOn.Kind, result.CreatedOn.ToLocalTime(), result.CreatedOn.ToUniversalTime());
}

output

1,9/13/2013 5:19:12 PM,Utc,9/13/2013 10:19:12 AM,9/13/2013 5:19:12 PM
like image 171
Zachary Yates Avatar answered Sep 24 '22 14:09

Zachary Yates