Timestamp
public struct Timestamp : IComparable<Timestamp>, IComparisonOperators<Timestamp, Timestamp, bool>, IEqualityOperators<Timestamp, Timestamp, bool>, IAdditionOperators<Timestamp, TimeSpan, Timestamp>, ISubtractionOperators<Timestamp, TimeSpan, Timestamp>, IInterlockedOperations<Timestamp>, IEquatable<Timestamp>
Represents timestamp.
using DotNext.Threading;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace DotNext.Diagnostics
{
public readonly struct Timestamp : IComparable<Timestamp>, IInterlockedOperations<Timestamp>, IEquatable<Timestamp>
{
private static readonly double TickFrequency = GetTickFrequency(TimeProvider.get_System());
private readonly long ticks;
public bool IsEmpty => ticks == 0;
public bool IsFuture => ticks > TimeProvider.get_System().GetTimestamp();
public bool IsPast => ticks < TimeProvider.get_System().GetTimestamp();
public TimeSpan Value => new TimeSpan(ToTicks((double)ticks));
public TimeSpan Elapsed => GetElapsedTime(TimeProvider.get_System());
public long ElapsedTicks => GetElapsedTicks(TimeProvider.get_System());
public double ElapsedMilliseconds => GetElapsedMilliseconds(TimeProvider.get_System());
private Timestamp(long ticks)
{
this.ticks = ticks;
}
public Timestamp()
{
this = new Timestamp(TimeProvider.get_System());
}
public Timestamp(TimeSpan ts)
{
if (!(ts >= TimeSpan.Zero))
throw new ArgumentOutOfRangeException("ts");
this = new Timestamp(FromTimeSpan(ts));
}
[NullableContext(1)]
public Timestamp(TimeProvider provider)
{
this = new Timestamp(Math.Max(provider.GetTimestamp(), 1));
}
private static double GetTickFrequency(TimeProvider provider)
{
return 10000000 / (double)provider.get_TimestampFrequency();
}
private static long ToTicks(double duration)
{
return (long)(TickFrequency * duration);
}
private static long ToTicks(double duration, TimeProvider provider)
{
return (long)(GetTickFrequency(provider) * duration);
}
private static long FromTimeSpan(TimeSpan value)
{
return (long)((double)value.Ticks / TickFrequency);
}
[NullableContext(1)]
public TimeSpan GetElapsedTime(TimeProvider provider)
{
return new TimeSpan(ToTicks((double)GetElapsedTicks(provider), provider));
}
[NullableContext(1)]
public long GetElapsedTicks(TimeProvider provider)
{
return Math.Max(1, provider.GetTimestamp() - ticks);
}
[NullableContext(1)]
public double GetElapsedMilliseconds(TimeProvider provider)
{
return (double)GetElapsedTicks(provider) / (double)provider.get_TimestampFrequency() * 1000;
}
public double ElapsedSince(Timestamp past)
{
return ElapsedSince(past, TimeProvider.get_System());
}
[NullableContext(1)]
public double ElapsedSince(Timestamp past, TimeProvider provider)
{
return (double)(ticks - past.ticks) / (double)provider.get_TimestampFrequency() * 1000;
}
public static explicit operator TimeSpan(Timestamp stamp)
{
return stamp.Value;
}
public int CompareTo(Timestamp other)
{
return ticks.CompareTo(other.ticks);
}
[NullableContext(1)]
public override string ToString()
{
return Value.ToString();
}
public static bool operator >(Timestamp first, Timestamp second)
{
return first.ticks > second.ticks;
}
public static bool operator <(Timestamp first, Timestamp second)
{
return first.ticks < second.ticks;
}
public static bool operator >=(Timestamp first, Timestamp second)
{
return first.ticks >= second.ticks;
}
public static bool operator <=(Timestamp first, Timestamp second)
{
return first.ticks <= second.ticks;
}
public static Timestamp op_CheckedAddition(Timestamp x, TimeSpan y)
{
long num = checked(x.ticks + FromTimeSpan(y));
if (num < 0)
throw new OverflowException();
return new Timestamp(num);
}
public static Timestamp operator +(Timestamp x, TimeSpan y)
{
long num = x.ticks + FromTimeSpan(y);
if (num <= 0)
return default(Timestamp);
return new Timestamp(num);
}
public static Timestamp op_CheckedSubtraction(Timestamp x, TimeSpan y)
{
long num = checked(x.ticks - FromTimeSpan(y));
if (num < 0)
throw new OverflowException();
return new Timestamp(num);
}
public static Timestamp operator -(Timestamp x, TimeSpan y)
{
long num = x.ticks - FromTimeSpan(y);
if (num <= 0)
return default(Timestamp);
return new Timestamp(num);
}
public static Timestamp VolatileRead([In] [RequiresLocation] ref Timestamp location)
{
return new Timestamp(Volatile.Read(ref location.ticks));
}
public static void VolatileWrite(ref Timestamp location, Timestamp newValue)
{
Volatile.Write(ref Unsafe.AsRef<long>(ref location.ticks), newValue.ticks);
}
void IInterlockedOperations<Timestamp>.VolatileWrite(ref Timestamp location, Timestamp newValue)
{
this.VolatileWrite(ref location, newValue);
}
public static void Refresh(ref Timestamp location)
{
Refresh(ref location, TimeProvider.get_System());
}
[NullableContext(1)]
public static void Refresh(ref Timestamp location, TimeProvider provider)
{
Volatile.Write(ref Unsafe.AsRef<long>(ref location.ticks), Math.Max(1, provider.GetTimestamp()));
}
public static Timestamp CompareExchange(ref Timestamp location, Timestamp value, Timestamp comparand)
{
return new Timestamp(Interlocked.CompareExchange(ref Unsafe.AsRef<long>(ref location.ticks), value.ticks, comparand.ticks));
}
Timestamp IInterlockedOperations<Timestamp>.CompareExchange(ref Timestamp location, Timestamp value, Timestamp comparand)
{
return this.CompareExchange(ref location, value, comparand);
}
[CompilerGenerated]
private bool PrintMembers(StringBuilder builder)
{
builder.Append("IsEmpty = ");
bool flag = IsEmpty;
builder.Append(flag.ToString());
builder.Append(", IsFuture = ");
flag = IsFuture;
builder.Append(flag.ToString());
builder.Append(", IsPast = ");
flag = IsPast;
builder.Append(flag.ToString());
builder.Append(", Value = ");
TimeSpan timeSpan = Value;
builder.Append(timeSpan.ToString());
builder.Append(", Elapsed = ");
timeSpan = Elapsed;
builder.Append(timeSpan.ToString());
builder.Append(", ElapsedTicks = ");
builder.Append(ElapsedTicks.ToString());
builder.Append(", ElapsedMilliseconds = ");
builder.Append(ElapsedMilliseconds.ToString());
return true;
}
public static bool operator !=(Timestamp left, Timestamp right)
{
return !(left == right);
}
public static bool operator ==(Timestamp left, Timestamp right)
{
return left.Equals(right);
}
[CompilerGenerated]
public override int GetHashCode()
{
return EqualityComparer<long>.Default.GetHashCode(ticks);
}
[CompilerGenerated]
public override bool Equals(object obj)
{
if (obj is Timestamp)
return Equals((Timestamp)obj);
return false;
}
[CompilerGenerated]
public bool Equals(Timestamp other)
{
return EqualityComparer<long>.Default.Equals(ticks, other.ticks);
}
static Timestamp IInterlockedOperations<Timestamp>.VolatileRead([In] [RequiresLocation] Timestamp location)
{
return VolatileRead(ref location);
}
}
}