Skip to content

Blazor Server Localization Middleware works but local time is always UTC. #23662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
micgreen opened this issue Jul 3, 2020 · 6 comments
Closed
Labels
affected-very-few This issue impacts very few customers area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one External This is an issue in a component not contained in this repository. It is open for tracking purposes. feature-blazor-server severity-minor This label is used by an internal tool
Milestone

Comments

@micgreen
Copy link

micgreen commented Jul 3, 2020

Describe the bug

Blazor Server Localization Middleware doesn't appear to work, ie it doesn't appear to convert UTC dates to local time.

I have followed the official guide to setup the Blazor Server Localization Middleware. As a test, I have deployed my app to South Korea (Azure) & I am printing out the UTC & Local Time.

I am located in Texas & when I browse the app, the UTC is displayed, even if I use ToLocalTime()

To Reproduce

Public repo can be found here

  1. Navigate to https://as-cse-egvb-kor.azurewebsites.net/ or https://as-cse-egvb-westus2.azurewebsites.net/
  2. Observe that Local Time is UTC for the client-browser

The relevant files are:
Startup.cs

// ConfigureServices
services.AddLocalization();

// Configure method
            var supportedCultures = new[] { "en-US", "fr-FR", "ko-KR" };
            var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
                .AddSupportedCultures(supportedCultures)
                .AddSupportedUICultures(supportedCultures);

_Host.cshtml

    @{
        this.HttpContext.Response.Cookies.Append(
        CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(
            new RequestCulture(
                CultureInfo.CurrentCulture,
                CultureInfo.CurrentUICulture)));
    }

Index.razor

<div>
    Culture Name: @CultureInfo.CurrentCulture.Name <br />
    Current Time (UTC): @DateTime.UtcNow <br />
    Current Time (Local): @DateTime.UtcNow.ToLocalTime().ToString()<br />
</div>

Exceptions (if any)

None (I am a Blazor newb, so could be user error 👍 )

Further technical details

  • ASP.NET Core 3.1
  • Blazor Server
  • VS
  • Azure App Server Plan - Windows
  • Azure Web App
  • dotnet --info
$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.301
 Commit:    7feb845744

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.301\

Host (useful for support):
  Version: 3.1.5
  Commit:  65cd789777

.NET Core SDKs installed:
  3.1.200 [C:\Program Files\dotnet\sdk]
  3.1.301 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

@micgreen
Copy link
Author

micgreen commented Jul 4, 2020

Actually, I think this may be a dupe of this issue but those were clearly WASM/Client-side Blazor, so not sure.

I've also deployed to West US 2:
https://as-cse-egvb-westus2.azurewebsites.net/

@micgreen micgreen changed the title Blazor Server Localization Middleware doesn't appear to be working. Local Time is still Server Time & not Client Time. Blazor Server Localization Middleware doesn't appear to be working. Time is always UTC. Jul 4, 2020
@micgreen micgreen changed the title Blazor Server Localization Middleware doesn't appear to be working. Time is always UTC. Blazor Server Localization Middleware works but local time is always UTC. Jul 4, 2020
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jul 4, 2020
@micgreen
Copy link
Author

micgreen commented Jul 4, 2020

Interesting.

When I debug locally, it works:
image

When deployed:
image

@mkArtakMSFT mkArtakMSFT added enhancement This issue represents an ask for new feature or an enhancement to an existing one External This is an issue in a component not contained in this repository. It is open for tracking purposes. labels Jul 6, 2020
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Jul 6, 2020
@pranavkm
Copy link
Contributor

pranavkm commented Jul 6, 2020

The problem isn't specific to Blazor, but to .NET Core in general. The runtime has a mechanism to set the CultureInfo on a per-thread basis (CultureInfo.CurrentCulture) and have it flow with the async context. Supporting this feature would require having an equivalent feature for timezones which does not exist today.

@DamianEdwards did we evaluate this scenario when designing the localization middleware?

@micgreen
Copy link
Author

micgreen commented Jul 6, 2020

@pranavkm , thanks for the info! Not a show stopper.

I attempted to implement the solution with JS (momenjs) but unfortunately, the returned JS localized date doesn't appear on the screen unless a user does something on the screen or a new item is added to the list (I display a list in a for loop) & the screen is updated.

I may open up another ticket for that but more than likely, will just wait until the feature is added as I would like to stay away from JS if I can.

Thanks

@pranavkm pranavkm added affected-very-few This issue impacts very few customers severity-minor This label is used by an internal tool labels Oct 5, 2020
@mkArtakMSFT
Copy link
Contributor

Hi. Thanks for contacting us.
We're closing this issue as there was not much community interest in this ask for quite a while now.
You can learn more about our triage process and how we handle issues by reading our Triage Process writeup.

@svenbaus
Copy link

Hi. Thanks for contacting us. We're closing this issue as there was not much community interest in this ask for quite a while now.

I would like to mention that there is a community interest, because we would like to use Blazor Server in a enterprise environment with multiple locations. For know it seems like there is just the workaround with Javascript, which is (in my opinion) really bad.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-very-few This issue impacts very few customers area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one External This is an issue in a component not contained in this repository. It is open for tracking purposes. feature-blazor-server severity-minor This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests

5 participants