Skip to content

Update DateTime stored as strings to be output with the UTC kind #315

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 1 commit 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
4 changes: 2 additions & 2 deletions src/SQLite.Net/SQLiteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType)
{
return new DateTime(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index), DateTimeKind.Utc);
}
return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture);
return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture).ToUniversalTime();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal)

}
if (clrType == typeof (DateTimeOffset))
{
Expand All @@ -488,7 +488,7 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType)
}
else
{
value = DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture);
value = DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture).ToUniversalTime();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal)

}
return Activator.CreateInstance(clrType, value);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/DateTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ private void TestDateTime(TestDb db)

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

Assert.AreEqual(fromDb.Time1.Kind, DateTimeKind.Utc);
Assert.AreEqual(fromDb.Time2.Kind, DateTimeKind.Utc);
}

[Test]
Expand Down
28 changes: 28 additions & 0 deletions tests/SerializableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ public void SupportsSerializableDateTime()

Assert.That(found.DateTimeValue.InnerValue.ToLocalTime(), Is.EqualTo(value1.ToLocalTime()));
Assert.That(found.DateTimeValue2.InnerValue.ToLocalTime(), Is.EqualTo(value2.ToLocalTime()));

Assert.That(found.DateTimeValue.InnerValue.Kind, Is.EqualTo(DateTimeKind.Utc));
Assert.That(found.DateTimeValue2.InnerValue.Kind, Is.EqualTo(DateTimeKind.Utc));
}

[Test]
public void SupportsSerializableDateTimeWithStrings()
{
var dbStrings = new TestDb(storeDateTimeAsTicks: false);
dbStrings.CreateTable<ComplexType>();

DateTime value1 = DateTime.UtcNow;
DateTime value2 = DateTime.Now;
var model = new ComplexType
{
DateTimeValue = new SerializableDateTime(value1),
DateTimeValue2 = new SerializableDateTime(value2)
};
dbStrings.Insert(model);
ComplexType found = dbStrings.Get<ComplexType>(m => m.ID == model.ID);
Assert.That(found.DateTimeValue.InnerValue.ToUniversalTime(), Is.EqualTo(value1.ToUniversalTime()));
Assert.That(found.DateTimeValue2.InnerValue.ToUniversalTime(), Is.EqualTo(value2.ToUniversalTime()));

Assert.That(found.DateTimeValue.InnerValue.ToLocalTime(), Is.EqualTo(value1.ToLocalTime()));
Assert.That(found.DateTimeValue2.InnerValue.ToLocalTime(), Is.EqualTo(value2.ToLocalTime()));

Assert.That(found.DateTimeValue.InnerValue.Kind, Is.EqualTo(DateTimeKind.Utc));
Assert.That(found.DateTimeValue2.InnerValue.Kind, Is.EqualTo(DateTimeKind.Utc));
}

[Test]
Expand Down