本文整理汇总了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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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));
}
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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";
}
}