【问题标题】:Get Timezone offset from Country and State Name in C#从 C# 中的国家和州名获取时区偏移量
【发布时间】:2021-06-12 11:59:45
【问题描述】:

在 C# 中有没有办法获得给定国家和州的时区偏移量?例如,如果输入是 Country - "Canada" 和 State - "Saskatchewan",那么输出是 "-06:00"?

【问题讨论】:

  • 这对您有帮助吗? stackoverflow.com/questions/19695439/…
  • 感谢您的快速回复,但我已经完成了这个答案。它需要区域 ID 信息。我只有国名和州名。
  • @vidhi Canada/Saskatchewan 是有效的 TZ 数据库时区名称。
  • 我想我误解了答案。它确实有助于找到不同国家的区域 ID。有没有办法从区域 id 中找出时间偏移量?

标签: c# timezone-offset datetimeoffset


【解决方案1】:

如 cmets 中所述,修改 SO 问题的代码如下所示以获取 latitudelongitude

IEnumerable<string> zoneIds = TzdbDateTimeZoneSource.Default.ZoneLocations
    .Where(x => x.CountryCode == countryCode)
    .Select(x => new { x.Latitude, x.Longitude});

那么,一种方法是使用Azure Maps API 获取Offset 和Transition 偏移量:

GET https://atlas.microsoft.com/timezone/byCoordinates/json?subscription-key=[subscription-key]&api-version=1.0&options=all&query=47.0,-122

示例响应:

{
  "Version": "2019a",
  "ReferenceUtcTimestamp": "2019-06-17T22:16:59.0765556Z",
  "TimeZones": [
    {
      "Id": "America/Los_Angeles",
      "Aliases": [
        "US/Pacific",
        "US/Pacific-New"
      ],
      "Countries": [
        {
          "Name": "United States",
          "Code": "US"
        }
      ],
      "Names": {
        "ISO6391LanguageCode": "en",
        "Generic": "Pacific Time",
        "Standard": "Pacific Standard Time",
        "Daylight": "Pacific Daylight Time"
      },
      "ReferenceTime": {
        "Tag": "PDT",
        "StandardOffset": "-08:00:00",
        "DaylightSavings": "01:00:00",
        "WallTime": "2019-06-17T15:16:59.0765556-07:00",
        "PosixTzValidYear": 2019,
        "PosixTz": "PST+8PDT,M3.2.0,M11.1.0",
        "Sunrise": "2019-06-17T05:12:21.267-07:00",
        "Sunset": "2019-06-17T21:05:18.017-07:00"
      },
      "RepresentativePoint": {
        "Latitude": 34.05222222222222,
        "Longitude": -118.24277777777778
      },
      "TimeTransitions": [
        {
          "Tag": "PDT",
          "StandardOffset": "-08:00:00",
          "DaylightSavings": "01:00:00",
          "UtcStart": "2019-03-10T10:00:00Z",
          "UtcEnd": "2019-11-03T09:00:00Z"
        },
        {
          "Tag": "PST",
          "StandardOffset": "-08:00:00",
          "DaylightSavings": "00:00:00",
          "UtcStart": "2019-11-03T09:00:00Z",
          "UtcEnd": "2020-03-08T10:00:00Z"
        },
        {
          "Tag": "PDT",
          "StandardOffset": "-08:00:00",
          "DaylightSavings": "01:00:00",
          "UtcStart": "2020-03-08T10:00:00Z",
          "UtcEnd": "2020-11-01T09:00:00Z"
        }
      ]
    }
  ]
}

其次,您可以使用Google APIs通过经纬度获取当地时间:

    {
      "Version": "2019a",
      "ReferenceUtcTimestamp": "2019-06-17T22:16:59.0765556Z",
      "TimeZones": [
        {
          "Id": "America/Los_Angeles",
          "Aliases": [
            "US/Pacific",
            "US/Pacific-New"
          ],
          "Countries": [
            {
              "Name": "United States",
              "Code": "US"
            }
          ],
          "Names": {
            "ISO6391LanguageCode": "en",
            "Generic": "Pacific Time",
            "Standard": "Pacific Standard Time",
            "Daylight": "Pacific Daylight Time"
          },
          "ReferenceTime": {
            "Tag": "PDT",
            "StandardOffset": "-08:00:00",
            "DaylightSavings": "01:00:00",
            "WallTime": "2019-06-17T15:16:59.0765556-07:00",
            "PosixTzValidYear": 2019,
            "PosixTz": "PST+8PDT,M3.2.0,M11.1.0",
            "Sunrise": "2019-06-17T05:12:21.267-07:00",
            "Sunset": "2019-06-17T21:05:18.017-07:00"
          },
          "RepresentativePoint": {
            "Latitude": 34.05222222222222,
            "Longitude": -118.24277777777778
          },
          "TimeTransitions": [
            {
              "Tag": "PDT",
              "StandardOffset": "-08:00:00",
              "DaylightSavings": "01:00:00",
              "UtcStart": "2019-03-10T10:00:00Z",
              "UtcEnd": "2019-11-03T09:00:00Z"
            },
            {
              "Tag": "PST",
              "StandardOffset": "-08:00:00",
              "DaylightSavings": "00:00:00",
              "UtcStart": "2019-11-03T09:00:00Z",
              "UtcEnd": "2020-03-08T10:00:00Z"
            },
            {
              "Tag": "PDT",
              "StandardOffset": "-08:00:00",
              "DaylightSavings": "01:00:00",
              "UtcStart": "2020-03-08T10:00:00Z",
              "UtcEnd": "2020-11-01T09:00:00Z"
            }
          ]
        }
      ]
    }

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多