修訂ANSWER
我原來的響應之下,仍然有效。但是現在有一種更簡單的方法,使用the TimeZoneNames library。安裝from 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 here中。
NodaTime既有數據庫又有映射。您可以從.Net DateTime
或DateTimeOffset
與TimeZoneInfo
,到NodaTime Instant
和DateTimeZone
。從那裏,你可以得到縮寫名稱。
// 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);
正如你所說 - 縮寫的時區名稱的問題是它們有多重含義。他們最好在國際申請中避免。例如,[EST](http://en.wikipedia.org/wiki/EST)至少有5個不同的時區含義。 – Oded 2013-03-08 19:53:50
那麼你總是可以提供縮寫和偏移量。但大多數時候只顯示偏移量不夠。 – Agzam 2013-03-08 20:30:38