當前位置: 首頁>>代碼示例>>C#>>正文


C# TimeZoneInfo.TryGetTransitionOffset方法代碼示例

本文整理匯總了C#中System.TimeZoneInfo.TryGetTransitionOffset方法的典型用法代碼示例。如果您正苦於以下問題:C# TimeZoneInfo.TryGetTransitionOffset方法的具體用法?C# TimeZoneInfo.TryGetTransitionOffset怎麽用?C# TimeZoneInfo.TryGetTransitionOffset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.TimeZoneInfo的用法示例。


在下文中一共展示了TimeZoneInfo.TryGetTransitionOffset方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: GetUtcOffsetHelper

        // This is an helper method used by the method above, do not use this on its own.
        private static TimeSpan GetUtcOffsetHelper (DateTime dateTime, TimeZoneInfo tz, out bool isDST)
        {
            if (dateTime.Kind == DateTimeKind.Local && tz != TimeZoneInfo.Local)
                throw new Exception ();

            isDST = false;

            if (tz == TimeZoneInfo.Utc)
                return TimeSpan.Zero;

            TimeSpan offset;
            if (tz.TryGetTransitionOffset(dateTime, out offset, out isDST))
                return offset;

            if (dateTime.Kind == DateTimeKind.Utc) {
                var utcRule = tz.GetApplicableRule (dateTime);
                if (utcRule != null && tz.IsInDST (utcRule, dateTime)) {
                    isDST = true;
                    return tz.BaseUtcOffset + utcRule.DaylightDelta;
                }

                return tz.BaseUtcOffset;
            }

            DateTime stdUtcDateTime;
            if (!TryAddTicks (dateTime, -tz.BaseUtcOffset.Ticks, out stdUtcDateTime, DateTimeKind.Utc))
                return tz.BaseUtcOffset;

            var tzRule = tz.GetApplicableRule (stdUtcDateTime);

            DateTime dstUtcDateTime = DateTime.MinValue;
            if (tzRule != null) {
                if (!TryAddTicks (stdUtcDateTime, -tzRule.DaylightDelta.Ticks, out dstUtcDateTime, DateTimeKind.Utc))
                    return tz.BaseUtcOffset;
            }

            if (tzRule != null && tz.IsInDST (tzRule, stdUtcDateTime) && tz.IsInDST (tzRule, dstUtcDateTime)) {
                isDST = true;
                return tz.BaseUtcOffset + tzRule.DaylightDelta;
            }

            return tz.BaseUtcOffset;
        }
開發者ID:barsgroup,項目名稱:linux-packaging-mono,代碼行數:44,代碼來源:TimeZoneInfo.cs

示例2: GetUtcOffsetHelper

        // This is an helper method used by the method above, do not use this on its own.
        private static TimeSpan GetUtcOffsetHelper (DateTime dateTime, TimeZoneInfo tz, out bool isDST)
        {
            if (dateTime.Kind == DateTimeKind.Local && tz != TimeZoneInfo.Local)
                throw new Exception ();

            isDST = false;

            if (tz == TimeZoneInfo.Utc)
                return TimeSpan.Zero;

            TimeSpan offset;
            if (tz.TryGetTransitionOffset(dateTime, out offset, out isDST))
                return offset;

            if (dateTime.Kind == DateTimeKind.Utc) {
                var utcRule = tz.GetApplicableRule (dateTime);
                if (utcRule != null && tz.IsInDST (utcRule, dateTime)) {
                    isDST = true;
                    return tz.BaseUtcOffset + utcRule.DaylightDelta;
                }

                return tz.BaseUtcOffset;
            }

            DateTime stdUtcDateTime;
            if (!TryAddTicks (dateTime, -tz.BaseUtcOffset.Ticks, out stdUtcDateTime, DateTimeKind.Utc))
                return tz.BaseUtcOffset;

            var tzRule = tz.GetApplicableRule (stdUtcDateTime);

            DateTime dstUtcDateTime = DateTime.MinValue;
            if (tzRule != null) {
                if (!TryAddTicks (stdUtcDateTime, -tzRule.DaylightDelta.Ticks, out dstUtcDateTime, DateTimeKind.Utc))
                    return tz.BaseUtcOffset;
            }

            if (tzRule != null && tz.IsInDST (tzRule, stdUtcDateTime)) {
                // Replicate what .NET does when given a time which falls into the hour which is lost when
                // DST starts. isDST should always be true but the offset should be BaseUtcOffset without the
                // DST delta while in that hour.
                isDST = true;
                if (tz.IsInDST (tzRule, dstUtcDateTime)) {
                    return tz.BaseUtcOffset + tzRule.DaylightDelta;
                } else {
                    return tz.BaseUtcOffset;
                }
            }

            return tz.BaseUtcOffset;
        }
開發者ID:ItsVeryWindy,項目名稱:mono,代碼行數:51,代碼來源:TimeZoneInfo.cs


注:本文中的System.TimeZoneInfo.TryGetTransitionOffset方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。