[BUG] LiteDB loses precision in DateTime
See original GitHub issueVersion 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:
- Created 3 years ago
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks!
If anyone is curious, I used the following code to make it work on 5.0.9:
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?