3 回答

TA貢獻1847條經驗 獲得超7個贊
你是兩個不同的結果,因為根據GetSystemTimeZones()TimeZoneInfo.cs
的實現將嘗試查找和收集時區數據。Registry.LocalMachine
在FindSystemTimeZoneById的情況下,它具有如下條件。如果不匹配,它將從Registry.LocalMachine
.
if (String.Compare(id, c_utcId, StringComparison.OrdinalIgnoreCase) == 0) { return TimeZoneInfo.Utc; }

TA貢獻1856條經驗 獲得超17個贊
答案可在以下參考資料中找到FindSystemTimeZoneById
:
// Special case for Utc as it will not exist in the dictionary with the rest
// of the system time zones. There is no need to do this check for Local.Id
// since Local is a real time zone that exists in the dictionary cache
if (String.Compare(id, c_utcId, StringComparison.OrdinalIgnoreCase) == 0) {
return TimeZoneInfo.Utc;
}
它所指的字典是從 填充的HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones,并且是GetSystemTimeZones返回的內容。
但是,我不認為評論和邏輯是正確的,因為我的 Windows 10 build 1803 機器在上述注冊表路徑中有一個“UTC”條目(假設您的計算機具有相同的條目)??雌饋磉@在早期版本的 Windows 中可能是正確的,但也許不再是這樣了 -已在 corefx 存儲庫中提交錯誤以獲得清晰度。

TA貢獻1821條經驗 獲得超6個贊
這段代碼是很久以前寫的,在那段時間假設 Utc 沒有在機器上枚舉,也不會存儲在本地字典中。我同意現在情況并非如此。雖然我沒有看到這會導致真正的問題,因為兩個 Utc 創建的對象都會正確運行,但我認為最好修復它并確保我們使用一個 Utc 對象。
@Ian Kemp,感謝您在 corefx 存儲庫中打開問題。我們將在 .NET Core 的未來版本中處理這個問題。
- 3 回答
- 0 關注
- 144 瀏覽
添加回答
舉報