public static TimeZoneInfo ConvertTimeZoneFormat(string timezone) { string convertedTimeZoneStr; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { convertedTimeZoneStr = TimeZoneConverter.IanaToWindows(timezone); } else { convertedTimeZoneStr = TimeZoneConverter.WindowsToIana(timezone); } TimeZoneInfo convertedTimeZone; try { convertedTimeZone = TimeZoneInfo.FindSystemTimeZoneById(convertedTimeZoneStr); } catch { throw new Exception($"{timezone} is an illegal timezone"); } return(convertedTimeZone); }
public void WindowsToIanaTest() { var input = "China Standard Time"; string result = TimeZoneConverter.WindowsToIana(input); var expect = "Asia/Shanghai"; Assert.AreEqual(result, expect); }
public void WindowsToIanaTest_NotLegal_Throws() { try { var input = "test"; string result = TimeZoneConverter.WindowsToIana(input); } catch (InvalidTimeZoneException) { return; } Assert.Fail("Should throw exception"); }