chip
chip

Reputation: 609

How to retrieve the system timezone in TZ format .net c#

I'm trying to work out how to retrieve the current system timezone in a (TZ) format on windows, ie. America/New_York, I need to supply this to an API this application communicates with.

I'm currently using

TimeZone.CurrentTimeZone

Which gives me this output

GMT Standard Time

What I hope to get is something like

Europe/London

Am I missing something simple or is this not available and thus does that mean I need to do the conversion myself?

Upvotes: 4

Views: 7451

Answers (3)

JeffC
JeffC

Reputation: 107

Starting with .NET6, TimeZoneInfo has been enhanced to handle the differences between IANA tz strings and Windows time zones.

// Conversion from IANA to Windows
string ianaId1 = "America/Los_Angeles";
if (!TimeZoneInfo.TryConvertIanaIdToWindowsId(ianaId1, out string winId1))
    throw new TimeZoneNotFoundException($"No Windows time zone found for \"{ ianaId1 }\".");
Console.WriteLine($"{ianaId1} => {winId1}");  // "America/Los_Angeles => Pacific Standard Time"

// Conversion from Windows to IANA when a region is unknown
string winId2 = "Eastern Standard Time";
if (!TimeZoneInfo.TryConvertWindowsIdToIanaId(winId2, out string ianaId2))
    throw new TimeZoneNotFoundException($"No IANA time zone found for \"{ winId2 }\".");
Console.WriteLine($"{winId2} => {ianaId2}");  // "Eastern Standard Time => America/New_York"

// Conversion from Windows to IANA when a region is known
string winId3 = "Eastern Standard Time";
string region = "CA"; // Canada
if (!TimeZoneInfo.TryConvertWindowsIdToIanaId(winId3, region, out string ianaId3))
    throw new TimeZoneNotFoundException($"No IANA time zone found for \"{ winId3 }\" in \"{ region }\".");
Console.WriteLine($"{winId3} + {region} => {ianaId3}");  // "Eastern Standard Time + CA => America/Toronto"

Note this code is from the Microsoft blog post about the .NET6 Time, Date, and Timezone changes.

Upvotes: 3

Pac0
Pac0

Reputation: 23174

I recommend to use NodaTime for that.

You can get the timezone of your system like that :

DateTimeZone tz = DateTimeZoneProviders.Tzdb.GetSystemDefault();

It will get the IANA Timezone as you need if you use tz.ToString()

(and apart from that, it is a very nice open source library that handles timezone, datetimes, instants and calendars in a IMHO much more structured and reliable way than the builtin .NET DateTime classes).

NodaTime is maintained and well supported by some high rep users here in SO ;) .


For information, the output that you are getting and you don't want, the one used by .NET, is called BCL Timezone, but you want the IANA Timezone (or TZDB) (which is more accurate)

Upvotes: 11

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241778

Noda Time is an excellent option. It is a much better and more comprehensive API for working with dates, times, and time zones than what comes built-in to .NET.

However, if getting the system time zone in IANA TZDB format is the only thing you are doing in this space, you may find it simpler to use my TimeZoneConverter library.

string tz = TZConvert.WindowsToIana(TimeZoneInfo.Local.Id);

Upvotes: 8

Related Questions