aspnet/Microsoft.Data.Sqlite

Wrong datetime value saved when using non-Gregorian calendar

Closed this issue · 1 comments

When migrating our app from System.Data.SQLite, I noticed that wrong dates were stored in the database when current thread culture was using non-Gregorian calendar.

When using the following code, value '1439-07-08 00:00:00' is stored in the database.

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ar-SA");
using (SqliteConnection conn = new SqliteConnection("Data Source=:memory:"))
{
    conn.Open();
    using (var command = conn.CreateCommand())
    {
        command.CommandText = "create table Person(DateOfBirth datetime);" +
                              "insert into Person(DateOfBirth) values (@DateOfBirth)";
        command.Parameters.AddWithValue("DateOfBirth", new DateTime(2018, 3, 25));
        command.ExecuteNonQuery();
    }
    using (var queryCommand = conn.CreateCommand())
    {
        queryCommand.CommandText = "select DateOfBirth from Person";
        Console.WriteLine(queryCommand.ExecuteScalar());  // returns '1439-07-08 00:00:00'
    }
}

I think in this case value '2018-03-25 00:00:00' should be stored regardless of current culture settings.

Fixed in 2.1 by #495.

Still needs triage to consider patching.