Skip to content

Commit 13c6c39

Browse files
committed
fix: Fix timezone bounded date expression creation.
1 parent 826876a commit 13c6c39

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

ExpressionBuilder.Test.NetCore/Unit/Helpers/TestData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public TestData()
3838
new Person { Name = "Jane Jones", Gender = PersonGender.Female, Salary=3500, Birth = new Person.BirthData { Date = new DateTime(1980, 12, 20), Country = "AUS" } },
3939
new Person { Name = "Fulano Silva", Gender = PersonGender.Male, Salary=3322, Birth = new Person.BirthData { Date = new DateTime(1983, 5, 10), Country = "BRA" }, Employer = company, Manager = manager2, EmployeeReferenceNumber = 986543434323 },
4040
new Person { Name = "John Hancock", Gender = PersonGender.Male, Employer = company },
41-
new Person { Name = "Jack Luffy", SalaryDate = new DateTime(2024, 10, 29, 23, 10, 10, DateTimeKind.Local), Gender = PersonGender.Male, Employer = company },
42-
new Person { Name = "Jack Luffy", SalaryDateOffset = new DateTimeOffset(2024, 10, 29, 23, 10, 10, new TimeSpan(3, 0, 0)), Gender = PersonGender.Male, Employer = company },
41+
new Person { Name = "Jack Luffy 30-21", SalaryDate = new DateTime(2024, 10, 30, 21, 10, 10, DateTimeKind.Utc), Gender = PersonGender.Male, Employer = company },
42+
new Person { Name = "Jack Luffy Offset 30-21", SalaryDateOffset = new DateTimeOffset(2024, 10, 30, 21, 10, 10, TimeSpan.Zero), Gender = PersonGender.Male, Employer = company },
4343
];
4444
var id = 1;
4545
foreach (var person in People)

ExpressionBuilder.Test.NetCore/Unit/Operations/DateEqualToTests.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public DateEqualToTests()
2020
TestData = new TestData();
2121
}
2222

23-
[TestCase("SalaryDate", "2024-10-29", TestName = "'DateEqualTo' operation - Get expression (DateTime? value)")]
23+
[TestCase("SalaryDate", "2024-10-31", TestName = "'DateEqualTo' operation - Get expression (DateTime? value)")]
24+
[TestCase("SalaryDate", "2024-10-30", TestName = "'DateEqualTo' operation - Get expression (DateTime? value)")]
2425
public void GetExpressionDateTimeValueTest(string propertyName, object value)
2526
{
2627
var dateValue = DateTime.Parse(value.ToString());
27-
dateValue = new DateTime(dateValue.Year, dateValue.Month, dateValue.Day, 0, 0, 0, DateTimeKind.Utc);
28-
var endDate = dateValue.AddDays(1).AddTicks(-1);
28+
var startDate = dateValue.ToUniversalTime();
29+
var endDate = startDate.AddDays(1).AddTicks(-1);
2930
var operation = new DateEqualTo();
3031
var param = Expression.Parameter(typeof(Person), "x");
3132
var member = Expression.Property(param, propertyName);
@@ -44,29 +45,30 @@ public void GetExpressionDateTimeValueTest(string propertyName, object value)
4445
Assert.That((shouldSubjectLeft.Left as MemberExpression).Member.Name, Is.EqualTo("Value"));
4546
Assert.That(shouldSubjectLeft.NodeType, Is.EqualTo(ExpressionType.GreaterThanOrEqual));
4647
Assert.That(shouldSubjectLeft.Right, Is.AssignableFrom<ConstantExpression>());
47-
Assert.That((shouldSubjectLeft.Right as ConstantExpression).Value, Is.LessThanOrEqualTo(dateValue));
48+
Assert.That((shouldSubjectLeft.Right as ConstantExpression).Value, Is.GreaterThanOrEqualTo(startDate));
4849

4950
var shouldSubjectRight = (BinaryExpression)((BinaryExpression)expression.Right).Right;
5051
Assert.That((shouldSubjectRight.Left as MemberExpression).Member.Name, Is.EqualTo("Value"));
5152
Assert.That(shouldSubjectRight.NodeType, Is.EqualTo(ExpressionType.LessThanOrEqual));
5253
Assert.That(shouldSubjectRight.Right, Is.AssignableFrom<ConstantExpression>());
53-
Assert.That((shouldSubjectRight.Right as ConstantExpression).Value, Is.GreaterThanOrEqualTo(dateValue));
54+
Assert.That((shouldSubjectRight.Right as ConstantExpression).Value, Is.LessThanOrEqualTo(endDate));
5455

5556
//Testing the operation execution
5657
var lambda = Expression.Lambda<Func<Person, bool>>(expression, param);
5758
var people = TestData.People.Where(lambda.Compile());
58-
var solutionMethod = (Func<Person, bool>)GetType().GetMethod(propertyName).Invoke(this, [dateValue, endDate]);
59+
var solutionMethod = (Func<Person, bool>)GetType().GetMethod(propertyName).Invoke(this, [startDate, endDate]);
5960
var solution = TestData.People.Where(solutionMethod);
6061
Assert.That(people, Is.EquivalentTo(solution));
6162
}
6263

6364
public static Func<Person, bool> SalaryDate(DateTime startDate, DateTime endDate) => x => x.SalaryDate != null && (x.SalaryDate.Value >= startDate && x.SalaryDate.Value <= endDate);
6465

