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 IanaToWindowsTest() { var input = "Asia/Shanghai"; string result = TimeZoneConverter.IanaToWindows(input); var expect = "China Standard Time"; Assert.AreEqual(result, expect); }
public void IanaToWindowsTest_NotLegal_Throws() { try { var input = "test"; string result = TimeZoneConverter.IanaToWindows(input); } catch (InvalidTimeZoneException) { return; } Assert.Fail("Should throw exception"); }