gpt4 book ai didi

c# - 在 C# 中是否有将城市名称转换为其时区名称(东京 --> 东京标准时间)的方法?

转载 作者:行者123 更新时间:2023-11-30 13:38:44 24 4
gpt4 key购买 nike

我有一个城市列表(纽约、伦敦等),我需要进行一些时区转换,但我看到大多数 API 都要求您知道时区名称(例如东部标准时间、东京标准时间等)。

有没有办法将城市名称转换成相应的时区名称? (通过纽约并返回“东部标准时间”)

最佳答案

不知道 C# 中内置的类似内容,但您可以为此使用 Google Maps API:

1) 获取城市的经度/纬度:http://maps.googleapis.com/maps/api/geocode/json?address=New%20York,%20CA&sensor=false

2) 现在使用那些 long/lat 来获取时区:https://maps.googleapis.com/maps/api/timezone/json?location=43.2994285,-74.21793260000001&timestamp=1362209227&sensor=false

示例代码:

   public TimeZoneResponse ConvertCityToTimeZoneName(string location)
{
TimeZoneResponse response = new TimeZoneResponse();
var plusName = location.Replace(" ", "+");
var address = "http://maps.google.com/maps/api/geocode/json?address=" + plusName + "&sensor=false";
var result = new System.Net.WebClient().DownloadString(address);
var latLongResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);

if (latLongResult.status == "OK")
{
var timeZoneRespontimeZoneRequest = "https://maps.googleapis.com/maps/api/timezone/json?location=" + latLongResult.results[0].geometry.location.lat + "," + latLongResult.results[0].geometry.location.lng + "&timestamp=1362209227&sensor=false";
var timeZoneResponseString = new System.Net.WebClient().DownloadString(timeZoneRespontimeZoneRequest);
var timeZoneResult = JsonConvert.DeserializeObject<TimeZoneResult>(timeZoneResponseString);

if (timeZoneResult.status == "OK")
{

response.TimeZoneName = timeZoneResult.timeZoneName;
response.Success = true;
return response;
}
}
return response;
}

关于c# - 在 C# 中是否有将城市名称转换为其时区名称(东京 --> 东京标准时间)的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15171430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com