-
Notifications
You must be signed in to change notification settings - Fork 164
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
Strange time zone propagation in the filter #1223
Labels
bug
Something isn't working
Comments
Related to #378 |
Great solution would be to be able to specify custom deserializer for DateTime, or more precisely converter from DateTimeOffset to DateTime. |
It is difficult to understand why such a serious and fundamental problem has not been taken seriously. public static void HackDateTimeConvert()
{
var targetType = AccessTools.TypeByName("Microsoft.AspNetCore.OData.Edm.EdmPrimitiveHelper");
var targetMethod = AccessTools.Method(targetType, "ConvertPrimitiveValue", new Type[] { typeof(object), typeof(Type), typeof(TimeZoneInfo) });
var prefix = new HarmonyMethod(typeof(ODataEdmStore).GetMethod("HackDateTimeConvertPrefix"));
var harmony = new Harmony("Microsoft.AspNetCore.OData.Edm.Patch");
harmony.Patch(targetMethod, prefix);
}
public static bool HackDateTimeConvertPrefix(ref object __result, object value, Type type, TimeZoneInfo timeZoneInfo)
{
if (value.GetType() == type || value.GetType() == Nullable.GetUnderlyingType(type))
{
__result = value;
return false;
}
if (type.IsInstanceOfType(value))
{
__result = value;
return false;
}
if (type == typeof(DateTime))
{
if (value is DateTimeOffset)
{
DateTimeOffset dateTimeOffsetValue = (DateTimeOffset)value;
TimeZoneInfo timeZone = timeZoneInfo ?? TimeZoneInfo.Local;
dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone);
__result = dateTimeOffsetValue.UtcDateTime;
return false;
}
}
return true;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current
Sample GET url http://localhost:51994/time-tracks?$filter=((dateFrom+eq+2024-04-17T09:45:00%2B02:00))&$orderby=dateFrom+desc&$skip=0&$top=25 contains time 2024-04-17T9:45:00.0000000+02:00
Produces SQL query that contains "2024-04-17T11:45:00.0000000+02:00". So it get's treated as if the date in the filter was UTC time.
The fault is in fact caused by DateTime being parsed as Kind.Unspecified (where expression in the query contains DateTime of this kind).
![image](https://private-user-images.githubusercontent.com/71645109/324918752-42eee92d-3b89-4e07-a0cd-b04bd3d32079.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzNTMxMzYsIm5iZiI6MTczOTM1MjgzNiwicGF0aCI6Ii83MTY0NTEwOS8zMjQ5MTg3NTItNDJlZWU5MmQtM2I4OS00ZTA3LWEwY2QtYjA0YmQzZDMyMDc5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDA5MzM1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTU5NDA2NjVkNTMwNWZjMzVkZTZjNWVkYmNmYWUwNjhkNjI5NjZlYjBkMGU2MGY5NzliZTkyMDgwODI5MTZhMTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.2H-HOUYB1-Pie3qpuUhfPq5SXvgTHREQibb0GI0rXGU)
Expected
When oDataOptions.TimeZone is set in the configuration of the DateTime will be of Kind.Local and if the timezone in the filter is not the same as specified in TimeZone, the time will be shifted.
This would lead to result query with "2024-04-17T09:45:00.0000000+02:00".
The text was updated successfully, but these errors were encountered: