Home How to retrieve the system timezone in TZ format .net c#
Reply: 3

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

chip
1#
chip Published in 2017-12-07 14:52:53Z

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?

Pac0
2#
Pac0 Reply to 2017-12-07 16:20:03Z

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)

EDIT : corrected with the right timezone provider

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)

Ajay Kumar
3#
Ajay Kumar Reply to 2017-12-07 16:24:46Z
localZone.DisplayName will be showing the names e-g, *Islamabad, Karachi.*


  TimeZoneInfo localZone = TimeZoneInfo.Local;
  Console.WriteLine("Local Time Zone ID: {0}", localZone.Id);
  Console.WriteLine("Display Name is: {0}.", localZone.DisplayName);
  Console.WriteLine("Standard name is: {0}.", localZone.StandardName);
  Console.WriteLine("Daylight saving name is: {0}.", localZone.DaylightName); 

out put:

Pakistan Standard Time
<UTC+05:00> Islamabad, Karachi.
Pakistan Standard Time.
Pakistan Daylight Time
Matt Johnson
4#
Matt Johnson Reply to 2017-12-07 19:03:40Z

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);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.333132 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO