![]() |
VOOZH | about |
dotnet add package Vivet.AspNetCore.RequestTimeZone --version 10.0.4
NuGet\Install-Package Vivet.AspNetCore.RequestTimeZone -Version 10.0.4
<PackageReference Include="Vivet.AspNetCore.RequestTimeZone" Version="10.0.4" />
<PackageVersion Include="Vivet.AspNetCore.RequestTimeZone" Version="10.0.4" />Directory.Packages.props
<PackageReference Include="Vivet.AspNetCore.RequestTimeZone" />Project file
paket add Vivet.AspNetCore.RequestTimeZone --version 10.0.4
#r "nuget: Vivet.AspNetCore.RequestTimeZone, 10.0.4"
#:package Vivet.AspNetCore.RequestTimeZone@10.0.4
#addin nuget:?package=Vivet.AspNetCore.RequestTimeZone&version=10.0.4Install as a Cake Addin
#tool nuget:?package=Vivet.AspNetCore.RequestTimeZone&version=10.0.4Install as a Cake Tool
👁 Build and Deploy
👁 NuGet
👁 NuGet
Middleware to configure request timezone options.
The current timezone on a request is set in the Request TimeZone Middleware. The middleware is enabled in the Startup.Configure method, and on every request the list of providers configured in the RequestTimeZoneOptions is enumerated, and the first provider in order, that can successfully determine the request timezone is applied. The default providers are:
?tz=myTimezone)tz=myTimezone)The default list goes from most specific to least specific. You can change the order and even add a custom timezone provider, similar to the implementation of Microsoft.AspNetCore.Localization. If none of the providers can determine the request timezone, the DefaultRequestTimeZone is used.
To configure the Request TimeZone Middleware, first add the required services to the IServiceCollection, as shown below.
services
.AddRequestTimeZone(x =>
{
x.DefaultTimeZone = "UTC";
x.EnableRequestToUtc = true;
x.EnableResponseToLocal = true;
x.JsonSerializerType = JsonSerializerType.Newtonsoft;
});
Next, register the middleware in the pipeline, as shown below.
applicationBuilder
.UseRequestTimeZone();
The middleware is now configured in the pipeline, and will register request timezone when suplied by one of the configured providers.
When a request model contains properties of type DateTimeOffset, those will be converted to utc datetime, based on the request timezone. Subsequently, when a response is serialized, the DateTimeOffset properties are converted back to local datetime.
When a timezone is provided as part of a request, it can be retrieved through the IRequestTimeZoneFeature from a controller, as follows:
var requestTimeZone = this.HttpContext.Features
.Get<tRequestTimeZoneFeature>()
.RequestTimeZone;
Alternatively, the feature may be accessed through the extension method GetUserTimeZone() of HttpContext, as shown below:
var requestTimeZone = httpContext.GetUserTimeZone();
Finally, the library also contains a ThreadStatic accessor, called DateTimeInfo.
The implementation exposes the RequestTimezone, as well an utc and local datetime that is based on the timezone.
var now = DateTimeInfo.Now // Gets datetime
var utc = DateTimeInfo.UtcNow // Gets utc datetime
var timezone = DateTimeInfo.TimeZone.Value // Gets the request timezone.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on Vivet.AspNetCore.RequestTimeZone:
| Package | Downloads |
|---|---|
|
Nano.App
This package is part of the Nano Library, a set of reusable .NET libraries for building microservice applications. Nano addresses common non-business concerns such as logging, persistence, messaging, validation, and documentation, while remaining fully configurable and extensible, so applications can stay focused on business logic. See https://github.com/Nano-Core/Nano.Library for details. |
This package is not used by any popular GitHub repositories.
- Updated NuGets.