CẬP NHẬT ĐÁP
phản ứng ban đầu của tôi là dưới đây, và vẫn còn hiệu lực. Tuy nhiên, hiện có một cách dễ dàng hơn, sử dụng the TimeZoneNames library. Sau khi cài đặt from Nuget, bạn có thể làm như sau:
string tzid = theTimeZoneInfo.Id; // example: "Eastern Standard time"
string lang = CultureInfo.CurrentCulture.Name; // example: "en-US"
var abbreviations = TZNames.GetAbbreviationsForTimeZone(tzid, lang);
Đối tượng kết quả sẽ có các tính chất tương tự như:
abbreviations.Generic == "ET"
abbreviations.Standard == "EST"
abbreviations.Daylight == "EDT"
Bạn cũng có thể sử dụng thư viện này cùng để có được những tên cục bộ hoàn toàn thời gian vùng. Thư viện sử dụng một bản sao tự nhúng của dữ liệu CLDR.
ĐÁP ORIGINAL
Như những người khác đã đề cập, Time khu viết tắt là mơ hồ. Nhưng nếu bạn thực sự muốn một màn hình hiển thị, bạn cần một cơ sở dữ liệu múi giờ IANA/Olson.
Bạn có thể chuyển từ múi giờ Windows sang múi giờ IANA/Olson và hướng khác. Nhưng lưu ý rằng có thể có nhiều vùng IANA/Olson cho bất kỳ vùng Windows cụ thể nào. Các ánh xạ này được duy trì trong CLDR here.
NodaTime có cả cơ sở dữ liệu lẫn ánh xạ. Bạn có thể đi từ một .Net DateTime
hoặc DateTimeOffset
với số TimeZoneInfo
, đến số NodaTime Instant
và DateTimeZone
. Từ đó, bạn có thể lấy tên viết tắt.
// 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);
Nguồn
2013-03-08 22:25:40
Như bạn đã nói - vấn đề với tên múi giờ viết tắt là chúng có nhiều ý nghĩa. Chúng tốt nhất nên tránh trong một ứng dụng quốc tế. [EST] (http://en.wikipedia.org/wiki/EST) có ít nhất 5 ý nghĩa múi giờ khác nhau, chẳng hạn. – Oded
bạn luôn có thể viết tắt và bù đắp. Nhưng hầu hết thời gian chỉ cho thấy sự bù đắp đơn giản là không đủ. – Agzam