c# - 如何从 TimeZoneInfo 获取国家名称
问题描述
如果我们有我们TimeZoneInfo
如何获得所选时区的国家名称。
例子:
Asia/Singapore = Singapore
Asia/Tokyo = Tokyo
Europe/Moscow = Russia
谢谢
解决方案
我认为没有内置的方法可以做到这一点。TimeZoneInfo 类没有返回国家代码的属性或方法。请参阅:https ://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo?view=netframework-4.7.2
TimeZoneInfo 的行为也取决于它运行的操作系统。见下文:
在 Windows 10 上的 NET Core 2.1 上运行以下代码:
TimeZoneInfo localZone = TimeZoneInfo.Local;
Console.WriteLine("Local Time Zone ID: {0}", localZone.Id);
Console.WriteLine(" Display Name is: {0}.", localZone.DisplayName);
Console.WriteLine(" Standard name is: {0}.", localZone.StandardName);
Console.WriteLine(" Daylight saving name is: {0}.", localZone.DaylightName);
给出输出:
Local Time Zone ID: Central Europe Standard Time
Display Name is: (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague.
Standard name is: Central Europe Standard Time.
Daylight saving name is: Central Europe Summer Time.
在 macOS High Sierra 上运行相同的代码 NET Core 2.1:
Local Time Zone ID: Europe/Budapest
Display Name is: GMT+01:00.
Standard name is: GMT+01:00.
Daylight saving name is: GMT+02:00.
您可以实现的最接近的近似值是:
下载 tz 数据库的最新副本。您可以使用https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
下载国家代码的副本。维基百科有一个列表:https ://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
实现通过 TZ 名称搜索 TZ 数据库的代码,例如“Europe/Budapest”。这会给你一个国家代码。然后使用两个字母的国家代码搜索您的国家数据库,这将为您提供一个国家名称。
此方法不跨平台!当国家代码列表的 tz 数据库发生变化时,您的应用程序必须更新。
推荐阅读
- ruby-on-rails - array.select 不遍历每个元素
- ios - xcode 10.2.1“架构 arm64 的未定义符号”
- redux - 可以组合类似的 redux 操作吗?
- ios - “Info.plist 文件中缺少目的字符串”实际上并未丢失
- python - 大熊猫滚动最大值的numpy版本
- c# - 如何使用 ASP.net MVC 在同一控制器中调用函数任务?
- java - 布尔属性如何与 createBooleanBinding 一起使用
- react-native - 卡片视图标题中的背景颜色(react-native)
- python - 如何使用python枚举一些文本文件?
- elasticsearch - 为什么kibana不显示所有搜索结果?