I'm working on porting a .NET Framework 4.5 app to .NET Core 2.0 and have started to do some testing on my Mac. When I run this code on .NET Core 2.0 on Windows it works fine:
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
but it throws the following exception on the Mac:
System.TimeZoneNotFoundException: The time zone ID 'Pacific Standard Time' was not found on the local computer. ---> System.IO.FileNotFoundException: Could not find file '/usr/share/zoneinfo/Pacific 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)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.File.ReadAllBytes(String path)
at System.TimeZoneInfo.TryGetTimeZoneFromLocalMachine(String id, TimeZoneInfo& value, Exception& e)
--- End of inner exception stack trace ---
at System.TimeZoneInfo.FindSystemTimeZoneById(String id)
After some poking around, it seems that using the time zone ID "America/Los_Angeles"
fixes the problem on the Mac.
I'm wondering what I should do to keep this platform agnostic. Should I try to find "Pacific Standard Time"
, and then try to find "America/Los Angeles"
in a catch block? That seems clumsy, and what if another platform has yet another string for it? Is there a better way to look up the TimeZoneInfo I want?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…