Skip to content

Commit

Permalink
Partial fix DateTime UTC/Local in index key litedb-org#921
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Mar 10, 2018
1 parent caab809 commit 73cf321
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
79 changes: 79 additions & 0 deletions LiteDB.Tests/Database/DateTimeMinMax_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.IO;
using System.Linq;
using LiteDB;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LiteDBDateTimeIndexMinMax
{
#region Model

public class DateTimeTest
{
public int Id { get; set; }
public DateTime? Date { get; set; }
}

#endregion

[TestClass]
public class DateTimeMinMax_Tests
{
[TestMethod]
public void DateTimeMinMax_Test()
{
var memory = new MemoryStream();

using (var db = new LiteDatabase(memory))
{
var col = db.GetCollection<DateTimeTest>();
col.EnsureIndex(x => x.Date);

col.Insert(new DateTimeTest() { Id = 1, Date = new DateTime(2018, 02, 22, 0, 0, 0) });
col.Insert(new DateTimeTest() { Id = 2, Date = new DateTime(2018, 02, 22, 23, 59, 59) });

MinMaxCommon(col);
}

using (var db = new LiteDatabase(memory))
{
var col = db.GetCollection<DateTimeTest>();

MinMaxCommon(col);

col.Insert(new DateTimeTest() { Id = 3, Date = new DateTime(2018, 02, 21, 23, 59, 59) });
col.Insert(new DateTimeTest() { Id = 4, Date = new DateTime(2018, 02, 23, 0, 0, 0) });
col.Insert(new DateTimeTest() { Id = 5, Date = new DateTime(2018, 02, 22, 0, 0, 1) });
col.Insert(new DateTimeTest() { Id = 6, Date = new DateTime(2018, 02, 22, 23, 59, 58) });

MinMaxCommon(col);
}

using (var db = new LiteDatabase(memory))
{
var col = db.GetCollection<DateTimeTest>();

MinMaxCommon(col);
}
}

private void MinMaxCommon(LiteCollection<DateTimeTest> coll)
{
var searchdatetime = new DateTime(2018, 02, 22, 0, 0, 10);

var min = coll.Min(x => x.Date).AsDateTime;
var max = coll.Max(x => x.Date).AsDateTime;

var smaller = coll.FindOne(x => x.Date < searchdatetime);
var greater = coll.FindOne(x => x.Date > searchdatetime);

var all = coll.FindAll().ToList();

var linqmin = all.Min(x => x.Date);
var linqmax = all.Max(x => x.Date);

Assert.AreEqual(min, linqmin);
Assert.AreEqual(max, linqmax);
}
}
}
7 changes: 6 additions & 1 deletion LiteDB/Utils/ByteReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ public string ReadString(int length)

public DateTime ReadDateTime()
{
return new DateTime(this.ReadInt64(), DateTimeKind.Utc);
// fix #921 converting index key into LocalTime
// this is not best solution because uctDate must be a global parameter
// this will be review in v5
var date = new DateTime(this.ReadInt64(), DateTimeKind.Utc);

return date.ToLocalTime();
}

public Guid ReadGuid()
Expand Down

0 comments on commit 73cf321

Please sign in to comment.