LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

LiteDB and MongoDB return time format is different

Open codeyu opened this issue 9 years ago • 9 comments

Hi, mbdavid
LiteDB and MongoDB stores times in UTC by default.
The time for LiteDB to return is locally relevant, but MongoDB return UTC format by default.
Will the future version of LiteDB be consistent with mongo?

Test as follows:

public class Person
    {
        public int Id { get; set; }
        public string Fullname { get; set; }
        public DateTime Birthday { get; set; }
    }

LiteDB:

var col = db.GetCollection<Person>("Person");
 var p = new Person {Fullname = "John", Birthday = DateTime.Now.Add(TimeSpan.FromDays(1))};
col.Insert(p);
var record = col.FindAll().ToList().FirstOrDefault(x => x.Fullname=="John");
Assert.IsTrue(record.Birthday.Kind == DateTimeKind.Local); // test pass

MongoDB(CSharpDriver):

var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("foo");
var collection = database.GetCollection<Person>("bar");
 var p = new Person {Id=1, Fullname = "John", Birthday = DateTime.Now.Add(TimeSpan.FromDays(1))};
collection.InsertOne(p);
var record = collection.Find(x => x.Id == 1).FirstOrDefault();
Assert.IsTrue(record.Birthday.Kind == DateTimeKind.Utc); // test pass

Reference article: https://docs.mongodb.com/manual/tutorial/model-time-data/
https://alexmg.com/datetime-precision-with-mongodb-and-the-c-driver/

codeyu avatar Aug 25 '17 09:08 codeyu

Hi @codeyu, I will take a look on this. But now I think it´s complicate to change this by default. Maybe add as parameter in some level (by connection, mapper, ...)

mbdavid avatar Aug 30 '17 16:08 mbdavid

Hi @mbdavid, can you include a fix for this in the next release? Thanks.

anouarhassine avatar Oct 02 '17 09:10 anouarhassine

I'm interested in using utc too. LiteEngine has a parameter for utc, but LiteDatabase has not.

Can we add this parameter for consistency?

viceice avatar Sep 05 '19 11:09 viceice

Hi @ViceIce, if you are talking about v5 this will be checked. EngineSettings are not same as ConnectionString in v5. UTC parameter is in Engine level (and not in ConnectionString level).

mbdavid avatar Sep 12 '19 13:09 mbdavid

@mbdavid I know, LiteDataBase simply creates LiteEngine with utc=false. So it would be nice to allow overriding this behavior. Or allow passing LiteEngine instead of IDiskService to ctor.

viceice avatar Sep 12 '19 14:09 viceice

@ViceIce, are you using v4 or v5? In v5 there is no more IDiskService

mbdavid avatar Sep 12 '19 14:09 mbdavid

I'm using v4, because v5 is alpha, and my current timeframe is a release at end of the year

viceice avatar Sep 12 '19 15:09 viceice

v4 support utc=true|false in connection string. You can see here:

https://github.com/mbdavid/LiteDB/blob/v4/LiteDB/Utils/ConnectionString.cs#L63

mbdavid avatar Sep 12 '19 16:09 mbdavid

I know the connection string constructor, but i would like to reuse my LiteEngine instance.

Most of my solution uses generic configuration based documents, but for some parts i would like to use the strong typed LiteDatabase and LiteCollection. So for concurrency it would be more performant to reuse the LiteEngine.

So my current workaround would be to create the LiteCollection directly without using LiteDatabase.

I'm using FileMode.Exclusive, because i have a single process accessing the files.

viceice avatar Sep 17 '19 04:09 viceice