Skip to content

Commit

Permalink
Fix/datetime filter fix (#1316)
Browse files Browse the repository at this point in the history
* datetime_filter_fix

* comment

* minor refactor

* minor refactor

* fix: set behavior depending on timeZoneInfo

* minor cleanup

* Update src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs

minor refactor

Co-authored-by: Samuel Wanjohi <[email protected]>

* check utc first

* improve tests

* Implicit test fix

* Implicit test fix

* add filter by dateTime test

* Revert "add filter by dateTime test"

This reverts commit 34b9ef8.

---------

Co-authored-by: Samuel Wanjohi <[email protected]>
  • Loading branch information
VitaliyChaban and WanjohiSammy authored Jan 9, 2025
1 parent f9101a1 commit e402fa7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo
{
DateTimeOffset dateTimeOffsetValue = (DateTimeOffset)value;
TimeZoneInfo timeZone = timeZoneInfo ?? TimeZoneInfo.Local;

dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone);
return dateTimeOffsetValue.DateTime;

DateTimeKind dateTimeKind = GetTargetDateTimeKind(timeZone);

return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, dateTimeKind);
}

if (value is Date)
Expand Down Expand Up @@ -183,4 +187,15 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo
}
}
}
}

private static DateTimeKind GetTargetDateTimeKind(TimeZoneInfo timeZone)
{
if (timeZone.Equals(TimeZoneInfo.Utc))
return DateTimeKind.Utc;

if (timeZone.Equals(TimeZoneInfo.Local))
return DateTimeKind.Local;

return DateTimeKind.Unspecified;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static TheoryDataSet<DateTimeOffset> ConvertDateTime_NonStandardPrimitive

[Theory]
[MemberData(nameof(ConvertPrimitiveValue_NonStandardPrimitives_Data))]
[MemberData(nameof(ConvertPrimitiveValue_NonStandardPrimitives_ExtraData))]
[MemberData(nameof(ConvertPrimitiveValue_NonStandardPrimitives_ExtraData))]
public void ConvertPrimitiveValue_NonStandardPrimitives(object valueToConvert, object result, Type conversionType)
{
// Arrange & Act
Expand Down Expand Up @@ -124,6 +124,52 @@ public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateT
Assert.Equal(valueToConvert.LocalDateTime, dt);
}

[Theory]
[MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))]
public void ConvertDateTimeValue_ImplicitKind(DateTimeOffset valueToConvert)
{
// Arrange & Act
object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime));

//if server local time is UTC, then expect Utc Kind
DateTimeKind expectedTimeKind = TimeZoneInfo.Local.Equals(TimeZoneInfo.Utc)
? DateTimeKind.Utc
: DateTimeKind.Local;

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(expectedTimeKind, dt.Kind);
}

[Theory]
[MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))]
public void ConvertDateTimeValue_ExplicitLocalKind(DateTimeOffset valueToConvert)
{
// Arrange & Act
object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Local);

//if server local time is UTC, then expect Utc Kind
DateTimeKind expectedTimeKind = TimeZoneInfo.Local.Equals(TimeZoneInfo.Utc)
? DateTimeKind.Utc
: DateTimeKind.Local;

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(expectedTimeKind, dt.Kind);
}

[Theory]
[MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))]
public void ConvertDateTimeValue_ExplicitUtcKind(DateTimeOffset valueToConvert)
{
// Arrange & Act
object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Utc);

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(DateTimeKind.Utc, dt.Kind);
}

[Theory]
[MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))]
public void ConvertDateTimeValue_NonStandardPrimitives_CustomTimeZoneInfo(DateTimeOffset valueToConvert)
Expand All @@ -146,4 +192,4 @@ public void ConvertPrimitiveValue_Throws(object valueToConvert, Type conversionT
() => EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, conversionType),
exception);
}
}
}

0 comments on commit e402fa7

Please sign in to comment.