在 C# 中是否有将城市名称转换为其时区名称(东京 --> 东京标准时间)的方法? [英] In C# is there anyway to convert a city name to its TimeZone name (Tokyo --> Tokyo Standard Time)?

查看:35
本文介绍了在 C# 中是否有将城市名称转换为其时区名称(东京 --> 东京标准时间)的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个城市列表(纽约、伦敦等.),我需要进行一些时区转换,但我看到大多数 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 响应 = 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(timeZoneResponseString);if (timeZoneResult.status == "OK"){response.TimeZoneName = timeZoneResult.timeZoneName;response.Success = true;返回响应;}}返回响应;}

I have a list of cities (New York, London, etc . .) and I need to do some timezone conversion but i see most of the APIs require that you know the time zone name (such as Eastern Standard Time, Tokyo Standard Time, etc).

Is there anyway to convert the city name into its appropriate TimeZone name ? (pass in New York and it returns "Eastern Standard Time")

解决方案

Not aware of anything like that built into C# but you can use Google Maps API for that:

1) Get the long/lat of the city: http://maps.googleapis.com/maps/api/geocode/json?address=New%20York,%20CA&sensor=false

2) Now use those long/lat to get the timezone: https://maps.googleapis.com/maps/api/timezone/json?location=43.2994285,-74.21793260000001&timestamp=1362209227&sensor=false

Example Code:

   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# 中是否有将城市名称转换为其时区名称(东京 --> 东京标准时间)的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