question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] LiteDB loses precision in DateTime

See original GitHub issue

Version LiteDB 5.0.8 .NET Framework 4.8 / .NET Standard 2.0

Describe the bug LiteDB loses precision when serializes DateTime.

Code to Reproduce

class Foo
{
	public DateTime Value { get;set; }
}
...
var db = new LiteDatabase(@"C:\Temp\database.db")) { UtcDate = true };
var col = db.GetCollection<Foo>();
var expected = new Foo() { Value = new DateTime(637310234309996491) };
col.Insert(expected);
var actual = col.FindAll().First();
Console.WriteLine(actual.Value == expected.Value); // false
Console.WriteLine(actual.Value.Ticks) // 637310234309990000 instead of 637310234309996491

Expected behavior DateTime is the same after serialization/deserialization to the database

Screenshots/Stacktrace If applicable, add screenshots/stacktrace

Additional context A similar issue was reported a while ago: https://github.com/mbdavid/LiteDB/issues/420

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

12reactions
dropsoniccommented, Aug 10, 2020

Thanks!

If anyone is curious, I used the following code to make it work on 5.0.9:

var mapper = new BsonMapper();
mapper.RegisterType<DateTime>(
    value => value.ToString("o", CultureInfo.InvariantCulture),
    bson => DateTime.ParseExact(bson, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind));
mapper.RegisterType<DateTimeOffset>(
    value => value.ToString("o", CultureInfo.InvariantCulture),
    bson => DateTimeOffset.ParseExact(bson, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind));

using (var db = new LiteDatabase("instance.db", mapper))
{
    ...
}
2reactions
henoncommented, Jul 27, 2020

I agree with @dropsonic, this is a massive pitfall which everyone who stores and compares loaded DateTimes will encounter.

How about keeping the current format how it is for backwards compatibility for DateTime values saved until now but save all future values without precision loss and denote the newly saved values accordingly so that they are not interpreted as old DateTime values?

Also, I don’t understand, why does LiteDB have to follow the BSON spec in every detail even if it is to the user’s disadvantage? Couldn’t it handle DateTimes better and just document that deviation from the BSON spec, to all user’s advantage?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recently Active 'litedb' Questions - Page 2
The application auto-creates a LiteDb database if it does not ... This is useful if you don't want to lose precision when storing...
Read more >
Newest 'litedb' Questions - Page 2
The application uses LiteDB (all else is straight c# winforms ... This is useful if you don't want to lose precision when storing...
Read more >
sqlite3 — DB-API 2.0 interface for SQLite databases
If a timestamp stored in SQLite has a fractional part longer than 6 numbers, its value will be truncated to microsecond precision by...
Read more >
ThinkAutomation Help
Welcome To ThinkAutomation. ThinkAutomation is a business process automation (BPA) solution designed to automate on-premises and cloud-based business ...
Read more >
Search Results - CVE
A bug in the implementation of the SM2 decryption code means that the calculation of the buffer size required to hold the plaintext...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found