Skip to content

Commit a0ee4c7

Browse files
committed
Forcing all DateTimeOffset queries that involve comparisons to be universal time. Closes GH-3590
1 parent 53e0257 commit a0ee4c7

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Marten;
5+
using Marten.Testing.Documents;
6+
using Marten.Testing.Harness;
7+
8+
namespace LinqTests.Acceptance;
9+
10+
public class against_date_time_offset : IntegrationContext
11+
{
12+
public against_date_time_offset(DefaultStoreFixture fixture) : base(fixture)
13+
{
14+
}
15+
16+
[Fact]
17+
public async Task query_against_date_time_offset_that_is_not_universal_time()
18+
{
19+
await theStore.BulkInsertDocumentsAsync(Target.GenerateRandomData(1000).ToArray());
20+
21+
var results = await theSession.Query<Target>().Where(x => x.DateOffset < DateTimeOffset.Now.AddDays(-1))
22+
.ToListAsync();
23+
}
24+
}

src/Marten/Linq/Members/DateTimeOffsetMember.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#nullable enable
2+
using System;
3+
using System.Linq.Expressions;
24
using System.Reflection;
5+
using Marten.Linq.SqlGeneration.Filters;
6+
using Weasel.Postgresql.SqlGeneration;
37

48
namespace Marten.Linq.Members;
59

@@ -15,4 +19,17 @@ public override string SelectorForDuplication(string pgType)
1519
{
1620
return TypedLocator.Replace("d.", "");
1721
}
22+
23+
public override ISqlFragment CreateComparison(string op, ConstantExpression constant)
24+
{
25+
if (constant.Value == null)
26+
{
27+
return op == "=" ? new IsNullFilter(this) : new IsNotNullFilter(this);
28+
}
29+
30+
var value = (DateTimeOffset)constant.Value;
31+
32+
var def = new CommandParameter(value.ToUniversalTime());
33+
return new MemberComparisonFilter(this, def, op);
34+
}
1835
}

0 commit comments

Comments
 (0)