Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcing all DateTimeOffset queries that involve comparisons to be uni… #3612

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading