时区的缩写

本文关键字:缩写 时区 | 更新日期: 2023-09-27 18:03:06

TimeZoneInfo不提供给定时区的缩写或短名称。唯一的好方法是有一个字典,它将缩写映射到Timezone.id, StandardNameDaylightName属性。但是,我搜索的所有资源的缩写列表都有不同的时区名称,即与Windows中的不同。

如何在。net中显示非全名、id或任何其他名称的用户?我不想要UtcOffset,而是要时区的缩写- PST代表太平洋,UTC代表通用,EST代表东部标准等等。是否有任何c#兼容列表或数据库与所有可能的时区和他们的缩写,与那些TimeZoneInfo.GetSystemTimeZones()给你兼容?

时区的缩写

更新答案

我的原始回复如下,仍然有效。但是现在有一种更简单的方法,使用timezonnames库。从Nuget安装后,您可以做以下操作:

string tzid = theTimeZoneInfo.Id;                // example: "Eastern Standard time"
string lang = CultureInfo.CurrentCulture.Name;   // example: "en-US"
var abbreviations = TZNames.GetAbbreviationsForTimeZone(tzid, lang);

结果对象的属性类似于:

abbreviations.Generic == "ET"
abbreviations.Standard == "EST"
abbreviations.Daylight == "EDT"

您还可以使用相同的库来获得时区的完全本地化名称。该库使用嵌入的CLDR数据的自包含副本。

原始回答

正如其他人提到的,时区缩写是不明确的。但如果你真的想要一个显示,你需要一个IANA/Olson时区数据库。

您可以从Windows时区切换到IANA/Olson时区,也可以从其他方向切换。但请注意,对于任何给定的Windows区域,可能存在多个IANA/Olson区域。这些映射在这里的CLDR中进行维护。

nodeatime同时拥有数据库和映射。您可以从。net DateTimeDateTimeOffsetTimeZoneInfo切换到nodeatime InstantDateTimeZone。从那里,您可以获得缩写名称。

// starting with a .Net TimeZoneInfo
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
// You need to resolve to a specific instant in time - a noda Instant
// For illustrative purposes, I'll start from a regular .Net UTC DateTime
var dateTime = DateTime.UtcNow;
var instant = Instant.FromDateTimeUtc(dateTime);
// note that if we really wanted to just get the current instant,
// it's better and easier to use the following:
// var instant = SystemClock.Instance.Now;

// Now let's map the Windows time zone to an IANA/Olson time zone,
// using the CLDR mappings embedded in NodaTime.  This will use
// the *primary* mapping from the CLDR - that is, the ones marked
// as "territory 001".
// we need the NodaTime tzdb source.  In NodaTime 1.1.0+:
var tzdbSource = TzdbDateTimeZoneSource.Default;
// in previous NodaTime releases:
// var tzdbSource = new TzdbDateTimeZoneSource("NodaTime.TimeZones.Tzdb");
// map to the appropriate IANA/Olson tzid
var tzid = tzdbSource.MapTimeZoneId(timeZoneInfo);
        
// get a DateTimeZone from that id
var dateTimeZone = DateTimeZoneProviders.Tzdb[tzid];

// Finally, let's figure out what the abbreviation is
// for the instant and zone we have.
// now get a ZoneInterval for the zone and the instant
var zoneInterval = dateTimeZone.GetZoneInterval(instant);
// finally, you can get the correct time zone abbreviation
var abbreviation = zoneInterval.Name;
// abbreviation will be either PST or PDT depending
// on what instant was provided
Debug.WriteLine(abbreviation);

这是一个棘手的要求,你能做的最好的是得到你选择的列表,并创建一个扩展/助手方法来获得给定TimeZoneInfo的缩写。

一旦开始的地方是http://www.timeanddate.com/library/abbreviations/timezones/,它有一个版本的列表,其中涵盖了我所知道的区域。

问题在于在给定时区存在多个缩写时选择合适的缩写。例如UTC可以表示为UTCWET(西欧时间)WEZ (Westeuropäische Zeit)WT(西撒哈拉标准时间)

您可能希望与您的涉众就您将遵循的给定选择的命名约定达成一致。

