本文介绍了获取默认时区的国家(通过CultureInfo的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个程序或表格提供了默认时区对每一个国家?

Is there a program or a table that provides the default timezone for every country?

是的,美国,加拿大,和放大器;俄罗斯有多个时区。 (我认为所有其他国家只有一个)。但最好还是开始对最有可能的,如果一个国家被称为,而不是仅仅提供起始于格林尼治标准​​时间的列表。

Yes, the US, Canada, & Russia have multiple timezones. (I think every other country has just one.) But it's better to start on the most likely if a country is known rather than just provide a list starting at GMT.

preferably在C#中,但我会接受它的任何东西,并转换为C#。

Preferably in C# but I'll take it in anything and convert to C#.

推荐答案

作为问题的意见确定的,你不会是能够得到一个时区的每一个国家。有些国家的太多的情况下,有多个时区。

As identified in the comments of the question, you aren't going to be able to get a single time zone for each country. There are just too many cases of countries that have multiple time zones.

您的能有什么的做的就是筛选标准的下降到某一特定国家内提供的。

What you can do is filter the list of standard IANA/Olson time zones down to those available within a specific country.

在C#这样做的一个方法是使用:

One way to do this in C# is with Noda Time:

IEnumerable<string> zoneIds = TzdbDateTimeZoneSource.Default.ZoneLocations
    .Where(x => x.CountryCode == countryCode)
    .Select(x => x.ZoneId);

传递一个两位数的ISO-3166国家code,如AU澳大利亚。结果是:

"Australia/Lord_Howe",
"Australia/Hobart",
"Australia/Currie",
"Australia/Melbourne",
"Australia/Sydney",
"Australia/Broken_Hill",
"Australia/Brisbane",
"Australia/Lindeman",
"Australia/Adelaide",
"Australia/Darwin",
"Australia/Perth",
"Australia/Eucla"

如果因为某些原因,你想,你可以使用的TimeZoneInfo 对象使用Windows时区标识符,野田佳彦时间可以映射那些过于:

And if for some reason you'd like Windows time zone identifiers that you can use with the TimeZoneInfo object, Noda Time can map those too:

var source = TzdbDateTimeZoneSource.Default;
IEnumerable<string> windowsZoneIds = source.ZoneLocations
    .Where(x => x.CountryCode == countryCode)
    .Select(tz => source.WindowsMapping.MapZones
        .FirstOrDefault(x => x.TzdbIds.Contains(
                             source.CanonicalIdMap.First(y => y.Value == tz.ZoneId).Key)))
    .Where(x => x != null)
    .Select(x => x.WindowsId)
    .Distinct()

再次调用AU澳洲返回:

"Tasmania Standard Time",
"AUS Eastern Standard Time",
"Cen. Australia Standard Time",
"E. Australia Standard Time",
"AUS Central Standard Time",
"W. Australia Standard Time"

如果你想知道这个数据的可靠性如何,该国TZID映射是IANA时区数据库本身的一部分,在的文件。该IANA到Windows测绘数据来自。它没有得到任何接近官方比。

If you're wondering about how reliable this data is, the country to tzid mapping is part of the IANA time zone database itself, in the zone.tab file. The IANA to Windows mapping data comes from the Unicode CLDR supplemental data. It doesn't get any closer to "official" than that.

这篇关于获取默认时区的国家(通过CultureInfo的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:06