DEV Community

Cover image for How to get TimeZoneInfo on Windows and Linux
Ankur Sheel
Ankur Sheel

Posted on • Originally published at ankursheel.com on

How to get TimeZoneInfo on Windows and Linux

Recently, I ran into a small issue with timezones. On my local system, I was able to get the timezone but as soon I deployed, I started getting an error.

Problem

Unfortunately, Windows and Linux use different timezone systems

  • On Windows, they are identified by a value provided by Microsoft and takes a form such as “New Zealand StandardTime”.
  • On Linux/OSX, they are identified by a value provided by IANA in the TZDB and takes a form such as“Pacific/Auckland”.

This makes it a little tricky to get the TimeZoneInfo, especially, if you want to make sure the code works regardless ofthe system it is running on.

Solution

The easiest way I have found to solve this is to check if the Windows value exists in the list of time zones. If itexists, use the Windows value to get the time zone info, else use the IANA value.

var nzTimeZone = TimeZoneInfo.GetSystemTimeZones().Any(x => x.Id == "New Zealand Standard Time")
    ? TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time")
    : TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland");

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more