2013-03-03 13 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