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


C# TimeZoneInfo.IsInvalidTime方法代码示例

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


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

示例1: ScheduleInstant

        public ScheduleInstant(DateTime nowInstant, TimeZoneInfo timeZone, [NotNull] CrontabSchedule schedule)
        {
            if (schedule == null) throw new ArgumentNullException("schedule");
            if (nowInstant.Kind != DateTimeKind.Utc)
            {
                throw new ArgumentException("Only DateTime values in UTC should be passed.", "nowInstant");
            }

            _timeZone = timeZone;
            _schedule = schedule;

            NowInstant = nowInstant.AddSeconds(-nowInstant.Second);

            var nextOccurrences = _schedule.GetNextOccurrences(
                TimeZoneInfo.ConvertTime(NowInstant, TimeZoneInfo.Utc, _timeZone),
                DateTime.MaxValue);

            foreach (var nextOccurrence in nextOccurrences)
            {
                if (_timeZone.IsInvalidTime(nextOccurrence)) continue;

                NextInstant = TimeZoneInfo.ConvertTime(nextOccurrence, _timeZone, TimeZoneInfo.Utc);
                break;
            }
        }
开发者ID:GitHuang,项目名称:Hangfire,代码行数:25,代码来源:ScheduleInstant.cs

示例2: ConvertToUtcTime

 /// <summary>
 /// Converts the date and time to Coordinated Universal Time (UTC)
 /// </summary>
 /// <param name="dt">The date and time to convert.</param>
 /// <param name="sourceTimeZone">The time zone of dateTime.</param>
 /// <returns>A DateTime value that represents the Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. The DateTime value's Kind property is always set to DateTimeKind.Utc.</returns>
 public virtual DateTime ConvertToUtcTime(DateTime dt, TimeZoneInfo sourceTimeZone)
 {
     if (sourceTimeZone.IsInvalidTime(dt))
     {
         //could not convert
         return dt;
     }
     return TimeZoneInfo.ConvertTimeToUtc(dt, sourceTimeZone);
 }
开发者ID:pauldotknopf,项目名称:Noodle,代码行数:15,代码来源:DateTimeHelper.cs

示例3: DateTimeToUtc

        public static DateTime DateTimeToUtc(TimeZoneInfo timeZone, DateTime local)
        {
            if (local.Kind == DateTimeKind.Utc || local == DateTime.MinValue || local == DateTime.MaxValue)
            {
                return local;
            }

            if (timeZone.IsInvalidTime(DateTime.SpecifyKind(local, DateTimeKind.Unspecified)))
            {
                // hack
                local = local.AddHours(1);
            }

            return TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(local, DateTimeKind.Unspecified), timeZone);

        }
开发者ID:vipwan,项目名称:CommunityServer,代码行数:16,代码来源:TenantUtil.cs

示例4: ConvertTimeToUtc

		static DateTime ConvertTimeToUtc (DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfoOptions flags)
		{
			if ((flags & TimeZoneInfoOptions.NoThrowOnInvalidTime) == 0) {
				if (sourceTimeZone == null)
					throw new ArgumentNullException ("sourceTimeZone");

				if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc)
					throw new ArgumentException ("Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc");

				if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != TimeZoneInfo.Local)
					throw new ArgumentException ("Kind property of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local");

				if (sourceTimeZone.IsInvalidTime (dateTime))
					throw new ArgumentException ("dateTime parameter is an invalid time");
			}

			if (dateTime.Kind == DateTimeKind.Utc)
				return dateTime;

			bool isDst;
			var utcOffset = sourceTimeZone.GetUtcOffset (dateTime, out isDst);

			DateTime utcDateTime;
			if (!TryAddTicks (dateTime, -utcOffset.Ticks, out utcDateTime, DateTimeKind.Utc))
				return DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Utc);

			return utcDateTime;
		}
开发者ID:barsgroup,项目名称:linux-packaging-mono,代码行数:28,代码来源:TimeZoneInfo.cs