您的问题没有指出您的应用程序必须在哪个时区内运行,但在我的特定实例中,我只需要关注美国时区和UTC。

美国时区的缩写总是时区名称中每个单词的第一个字符。例如,"山区标准时间"的缩写是"MST","东部夏令时"的缩写是"EDT"。

如果您有类似的需求,您可以轻松地直接从本地时区的名称派生出本地时区的时区缩写,如下所示(注意:这里我是根据当前日期和时间确定适当的名称):

string timeZoneName = TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now)
                          ? TimeZone.CurrentTimeZone.DaylightName 
                          : TimeZone.CurrentTimeZone.StandardName;
string timeZoneAbbrev = GetTzAbbreviation(timeZoneName);

GetTzInitials()函数的代码非常简单。值得一提的是,有些时区可能被设置为墨西哥或加拿大,这些时区的名称将与国家名称一起出现在括号中,例如"太平洋标准时间(墨西哥)"。为了解决这个问题,任何加括号的数据都被直接传回。上面返回的缩写将是"PST(Mexico)",这对我来说是有效的。

string GetTzAbbreviation(string timeZoneName) {
    string output = string.Empty;
    string[] timeZoneWords = timeZoneName.Split(' ');
    foreach (string timeZoneWord in timeZoneWords) {
        if (timeZoneWord[0] != '(') {
            output += timeZoneWord[0];
        } else {
            output += timeZoneWord;
        }
    }
    return output;
}

下面是另一个使用nodeatime的代码片段:

NodaTime.ZonedDateTime hereAndNow = NodaTime.SystemClock.Instance.Now.InZone(
    NodaTime.DateTimeZoneProviders.Tzdb.GetSystemDefault());
System.TimeSpan zoneOffset = hereAndNow.ToDateTimeOffset().Offset;
string sTimeDisplay = string.Format("{0:G} {1} (UTC{2}{3:hh'':mm} {4})", 
    hereAndNow.ToDateTimeOffset(), 
    hereAndNow.Zone.GetZoneInterval(hereAndNow.ToInstant()).Name, 
    zoneOffset < TimeSpan.Zero ? "-" : "+", 
    zoneOffset, 
    hereAndNow.Zone.Id);

在我的系统上,它产生:"4/11/2013 5:03:23 PM CDT (UTC-05:00美国/芝加哥)"

(感谢Matt Johnson的回答,该缩写存在于TimeZoneInterval中)

如果noatime。ZonedDateTime有一个GetZoneInterval方法,但也许我遗漏了一些东西。

这对任何在iOS或Android上使用Xamarin的人都很有帮助,因为根据文档"显示名称是根据Windows操作系统安装的文化进行本地化的"。当在中央时区使用此函数时,对于日期时间"2016-09-01 12:00:00 GMT",此函数返回"CDT",这正是我遇到此问题时所需要的。

public static string GetTimeZoneAbbreviation(DateTime time)
{
    string timeZoneAbbr;
    if(time.IsDaylightSavingTime() == true)
    {
        timeZoneAbbr = TimeZoneInfo.Local.DaylightName;
    }
    else
    {
        timeZoneAbbr = TimeZoneInfo.Local.StandardName;
    }
    return timeZoneAbbr;
}

我将所有日期存储在UTC中,并且经常必须以本地时间显示它们,因此我创建了一个扩展方法ToAbbreviation()

    public static string ToAbbreviation(this TimeZone theTimeZone)
    {
        string timeZoneString = theTimeZone.StandardName;
        string result = string.Concat(System.Text.RegularExpressions.Regex
           .Matches(timeZoneString, "[A-Z]")
           .OfType<System.Text.RegularExpressions.Match>()
           .Select(match => match.Value));
        return result;
    }

使用例子:

string tz = TimeZone.CurrentTimeZone.ToAbbreviation();
string formattedDate = String.Format("{0:yyyy/MM/dd hh:mm:ss} {1}", myDate, tz);

或者,如果您只想从DateTime对象中获取格式化的日期字符串:

