diff --git a/LiteDB.Tests/Database/DateTimeMinMax_Tests.cs b/LiteDB.Tests/Database/DateTimeMinMax_Tests.cs new file mode 100644 index 000000000..0f16034b1 --- /dev/null +++ b/LiteDB.Tests/Database/DateTimeMinMax_Tests.cs @@ -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(); + 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(); + + 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(); + + MinMaxCommon(col); + } + } + + private void MinMaxCommon(LiteCollection 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); + } + } +} \ No newline at end of file diff --git a/LiteDB/Utils/ByteReader.cs b/LiteDB/Utils/ByteReader.cs index e7f1133c7..7897a05ea 100644 --- a/LiteDB/Utils/ByteReader.cs +++ b/LiteDB/Utils/ByteReader.cs @@ -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()