سؤال

For one lives in Republic of Molossia, the following code in Java:

String TimeZoneId=TimeZone.getDefault().getID();
System.out.println(TimeZoneId);

will print

America/Regina

I tried TimeZoneInfo.GetSystemTimeZones() but the return value doesn't seem a tzdb id. Is there an equivalent method in C#?

هل كانت مفيدة؟

المحلول

.Net does not natievely support the TZDB, because Microsoft has it's own time zone database. See the TimeZone tag wiki for details.

If you want to work with TZDB zones, use the NodaTime library. The exact translation of what you asked is:

using NodaTime;

...

var timeZoneId = DateTimeZoneProviders.Tzdb.GetSystemDefault().Id;
Console.WriteLine(timeZoneId);

نصائح أخرى

Typically this can be done using the TimeZone class. This is the closest that you will get with the native libraries from my understanding. Why do you need that specific result?

Take a look at Microsoft Time Zones and the Conversion Table* for the Windows Equivalent as Windows does not fully support the TZID, take a look at the stack overflow question below for more information. I hope this is more useful as I misread your previous question!!! - sorry!

//Get A reference to our timezone object
TimeZone t = TimeZone.CurrentTimeZone;

//Print out the display name
Console.WriteLine(t.StandardName);

MSDN REFERENCE

http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html

Please refer this previous stackoverflow question .NET TimeZoneInfo from Olson time zone

Similar value is returned by

Calendar.GetTimeZone

for more information: https://docs.microsoft.com/en-us/uwp/api/windows.globalization.calendar.gettimezone?view=winrt-20348

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top