Example #1
0
        /// <summary>
        /// Converts a localTime from a source time zone to a target time zone, adjusting for DST as needed.
        /// The localTime must be a local time in the sourceTimeZoneName time zone.
        /// </summary>
        /// <param name="sourceTimeZoneName">Time zone name which represents localTime.</param>
        /// <param name="localTime">The source local time.</param>
        /// <param name="targetTimeZoneName">The time zone name which to convert the localTime.</param>
        /// <returns>The local time for targetTimeZoneName.</returns>
        public static DateTime ToLocalTime(string sourceTimeZoneName, DateTime localTime, string targetTimeZoneName)
        {
            DateTime utc = TimeZoneInformation.ToUniversalTime(sourceTimeZoneName, localTime);
            DateTime lt  = TimeZoneInformation.ToLocalTime(utc, targetTimeZoneName);

            return(lt);
        }
Example #2
0
        /// <summary>
        /// Converts a local time in specified time zone to UTC time.
        /// </summary>
        /// <param name="standardTimeZoneName">The standard time zone name.</param>
        /// <param name="local">The local time to convert.</param>
        /// <returns>The UTC time.</returns>
        /// <exception cref="ArgumentException">Thrown if time zone name not found.</exception>
        /// <exception cref="NotSupportedException">Thrown if the method failed due to missing platform support.</exception>
        public static DateTime ToUniversalTime(string standardTimeZoneName, DateTime local)
        {
            TimeZoneInformation tzi = TimeZoneInformation.GetTimeZone(standardTimeZoneName);

            return(tzi.ToUniversalTime(local));
        }