Table of Contents

Struct Duration

Namespace
DataStax.AstraDB.DataApi.Core
Assembly
DataStax.AstraDB.DataApi.dll

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

months int

The number of months.

days int

The number of days.

nanoseconds long

The number of nanoseconds.

Fields

Zero

A Duration representing zero length (0 months, 0 days, 0 nanoseconds).

public static readonly Duration Zero

Field Value

Duration

Properties

Days

Gets the number of days.

public readonly int Days { get; }

Property Value

int

Months

Gets the number of months.

public readonly int Months { get; }

Property Value

int

Nanoseconds

Gets the number of nanoseconds.

public readonly long Nanoseconds { get; }

Property Value

long

Methods

CompareTo(Duration)

Compares this instance against another Duration instance based on the bytes representation of the values.

public int CompareTo(Duration other)

Parameters

other Duration

Returns

int

Equals(Duration)

Returns true if the value of the Duration is the same.

public bool Equals(Duration other)

Parameters

other Duration

Returns

bool

Equals(object)

Returns true if the value of the Duration is the same.

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

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

timespan TimeSpan

Returns

Duration

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

input string

Returns

Duration

ToIsoString()

A string representation of this duration using ISO-8601 based representation, such as PT8H6M12.345S.

public string ToIsoString()

Returns

string

ToString()

Returns the string representation of the value.

public override string ToString()

Returns

string

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

TimeSpan

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

a Duration
b Duration

Returns

bool

explicit operator Duration(TimeSpan)

Explicitly converts a TimeSpan to a Duration.

public static explicit operator Duration(TimeSpan ts)

Parameters

ts TimeSpan

The TimeSpan to convert.

Returns

Duration

operator !=(Duration, Duration)

Compares value inequality of 2 DateRange instances.

public static bool operator !=(Duration a, Duration b)

Parameters

a Duration
b Duration

Returns

bool