示例5: ConvertTime

		public static DateTime ConvertTime (DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
		{
			if (sourceTimeZone == null)
				throw new ArgumentNullException ("sourceTimeZone");

			if (destinationTimeZone == null)
				throw new ArgumentNullException ("destinationTimeZone");
			
			if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != TimeZoneInfo.Local)
				throw new ArgumentException ("Kind property of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local");

			if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc)
				throw new ArgumentException ("Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc");
			
			if (sourceTimeZone.IsInvalidTime (dateTime))
				throw new ArgumentException ("dateTime parameter is an invalid time");

			if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone == TimeZoneInfo.Local && destinationTimeZone == TimeZoneInfo.Local)
				return dateTime;

			DateTime utc = ConvertTimeToUtc (dateTime, sourceTimeZone);

			if (destinationTimeZone != TimeZoneInfo.Utc) {
				utc = ConvertTimeFromUtc (utc, destinationTimeZone);
				if (dateTime.Kind == DateTimeKind.Unspecified)
					return DateTime.SpecifyKind (utc, DateTimeKind.Unspecified);
			}
			
			return utc;
		}
开发者ID:barsgroup,项目名称:linux-packaging-mono,代码行数:30,代码来源:TimeZoneInfo.cs

示例6: ConvertToUtcDateTime

        /// <summary>
        /// Converts a DateTime to Utc, considering DST jumps when starting or ending.
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="timeZoneInfo"></param>
        /// <returns></returns>
        public static DateTime ConvertToUtcDateTime(DateTime dateTime, TimeZoneInfo timeZoneInfo)
        {
            if (timeZoneInfo.IsInvalidTime(dateTime))
            {
                // Determinando o intervalo inexistente, por conta do
                // adiantamento de horário de verão,
                // que contém a data e hora passada em practiceDateTime.
                // e.g. horário é adiantado no dia 2012/10/21 às 00:00 em 1 hora,
                // então todos os horários no intervalo seguinte são inválidos:
                // [2012/10/21 00:00:00,0000000 ~ 2012/10/21 01:00:00,0000000[
                // O intervalo retornado é exclusivo em ambas as pontas,
                // de forma que as data em ambas as pontas sejam VÁLIDAS.
                // Então o intervalo de exemplo acima será dado assim:
                // ]2012/10/20 23:59:59,9999999 ~ 2012/10/21 01:00:00,0000000[
                var invalidIntervals = timeZoneInfo.GetAdjustmentRules()
                    .Where(ar => ar.DaylightDelta != TimeSpan.Zero)
                    .Select(ar => ar.DaylightDelta > TimeSpan.Zero
                        ? new { Delta = ar.DaylightDelta, Transition = ar.DaylightTransitionStart, Date = ar.DateStart }
                        : new { Delta = -ar.DaylightDelta, Transition = ar.DaylightTransitionEnd, Date = ar.DateStart })
                    .Select(x => new { Start = GetTransitionDateTime(x.Transition, x.Date.Year), x.Delta })
                    .Select(x => new { x.Start, x.Delta, End = (x.Start + x.Delta).AddTicks(-1) })
                    .Select(x => new { StartExclusive = x.Start.AddTicks(-1), EndExclusive = x.End.AddTicks(1) })
                    .Where(x => x.StartExclusive < dateTime && x.EndExclusive > dateTime)
                    .ToList();

                // Deve haver apenas um intervalo.
                var invalidInterval = invalidIntervals.Single();

                // Determinando a ponta do intervalo que está dentro do dia atual, passado em practiceDateTime.
                var dateTime2 = dateTime.Day == invalidInterval.StartExclusive.Day
                                    ? invalidInterval.StartExclusive
                                    : invalidInterval.EndExclusive;

                // Convertendo a data para UTC.
                var result = TimeZoneInfo.ConvertTimeToUtc(dateTime2, timeZoneInfo);
                return result;
            }
            else
            {
                var result = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZoneInfo);
                return result;
            }
        }
开发者ID:andrerpena,项目名称:Cerebello,代码行数:49,代码来源:DateTimeHelper.cs

示例7: 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

示例8: 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

示例9: 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

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