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

Fix/datetime filter fix #1316

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
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;

var 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.Local))
return DateTimeKind.Local;

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

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,42 @@ 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));

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(DateTimeKind.Local, 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);

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(DateTimeKind.Local, 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 +182,4 @@ public void ConvertPrimitiveValue_Throws(object valueToConvert, Type conversionT
() => EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, conversionType),
exception);
}
}
}