Closed xxpau-eg closed 3 months ago
@xxpau-eg OData does not natively have a DateTime type without offset. So .NET DateTime
objects are converted to Edm.DateTimeOffset
and vice-versa. If you do not store timezones in your database, then you could configure a default timezone in your OData server and use that timezone in your OData queries, it should return the same result as not having a timezone I think.
builder.Services.AddControllers()
.AddOData(options =>
{
options.TimeZone = TimeZoneInfo.Utc;
// ...
}
And then you could run your queries as
?$filter=priceStartLocalDateTimeUnspect eq 2010-01-02T20:00Z
The Z
at the end represents the UTC timezone.
If you don't want to include the timezone, you could also break out the date and time components using the date
and time
functions:
?$filter=date(DateField) eq 2010-01-02 and time(DateField) eq 20:00
What's interesting is that we do actually support filtering against the date without the time component, e.g.:
?$filter=TransactionDate gt 2024-07-02
The exception only occurs when you add the time component.
I think a date without timezone is just as a ambiguous as time without timezone, so maybe we should support something like 2024-07-02T20:00 as well.
Let me know if this helps.
Thanks for the reply.
Yeah, that's what I'll do, just UTC on the server and masquerade the filters as UTC (even though, they are not really).
For the filtering on date (e.g. TransactionDate gt 2024-07-02
) - I've noticed that as well, but won't use it since in the end it's converted to some DATEPART
function calls on SQL Server side - meaning no index usage. And this happens even if I have DateOnly
on the model side.
Trying to filter rows based on
DateTime
column, without considering time zone causes exception.Assemblies affected
Microsoft.AspNetCore.OData 8.2.0
Reproduce steps
A model like this:
Which is returned from a controller with
[EnableQuery]
andIQueryable<DateAndTimeGridRow>
. Try to get data from the endpoint with an url that contains filtering on theDateTime
: https://localhost:44466/api/DateAndTimes?$skip=0&$top=100&$filter=(priceStartLocalDateTimeUnspec%20eq%202024-01-01T10:00)Expected result
Results from the endpoint. If using EF and SQL Server, I would expect a query that has
WHERE PriceStartLocalDateTimeUnspec = '2024-01-01T10:00'
Actual result
Additional detail
I understand the rationale for that exception (to reduce the likely hood of ambiguity), but in my case - I do store the DateTime in db without the timezone information on purpose. And now, I want to get all rows that match particular filter. E.g.
To give an example (assuming I would store DateTimeOffset) 2010-01-02T20:00+2:00 2010 -01-02T20:00-3:00 2010-01-02T21:00+1:00
My goal is to be able to filter in such a way as to return first two rows when filtering on 2010-01-02T20:00