Skip to content

Commit

Permalink
Forcing all DateTimeOffset queries that involve comparisons to be uni…
Browse files Browse the repository at this point in the history
…versal time. Closes GH-3590
  • Loading branch information
jeremydmiller committed Dec 26, 2024
1 parent 53e0257 commit a0ee4c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/LinqTests/Acceptance/against_date_time_offset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Testing.Documents;
using Marten.Testing.Harness;

namespace LinqTests.Acceptance;

public class against_date_time_offset : IntegrationContext
{
public against_date_time_offset(DefaultStoreFixture fixture) : base(fixture)
{
}

[Fact]
public async Task query_against_date_time_offset_that_is_not_universal_time()
{
await theStore.BulkInsertDocumentsAsync(Target.GenerateRandomData(1000).ToArray());

var results = await theSession.Query<Target>().Where(x => x.DateOffset < DateTimeOffset.Now.AddDays(-1))
.ToListAsync();
}
}
17 changes: 17 additions & 0 deletions src/Marten/Linq/Members/DateTimeOffsetMember.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#nullable enable
using System;
using System.Linq.Expressions;
using System.Reflection;
using Marten.Linq.SqlGeneration.Filters;
using Weasel.Postgresql.SqlGeneration;

namespace Marten.Linq.Members;

Expand All @@ -15,4 +19,17 @@ public override string SelectorForDuplication(string pgType)
{
return TypedLocator.Replace("d.", "");
}

public override ISqlFragment CreateComparison(string op, ConstantExpression constant)
{
if (constant.Value == null)
{
return op == "=" ? new IsNullFilter(this) : new IsNotNullFilter(this);
}

var value = (DateTimeOffset)constant.Value;

var def = new CommandParameter(value.ToUniversalTime());
return new MemberComparisonFilter(this, def, op);
}
}

0 comments on commit a0ee4c7

Please sign in to comment.