Struct Duration
Represents a duration in time, or elapsed time, or a difference between two timestamps.
public readonly struct Duration : IEquatable<Duration>, IComparable<Duration>, IComparable
- Implements
- Inherited Members
Constructors
Duration(int, int, int, int, int, int)
Creates a new Duration instance.
public Duration(int hours, int minutes, int seconds = 0, int milliseconds = 0, int microseconds = 0, int ticks = 0)
Parameters
hoursintNumber of hours.
minutesintNumber of minutes.
secondsintNumber of seconds.
millisecondsintNumber of milliseconds.
microsecondsintNumber of microseconds.
ticksintNumber of ticks.
Duration(long)
Creates a new Duration instance.
public Duration(long ticks)
Parameters
tickslongNumber of ticks.
Duration(TimeSpan)
Creates a new Duration instance.
public Duration(TimeSpan timeSpan)
Parameters
Fields
MaxValue
Specifies minimum possible Duration.
public static readonly Duration MaxValue
Field Value
MinValue
Specifies maximum possible Duration.
public static readonly Duration MinValue
Field Value
Zero
Specifies the Duration of 0 length.
public static readonly Duration Zero
Field Value
Properties
FullHours
Total number of full hours.
public long FullHours { get; }
Property Value
FullMicroseconds
Total number of full microseconds.
public long FullMicroseconds { get; }
Property Value
FullMilliseconds
Total number of full milliseconds.
public long FullMilliseconds { get; }
Property Value
FullMinutes
Total number of full minutes.
public long FullMinutes { get; }
Property Value
FullSeconds
Total number of full seconds.
public long FullSeconds { get; }
Property Value
MicrosecondsInMillisecond
Number of microseconds in the millisecond component.
public int MicrosecondsInMillisecond { get; }
Property Value
MillisecondsInSecond
Number of milliseconds in the second component.
public int MillisecondsInSecond { get; }
Property Value
MinutesInHour
Number of minutes in the hour component.
public int MinutesInHour { get; }
Property Value
SecondsInMinute
Number of seconds in the minute component.
public int SecondsInMinute { get; }
Property Value
Ticks
Total number of ticks. One tick is equivalent to 100 nanoseconds.
public long Ticks { get; }
Property Value
TicksInMicrosecond
Number of ticks in the microsecond component.
public int TicksInMicrosecond { get; }
Property Value
TotalHours
Total number of hours.
public double TotalHours { get; }
Property Value
TotalMicroseconds
Total number of microseconds.
public double TotalMicroseconds { get; }
Property Value
TotalMilliseconds
Total number of milliseconds.
public double TotalMilliseconds { get; }
Property Value
TotalMinutes
Total number of minutes.
public double TotalMinutes { get; }
Property Value
TotalSeconds
Total number of seconds.
public double TotalSeconds { get; }
Property Value
Methods
Abs()
Creates a new Duration instance by calculating an absolute value from this instance.
[Pure]
public Duration Abs()
Returns
Add(Duration)
Creates a new Duration instance by adding other to this instance.
[Pure]
public Duration Add(Duration other)
Parameters
otherDurationOther instance to add.
Returns
AddHours(double)
Creates a new Duration instance by adding the specified number of hours.
[Pure]
public Duration AddHours(double hours)
Parameters
hoursdoubleHours to add.
Returns
AddHours(long)
Creates a new Duration instance by adding the specified number of hours.
[Pure]
public Duration AddHours(long hours)
Parameters
hourslongHours to add.
Returns
AddMicroseconds(double)
Creates a new Duration instance by adding the specified number of microseconds.
[Pure]
public Duration AddMicroseconds(double microseconds)
Parameters
microsecondsdoubleMicroseconds to add.
Returns
AddMicroseconds(long)
Creates a new Duration instance by adding the specified number of microseconds.
[Pure]
public Duration AddMicroseconds(long microseconds)
Parameters
microsecondslongMicroseconds to add.
Returns
AddMilliseconds(double)
Creates a new Duration instance by adding the specified number of milliseconds.
[Pure]
public Duration AddMilliseconds(double milliseconds)
Parameters
millisecondsdoubleMilliseconds to add.
Returns
AddMilliseconds(long)
Creates a new Duration instance by adding the specified number of milliseconds.
[Pure]
public Duration AddMilliseconds(long milliseconds)
Parameters
millisecondslongMilliseconds to add.
Returns
AddMinutes(double)
Creates a new Duration instance by adding the specified number of minutes.
[Pure]
public Duration AddMinutes(double minutes)
Parameters
minutesdoubleMinutes to add.
Returns
AddMinutes(long)
Creates a new Duration instance by adding the specified number of minutes.
[Pure]
public Duration AddMinutes(long minutes)
Parameters
minuteslongMinutes to add.
Returns
AddSeconds(double)
Creates a new Duration instance by adding the specified number of seconds.
[Pure]
public Duration AddSeconds(double seconds)
Parameters
secondsdoubleSeconds to add.
Returns
AddSeconds(long)
Creates a new Duration instance by adding the specified number of seconds.
[Pure]
public Duration AddSeconds(long seconds)
Parameters
secondslongSeconds to add.
Returns
AddTicks(long)
Creates a new Duration instance by adding the specified number of ticks.
[Pure]
public Duration AddTicks(long ticks)
Parameters
tickslongTicks to add.
Returns
CompareTo(Duration)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
[Pure]
public int CompareTo(Duration other)
Parameters
otherDurationAn object to compare with this instance.
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes otherin the sort order.Zero This instance occurs in the same position in the sort order as other.Greater than zero This instance follows otherin the sort order.
CompareTo(object?)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
[Pure]
public int CompareTo(object? obj)
Parameters
objobjectAn object to compare with this instance.
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes objin the sort order.Zero This instance occurs in the same position in the sort order as obj.Greater than zero This instance follows objin the sort order.
Exceptions
- ArgumentException
objis not the same type as this instance.
Divide(double)
Creates a new Duration instance by dividing this instance by the provided divisor.
[Pure]
public Duration Divide(double divisor)
Parameters
divisordoubleValue to divide by.
Returns
Exceptions
- DivideByZeroException
When
divisoris equal to 0.
Equals(Duration)
Indicates whether the current object is equal to another object of the same type.
[Pure]
public bool Equals(Duration other)
Parameters
otherDurationAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
[Pure]
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
FromHours(double)
Creates a new Duration instance.
[Pure]
public static Duration FromHours(double hours)
Parameters
hoursdoubleNumber of hours.
Returns
FromHours(long)
Creates a new Duration instance.
[Pure]
public static Duration FromHours(long hours)
Parameters
hourslongNumber of hours.
Returns
FromMicroseconds(double)
Creates a new Duration instance.
[Pure]
public static Duration FromMicroseconds(double microseconds)
Parameters
microsecondsdoubleNumber of microseconds.
Returns
FromMicroseconds(long)
Creates a new Duration instance.
[Pure]
public static Duration FromMicroseconds(long microseconds)
Parameters
microsecondslongNumber of microseconds.
Returns
FromMilliseconds(double)
Creates a new Duration instance.
[Pure]
public static Duration FromMilliseconds(double milliseconds)
Parameters
millisecondsdoubleNumber of milliseconds.
Returns
FromMilliseconds(long)
Creates a new Duration instance.
[Pure]
public static Duration FromMilliseconds(long milliseconds)
Parameters
millisecondslongNumber of milliseconds.
Returns
FromMinutes(double)
Creates a new Duration instance.
[Pure]
public static Duration FromMinutes(double minutes)
Parameters
minutesdoubleNumber of minutes.
Returns
FromMinutes(long)
Creates a new Duration instance.
[Pure]
public static Duration FromMinutes(long minutes)
Parameters
minuteslongNumber of minutes.
Returns
FromSeconds(double)
Creates a new Duration instance.
[Pure]
public static Duration FromSeconds(double seconds)
Parameters
secondsdoubleNumber of seconds.
Returns
FromSeconds(long)
Creates a new Duration instance.
[Pure]
public static Duration FromSeconds(long seconds)
Parameters
secondslongNumber of seconds.
Returns
FromTicks(long)
Creates a new Duration instance.
[Pure]
public static Duration FromTicks(long ticks)
Parameters
tickslongNumber of ticks.
Returns
GetHashCode()
Returns the hash code for this instance.
[Pure]
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Multiply(Percent)
Creates a new Duration instance by multiplying this instance by the provided percent.
[Pure]
public Duration Multiply(Percent percent)
Parameters
percentPercentPercent to multiply by.
Returns
Multiply(double)
Creates a new Duration instance by multiplying this instance by the provided multiplier.
[Pure]
public Duration Multiply(double multiplier)
Parameters
multiplierdoubleValue to multiply by.
Returns
Negate()
Creates a new Duration instance by negating this instance.
[Pure]
public Duration Negate()
Returns
SetHours(long)
Creates a new Duration instance by setting the number of hours.
[Pure]
public Duration SetHours(long value)
Parameters
valuelongNew value.
Returns
SetMicrosecondsInMillisecond(int)
Creates a new Duration instance by setting the number of microseconds in the millisecond component.
[Pure]
public Duration SetMicrosecondsInMillisecond(int value)
Parameters
valueintNew value.
Returns
Exceptions
- ArgumentOutOfRangeException
When
valueis not in a valid range.
SetMillisecondsInSecond(int)
Creates a new Duration instance by setting the number of milliseconds in the second component.
[Pure]
public Duration SetMillisecondsInSecond(int value)
Parameters
valueintNew value.
Returns
Exceptions
- ArgumentOutOfRangeException
When
valueis not in a valid range.
SetMinutesInHour(int)
Creates a new Duration instance by setting the number of minutes in the hour component.
[Pure]
public Duration SetMinutesInHour(int value)
Parameters
valueintNew value.
Returns
Exceptions
- ArgumentOutOfRangeException
When
valueis not in a valid range.
SetSecondsInMinute(int)
Creates a new Duration instance by setting the number of seconds in the minute component.
[Pure]
public Duration SetSecondsInMinute(int value)
Parameters
valueintNew value.
Returns
Exceptions
- ArgumentOutOfRangeException
When
valueis not in a valid range.
SetTicksInMicrosecond(int)
Creates a new Duration instance by setting the number of ticks in the microsecond component.
[Pure]
public Duration SetTicksInMicrosecond(int value)
Parameters
valueintNew value.
Returns
Exceptions
- ArgumentOutOfRangeException
When
valueis not in a valid range.
Subtract(Duration)
Creates a new Duration instance by subtracting other from this instance.
[Pure]
public Duration Subtract(Duration other)
Parameters
otherDurationOther instance to subtract.
Returns
SubtractHours(double)
Creates a new Duration instance by subtracting the specified number of hours.
[Pure]
public Duration SubtractHours(double hours)
Parameters
hoursdoubleHours to subtract.
Returns
SubtractHours(long)
Creates a new Duration instance by subtracting the specified number of hours.
[Pure]
public Duration SubtractHours(long hours)
Parameters
hourslongHours to subtract.
Returns
SubtractMicroseconds(double)
Creates a new Duration instance by subtracting the specified number of microseconds.
[Pure]
public Duration SubtractMicroseconds(double microseconds)
Parameters
microsecondsdoubleMicroseconds to subtract.
Returns
SubtractMicroseconds(long)
Creates a new Duration instance by subtracting the specified number of microseconds.
[Pure]
public Duration SubtractMicroseconds(long microseconds)
Parameters
microsecondslongMicroseconds to subtract.
Returns
SubtractMilliseconds(double)
Creates a new Duration instance by subtracting the specified number of milliseconds.
[Pure]
public Duration SubtractMilliseconds(double milliseconds)
Parameters
millisecondsdoubleMilliseconds to subtract.
Returns
SubtractMilliseconds(long)
Creates a new Duration instance by subtracting the specified number of milliseconds.
[Pure]
public Duration SubtractMilliseconds(long milliseconds)
Parameters
millisecondslongMilliseconds to subtract.
Returns
SubtractMinutes(double)
Creates a new Duration instance by subtracting the specified number of minutes.
[Pure]
public Duration SubtractMinutes(double minutes)
Parameters
minutesdoubleMinutes to subtract.
Returns
SubtractMinutes(long)
Creates a new Duration instance by subtracting the specified number of minutes.
[Pure]
public Duration SubtractMinutes(long minutes)
Parameters
minuteslongMinutes to subtract.
Returns
SubtractSeconds(double)
Creates a new Duration instance by subtracting the specified number of seconds.
[Pure]
public Duration SubtractSeconds(double seconds)
Parameters
secondsdoubleSeconds to subtract.
Returns
SubtractSeconds(long)
Creates a new Duration instance by subtracting the specified number of seconds.
[Pure]
public Duration SubtractSeconds(long seconds)
Parameters
secondslongSeconds to subtract.
Returns
SubtractTicks(long)
Creates a new Duration instance by subtracting the specified number of ticks.
[Pure]
public Duration SubtractTicks(long ticks)
Parameters
tickslongTicks to subtract.
Returns
ToString()
Returns a string representation of this Duration instance.
[Pure]
public override string ToString()
Returns
- string
String representation.
TrimToHour()
Creates a new Duration instance by truncating this instance to hours.
[Pure]
public Duration TrimToHour()
Returns
TrimToMicrosecond()
Creates a new Duration instance by truncating this instance to microseconds.
[Pure]
public Duration TrimToMicrosecond()
Returns
TrimToMillisecond()
Creates a new Duration instance by truncating this instance to milliseconds.
[Pure]
public Duration TrimToMillisecond()
Returns
TrimToMinute()
Creates a new Duration instance by truncating this instance to minutes.
[Pure]
public Duration TrimToMinute()
Returns
TrimToSecond()
Creates a new Duration instance by truncating this instance to seconds.
[Pure]
public Duration TrimToSecond()
Returns
Operators
operator +(Duration, Duration)
Creates a new Duration instance by adding a and b together.
[Pure]
public static Duration operator +(Duration a, Duration b)
Parameters
Returns
operator /(Duration, double)
Creates a new Duration instance by dividing a by b.
[Pure]
public static Duration operator /(Duration a, double b)
Parameters
Returns
Exceptions
- DivideByZeroException
When
bis equal to 0.
operator ==(Duration, Duration)
Checks if a is equal to b.
[Pure]
public static bool operator ==(Duration a, Duration b)
Parameters
Returns
- bool
true when operands are equal, otherwise false.
operator >(Duration, Duration)
Checks if a is greater than b.
[Pure]
public static bool operator >(Duration a, Duration b)
Parameters
Returns
- bool
true when
ais greater thanb, otherwise false.
operator >=(Duration, Duration)
Checks if a is greater than or equal to b.
[Pure]
public static bool operator >=(Duration a, Duration b)
Parameters
Returns
- bool
true when
ais greater than or equal tob, otherwise false.
implicit operator FloatingDuration(Duration)
Converts the provided duration to FloatingDuration.
[Pure]
public static implicit operator FloatingDuration(Duration d)
Parameters
dDurationValue to convert.
Returns
- FloatingDuration
New FloatingDuration instance.
implicit operator TimeSpan(Duration)
Converts the provided duration to TimeSpan.
[Pure]
public static implicit operator TimeSpan(Duration d)
Parameters
dDurationValue to convert.
Returns
operator !=(Duration, Duration)
Checks if a is not equal to b.
[Pure]
public static bool operator !=(Duration a, Duration b)
Parameters
Returns
- bool
true when operands are not equal, otherwise false.
operator <(Duration, Duration)
Checks if a is less than b.
[Pure]
public static bool operator <(Duration a, Duration b)
Parameters
Returns
- bool
true when
ais less thanb, otherwise false.
operator <=(Duration, Duration)
Checks if a is less than or equal to b.
[Pure]
public static bool operator <=(Duration a, Duration b)
Parameters
Returns
- bool
true when
ais less than or equal tob, otherwise false.
operator *(Duration, Percent)
Creates a new Duration instance by multiplying a and b together.
[Pure]
public static Duration operator *(Duration a, Percent b)
Parameters
Returns
operator *(Duration, double)
Creates a new Duration instance by multiplying a and b together.
[Pure]
public static Duration operator *(Duration a, double b)
Parameters
Returns
operator *(Percent, Duration)
Creates a new Duration instance by multiplying a and b together.
[Pure]
public static Duration operator *(Percent a, Duration b)
Parameters
Returns
operator -(Duration, Duration)
Creates a new Duration instance by subtracting b from a.
[Pure]
public static Duration operator -(Duration a, Duration b)
Parameters
Returns
operator -(Duration)
Creates a new Duration instance by negating the provided a.
[Pure]
public static Duration operator -(Duration a)
Parameters
aDurationOperand.