Skip to content

DateTime.MinValue should not be converted to utc #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/SQLite.Net/SQLiteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,20 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in
{
if (storeDateTimeAsTicks)
{
long ticks = ((DateTime) value).ToUniversalTime().Ticks;
DateTime date = (DateTime)value;
if (date != DateTime.MinValue)
date = date.ToUniversalTime();

long ticks = date.Ticks;
isqLite3Api.BindInt64(stmt, index, ticks);
}
else
{
string val = ((DateTime) value).ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture);
DateTime date = (DateTime)value;
if (date != DateTime.MinValue)
date = date.ToUniversalTime();

string val = date.ToString(DateTimeFormat, CultureInfo.InvariantCulture);
isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer);
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/DateTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private class TestObj
public string Name { get; set; }
public DateTime Time1 { get; set; }
public DateTime Time2 { get; set; }
public DateTime Time3 { get; set; }
}


Expand All @@ -28,6 +29,7 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim
{
Time1 = DateTime.UtcNow,
Time2 = DateTime.Now,
Time3 = DateTime.MinValue
};
await db.InsertAsync(org);
var fromDb = await db.GetAsync<TestObj>(org.Id);
Expand All @@ -36,6 +38,8 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim

Assert.AreEqual(fromDb.Time1.ToLocalTime(), org.Time1.ToLocalTime());
Assert.AreEqual(fromDb.Time2.ToLocalTime(), org.Time2.ToLocalTime());

Assert.AreEqual(fromDb.Time3, org.Time3);
}

private void TestDateTime(TestDb db)
Expand All @@ -49,6 +53,7 @@ private void TestDateTime(TestDb db)
{
Time1 = DateTime.UtcNow,
Time2 = DateTime.Now,
Time3 = DateTime.MinValue
};
db.Insert(org);
var fromDb = db.Get<TestObj>(org.Id);
Expand All @@ -57,6 +62,8 @@ private void TestDateTime(TestDb db)

Assert.AreEqual(fromDb.Time1.ToLocalTime(), org.Time1.ToLocalTime());
Assert.AreEqual(fromDb.Time2.ToLocalTime(), org.Time2.ToLocalTime());

Assert.AreEqual(fromDb.Time3, org.Time3);
}

[Test]
Expand Down