示例#1
0
        /// <summary>
        /// Converts a local time of the source time-zone to a Utc time
        /// </summary>
        /// <param name="source">The source time-zone</param>
        /// <param name="sourceLocalTime">The local time in the source time-zone</param>
        /// <returns>The Utc time that is equivalent to the local time in the source time-zone.</returns>
        public static DateTime ToUniversalTime(TimeZoneInfo source, DateTime sourceLocalTime)
        {
            // Min and Max value are treated as 'special' dates that should never need converted.
            if (sourceLocalTime == DateTime.MinValue || sourceLocalTime == DateTime.MaxValue)
            {
                return(DateTime.SpecifyKind(sourceLocalTime, DateTimeKind.Utc));
            }

            //first we must determine if the specified local time is during the daylight saving
            //time period.  If it is, then we add the Bias and Daylight bias to the value, otherwise
            //we add the Bias and Standard bias (I believe that StandardBias is always 0)

            DateTime utcTime;

            if (source.IsDaylightSavingTime(sourceLocalTime))
            {
                utcTime = sourceLocalTime.AddMinutes(source.Bias + source.DaylightBias);
            }
            else
            {
                utcTime = sourceLocalTime.AddMinutes(source.Bias + source.StandardBias);
            }

            return(DateTime.SpecifyKind(utcTime, DateTimeKind.Utc));
        }
示例#2
0
        /// <summary>
        /// Converts the UtcTime to the local time of the destination time-zone.
        /// </summary>
        /// <param name="destination">The destination time-zone</param>
        /// <param name="utcTime">Utc time that is to be converted to the local time of
        /// the destination time-zone.</param>
        /// <returns>DateTime that represents the local time in the destination time-zone</returns>
        public static DateTime ToLocalTime(TimeZoneInfo destination, DateTime utcTime)
        {
            // Min and Max value are treated as 'special' dates that should never need converted.
            if (utcTime == DateTime.MinValue || utcTime == DateTime.MaxValue)
            {
                return(DateTime.SpecifyKind(utcTime, DateTimeKind.Local));
            }

            //first we must convert the utcTime to the local time without any regard to the
            //daylight saving issues.  We'll deal with that next
            DateTime localTime = utcTime.AddMinutes(-destination.Bias);

            // Need to set it to kind of Local, because System.TimeZone.IsDaylightSavingTime
            // will return false if the time passed into it is not Local.
            localTime = DateTime.SpecifyKind(localTime, DateTimeKind.Local);

            //now we must determine if the specified local time is during the daylight saving
            //time period.  If it is, then we add the Bias and Daylight bias to the value, otherwise
            //we add the Bias and Standard bias (I believe that StandardBias is always 0)
            if (destination.IsDaylightSavingTime(localTime))
            {
                return(localTime.AddMinutes(-destination.DaylightBias));
            }
            else
            {
                return(localTime.AddMinutes(-destination.StandardBias));
            }
        }