Show / Hide Table of Contents

Struct Timestamp

Represents timestamp.

Implements
IComparable<Timestamp>
IComparisonOperators<Timestamp, Timestamp, bool>
IEqualityOperators<Timestamp, Timestamp, bool>
IAdditionOperators<Timestamp, TimeSpan, Timestamp>
ISubtractionOperators<Timestamp, TimeSpan, Timestamp>
IEquatable<Timestamp>
Inherited Members
object.GetType()
object.Equals(object, object)
object.ReferenceEquals(object, object)
Namespace: DotNext.Diagnostics
Assembly: DotNext.dll
Syntax
public readonly struct Timestamp : IComparable<Timestamp>, IComparisonOperators<Timestamp, Timestamp, bool>, IEqualityOperators<Timestamp, Timestamp, bool>, IAdditionOperators<Timestamp, TimeSpan, Timestamp>, ISubtractionOperators<Timestamp, TimeSpan, Timestamp>, IEquatable<Timestamp>
Remarks

This class can be used as allocation-free alternative to Stopwatch.

Constructors

View Source

Timestamp()

Captures the current point in time.

Declaration
public Timestamp()
View Source

Timestamp(TimeProvider)

Captures the current point in time.

Declaration
public Timestamp(TimeProvider provider)
Parameters
Type Name Description
TimeProvider provider

The time provider.

View Source

Timestamp(TimeSpan)

Constructs timestamp from TimeSpan.

Declaration
public Timestamp(TimeSpan ts)
Parameters
Type Name Description
TimeSpan ts

The point in time.

Exceptions
Type Condition
ArgumentOutOfRangeException

ts is negative.

Properties

View Source

Elapsed

Gets precise difference between the current point in time and this timestamp.

Declaration
public TimeSpan Elapsed { get; }
Property Value
Type Description
TimeSpan
Remarks

This property is always greater than or equal to Zero.

View Source

ElapsedMilliseconds

Gets the total elapsed time measured by the current instance, in milliseconds.

Declaration
public double ElapsedMilliseconds { get; }
Property Value
Type Description
double
View Source

ElapsedTicks

Gets the total elapsed time measured by the current instance, in timer ticks.

Declaration
public long ElapsedTicks { get; }
Property Value
Type Description
long
View Source

IsEmpty

Gets a value indicating that the timestamp is zero.

Declaration
public bool IsEmpty { get; }
Property Value
Type Description
bool
View Source

IsFuture

Gets a value indicating that the current timestamp represents the future point in time.

Declaration
public bool IsFuture { get; }
Property Value
Type Description
bool
View Source

IsPast

Gets a value indicating that the current timestamp represents the past point in time.

Declaration
public bool IsPast { get; }
Property Value
Type Description
bool
View Source

Value

Gets TimeSpan representing this timestamp.

Declaration
public TimeSpan Value { get; }
Property Value
Type Description
TimeSpan
Remarks

This property may return a value with lost precision.

Methods

View Source

CompareExchange(ref Timestamp, Timestamp, Timestamp)

Declaration
public static Timestamp CompareExchange(ref Timestamp location, Timestamp value, Timestamp comparand)
Parameters
Type Name Description
Timestamp location
Timestamp value
Timestamp comparand
Returns
Type Description
Timestamp
View Source

CompareTo(Timestamp)

Compares this timestamp with the given value.

Declaration
public int CompareTo(Timestamp other)
Parameters
Type Name Description
Timestamp other

The timestamp to compare.

Returns
Type Description
int

The result of comparison.

View Source

ElapsedSince(Timestamp)

Gets a difference between two timestamps, in milliseconds.

Declaration
public double ElapsedSince(Timestamp past)
Parameters
Type Name Description
Timestamp past

The timestamp in the past.

Returns
Type Description
double

The number of milliseconds since past.

View Source

ElapsedSince(Timestamp, TimeProvider)

Gets a difference between two timestamps, in milliseconds.

Declaration
public double ElapsedSince(Timestamp past, TimeProvider provider)
Parameters
Type Name Description
Timestamp past

The timestamp in the past.

TimeProvider provider

Time provider.

Returns
Type Description
double

The number of milliseconds since past.

View Source

Equals(Timestamp)

Declaration
public bool Equals(Timestamp other)
Parameters
Type Name Description
Timestamp other
Returns
Type Description
bool
View Source

Equals(object)

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
object obj
Returns
Type Description
bool
Overrides
ValueType.Equals(object)
View Source

GetElapsedMilliseconds(TimeProvider)

Gets the total elapsed time measured by the current instance, in milliseconds.

Declaration
public double GetElapsedMilliseconds(TimeProvider provider)
Parameters
Type Name Description
TimeProvider provider

Time provider.

Returns
Type Description
double

The elapsed time, in ms, between the starting timestamp and the time of this call.

View Source

GetElapsedTicks(TimeProvider)

Gets the total elapsed time measured by the current instance, in timer ticks.

Declaration
public long GetElapsedTicks(TimeProvider provider)
Parameters
Type Name Description
TimeProvider provider

Time provider.

Returns
Type Description
long

The elapsed time, in ticks, between the starting timestamp and the time of this call.

View Source

GetElapsedTime(TimeProvider)

Gets precise difference between the current point in time and this timestamp.

Declaration
public TimeSpan GetElapsedTime(TimeProvider provider)
Parameters
Type Name Description
TimeProvider provider

Time provider.

Returns
Type Description
TimeSpan

The elapsed time between the starting timestamp and the time of this call.

View Source

GetHashCode()

Declaration
public override int GetHashCode()
Returns
Type Description
int
Overrides
ValueType.GetHashCode()
View Source

