我有以下代码从位置转换为TimeZone名称。为什么我得到这个时区错误,是否有更好的方法来映射谷歌地图时区和Windows时区?
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 + "×tamp=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;
}
所以,当我通过在“纽约,美国”,则返回“东部标准时间”
我再有一次从一个源时区转换第二个功能到上面的其他检索时区。
var timeInDestTimeZone = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(sourceDate.Date, TimeZoneInfo.Local.Id, destination.TimeZoneName);
它的工作很好,直到我遇到这个例子。当我通过:澳大利亚墨尔本第一个函数,我回去:澳大利亚东部夏令时
当我通过澳大利亚东部夏令时进入我的第二个功能(作为最后一个参数),我得到这个错误回:
时区ID“澳大利亚东部夏令时”未在本地计算机
什么我做错任何建议上发现了什么?当我看到从第二个谷歌地图API的响应调用这些都是我回来(使用LA为例)的字段:
{
"dstOffset" : 0.0,
"rawOffset" : -28800.0,
"status" : "OK",
"timeZoneId" : "America/Los_Angeles",
"timeZoneName" : "Pacific Standard Time"
}
当我通过在墨尔本,我看到了TimeZoneId字段设置到:“”澳大利亚/霍巴特“”。这是正确的领域来看待时区计算。或者我应该看看其他的“偏移”字段?
任何建议将不胜感激。
你有徽章几乎乔恩斯基特... :) – gdoron 2013-03-03 18:48:41
这可现在用Noda Time 1.1.0完成。 [我在这里发布了转换函数。](http://stackoverflow.com/q/17348807/634824) – 2013-06-27 17:29:49