odata.net icon indicating copy to clipboard operation
odata.net copied to clipboard

TimeSpan? in OData

Open ricardo-valero opened this issue 4 years ago • 3 comments

I wish to learn more about handling TimeSpan. It appears that the default is ISO 8601 duration format.

Assemblies affected

Microsoft.AspNetCore.OData 7.5.4

Reproduce steps

Create an OData Controller and regular scaffolded Web Api Controller using a class with TimeSpan and/or TimeSpan?. I am using Blazor Wasm, thus the need for the JsonTimeSpanConverter. I used Macross Json Extensions.

Expected result

I don't know if it is a feature. I wish only to know the proper way to do this.

Actual result

Without annotations Api Controller: "standardTime": { "ticks": 900000000, "days": 0, "hours": 0, "milliseconds": 0, "minutes": 1, "seconds": 30, "totalDays": 0.0010416666666666667, "totalHours": 0.025, "totalMilliseconds": 90000, "totalMinutes": 1.5, "totalSeconds": 90 } OData Controller: "StandardTime": "PT1M30S"

With annotations [Column(TypeName = "Time")] [JsonConverter(typeof(JsonTimeSpanConverter))] Api Controller: "standardTime": "00:01:30" OData Controller: "StandardTime": "00:01:30.0000000"

I also could have written my own Json converter as seen in https://github.com/dotnet/runtime/issues/29932#issuecomment-540200476, but couldn't get it to work for both nullable and non nullable TimeSpan. They mention this in https://github.com/dotnet/runtime/issues/30843#issuecomment-555395612 but I found using the previosly mentioned extensions easier.

What is the best way to approach this?

ricardo-valero avatar Jan 18 '21 01:01 ricardo-valero

From OData ABNF:

durationValue = [ SIGN ] "P" [ 1DIGIT "D" ] [ "T" [ 1DIGIT "H" ] [ 1DIGIT "M" ] [ 1DIGIT [ "." 1*DIGIT ] "S" ] ] ; the above is an approximation of the rules for an xml dayTimeDuration.

xuzhg avatar Jan 19 '21 17:01 xuzhg

@mikepizzo

xuzhg avatar Jan 19 '21 17:01 xuzhg

From OData ABNF:

durationValue = [ SIGN ] "P" [ 1_DIGIT "D" ] [ "T" [ 1_DIGIT "H" ] [ 1_DIGIT "M" ] [ 1_DIGIT [ "." 1*DIGIT ] "S" ] ] ; the above is an approximation of the rules for an xml dayTimeDuration.

Hey xuzhg, from your reply, does it mean odata supports duration as data type? If yes, what EDM type people should use to define a TimeSpan property and how the TimeSpan property should be used in $filter

cppcraze avatar Jul 26 '22 15:07 cppcraze

@cppcraze Look at the tests from this line https://github.com/OData/odata.net/blob/370cd96aa2a78aecff1b7f9ebe9bebaa7c25ab81/test/FunctionalTests/Microsoft.OData.Core.Tests/ScenarioTests/UriParser/FilterAndOrderByFunctionalTests.cs#L1619

KenitoInc avatar Nov 20 '22 10:11 KenitoInc

Closing this issue due to inactivity. If this issue still persists, feel free to create a new issue.

KenitoInc avatar Feb 09 '23 06:02 KenitoInc