Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

timezone

c#

.net

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?

like image 631
chip Avatar asked Dec 03 '22 12:12

chip


1 Answers

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)

like image 58
Pac0 Avatar answered Dec 25 '22 01:12

Pac0