65-
[TestCase("SalaryDateOffset", "2024-10-29", TestName = "'DateEqualTo Offset' operation - Get expression (DateTimeOffset? value)")]
66+
[TestCase("SalaryDateOffset", "2024-10-31", TestName = "'DateEqualTo Offset' operation - Get expression (DateTimeOffset? value)")]
67+
[TestCase("SalaryDateOffset", "2024-10-30", TestName = "'DateEqualTo Offset' operation - Get expression (DateTimeOffset? value)")]
6668
public void GetExpressionDateTimeOffsetValueTest(string propertyName, object value)
6769
{
6870
var dateValue = DateTimeOffset.Parse(value.ToString());
69-
dateValue = new DateTimeOffset(dateValue.Year, dateValue.Month, dateValue.Day, 0, 0, 0, TimeSpan.Zero);
71+
var startDate = dateValue.ToUniversalTime();
7072
var endDate = dateValue.AddDays(1).AddTicks(-1);
7173
var operation = new DateEqualTo();
7274
var param = Expression.Parameter(typeof(Person), "x");
@@ -97,10 +99,10 @@ public void GetExpressionDateTimeOffsetValueTest(string propertyName, object val
9799
//Testing the operation execution
98100
var lambda = Expression.Lambda<Func<Person, bool>>(expression, param);
99101
var people = TestData.People.Where(lambda.Compile());
100-
var solutionMethod = (Func<Person, bool>)GetType().GetMethod(propertyName).Invoke(this, [dateValue, endDate]);
102+
var solutionMethod = (Func<Person, bool>)GetType().GetMethod(propertyName).Invoke(this, [startDate, endDate]);
101103
var solution = TestData.People.Where(solutionMethod);
102104
Assert.That(people, Is.EquivalentTo(solution));
103105
}
104106

105-
public static Func<Person, bool> SalaryDateOffset(DateTimeOffset startDate, DateTimeOffset endDate) => x => x.SalaryDate != null && (x.SalaryDate.Value >= startDate && x.SalaryDate.Value <= endDate);
107+
public static Func<Person, bool> SalaryDateOffset(DateTimeOffset startDate, DateTimeOffset endDate) => x => x.SalaryDateOffset != null && (x.SalaryDateOffset.Value >= startDate && x.SalaryDateOffset.Value <= endDate);
106108
}

ExpressionBuilder/ExpressionBuilder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</Description>
1919
<AssemblyVersion>1.0.0.0</AssemblyVersion>
2020
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
21-
<Version>1.0.10</Version>
21+
<Version>1.0.11</Version>
2222
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2323
<PackageId>Milvasoft.ExpressionBuilder</PackageId>
2424
</PropertyGroup>

ExpressionBuilder/Operations/DateEqualTo.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,29 @@ private static (ConstantExpression, ConstantExpression) GetStartAndEndDates(Cons
4242
if (constant.Type == typeof(DateTime) || constant.Type == typeof(DateTime?))
4343
{
4444
var valueAsDateTime = (DateTime)constant.Value;
45-
DateTime startDate = new(valueAsDateTime.Year, valueAsDateTime.Month, valueAsDateTime.Day, 0, 0, 0, kind: DateTimeKind.Utc);
46-
DateTime endDate = startDate.AddDays(1).AddTicks(-1);
45+
46+
// Kullanıcının tarihine göre günün başı ve sonu
47+
DateTime startDateLocal = new DateTime(valueAsDateTime.Year, valueAsDateTime.Month, valueAsDateTime.Day, 0, 0, 0, DateTimeKind.Local);
48+
DateTime endDateLocal = startDateLocal.AddDays(1).AddTicks(-1);
49+
50+
// UTC'ye dönüştürme
51+
DateTime startDate = startDateLocal.ToUniversalTime();
52+
DateTime endDate = endDateLocal.ToUniversalTime();
4753

4854
startDateExpression = Expression.Constant(startDate);
4955
endDateExpression = Expression.Constant(endDate);
5056
}
5157
else if (constant.Type == typeof(DateTimeOffset) || constant.Type == typeof(DateTimeOffset?))
5258
{
5359
var valueAsDateTimeOffset = (DateTimeOffset)constant.Value;
54-
DateTimeOffset startDate = new(valueAsDateTimeOffset.Year, valueAsDateTimeOffset.Month, valueAsDateTimeOffset.Day, 0, 0, 0, TimeSpan.Zero); // UTC
55-
DateTimeOffset endDate = startDate.AddDays(1).AddTicks(-1);
60+
61+
// Kullanıcının saat dilimine göre gün başlangıcı ve sonu
62+
DateTimeOffset startDateLocal = new(valueAsDateTimeOffset.Year, valueAsDateTimeOffset.Month, valueAsDateTimeOffset.Day, 0, 0, 0, valueAsDateTimeOffset.Offset);
63+
DateTimeOffset endDateLocal = startDateLocal.AddDays(1).AddTicks(-1);
64+
65+
// UTC'ye dönüştürme
66+
DateTimeOffset startDate = startDateLocal.ToOffset(TimeSpan.Zero);
67+
DateTimeOffset endDate = endDateLocal.ToOffset(TimeSpan.Zero);
5668

5769
startDateExpression = Expression.Constant(startDate);
5870
endDateExpression = Expression.Constant(endDate);

0 commit comments

Comments
 (0)