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

Strange time zone propagation in the filter #1223

Closed
cts-tradeit opened this issue Apr 23, 2024 · 3 comments · Fixed by #1316
Closed

Strange time zone propagation in the filter #1223

cts-tradeit opened this issue Apr 23, 2024 · 3 comments · Fixed by #1316
Labels
bug Something isn't working

Comments

@cts-tradeit
Copy link

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

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".

@cts-tradeit cts-tradeit added the bug Something isn't working label Apr 23, 2024
@cts-tradeit
Copy link
Author

cts-tradeit commented Apr 23, 2024

Related to #378

@cts-tradeit
Copy link
Author

Great solution would be to be able to specify custom deserializer for DateTime, or more precisely converter from DateTimeOffset to DateTime.

@Yumitoya8569
Copy link

It is difficult to understand why such a serious and fundamental problem has not been taken seriously.
Though I don't really want to use the hacky method, it is the only way to fix the bug at present.

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
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants