TimeZoneInfo in .NET Core when hosting on unix (nginx)

后端 未结 5 757
半阙折子戏
半阙折子戏 2020-12-09 07:55

For example, when I try to do the following.

TimeZoneInfo.FindSystemTimeZoneById(\"Central European Standard Time\")

I get the error, that

5条回答
  •  猫巷女王i
    2020-12-09 08:15

    Working of off the previous answer, we can avoid the expensive try/catch by checking which OS we're running on:

    using System;
    using System.Runtime.InteropServices;
    
    TimeZoneInfo easternStandardTime;
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
      easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    }
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
      easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
    }
    if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    {
      throw new NotImplementedException("I don't know how to do a lookup on a Mac.");
    }
    

提交回复
热议问题