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 13 commits into
base: main
Choose a base branch
from
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;
VitaliyChaban marked this conversation as resolved.
Show resolved Hide resolved
}
}
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);
}
}
}