2013-03-03 11 views
6

我有以下代码从位置转换为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 + "&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; 

}

所以,当我通过在“纽约,美国”,则返回“东部标准时间”

我再有一次从一个源时区转换第二个功能到上面的其他检索时区。

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字段设置到:“”澳大利亚/霍巴特“”。这是正确的领域来看待时区计算。或者我应该看看其他的“偏移”字段?

任何建议将不胜感激。

+1

你有徽章几乎乔恩斯基特... :) – gdoron 2013-03-03 18:48:41

+0

这可现在用Noda Time 1.1.0完成。 [我在这里发布了转换函数。](http://stackoverflow.com/q/17348807/634824) – 2013-06-27 17:29:49

回答

5

由于.NET如何实现时区,因此没有内置的方法来执行转换。你将不得不诉诸使用第三方库(或实现你自己的转换)。


question要求为您寻找一个非常类似的解决方案。

提问者在内部通过使用Olson Time Zone Databasetz数据库/区域信息数据库/ IANA时区数据库)找到了解决方案。提问者参考了一个page,它解释了一些关于转换的内容。


最后,您可以使用Noda Time,它实现了这一点,你正在寻找非常功能。 Jon Skeet的answer早期回顾了图书馆在2011年的发展情况。

图书馆的Key Concepts page包含一个说明转换功能的Date/Time Zone块。


UPDATE

这里有一个关于如何创建这样一个查找表example

// Note: this version lets you work with any IDateTimeZoneSource, although as the only 
// other built-in source is BclDateTimeZoneSource, that may be less useful :) 
private static IDictionary<string, string> LoadTimeZoneMap(IDateTimeZoneSource source) 
{ 
    var nodaToWindowsMap = new Dictionary<string, string>(); 
    foreach (var bclZone in TimeZoneInfo.GetSystemTimeZones()) 
    { 
     var nodaId = source.MapTimeZoneId(bclZone); 
     if (nodaId != null) 
     { 
      nodaToWindowsMap[nodaId] = bclZone.Id; 
     } 
    } 
    return nodaToWindowsMap; 
} 
+0

我给出的链接也是Olson的。你的回答更适合OP的情况。 +1 – 2013-03-03 19:34:50

-1

我觉得似乎没有任何这样的时区。请与GetSystemTimeZones联系。

var timeInDestTimeZone = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(sourceDate.Date, TimeZoneInfo.Local.Id, destination.TimeZoneName//There is not maybe); 

foreach (TimeZoneInfo timeZone in timeZones) 
{ 
    Check here 
} 
+0

我明白这一点。我想了解我是否正在查看错误的字段来进行映射。我已经更新了这个问题,以便更加明确。我明确想要一个可以在100%的时间内工作的改动 – leora 2013-03-03 19:05:23

1

100%?然后使用标准时区名称来验证:

http://en.wikipedia.org/wiki/List_of_tz_database_time_zones 

如果给定的机器上未找到时区有没有保证的替代方法。你应该让你的代码创建一个错误消息,说明为什么有问题。

您意识到给定系统的所有者/管理员可以更改这些设置或添加新设置 - 这意味着不在tz数据库中的非标准tz名称。

+0

我没有任何来自人员联系信息的输入信息位的控制权,我无法控制 – leora 2013-03-03 19:27:18