public static string ToLocalTimeWithTimeZoneAbbreviation(this DateTime dt)
{
    DateTime localtime = dt.ToLocalTime();
    string tz = TimeZone.CurrentTimeZone.ToAbbreviation();
    string formattedDate = String.Format("{0:yyyy/MM/dd hh:mm:ss} {1}", localtime, tz);
    return formattedDate;
}

,使用方法如下:

string formattedDate = myDateTimeObject.ToLocalTimeWithTimeZoneAbbreviation()

输出:2019-06-24 02:26:31 EST

void Main()
{
Console.WriteLine(TimeZoneDecoder(TimeZoneInfo.Local.Id));
}
private string TimeZoneDecoder(string timeZone)
        {
            string abb;
            switch (timeZone)
            {
                case "Eastern Standard Time":
                    abb = "ET";
                    break;
                case "Central Standard Time":
                    abb = "CT";
                    break;
                case "Mountain Standard Time":
                    abb = "MST";
                    break;
                case "Pacific Standard Time":
                    abb = "PT";
                    break;
                case "Alaskan Standard Time":
                    abb = "AK";
                    break;
                case "Hawaiian Standard Time":
                    abb = "HAST";
                    break;
                case "Atlantic Standard Time":
                    abb = "AST";
                    break;
                case "Morocco Standard Time":
                    abb = "MOST";
                    break;
                case "UTC":
                    abb = "UTC";
                    break;
                case "GMT Standard Time":
                    abb = "GMT";
                    break;
                case "Greenwich Standard Time":
                    abb = "GST";
                    break;
                case "West Europe Standard Time":
                    abb = "WET";
                    break;
                case "Central Europe Standard Time":
                    abb = "CET";
                    break;
                case "Romance Standard Time":
                    abb = "RST";
                    break;
                case "Central European Standard Time":
                    abb = "CEST";
                    break;
                case "W. Central Africa Standard Time":
                    abb = "ECT";
                    break;
                case "Jordan Standard Time":
                    abb = "JST";
                    break;
                case "GTB Standard Time":
                    abb = "GTBST";
                    break;
                case "Middle East Standard Time":
                    abb = "MEST";
                    break;
                case "Egypt Standard Time":
                    abb = "EGST";
                    break;
                case "Syria Standard Time":
                    abb = "SST";
                    break;
                case "South Africa Standard Time":
                    abb = "SAST";
                    break;
                case "FLE Standard Time":
                    abb = "EET";
                    break;
                case "Israel Standard Time":
                    abb = "ISST";
                    break;
                case "E. Europe Standard Time":
                    abb = "EEST";
                    break;
                case "Namibia Standard Time":
                    abb = "NMST";
                    break;
                case "Arabic Standard Time":
                    abb = "ARST";
                    break;
                case "Arab Standard Time":
                    abb = "ABST";
                    break;
                case "Russian Standard Time":
                    abb = "MSK";
                    break;
                case "E. Africa Standard Time":
                    abb = "EAT";
                    break;
                case "Iran Standard Time":
                    abb = "IRST";
                    break;
                case "Arabian Standard Time":
                    abb = "ARBST";
                    break;
                case "Azerbaijan Standard Time":
                    abb = "AZT";
                    break;
                case "Mauritius Standard Time":
                    abb = "MUT";
                    break;
                case "Georgian Standard Time":
                    abb = "GET";
                    break;
                case "Caucasus Standard Time":
                    abb = "AMT";
                    break;
                case "Afghanistan Standard Time":
                    abb = "AFT";
                    break;
                case "Ekaterinburg Standard Time":
                    abb = "YEKT";
                    break;
                case "Pakistan Standard Time":
                    abb = "PKT";
                    break;
                case "West Asia Standard Time":
                    abb = "WAST";
                    break;
                case "India Standard Time":
                    abb = "IST";
                    break;
                case "Sri Lanka Standard Time":
                    abb = "SLT";
                    break;
                case "Nepal Standard Time":
                    abb = "NPT";
                    break;
                case "Central Asia Standard Time":
                    abb = "BTT";
                    break;
                case "Bangladesh Standard Time":
                    abb = "BST";
                    break;
                case "N. Central Asia Standard Time":
                    abb = "NCAST";
                    break;
                case "Myanmar Standard Time":
                    abb = "MYST";
                    break;
                case "SE Asia Standard Time":
                    abb = "THA";
                    break;
                case "North Asia Standard Time":
                    abb = "KRAT";
                    break;
                case "China Standard Time":
                    abb = "CST";
                    break;
                case "North Asia East Standard Time":
                    abb = "IRKT";
                    break;
                case "Singapore Standard Time":
                    abb = "SNST";
                    break;
                case "W. Australia Standard Time":
                    abb = "AWST";
                    break;
                case "Taipei Standard Time":
                    abb = "TIST";
                    break;
                case "Ulaanbaatar Standard Time":
                    abb = "UST";
                    break;
                case "Tokyo Standard Time":
                    abb = "TST";
                    break;
                case "Korea Standard Time":
                    abb = "KST";
                    break;
                case "Yakutsk Standard Time":
                    abb = "YAKT";
                    break;
                case "Cen. Australia Standard Time":
                    abb = "CAUST";
                    break;
                case "AUS Central Standard Time":
                    abb = "ACST";
                    break;
                case "E. Australia Standard Time":
                    abb = "EAST";
                    break;
                case "AUS Eastern Standard Time":
                    abb = "AEST";
                    break;
                case "West Pacific Standard Time":
                    abb = "WPST";
                    break;
                case "Tasmania Standard Time":
                    abb = "TAST";
                    break;
                case "Vladivostok Standard Time":
                    abb = "VLAT";
                    break;
                case "Central Pacific Standard Time":
                    abb = "SBT";
                    break;
                case "New Zealand Standard Time":
                    abb = "NZST";
                    break;
                case "UTC+12":
                    abb = "UTC12";
                    break;
                case "Fiji Standard Time":
                    abb = "FJT";
                    break;
                case "Kamchatka Standard Time":
                    abb = "PETT";
                    break;
                case "Tonga Standard Time":
                    abb = "PHOT";
                    break;
                case "Azores Standard Time":
                    abb = "AZOST";
                    break;
                case "Cape Verde Standard Time":
                    abb = "CVT";
                    break;
                case "E. South America Standard Time":
                    abb = "ESAST";
                    break;
                case "Argentina Standard Time":
                    abb = "ART";
                    break;
                case "SA Eastern Standard Time":
                    abb = "SAEST";
                    break;
                case "Greenland Standard Time":
                    abb = "GNST";
                    break;
                case "Montevideo Standard Time":
                    abb = "MVST";
                    break;
                case "Newfoundland Standard Time":
                    abb = "NST";
                    break;
                case "Paraguay Standard Time":
                    abb = "PRST";
                    break;
                case "Central Brazilian Standard Time":
                    abb = "CBST";
                    break;
                case "SA Western Standard Time":
                    abb = "SAWST";
                    break;
                case "Pacific SA Standard Time":
                    abb = "PSAST";
                    break;
                case "Venezuela Standard Time":
                    abb = "VST";
                    break;
                case "SA Pacific Standard Time":
                    abb = "SAPST";
                    break;
                case "US Eastern Standard Time":
                    abb = "EST";
                    break;
                case "Central America Standard Time":
                    abb = "CAST";
                    break;
                case "Central Standard Time (Mexico)":
                    abb = "CST";
                    break;
                case "Canada Central Standard Time":
                    abb = "CCST";
                    break;
                case "Mountain Standard Time (Mexico)":
                    abb = "MSTM";
                    break;
                case "Pacific Standard Time (Mexico)":
                    abb = "PST";
                    break;
                case "Samoa Standard Time":
                    abb = "SMST";
                    break;
                case "Dateline Standard Time":
                    abb = "BIT";
                    break;
                default:
                    abb = timeZone;
                    break;
            }
            return abb;
        }

您可以将此电子表格与所有时区缩写组合使用,如:

COUNTRY_CODE    STD_UTC_OFFSET  DST_UTC_OFFSET  STD_ABBREVIATION    DST_ABBREVIATION
MX              −08:00          −07:00          PST                 PDT     
COORDINATES IANA    DESCRIPTION             LOCATION
America/Ensenada    Pitcairn Standard Time  Pacific