将地址映射到 Windows 时区

Map an address to a Windows Timezone

我正在研究将地址等位置映射到 Windows 时区格式。例如,如果地址包含哥本哈根市,则服务应将其映射到 (UTC+01:00) 布鲁塞尔、哥本哈根、马德里、巴黎。

如果地址包含靠近维也纳的城市,则应将其映射到 (UTC+01:00) 阿姆斯特丹、柏林、伯尔尼、罗马、斯德哥尔摩、维也纳。

我查看了 Google 的 GeoCoding 和 TimeZone API,但它只给我时区的名称,如 "Central European Time"。

这是一个多步骤过程:

  1. 确定位置的经纬度坐标。

    • 您提到了 Google 的地理编码 API。这样就可以了,还有其他几个可供选择。
  2. 使用 any of these methods.

    根据这些坐标确定时区标识符
    • 在大多数情况下,您会收到一个 IANA 时区标识符,例如 "Europe/Vienna"。您提到了 Google 时区 API - returns 这个值在 timeZoneId 字段中( 不是 timeZoneName) .
  3. 使用 IANA 时区标识符获取 .NET TimeZoneInfo 对象。

    • 如果您在非 Windows 平台(Linux、OSX 等)上 运行ning .NET,您可以直接从IANA时区标识符获取一个TimeZoneInfo对象:

      // This will work on platforms that use IANA time zones natively
      TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Europe/Vienna");
      
    • 如果您在 Windows 上 运行ning .NET,或者想在 any[=70 上将您的应用程序写入 运行 =] 平台,然后使用我的 TimeZoneConverter 库检索 TimeZoneInfo 对象:

      // This will work on any platform
      TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("Europe/Vienna");
      

如果您确实需要 Windows 时区标识符,那么 TimeZoneConverter 也可以做到:

string tz = TZConvert.IanaToWindows("Europe/Vienna");
//=> "W. Europe Standard Time"

另外,如果你想得到实际的字符串"(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna",那么:

  • 如果在 Windows 上,并且 Windows OS 语言环境是 English,那么您可以得到 .DisplayName 来自 TimeZoneInfo 对象。

  • 在所有其他情况下,您可以跳过上面的第 3 步,只需使用我的 TimeZoneNames 库,它具有所有语言的所有显示名称:

    string displayName = TZNames.GetDisplayNameForTimeZone("Europe/Vienna", "en");
    

此外,您可以考虑使用 NodaTime 而不是 TimeZoneInfo 对象。