Even though I'm clearly specifying that I want to do everything in UTC, somewhere along the line my DateTime values are acting as if they're being written out to the SQLite DB as strings in local time, with no time zone specified, then read back in as if those same digits were intended to be UTC time. Since my local timezone is currently UTC-0400, that means I'm reading back the time saved as if it were four hours older than it should be.
The DateTime value is defined like this:
public class OrderModel
{
...
public virtual DateTime ReceivedTime { get; set; }
...
The mapping for this value looks like this:
{
map.Column("RECEIVED_TIME");
map.Type<NHibernate.Type.UtcDateTimeType>();
});
I had to make a contrived example to simplify what's going on, and the interesting part was that the problem didn't show up while using the same session. I had to write the data in one session, then read it back from another session before the problem appeared.
var sessionFactory = new TestSessionFactory();
var session = sessionFactory.OpenSession();
var transaction = session.BeginTransaction();
var now = DateTime.UtcNow;
var time1 = now.ToString("u");
session.Save(new OrderModel() { ReceivedTime = now });
transaction.Commit();
session.Dispose();
session = sessionFactory.OpenSession();
var time2 = session.Query<OrderModel>().First().ReceivedTime.ToString("u");
Console.WriteLine(time1 + ", " + time2);
The output from the above code was: 2020-03-24 22:13:00Z, 2020-03-24 18:13:00Z
The same time value... except for the 4-hour error.
Anything that I'm missing? I've done a lot of searching for answers so far, but there's nothing to indicate that there's more setup that I should need. I have read of other people having a similar problem (from many years ago, however) and settling on making their own custom data types to solve the problem -- which certainly seems like overkill for something that should be as basic and universally supported as a DateTime value.