From aecb2d66369af905e1febde5541e036e3618be16 Mon Sep 17 00:00:00 2001 From: mbdavid Date: Sat, 17 Mar 2018 08:52:14 -0300 Subject: [PATCH] Fix index DateTime #922 --- LiteDB.Shell/Properties/AssemblyInfo.cs | 6 +- LiteDB.Tests/Database/DateTimeMinMaxTests.cs | 79 ++++++++++++++++++++ LiteDB.Tests/Properties/AssemblyInfo.cs | 6 +- LiteDB.nuspec | 2 +- LiteDB/Properties/AssemblyInfo.cs | 6 +- LiteDB/Utils/ByteReader.cs | 7 +- 6 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 LiteDB.Tests/Database/DateTimeMinMaxTests.cs diff --git a/LiteDB.Shell/Properties/AssemblyInfo.cs b/LiteDB.Shell/Properties/AssemblyInfo.cs index 4963cac6d..fbf651311 100644 --- a/LiteDB.Shell/Properties/AssemblyInfo.cs +++ b/LiteDB.Shell/Properties/AssemblyInfo.cs @@ -7,12 +7,12 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("LiteDB.Shell")] -[assembly: AssemblyCopyright("MIT © 2017")] +[assembly: AssemblyCopyright("MIT © 2014-2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("01ce385b-31a7-4b1a-9487-23fe8acb3888")] -[assembly: AssemblyVersion("3.1.4.0")] -[assembly: AssemblyFileVersion("3.1.4.0")] +[assembly: AssemblyVersion("3.1.5.0")] +[assembly: AssemblyFileVersion("3.1.5.0")] [assembly: NeutralResourcesLanguage("en")] diff --git a/LiteDB.Tests/Database/DateTimeMinMaxTests.cs b/LiteDB.Tests/Database/DateTimeMinMaxTests.cs new file mode 100644 index 000000000..83cce26f2 --- /dev/null +++ b/LiteDB.Tests/Database/DateTimeMinMaxTests.cs @@ -0,0 +1,79 @@ +using System; +using System.IO; +using System.Linq; +using LiteDB; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace LiteDB.Tests +{ + #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.Tests/Properties/AssemblyInfo.cs b/LiteDB.Tests/Properties/AssemblyInfo.cs index 1e1f30a94..34df24c0e 100644 --- a/LiteDB.Tests/Properties/AssemblyInfo.cs +++ b/LiteDB.Tests/Properties/AssemblyInfo.cs @@ -7,11 +7,11 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("LiteDB.Tests")] -[assembly: AssemblyCopyright("MIT © 2017")] +[assembly: AssemblyCopyright("MIT © 2014-2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: NeutralResourcesLanguage("en")] [assembly: ComVisible(false)] [assembly: Guid("de183e83-7df6-475c-8185-b0070d098821")] -[assembly: AssemblyVersion("3.1.4.0")] -[assembly: AssemblyFileVersion("3.1.4.0")] +[assembly: AssemblyVersion("3.1.5.0")] +[assembly: AssemblyFileVersion("3.1.5.0")] diff --git a/LiteDB.nuspec b/LiteDB.nuspec index 546c7311b..730fc76e9 100644 --- a/LiteDB.nuspec +++ b/LiteDB.nuspec @@ -2,7 +2,7 @@ LiteDB - 3.1.3 + 3.1.5 LiteDB Mauricio David http://www.litedb.org diff --git a/LiteDB/Properties/AssemblyInfo.cs b/LiteDB/Properties/AssemblyInfo.cs index 889fc1cbb..b895d6422 100644 --- a/LiteDB/Properties/AssemblyInfo.cs +++ b/LiteDB/Properties/AssemblyInfo.cs @@ -8,11 +8,11 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Maurício David")] [assembly: AssemblyProduct("LiteDB")] -[assembly: AssemblyCopyright("MIT © 2017")] +[assembly: AssemblyCopyright("MIT © 2014-2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("54989b5c-4bcf-4d58-b8ba-9b014a324f76")] -[assembly: AssemblyVersion("3.1.4.0")] -[assembly: AssemblyFileVersion("3.1.4.0")] +[assembly: AssemblyVersion("3.1.5.0")] +[assembly: AssemblyFileVersion("3.1.5.0")] [assembly: NeutralResourcesLanguage("en")] diff --git a/LiteDB/Utils/ByteReader.cs b/LiteDB/Utils/ByteReader.cs index 2a476b825..75044d9c0 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()