murtazat
murtazat

Reputation: 419

Getting local timezone identifier when OS display language is non-english

Strangely, TimeZone.CurrentTimeZone.StandardName returns localized name as per computer display language. I want a programmatic identifier that I can provide to TimeZoneInfo in following code.

TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone);

FindSystemTimeZoneById expects a unique un-localized programmatic identifier

I changed my computer display language to Chinese and I was getting a localized unicode string when I do TimeZone.CurrentTimeZone.StandardName. However the value was correct but it was localized in computer display language which is something I do not want.

I do not have option of using TimeZoneInfo.Local.Id right now because my project is in .Net 2.0. What other options do I have to get un-localized timezone identifier?

Upvotes: 3

Views: 1633

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241808

To get the equivalent of TimeZoneInfo.Local.Id without being able to use the TimeZoneInfo class, you will have to go directly to the registry.

In .NET 2.0 C#, you can retrieve it with the following method:

private static string GetLocalTimeZoneId()
{
    RegistryKey key = Registry.LocalMachine.OpenSubKey(
                        @"SYSTEM\CurrentControlSet\Control\TimeZoneInformation");
    string value = (string)key.GetValue("TimeZoneKeyName");
    if (string.IsNullOrEmpty(value))
        value = (string)key.GetValue("StandardName");
    key.Close();
    return value;
}

Windows Vista and greater have the TimeZoneKeyName value, and have a @tzres.dll pointer entry in the StandardName value.

Before Windows Vista, the StandardName value contained the key name, and was not localized.

The above code accounts for both variations.

Upvotes: 2

Related Questions