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


C# TimeSpan.Duration方法代码示例

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


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

示例1: OnActionExecuted

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            DateTime dte = DateTime.Now;
            Debug.WriteLine("OnActionExecuted: " + dte.ToString("yyyy/MM/dd HH:mm:sss"));
            DateTime dts = Convert.ToDateTime(filterContext.Controller.ViewBag.startTime);
            TimeSpan s = new TimeSpan(dte.Ticks - dts.Ticks);
            Debug.WriteLine("ActionResult 執行的時間(毫秒): " + s.Duration().TotalMilliseconds);

            base.OnActionExecuted(filterContext);
        }
开发者ID:haley83,项目名称:ClientManageMVC,代码行数:10,代码来源:MyFilterAttribute.cs

示例2: ToReadableString

        public static string ToReadableString(this TimeSpan span)
        {
            span = new TimeSpan(span.Days * 24 / 8 + span.Hours / 8, span.Hours % 8, span.Minutes, 0);
            string formatted = string.Format("{0}{1}{2}",
                (span.Duration().Hours / 8) > 0 ? string.Format("{0:0}d", span.Hours/8) : string.Empty,
                span.Duration().Hours > 0 ? string.Format("{0:0}h", span.Hours) : string.Empty,
                span.Duration().Minutes > 0 ? string.Format("{0:0}m", span.Minutes) : string.Empty);

            if (string.IsNullOrEmpty(formatted)) return "-";
            return formatted;
        }
开发者ID:pakrym,项目名称:YouTrackBoard,代码行数:11,代码来源:TimeSpanUtilities.cs

示例3: ToReadableString

        public static string ToReadableString(TimeSpan span)
        {
            string formatted = string.Format("{0}{1}{2}{3}",
                span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty,
                span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
                span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
                span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);

            if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);

            if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";

            return formatted;
        }
开发者ID:MagistrAVSH,项目名称:manicdigger,代码行数:14,代码来源:HttpStats.cs

示例4: ToReadableString

        public static string ToReadableString(TimeSpan span)
        {
            string formatted = string.Format("{0}{1}{2}{3}",
                span.Duration().Days > 0 ? string.Format("{0:0}:", span.Days) : string.Empty,
                span.Duration().Hours > 0 ? string.Format("{00:00}:", span.Hours) : string.Format("00:"),
                span.Duration().Minutes > 0 ? string.Format("{00:00}:", span.Minutes) : string.Format("00:"),
                span.Duration().Seconds > 0 ? string.Format("{00:00}", span.Seconds) : string.Format("00"));

            if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);

            if (string.IsNullOrEmpty(formatted)) formatted = "00:00";

            return formatted;
        }
开发者ID:jcsla,项目名称:Orange-Client-For-Windows,代码行数:14,代码来源:ConvertTimespanToString.cs

