![](/img/trans.png)
[英]Get ZoneId using country name, datetime and timezone offset values in c#
[英]Get Timezone offset from Country and State Name in C#
C# 中有沒有辦法獲取給定國家和 State 的時區偏移量? 例如,如果輸入是 Country - "Canada" 和 State - "Saskatchewan",那么 output 是 "-06:00"?
如評論中所述,修改 SO 問題的代碼如下所示以獲取latitude
和longitude
:
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 API使用緯度和經度獲取本地時間:
{
"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"
}
]
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.