diff --git a/src/Microsoft.AspNetCore.OData/Query/ODataQueryContextExtensions.cs b/src/Microsoft.AspNetCore.OData/Query/ODataQueryContextExtensions.cs index 256acedf7..7863003d3 100644 --- a/src/Microsoft.AspNetCore.OData/Query/ODataQueryContextExtensions.cs +++ b/src/Microsoft.AspNetCore.OData/Query/ODataQueryContextExtensions.cs @@ -7,6 +7,7 @@ using System.Linq; using Microsoft.AspNetCore.OData.Abstracts; +using Microsoft.AspNetCore.OData.Extensions; using Microsoft.AspNetCore.OData.Query.Expressions; using Microsoft.AspNetCore.OData.Query.Validator; using Microsoft.Extensions.DependencyInjection; @@ -25,6 +26,8 @@ public static ODataQuerySettings GetODataQuerySettings(this ODataQueryContext co returnSettings.CopyFrom(settings); } + returnSettings.TimeZone ??= context.Request.GetTimeZoneInfo(); + return returnSettings; } diff --git a/test/Microsoft.AspNetCore.OData.Tests/Query/Query/ODataQueryContextExtensionsTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Query/Query/ODataQueryContextExtensionsTests.cs new file mode 100644 index 000000000..ee44c4f45 --- /dev/null +++ b/test/Microsoft.AspNetCore.OData.Tests/Query/Query/ODataQueryContextExtensionsTests.cs @@ -0,0 +1,44 @@ +//----------------------------------------------------------------------------- +// +// Copyright (c) .NET Foundation and Contributors. All rights reserved. +// See License.txt in the project root for license information. +// +//------------------------------------------------------------------------------ + +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.OData.Query; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using Xunit; + +namespace Microsoft.AspNetCore.OData.Tests.Query.Query; + +public class ODataQueryContextExtensionsTests +{ + [Fact] + public void GetODataQuerySettings_WithContext_SetsTimeZone() + { + // Arrange + ServiceCollection services = new ServiceCollection(); + services.AddLogging().AddControllers().AddOData(); + IServiceProvider serviceProvider = services.BuildServiceProvider(); + + Mock mockHttpRequest = new Mock(); + Mock mockHttpContext = new Mock(); + mockHttpContext.SetupGet(x => x.RequestServices).Returns(serviceProvider); + mockHttpRequest.Setup(x => x.HttpContext) + .Returns(mockHttpContext.Object); + + ODataQueryContext context = new ODataQueryContext + { + Request = mockHttpRequest.Object, + }; + + // Act + ODataQuerySettings querySettings = context.GetODataQuerySettings(); + + // Assert + Assert.NotNull(querySettings.TimeZone); + } +}