示例5: PosTest1

    public bool PosTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest1: Call Duration on a TimeSpan instance whose value is 0");

        try
        {
            TimeSpan expected = new TimeSpan(0);
            TimeSpan actual = expected.Duration();

            if (actual.Ticks != expected.Ticks)
            {
                TestLibrary.TestFramework.LogError("001.1", "Calling Duration on a TimeSpan instance whose value is 0");
                TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actual.Ticks = " + actual.Ticks + ", expected.Ticks = " + expected.Ticks);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("001.0", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return retVal;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:27,代码来源:timespanduration.cs

示例6: PosTest2

    public bool PosTest2()
    {
        bool retVal = true;
        long randValue = 0;

        TestLibrary.TestFramework.BeginScenario("PosTest2: Call Duration on a TimeSpan instance whose value is a positive value");

        try
        {
            do
            {
                randValue = TestLibrary.Generator.GetInt64(-55);
            } while (randValue == 0);
            if (randValue < 0) randValue *= -1;

            TimeSpan expected = new TimeSpan(randValue);
            TimeSpan actual = expected.Duration();

            if (actual.Ticks != expected.Ticks)
            {
                TestLibrary.TestFramework.LogError("002.1", "Calling Duration on a TimeSpan instance whose value is a positive value");
                TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actual.Ticks = " + actual.Ticks + ", expected.Ticks = " + expected.Ticks + ", randValue = " + randValue);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002.0", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] randValue = " + randValue);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return retVal;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:35,代码来源:timespanduration.cs

示例7: Retry

        private Retry(bool shouldBeRetried, TimeSpan retryAfter)
        {
            if (retryAfter != TimeSpan.Zero && retryAfter != retryAfter.Duration()) throw new ArgumentOutOfRangeException("retryAfter");
            if (!shouldBeRetried && retryAfter != TimeSpan.Zero) throw new ArgumentException("Invalid combination");

            ShouldBeRetried = shouldBeRetried;
            RetryAfter = retryAfter;
        }
开发者ID:gitter-badger,项目名称:EventFlow,代码行数:8,代码来源:Retry.cs

示例8: ThrowIfValueIsNotPositive

        private static void ThrowIfValueIsNotPositive(TimeSpan value, string fieldName)
        {
            var message = $"The {fieldName} property value should be positive. Given: {value}.";

            if (value == TimeSpan.Zero)
            {
                throw new ArgumentException(message, nameof(value));
            }
            if (value != value.Duration())
            {
                throw new ArgumentException(message, nameof(value));
            }
        }
开发者ID:ahydrax,项目名称:Hangfire.PostgreSql,代码行数:13,代码来源:PostgreSqlStorageOptions.cs

示例9: GetNonZeroFragments

        private static IEnumerable<string> GetNonZeroFragments(TimeSpan timeSpan)
        {
            TimeSpan absoluteTimespan = timeSpan.Duration();

            var fragments = new List<string>();

            AddDaysIfNotZero(absoluteTimespan, fragments);
            AddHoursIfNotZero(absoluteTimespan, fragments);
            AddMinutesIfNotZero(absoluteTimespan, fragments);
            AddSecondsIfNotZero(absoluteTimespan, fragments);

            return fragments;
        }
开发者ID:lothrop,项目名称:fluentassertions,代码行数:13,代码来源:TimeSpanValueFormatter.cs

示例10: FromTimeSpan

		public static TimeVal FromTimeSpan (TimeSpan span) {
			TimeVal tv = new TimeVal();
			long nanoseconds;

			/* make sure we're dealing with a positive TimeSpan */
			span = span.Duration();

			nanoseconds = span.Ticks * 100;

			tv.tv_sec = (int)(nanoseconds / 1E+09);
			tv.tv_usec = (int)((nanoseconds % 1E+09) / 1000);

			return tv;
		}
开发者ID:REALTOBIZ,项目名称:mono,代码行数:14,代码来源:LDAP.cs

示例11: RateGate

        /// <summary>
        /// Initializes a <see cref="RateGate"/> with a rate of <paramref name="occurrences"/>
        /// per <paramref name="timeUnit"/>.
        /// </summary>
        /// <param name="occurrences">Number of occurrences allowed per unit of time.</param>
        /// <param name="timeUnit">Length of the time unit.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="occurrences"/> or <paramref name="timeUnit"/> is negative.
        /// </exception>
        public RateGate(int occurrences, TimeSpan timeUnit)
        {
            if (occurrences <= 0) throw new ArgumentOutOfRangeException("occurrences", "Number of occurrences must be a positive integer");
            if (timeUnit != timeUnit.Duration()) throw new ArgumentOutOfRangeException("timeUnit", "Time unit must be a positive span of time");
            if (timeUnit >= TimeSpan.FromMilliseconds(UInt32.MaxValue)) throw new ArgumentOutOfRangeException("timeUnit", "Time unit must be less than 2^32 milliseconds");

            Occurrences = occurrences;
            TimeUnitMilliseconds = (int)timeUnit.TotalMilliseconds;

            // Create the semaphore, with the number of occurrences as the maximum count.
            _semaphore = new SemaphoreSlim(Occurrences, Occurrences);

            // Create a queue to hold the semaphore exit times.
            _exitTimes = new ConcurrentQueue<int>();

            // Create a timer to exit the semaphore. Use the time unit as the original
            // interval length because that's the earliest we will need to exit the semaphore.
            _exitTimer = new Timer(ExitTimerCallback, null, TimeUnitMilliseconds, -1);
        }
开发者ID:hamstercat,项目名称:perfect-media,代码行数:28,代码来源:RateGate.cs

示例12: GetDifference

		public DifferenceIn GetDifference(TimeSpan span)
		{
			span = span.Duration();

			DifferenceIn diff;
			if (span.Days > 365)
				diff = DifferenceIn.Year;
			else if (span.Days > 0)
				diff = DifferenceIn.Day;
			else if (span.Hours > 0)
				diff = DifferenceIn.Hour;
			else if (span.Minutes > 0)
				diff = DifferenceIn.Minute;
			else if (span.Seconds > 0)
				diff = DifferenceIn.Second;
			else
				diff = DifferenceIn.Millisecond;

			return diff;
		}
开发者ID:BdGL3,项目名称:CXPortal,代码行数:20,代码来源:ExtendedDaysStrategy.cs

示例13: GetDifference

        public virtual DifferenceIn GetDifference(TimeSpan span)
        {
            span = span.Duration();

            DifferenceIn diff;
            if (span.Days /2> 365)
                diff = DifferenceIn.Year;
            else if (span.Days/2 > 30)
                diff = DifferenceIn.Month;
            else if (span.Days/2 > 0)
                diff = DifferenceIn.Day;
            else if (span.Hours/2 > 0)
                diff = DifferenceIn.Hour;
            else if (span.Minutes/2 > 0)
                diff = DifferenceIn.Minute;
            else if (span.Seconds/2 > 0)
                diff = DifferenceIn.Second;
            else
                diff = DifferenceIn.Millisecond;

            return diff;
        }
开发者ID:XiBeichuan,项目名称:hydronumerics,代码行数:22,代码来源:ExtendedAxisDateTimeTicksStrategy.cs

示例14: GetTimeSpan

        public string GetTimeSpan(DateTime PostedDate)
        {
            string strTimeSpan = string.Empty;
            TimeSpan timeSpan = new TimeSpan();
            try
            {
                timeSpan = DateTime.Now.Subtract(PostedDate);
                timeSpan.Duration();

                if (timeSpan.Days >= 1)
                {
                    strTimeSpan = timeSpan.Days.ToString() + " days ago";
                }
                else if (timeSpan.Hours >= 1)
                {
                    strTimeSpan = timeSpan.Hours.ToString() + " hours ago";
                }
                else
                {
                    strTimeSpan = timeSpan.Minutes.ToString() + " mins ago";
                }
            }
            catch (Exception ex)
            {
            }

            return strTimeSpan;
        }
开发者ID:rchawla,项目名称:CourseLogicMVC,代码行数:28,代码来源:CourseItemsDAL.cs

示例15: TSpan

		///<summary>Timespans that might be invalid time of day.  Can be + or - and can be up to 800+ hours.  Stored in Oracle as varchar2.  Never encapsulates</summary>
		public static string TSpan(TimeSpan myTimeSpan) {
			if(myTimeSpan==System.TimeSpan.Zero) {
				return "00:00:00"; ;
			}
			try {
				string retval="";
				if(myTimeSpan < System.TimeSpan.Zero) {
					retval+="-";
					myTimeSpan=myTimeSpan.Duration();
				}
				int hours=(myTimeSpan.Days*24)+myTimeSpan.Hours;
				retval+=hours.ToString().PadLeft(2,'0')+":"+myTimeSpan.Minutes.ToString().PadLeft(2,'0')+":"+myTimeSpan.Seconds.ToString().PadLeft(2,'0');
				return retval;
			} 
			catch {
				return "00:00:00";
			}
		}
开发者ID:mnisl,项目名称:OD,代码行数:19,代码来源:POut.cs


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