[Workaround] How to resolve local time zone on Blazor SPA (v.0.5.x)?
Tags: blazor, spa
Published: 2018-09-04 14:11:31

---
title: [Workaround] How to resolve local time zone on Blazor SPA (v.0.5.x)?
published: true
description: This topic describe how to resolve local time zone on Blazor SPA.
tags: blazor, spa
---

## Introduction: What's "Blazor"?

"Blazor" is a Single Page Web Application (a.k.a "SPA") framework.

Blazor allows you to running C# code and .NET Standard 2.0 native assembly (.dll) on a top of modern web browser, without any .NET server processes.

If you are new to Blazor, I recommend watching this YouTube video.

[NDC Oslo 2017 - "Web Apps can’t really do *that*, can they? - Steve Sanderson"](https://www.youtube.com/watch?v=MiLAE6HMr10&feature=youtu.be&t=31m59s)

The video was about prototype of Blazor, and the current version when I wrote this post was v.0.5.x, which had some breaking changes. But that video is useful to understanding the concept and pros/cons of "Blazor".

## Problem: Blazor SPA has no time zone information ...!

I feel Blazor is a great framework for C# developers.

However, Blazor is still an experimental project at the moment.

Recently I found one of the cases showing that a Blazor is still an experimental project.

Amazingly, **count of time zone information on Blazor application is "0"!**

Therefore, `"TimeZoneInfo.Local"` always returns the UTC time zone, `" DateTime.Now"` always returns the UTC time, regardless of the local setting of the browser or OS.

![fig.1](https://github.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/blob/master/.assets/fig-2018-09-03-001.png?raw=true)

What can we do if we need to access local time or any time zone information?

## Quick Solution: Use "Toolbelt.Blazor.TimeZoneKit" NuGet package.

You can resolve this issue by installing ["Toolbelt.Blazor.TimeZoneKit" NuGet package](https://www.nuget.org/packages/Toolbelt.Blazor.TimeZoneKit/) on your Blazor application project as follows.

**Step.1** - Install the NuGet package, like follows.

```shell
> dotnet add package Toolbelt.Blazor.TimeZoneKit
```

**Step.2** - call `"UseLocalTimeZone()"` extension method that is provided by the package, in `"Configure()"` method of the startup class.

```csharp
...
using Toolbelt.Blazor.TimeZoneKit;

public class Startup
{
    ...
    public void Configure(IBlazorApplicationBuilder app)
    {
        app.UseLocalTimeZone();
    ...
```

That's all!

![fig.2](https://github.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/blob/master/.assets/fig-2018-09-03-002.png?raw=true)


## How does it setup the system time zone information?

There is no magic.

"Toolbelt.Blazor.TimeZoneKit" just contains all of the .NET system time zone information in it's assembly (.dll) file.

(see: ["TimeZoneKit.CreateSystemTimeZones.cs"](https://github.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/blob/master/Toolbelt.Blazor.TimeZoneKit/TimeZoneKit.CreateSystemTimeZones.cs#L16))

`"UseLocalTimeZone()"` extension method override private field of `"TimeZoneInfo"` by reflection technology to setup the system time zone information on the application runtime.

## How does it get the local time zone of the browser?

Recently, most web browsers support `"Intl.DateTimeFormat().resolvedOptions().timeZone"` JavaScript code.

That JavaScript code returns local time zone name.


Blazor provides a way of interoperability of C# code and JavaScript code, so `"UseLocalTimeZone()"` extension method can calls the JavaScript code that to get local timezone name.

![fig.3](https://github.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/blob/master/.assets/fig-2018-09-03-003.png?raw=true)

However, the interope code from C# to JavaScript is asynchronous.

If `"UseLocalTimeZone()"` extension method can't complete setting up of local time zone synchronously during its execution time, it will cause terrible bags.

![fig.4](https://raw.githubusercontent.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/master/.assets/fig-2018-09-04-001.png)

However, because it causes a deadlock, we can not wait synchronously for task results.

![fig.5](https://raw.githubusercontent.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/master/.assets/fig-2018-09-04-002.png)

To avoid this problem, `"UseLocalTimeZone()"` extension method invokes the JavaScript code that calls back C# code.

Since the interope code from JavaScript to C# is synchronously, the loca time zone setup process will complete until `"UseLocalTimeZone()"` extension method is returned ;)

![fig.6](https://github.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/blob/master/.assets/fig-2018-09-04-003.png?raw=true)

## How to convert IANA time zone name to .NET time zone id?

The JavaScript code `"Intl.DateTimeFormat().resolvedOptions().timeZone"` returns the time zone name, but it's not the .NET time zone id.

The local time zone name of JavaScript is called "IANA time zone name".

In order to convert IANA time zone name to .NET time zone id, "Toolbelt.Blazor.TimeZoneKit" contains the mapping information from IANA time zone name to .NET time zone id, in it's assembly (.dll) file.

(see: ["TimeZoneKit.IANAtoTZIdMap.cs"](https://github.com/jsakamoto/Toolbelt.Blazor.TimeZoneKit/blob/master/Toolbelt.Blazor.TimeZoneKit/TimeZoneKit.IANAtoTZIdMap.cs#L5))

## Note

- **WARNING** - The library accesses private members of System.TimeZoneInfo using the "Reflection" .NET API, so it may break in future .NET runtime (mono.wasm) release.
- In my test case, the library increased the size of a Blazor browser application content by 154 KB. (by 20KB gzip transfer.)

## Conclusion

The future release of Blazor will be supported local time zone, I think.

See also: {% github https://github.com/aspnet/Blazor/issues/1166 %}

"Toolbelt.Blazor.TimeZoneKit" NuGet package will help you until Blazor support time zones.

Let's look forward to the new release of Blazor!

Happy coding with Blazor :)