如何获得TimeZoneInfo的简称 [英] how to get TimeZoneInfo short name

查看:66
本文介绍了如何获得TimeZoneInfo的简称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以从
System.TimeZoneInfo.Local
中获得3个字符代码?

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

例如EDT而不是美国东部夏令时等等。

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

推荐答案

不幸的是,我没有一个简单的内置方法。但是,您可以自己整理一些东西。这是一个示例:

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();

    }

}

使用如下:

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

如果您需要更多转换,可以将其插入Map字典中。

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

TimeZoneInfo.Id将是与[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones]中给定键相匹配的字符串。如果您可以在线找到匹配的数据库,其中包含相同的ID和缩写,则可以快速提取并导入对(例如,使用正则表达式)并将其放入Map字典中。

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.

这篇关于如何获得TimeZoneInfo的简称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