当前位置: 首页>>代码示例>>C#>>正文


C# TimeZoneInfo.GetAmbiguousTimeOffsets方法代码示例

本文整理汇总了C#中System.TimeZoneInfo.GetAmbiguousTimeOffsets方法的典型用法代码示例。如果您正苦于以下问题:C# TimeZoneInfo.GetAmbiguousTimeOffsets方法的具体用法?C# TimeZoneInfo.GetAmbiguousTimeOffsets怎么用?C# TimeZoneInfo.GetAmbiguousTimeOffsets使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.TimeZoneInfo的用法示例。


在下文中一共展示了TimeZoneInfo.GetAmbiguousTimeOffsets方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetUtcOffset

        public static TimeSpan GetUtcOffset(DateTime dateTime, TimeZoneInfo timeZoneInfo)
        {
            // Unlike the default behavior of TimeZoneInfo.GetUtcOffset, it is prefered to choose
            // the DAYLIGHT time when the input is ambiguous, because the daylight instance is the
            // FIRST instance, and time moves in a forward direction.

            TimeSpan offset = timeZoneInfo.IsAmbiguousTime(dateTime)
                ? timeZoneInfo.GetAmbiguousTimeOffsets(dateTime).Max()
                : timeZoneInfo.GetUtcOffset(dateTime);

            return offset;
        }
开发者ID:quartznet,项目名称:quartznet,代码行数:12,代码来源:TimeZoneUtil.cs

示例2: RoundToHour

        /// <summary>
        /// Rounds the time to the start of the hour in the specified time zone.
        /// </summary>
        /// <param name="time">The time in UTC.</param>
        /// <param name="timeZone">The time zone. (Can be <see langword="null"/>.)</param>
        /// <param name="localTime">Out: The time in the specified time zone.</param>
        /// <returns>
        /// The start of the day in the specified time zone. The returned value is given in UTC.
        /// </returns>
        /// <remarks>
        /// <para>
        /// The method returns <paramref name="time"/> unchanged if <paramref name="timeZone"/> is
        /// <see cref="TimeZoneInfo.Utc"/> or <see langword="null"/>.
        /// </para>
        /// <para>
        /// <strong>Important:</strong> The returned <paramref name="localTime"/> is valid (i.e. has
        /// an equivalent in UTC), but it may be ambiguous (i.e. has more than one equivalent in
        /// UTC)!
        /// </para>
        /// </remarks>
        private static DateTime RoundToHour(DateTime time, TimeZoneInfo timeZone, out DateTime localTime)
        {
            Debug.Assert(time.Kind == DateTimeKind.Utc, "Time is expected to be UTC.");
            if (timeZone == null || timeZone.Equals(TimeZoneInfo.Utc))
            {
                localTime = time;
                return time;
            }

            // Convert time to specified time zone.
            localTime = TimeZoneInfo.ConvertTime(time, timeZone);

            // Add half an hour for rounding.
            localTime = localTime.AddMinutes(30);

#if SILVERLIGHT
            // Silverlight supports only conversion Local <-> UTC:
            localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day, localTime.Hour, 0, 0, DateTimeKind.Local);
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            // When switching back from Daylight Saving Time to normal time, the time in the
            // local time zone is ambiguous and can be mapped to different time values in UTC.
            if (timeZone.IsAmbiguousTime(localTime))
            {
                // Map the local time to the time in UTC which is closest to the original value.
                TimeSpan[] offsets = timeZone.GetAmbiguousTimeOffsets(localTime);
                TimeSpan minDistance = TimeSpan.MaxValue;
                DateTime closestTime = new DateTime();
                foreach (var offset in offsets)
                {
                    DateTime timeUtc = localTime - offset;
                    TimeSpan distance = (timeUtc - time).Duration();
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        closestTime = timeUtc;
                    }
                }

                time = DateTime.SpecifyKind(closestTime, DateTimeKind.Utc);
            }
            else
            {
                time = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Utc);
            }
#else
            localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day, localTime.Hour, 0, 0);
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            // When switching back from Daylight Saving Time to normal time, the time in the 
            // local time zone is ambiguous and can be mapped to different time values in UTC.  
            if (timeZone.IsAmbiguousTime(localTime))
            {
                // Map the local time to the time in UTC which is closest to the original value.
                TimeSpan[] offsets = timeZone.GetAmbiguousTimeOffsets(localTime);
                TimeSpan minDistance = TimeSpan.MaxValue;
                DateTime closestTime = new DateTime();
                foreach (var offset in offsets)
                {
                    DateTime timeUtc = localTime - offset;
                    TimeSpan distance = (timeUtc - time).Duration();
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        closestTime = timeUtc;
                    }
                }

                time = DateTime.SpecifyKind(closestTime, DateTimeKind.Utc);
            }
            else
            {
                time = TimeZoneInfo.ConvertTime(localTime, timeZone, TimeZoneInfo.Utc);
            }
#endif

            return time;
        }
开发者ID:Zolniu,项目名称:DigitalRune,代码行数:100,代码来源:DateTimeScale.cs


注:本文中的System.TimeZoneInfo.GetAmbiguousTimeOffsets方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。