簡體   English   中英

為什么我會收到此時區錯誤,是否有更好的方法在Google地圖時區和Windows時區之間進行映射?

[英]Why am i getting this timezone error and is there a better way to map between Google Maps Timezone and Windows Time Zones?

我有以下代碼從位置轉換為TimeZone名稱。

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“Australian Eastern Daylight Time”

關於我做錯了什么的任何建議? 當我查看來自第二個谷歌地圖API調用的響應時,這些是我得到的所有字段(以LA為例):

{
 "dstOffset" : 0.0,
 "rawOffset" : -28800.0,
 "status" : "OK",
 "timeZoneId" : "America/Los_Angeles",
 "timeZoneName" : "Pacific Standard Time"
}

當我通過墨爾本時,我看到TimeZoneId字段設置為:“”Australia / Hobart“”。 這是查看時區計算的正確字段嗎? 或者我應該看看其他“偏移”字段?

任何建議將不勝感激。

由於.NET實現了時區,因此沒有內置的方法來進行1:1轉換。 您將不得不求助於使用第三方庫(或實施您自己的轉換)。


這個問題要求提供與您正在尋找的非常相似的解決方案。

提問者在內部使用Olson時區數據庫tz數據庫/ zoneinfo數據庫/ IANA時區數據庫 )找到了解決方案。 提問者引用頁面這就解釋了一下有關轉換。


最后,您可以使用Noda Time ,它實現了您正在尋找的功能。 Jon Skeet的回答早在2011年就可以看到圖書館的發展。

庫的Key Concepts頁面包含一個日期/時區塊,用於解釋轉換功能。


UPDATE

這是一個關於如何創建這樣的查找表示例

// 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;
}

100%? 然后使用標准時區名稱來驗證:

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

如果在給定的計算機上找不到時區,則無法保證可以使用。 您應該讓代碼創建一條錯誤消息,說明出現問題的原因。

您確實意識到給定系統的所有者/管理員可以更改這些設置或添加新設置 - 這意味着非標准tz名稱不在tz數據庫中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM