Skip to content
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

DateTime storing problem #420

Closed
podkolzzzin opened this issue Jan 10, 2017 · 8 comments
Closed

DateTime storing problem #420

podkolzzzin opened this issue Jan 10, 2017 · 8 comments

Comments

@podkolzzzin
Copy link

podkolzzzin commented Jan 10, 2017

As for me the line "Fail" shouldn't be ever printed.
After a bit of debugging I understood that it's because you store DateTime with the less precision.
It would be interesting for me to read why you have done like this? As for me DateTime is only 8 bytes, and you have probably saved 1 or 2, or even lose something using string.

The code is:

       DateTime dt = DateTime.Now;
        var testId = ObjectId.NewObjectId();
        using (LiteDatabase db = new LiteDatabase("file.db"))
        {
            var doc = new BsonDocument();
            doc["t"] = dt;
            doc["_id"] = testId;
            db.GetCollection("Test").Insert(doc);
        }

        using (LiteDatabase db = new LiteDatabase("file.db"))
        {
            var doc = new BsonDocument();
            doc["t"] = dt;
            var dt2 = db.GetCollection("Test").FindById(testId)["t"].AsDateTime;
            if (dt != dt2)
            {
                Console.WriteLine("Fail");
            }
        }
@mbdavid
Copy link
Owner

mbdavid commented Jan 10, 2017

Hi @podkolzzzin, LiteDB stores DateTime using BitConverter. Did you check if date diff are not about timezone?

@henon
Copy link

henon commented Jan 10, 2017

Yep, I can confirm that. LiteDB stores DateTime with millisecond precision and truncates the nanoseconds. I think the reason is to be compatible to JSON/BSON. That is why I chose to save the Ticks instead and convert them back to DateTime.

@podkolzzzin
Copy link
Author

There are perfect methods ToBinary/FromBinary in DateTime structure, you could probably use them

@mbdavid
Copy link
Owner

mbdavid commented Jan 15, 2017

Hi @henon, LiteDB store in ticks.

    public void Write(DateTime value)
    {
        this.Write(value.ToUniversalTime().Ticks);
    }

I didnt know about ToBinary/FromBinary methods. It's not possible change now because all datafiles will be affected.

@dropsonic
Copy link

@mbdavid I faced the same issue.
Original DateTime (Ticks property): 637310234309996491
Ticks property after I saved and read the value from LiteDB: 637310234309990000

@lbnascimento
Copy link
Collaborator

@dropsonic This is a limitation in the BSON specification, which LiteDB implements: dates are stores as UTC milliseconds since the Unix epoch.

If you want to keep all the precision from DateTime, you should store Ticks.

@dropsonic
Copy link

dropsonic commented Jul 27, 2020

It seems inconvenient because I'd like to use auto-serialization of POCO.
According to the answer above, LiteDB stores ticks so it is quite strange that this issue exists.

@martin-slater
Copy link

martin-slater commented Jul 23, 2021

Just a me to to this as I have been bitten by it and now need to add a hack somewhere else to work around it. It is a surprising result and just wrong. It would seem you could add a pragma to allow users to enable correct behaviour (serializing and deserializing a DateTime will compare equals).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants