最近想部屬應用程式到 Linux 上玩玩,碰到了時區轉換的問題,在 Window 中可以使用
TimeZoneInfo.FindSystemTimeZoneById("Taipei Standard Time")
取得台灣時區,放到 Linux 卻噴了錯誤給我看
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {1e7284ba-b3b0-4c9a-8e1c-4dac05942fc7} may be persisted to storage in unencrypted form. Application startup exception: System.TimeZoneNotFoundException: The time zone ID 'Taipei Standard Time' was not found on the local computer. ---> System.IO.FileNotFoundException: Could not find file '/usr/share/zoneinfo/Taipei Standard Time'. at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
Linux 認不得 Taipei Standard Time
那該怎麼辦呢?
簡單的兩行程式就可以依照平台切換時區名稱
string id = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Taipei Standard Time" : "Asia/Taipei"; TimeZoneInfo tw = TimeZoneInfo.FindSystemTimeZoneById(id);