views:

499

answers:

1

Is there any method to get the 3 char code from System.TimeZoneInfo.Local ?

e.g. EDT instead of Eastern Daylight time etc.

+3  A: 

Unfortunately, there is no easy built-in way of doing this that I know of. However, you could put something together yourself. Here's an example:

public static class TimeZoneInfoExtensions {

        public static string Abbreviation(this TimeZoneInfo Source) {

        var Map = new Dictionary<string, string>()
        {
            {"eastern standard time","est"},
            {"mountain standard time","mst"},
            {"central standard time","cst"},
            {"pacific standard time","pst"}
            //etc...
        };

        return Map[Source.Id.ToLower()].ToUpper();

    }

}

Use as follows:

string CurrentTimeZoneAbbreviation = System.TimeZoneInfo.Local.Abbreviation();

If you need more conversions you could just plug them into the Map dictionary.

TimeZoneInfo.Id will be a string matching a given key in [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones]. If you can find a matching database online, containing the same Ids as well as the abbreviations, it would be possible to quickly extract and import the pairs (with regular expressions, for example) and drop those into the Map dictionary.

Robert Venables
The possible values for `Id` can be found in the registry under `HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zone` (they're different from the names in http://www.timeanddate.com/library/abbreviations/timezones/ )
dtb
@dtb: Thank you.
Robert Venables