Struct Duration
Represents a duration. A duration stores separately months, days, and seconds due to the fact that the number of days in a month varies, and a day can have 23 or 25 hours if a daylight saving is involved.
public struct Duration : IEquatable<Duration>, IComparable<Duration>
- Implements
- Inherited Members
Remarks
Duration instances are immutable and thread-safe.
Constructors
Duration(int, int, long)
Creates a new instance of Duration.
public Duration(int months, int days, long nanoseconds)
Parameters
monthsintThe number of months.
daysintThe number of days.
nanosecondslongThe number of nanoseconds.
Fields
Zero
A Duration representing zero length (0 months, 0 days, 0 nanoseconds).
public static readonly Duration Zero
Field Value
Properties
Days
Gets the number of days.
public readonly int Days { get; }
Property Value
Months
Gets the number of months.
public readonly int Months { get; }
Property Value
Nanoseconds
Gets the number of nanoseconds.
public readonly long Nanoseconds { get; }
Property Value
Methods
CompareTo(Duration)
Compares this instance against another Duration instance based on the bytes representation of the values.
public int CompareTo(Duration other)
Parameters
otherDuration
Returns
Equals(Duration)
Returns true if the value of the Duration is the same.
public bool Equals(Duration other)
Parameters
otherDuration
Returns
Equals(object)
Returns true if the value of the Duration is the same.
public override bool Equals(object obj)
Parameters
objobject
Returns
FromTimeSpan(TimeSpan)
Creates a new Duration instance based on the TimeSpan provided.
Consider that 24 hour days (no leap seconds) are used to calculate the days portion.
public static Duration FromTimeSpan(TimeSpan timespan)
Parameters
timespanTimeSpan
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Parse(string)
Creates a new <xref href="DataStax.AstraDB.DataApi.Core.Duration" data-throw-if-not-resolved="false"></xref> instance from the string representation of the value.
<p>Accepted formats:</p>
<ul>
<li>multiple digits followed by a time unit like: 12h30m where the time unit can be:
<ul>
<li><code>y</code>: years</li>
<li><code>m</code>: months</li>
<li><code>w</code>: weeks</li>
<li><code>d</code>: days</li>
<li><code>h</code>: hours</li>
<li><code>m</code>: minutes</li>
<li><code>s</code>: seconds</li>
<li><code>ms</code>: milliseconds</li>
<li><code>us</code> or <code>µs</code>: microseconds</li>
<li><code>ns</code>: nanoseconds</li>
</ul>
</li>
<li>ISO 8601 format:
P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W
<li>ISO 8601 alternative format:
P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]
</ul>
public static Duration Parse(string input)
Parameters
inputstring
Returns
ToIsoString()
A string representation of this duration using ISO-8601 based representation, such as PT8H6M12.345S.
public string ToIsoString()
Returns
ToString()
Returns the string representation of the value.
public override string ToString()
Returns
ToTimeSpan()
Returns a TimeSpan instance that represents the same interval as this instance.
You should take into consideration that TimeSpan is internally represented in ticks, so for the conversion, 24h days will be used (leap seconds are not considered). For Duration values with month portion, it will throw an InvalidOperationException.
public TimeSpan ToTimeSpan()
Returns
Exceptions
- InvalidOperationException
values including month portion.
Operators
operator ==(Duration, Duration)
Compares value equality of 2 DateRange instances.
public static bool operator ==(Duration a, Duration b)
Parameters
Returns
explicit operator Duration(TimeSpan)
public static explicit operator Duration(TimeSpan ts)
Parameters
Returns
operator !=(Duration, Duration)
Compares value inequality of 2 DateRange instances.
public static bool operator !=(Duration a, Duration b)