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


C# TimeZoneInfo.Equals方法代碼示例

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


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

示例1: IsCronTrigger

        private static bool IsCronTrigger(Trigger trigger, string cronExpression, TimeZoneInfo timeZone)
        {
            var cronTrigger = trigger as CronTrigger;

            return cronTrigger != null && cronTrigger.CronExpressionString == cronExpression && timeZone.Equals(cronTrigger.TimeZone);
        }
開發者ID:mparsin,項目名稱:Elements,代碼行數:6,代碼來源:ActionManagerScheduler.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

示例3: RoundToMonth

        /// <summary>
        /// Rounds the time to the start of the month 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>
        /// <returns>
        /// The start of the month in the specified time zone. The returned value is given in UTC.
        /// </returns>
        /// <remarks>
        /// The method returns <paramref name="time"/> unchanged if <paramref name="timeZone"/> is
        /// <see cref="TimeZoneInfo.Utc"/> or <see langword="null"/>.
        /// </remarks>
        private static DateTime RoundToMonth(DateTime time, TimeZoneInfo timeZone)
        {
            Debug.Assert(time.Kind == DateTimeKind.Utc, "Time is expected to be UTC.");
            if (timeZone == null || timeZone.Equals(TimeZoneInfo.Utc))
                return time;

            // Add a half month for safety and then round down. (See RoundToYear for comments.)
            time = time.AddDays(15);

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

            time = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Utc);
#else
            var localTime = new DateTime(time.Year, time.Month, 1);
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            time = TimeZoneInfo.ConvertTime(localTime, timeZone, TimeZoneInfo.Utc);
#endif

            return time;
        }
開發者ID:Zolniu,項目名稱:DigitalRune,代碼行數:38,代碼來源:DateTimeScale.cs

示例4: RoundToYear

        /// <summary>
        /// Rounds the time to the start of the year 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>
        /// <returns>
        /// The start of the year in the specified time zone. The returned value is given in UTC.
        /// </returns>
        /// <remarks>
        /// The method returns <paramref name="time"/> unchanged if <paramref name="timeZone"/> is
        /// <see cref="TimeZoneInfo.Utc"/> or <see langword="null"/>.
        /// </remarks>
        private static DateTime RoundToYear(DateTime time, TimeZoneInfo timeZone)
        {
            Debug.Assert(time.Kind == DateTimeKind.Utc, "Time is expected to be UTC.");
            if (timeZone == null || timeZone.Equals(TimeZoneInfo.Utc))
                return time;

            // The time in UTC does not exactly match the start of the year in the local
            // time zone. In order to ensure that year is correct we can simply add one
            // month for safety and then round down.
            time = time.AddMonths(1);

#if SILVERLIGHT
            // Silverlight supports only conversion Local <-> UTC:
            var localTime = new DateTime(time.Year, 1, 1, 0, 0, 0, DateTimeKind.Local);

            // The local time may be invalid. For example, the date 2009-01-01 0:00 
            // does not exist in the time zone "(UTC -3:00) Buenos Aires"!
            // --> Pick the first valid hour as the start of the year.
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            time = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Utc);
#else
            var localTime = new DateTime(time.Year, 1, 1);

            // The local time may be invalid. For example, the date 2009-01-01 0:00
            // does not exist in the time zone "(UTC -3:00) Buenos Aires"!
            // --> Pick the first valid hour as the start of the year.
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            time = TimeZoneInfo.ConvertTime(localTime, timeZone, TimeZoneInfo.Utc);
#endif

            return time;
        }
開發者ID:Zolniu,項目名稱:DigitalRune,代碼行數:48,代碼來源:DateTimeScale.cs

示例5: RoundToDay

        /// <summary>
        /// Rounds the time to the start of the day in the specified time zone.
        /// </summary>
        /// <param name="time">
        /// The time in UTC, which should to be close to the actual start of the day in specified
        /// time zone!
        /// </param>
        /// <param name="timeZone">The time zone. (Can be <see langword="null"/>.)</param>
        /// <returns>
        /// The start of the day in the specified time zone. The returned value is given in UTC.
        /// </returns>
        /// <remarks>
        /// The method returns <paramref name="time"/> unchanged if <paramref name="timeZone"/> is
        /// <see cref="TimeZoneInfo.Utc"/> or <see langword="null"/>.
        /// </remarks>
        private static DateTime RoundToDay(DateTime time, TimeZoneInfo timeZone)
        {
            Debug.Assert(time.Kind == DateTimeKind.Utc, "Time is expected to be UTC.");
            if (timeZone == null || timeZone.Equals(TimeZoneInfo.Utc))
                return time;

            // Rounding can be difficult because time zone offsets range from -12 to +13 h.
            // Precondition: The specified time in UTC must be close to the start of the day
            // in the local time zone!

            // Convert time to current time zone to get the correct number of the day.
            var localTime = TimeZoneInfo.ConvertTime(time, timeZone);

            // Add a half day for safety.
            localTime = localTime.AddHours(12);

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

            time = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Utc);
#else
            localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day);
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            time = TimeZoneInfo.ConvertTime(localTime, timeZone, TimeZoneInfo.Utc);
#endif

            return time;
        }
開發者ID:Zolniu,項目名稱:DigitalRune,代碼行數:48,代碼來源:DateTimeScale.cs


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