Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public void IanaToWindowsTest()
        {
            var    input  = "Asia/Shanghai";
            string result = TimeZoneConverter.IanaToWindows(input);
            var    expect = "China Standard Time";

            Assert.AreEqual(result, expect);
        }
Exemplo n.º 3
0
        public void IanaToWindowsTest_NotLegal_Throws()
        {
            try
            {
                var    input  = "test";
                string result = TimeZoneConverter.IanaToWindows(input);
            }
            catch (InvalidTimeZoneException)
            {
                return;
            }

            Assert.Fail("Should throw exception");
        }