Refresh(ref Timestamp)

Updates the timestamp to the current point in time and prevents the processor from reordering memory operations.

Declaration
public static void Refresh(ref Timestamp location)
Parameters
Type Name Description
Timestamp location

The location of the timestamp to update.

View Source

Refresh(ref Timestamp, TimeProvider)

Updates the timestamp to the current point in time and prevents the processor from reordering memory operations.

Declaration
public static void Refresh(ref Timestamp location, TimeProvider provider)
Parameters
Type Name Description
Timestamp location

The location of the timestamp to update.

TimeProvider provider

Time provider.

View Source

ToString()

Gets timestamp in the form of the string.

Declaration
public override string ToString()
Returns
Type Description
string

The string representing this timestamp.

Overrides
ValueType.ToString()
View Source

VolatileRead(ref readonly Timestamp)

Reads the timestamp and prevents the processor from reordering memory operations.

Declaration
public static Timestamp VolatileRead(ref readonly Timestamp location)
Parameters
Type Name Description
Timestamp location

The managed pointer to the timestamp.

Returns
Type Description
Timestamp

The value at the specified location.

View Source

VolatileWrite(ref Timestamp, Timestamp)

Writes the timestamp and prevents the processor from reordering memory operations.

Declaration
public static void VolatileWrite(ref Timestamp location, Timestamp newValue)
Parameters
Type Name Description
Timestamp location

The managed pointer to the timestamp.

Timestamp newValue

The value to write.

Operators

View Source

explicit operator TimeSpan(Timestamp)

Gets TimeSpan representing the given timestamp.

Declaration
public static explicit operator TimeSpan(Timestamp stamp)
Parameters
Type Name Description
Timestamp stamp

The timestamp to convert.

Returns
Type Description
TimeSpan
Remarks

This operation leads to loss of precision.

Explicit Interface Implementations

View Source

operator +(Timestamp, TimeSpan)

Adds the specified duration to the timestamp.

Declaration
static Timestamp operator +(Timestamp x, TimeSpan y)
Parameters
Type Name Description
Timestamp x

The timestamp value.

TimeSpan y

The delta.

Returns
Type Description
Timestamp

The modified timestamp.

View Source

operator checked +(Timestamp, TimeSpan)

Adds the specified duration to the timestamp.

Declaration
static Timestamp operator checked +(Timestamp x, TimeSpan y)
Parameters
Type Name Description
Timestamp x

The timestamp value.

TimeSpan y

The delta.

Returns
Type Description
Timestamp

The modified timestamp.

Exceptions
Type Condition
OverflowException

y is too large.

View Source

operator checked -(Timestamp, TimeSpan)

Subtracts the specified duration from the timestamp.

Declaration
static Timestamp operator checked -(Timestamp x, TimeSpan y)
Parameters
Type Name Description
Timestamp x

The timestamp value.

TimeSpan y

The delta.

Returns
Type Description
Timestamp

The modified timestamp.

Exceptions
Type Condition
OverflowException

y is too large.

View Source

operator ==(Timestamp, Timestamp)

Declaration
static bool operator ==(Timestamp left, Timestamp right)
Parameters
Type Name Description
Timestamp left
Timestamp right
Returns
Type Description
bool
View Source

operator >(Timestamp, Timestamp)

Determines whether the first timestamp is greater than the second.

Declaration
static bool operator >(Timestamp first, Timestamp second)
Parameters
Type Name Description
Timestamp first

The first timestamp to compare.

Timestamp second

The second timestamp to compare.

Returns
Type Description
bool

true if first is greater than second.

View Source

operator >=(Timestamp, Timestamp)

Determines whether the first timestamp is greater than or equal to the second.

Declaration
static bool operator >=(Timestamp first, Timestamp second)
Parameters
Type Name Description
Timestamp first

The first timestamp to compare.

Timestamp second

The second timestamp to compare.

Returns
Type Description
bool

true if first is greater than or equal to second.

View Source

operator !=(Timestamp, Timestamp)

Declaration
static bool operator !=(Timestamp left, Timestamp right)
Parameters
Type Name Description
Timestamp left
Timestamp right
Returns
Type Description
bool
View Source

operator <(Timestamp, Timestamp)

Determines whether the first timestamp is less than the second.

Declaration
static bool operator <(Timestamp first, Timestamp second)
Parameters
Type Name Description
Timestamp first

The first timestamp to compare.

Timestamp second

The second timestamp to compare.

Returns
Type Description
bool

true if first is less than second.

View Source

operator <=(Timestamp, Timestamp)

Determines whether the first timestamp is less than or equal to the second.

Declaration
static bool operator <=(Timestamp first, Timestamp second)
Parameters
Type Name Description
Timestamp first

The first timestamp to compare.

Timestamp second

The second timestamp to compare.

Returns
Type Description
bool

true if first is less than or equal to second.

View Source

operator -(Timestamp, TimeSpan)

Subtracts the specified duration from the timestamp.

Declaration
static Timestamp operator -(Timestamp x, TimeSpan y)
Parameters
Type Name Description
Timestamp x

The timestamp value.

TimeSpan y

The delta.

Returns
Type Description
Timestamp

The modified timestamp.

Exceptions
Type Condition
OverflowException

y is too large.

Implements

IComparable<T>
IComparisonOperators<TSelf, TOther, TResult>
IEqualityOperators<TSelf, TOther, TResult>
IAdditionOperators<TSelf, TOther, TResult>
ISubtractionOperators<TSelf, TOther, TResult>
IEquatable<T>

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
TimestampExtensions.IsFuture(Timestamp, TimeProvider)
TimestampExtensions.IsPast(Timestamp, TimeProvider)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