
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Vivet.AspNetCore.RequestTimeZone
Advanced tools
Middleware for handling timezone in http requests. Similar implementation as Microsoft.AspNetCore.Localization.
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("myDefaultTimeZone");
Or,
services
.AddRequestTimeZone(x =>
{
// Configuration.
});
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.
FAQs
Middleware for handling timezone in http requests. Similar implementation as Microsoft.AspNetCore.Localization.
We found that vivet.aspnetcore.requesttimezone demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.