.NET Tips: Get TimeZone Data on Different Platforms

Photo by Matthew Henry from Burst

This post is available at Medium as well.

If you have developed a cross-platform application before, you might have encountered a situation where different platforms still have different performance despite using a cross-platform solution. And this post will discuss a situation where the result of getting the Time Zone data are different on different platforms in .NET Core.

Get Time Zone Data on the Windows platform

Firstly, let’s create a new .NET Core console app on the Windows platform. In this case, I will take New Zealand Time as an example.

using System;
namespace DotNETTips
{
    class Program
    {
        static void Main(string[] args)
        {
            var dateTime = DateTime.UtcNow;
            Console.WriteLine($"dateTime in UTC : {dateTime}");
            var nzTzInfo = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
            dateTime = TimeZoneInfo.ConvertTimeFromUtc(dateTime, nzTzInfo);
            Console.WriteLine($"dateTime in NZ Time Zone : {dateTime}");
            Console.ReadKey();
        }
    }
}

In the above code, the time zone id of New Zealand on the Windows platform is “New Zealand Standard Time”, as you can see in the screenshot below.

And if you are interested in the topic of TimeZone Data on the Windows platform, you can find the list of default Time Zones on Windows at the following link.

Default Time Zones | Microsoft Docs

Therefore, we can get the TimeZoneInfo data of New Zealand by calling the FindSystemTimeZoneById method.

TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");

Then we can convert the date-time data in UTC format into the NZDT.

TimeZoneInfo.ConvertTimeFromUtc(dateTime, nzTzInfo);

The above code works very well on the Windows platform actually, and you can find the date-time is printed as expected in the terminal.

Get Time Zone Data on macOS/Linux

However, if you want to run the same code on your macbook pro laptop or your linux computer, it won’t print out the date-time in the New Zealand time zone, but an TimeZoneNotFoundException.

This is not because your macbook pro doesn’t have time zone data installed, but the macOS uses another time zone data provider — IANA Time Zone Database.

Unlike the Time Zone Id on the Windows platform, the IANA Time Zone identifier for New Zealand is “Pacific/Auckland”. And there is a time zone list for you as well, you can click the following link to check the time zone list in the Time Zone Database.

List of tz database time zones - Wikipedia

Therefore, if you want to let your code run as expected, you have to modify the argument of the FindSystemTimeZoneById method from “New Zealand Standard Time” to “Pacific/Auckland” on macOS/Linux.

And the correct date-time will be printed in the console. So if your app will run on different platforms you will need to handle the differences.

The Solution

However, if you don’t want to handle the differences yourself, because it’s difficult to maintain, there is an available open-source package on GitHub that can help you address this problem.

mattjohnsonpint/TimeZoneConverter

TimeZoneConverter is a lightweight library to convert quickly between IANA, Windows, and Rails time zone names.

We can install the package very easily via NuGet.

I used VS Code on macOS to install this NuGet package, then I modified the FindSystemTimeZoneById to GetTimeZoneInfo and passed the same Time Zone identifier/id used on Windows, “New Zealand Standard Time”, to the method. And the code printed the correct result in the console.

In this way, we are able to work with different Time Zone providers in a uniform way and it’s easier to maintain our code.


Thanks for reading and hope it’s helpful.

Have a chat at

https://www.linkedin.com/in/chenjd/


Subscribe To Jiadong Chen's Blog

Avatar
Jiadong Chen
Cloud Architect/Senior Developer

Cloud Architect at Company-X | Microsoft MVP, MCT | Azure Certified Solutions Architect & Cybersecurity Architect Expert | Member of .NET Foundation | Packt Author ㅣ Opinions = my own.

comments powered by Disqus