Vivet.AspNetCore.RequestTimeZone 8.1.1

dotnet add package Vivet.AspNetCore.RequestTimeZone --version 8.1.1                
NuGet\Install-Package Vivet.AspNetCore.RequestTimeZone -Version 8.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vivet.AspNetCore.RequestTimeZone" Version="8.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Vivet.AspNetCore.RequestTimeZone --version 8.1.1                
#r "nuget: Vivet.AspNetCore.RequestTimeZone, 8.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Vivet.AspNetCore.RequestTimeZone as a Cake Addin
#addin nuget:?package=Vivet.AspNetCore.RequestTimeZone&version=8.1.1

// Install Vivet.AspNetCore.RequestTimeZone as a Cake Tool
#tool nuget:?package=Vivet.AspNetCore.RequestTimeZone&version=8.1.1                

Vivet.AspNetCore.RequestTimeZone

Build status 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:

  • QueryStringRequestTimeZoneProvider (?tz=myTimezone)
  • CookieRequestTimeZoneProvider
  • HeaderRequestTimeZoneProvider (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.

Registration

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.

Accessors

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 Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Vivet.AspNetCore.RequestTimeZone:

Package Downloads
NanoCore

The project is inspired by years of tedious repetitions, continuously re-writing similar code-snippets and libraries, to handle common functionality, not related to the business domain, such as logging, data persistence, message queuing, documentation, validation and similar.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.1.1 1,377 8/29/2024
8.1.0 95 8/29/2024
8.0.3 628 7/28/2024
7.0.4 3,021 10/5/2023
6.0.4 8,719 9/21/2022
3.1.1 14,266 6/30/2020

- Updated NuGets.