-
Notifications
You must be signed in to change notification settings - Fork 1
/
MonotonicStampContext.cs
288 lines (264 loc) · 13.7 KB
/
MonotonicStampContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using HpTimeStamps.BigMath;
using JetBrains.Annotations;
namespace HpTimeStamps
{
/// <summary>
/// The implementation of <see cref="IMonotonicStampContext"/> this library provides.
/// </summary>
[DataContract]
public readonly struct MonotonicStampContext : IMonotonicStampContext, IEquatable<MonotonicStampContext>, IComparable<MonotonicStampContext>
{
/// <summary>
/// Create a stamp context to serve as reference point for monotonic timestamps
/// </summary>
/// <returns>context value</returns>
public static MonotonicStampContext CreateStampContext()
{
Guid id = Guid.NewGuid();
long refTicks = Stopwatch.GetTimestamp();
DateTime utcTime = DateTime.UtcNow;
DateTime localTime = utcTime.ToLocalTime();
long ticksPerSecond = Stopwatch.Frequency;
if (!Stopwatch.IsHighResolution)
{
throw new MonotonicClockNotAvailableException();
}
return new MonotonicStampContext(in id, utcTime, localTime, refTicks, ticksPerSecond);
}
#region Properties
/// <summary>
/// # nano seconds in a second
/// </summary>
public const long NanosecondsPerSecond = 1_000_000_000;
/// <summary>
/// number of nanoseconds in a second
/// </summary>
public long NanosecondsFrequency => NanosecondsPerSecond;
/// <inheritdoc />
public bool EasyConversionAllWays => EasyConversionToAndFromNanoseconds && EasyConversionToAndFromTimespanTicks;
/// <inheritdoc />
public bool EasyConversionToAndFromTimespanTicks { get; }
/// <inheritdoc />
public bool EasyConversionToAndFromNanoseconds { get; }
/// <inheritdoc />
public bool IsInvalid => ContextId == default;
/// <inheritdoc />
[DataMember] public Guid ContextId { get; }
/// <inheritdoc />
[DataMember] public DateTime UtcDateTimeBeginReference { get; }
/// <inheritdoc />
public DateTime LocalTimeBeginReference =>
DateTime.SpecifyKind(UtcDateTimeBeginReference + UtcLocalTimeOffset, DateTimeKind.Local);
/// <inheritdoc />
[DataMember] public long ReferenceTicks { get; }
/// <inheritdoc />
[DataMember] public long TicksPerSecond { get; }
/// <inheritdoc />
[DataMember] public TimeSpan UtcLocalTimeOffset { get; }
/// <summary>
/// The utc local offset expressed as a duration
/// </summary>
public ref readonly Duration UtcLocalTimeOffsetAsDuration
{
get
{
ref readonly Duration ret = ref Duration.Zero;
switch (_utcOffsetAsDuration?.IsSet)
{
case true:
ret = ref _utcOffsetAsDuration.Value;
break;
case null:
break;
case false:
CalculateValue();
Debug.Assert(_utcOffsetAsDuration.IsSet, "Calculate value should guarantee it is set.");
ret = ref _utcOffsetAsDuration.Value;
break;
}
return ref ret;
}
}
private void CalculateValue()
{
Debug.Assert(_utcOffsetAsDuration != null, "Do not call if null.");
_utcOffsetAsDuration.TrySetValue(Duration.FromStopwatchTicks(((Int128) UtcLocalTimeOffset.Ticks) *
TicksPerSecond / TimeSpan.TicksPerSecond));
Debug.Assert(_utcOffsetAsDuration.IsSet,
"Should always be set after call if no except ... whether set occured on this " +
"thread with the preceding line or on a prior call on another thread.");
}
/// <inheritdoc />
public bool AllTimestampsUtc => UtcDateTimeBeginReference == LocalTimeBeginReference;
#endregion
#region Private CTOR
private MonotonicStampContext(in Guid contextId, DateTime utcBeginRef, DateTime localTimeRef, long refTicks,
long ticksPerSecond)
{
if (utcBeginRef.Kind != DateTimeKind.Utc) throw new ArgumentException(@"Parameter is not a utc datetime.", nameof(utcBeginRef));
if (localTimeRef.Kind != DateTimeKind.Local) throw new ArgumentException(@"Parameter is not a local date time.", nameof(localTimeRef));
if (localTimeRef.ToUniversalTime() != utcBeginRef) throw new ArgumentException($"Parameter {localTimeRef} is not the local variant of parameter {utcBeginRef}.");
ContextId = contextId;
ReferenceTicks = refTicks;
UtcDateTimeBeginReference = utcBeginRef;
UtcLocalTimeOffset = localTimeRef - UtcDateTimeBeginReference;
TicksPerSecond = ticksPerSecond;
EasyConversionToAndFromTimespanTicks = DetermineIsEasyConversion(TicksPerSecond, TimeSpan.TicksPerSecond);
EasyConversionToAndFromNanoseconds = DetermineIsEasyConversion(TicksPerSecond, NanosecondsPerSecond);
if (TicksPerSecond < Duration.MinimumSupportedStopwatchTicksPerSecond)
{
throw new UnsupportedStopwatchResolutionException(TicksPerSecond, Duration.MinimumSupportedStopwatchTicksPerSecond);
}
_utcOffsetAsDuration = new LocklessWriteOnceValue<Duration>();
static bool DetermineIsEasyConversion(long frequencyOne, long frequencyTwo)
{
if (frequencyOne == frequencyTwo) return true;
long bigger = Math.Max(frequencyOne, frequencyTwo);
long smaller = Math.Min(frequencyOne, frequencyTwo);
long tsConvertRem = bigger % smaller;
long tsConvertQuot = bigger / smaller;
return tsConvertRem == 0 && (tsConvertQuot == 1 || tsConvertQuot % 10 == 0);
}
}
#endregion
/// <inheritdoc />
public override string ToString() =>
$"[{nameof(MonotonicStampContext)}] -- Reference Ticks: [{ReferenceTicks:N0}], " +
$"Ticks / second: [{TicksPerSecond:N0}], UtcRef: [{UtcDateTimeBeginReference:O}], " +
$"Local Offset: [{UtcLocalTimeOffset.TotalHours:N6}]";
/// <summary>
/// Get a value-based hash code for this object
/// </summary>
/// <returns>a hash code</returns>
public override int GetHashCode() => ContextId.GetHashCode();
/// <summary>
/// Test another object to see whether it has the same value as this one.
/// </summary>
/// <param name="obj">The other object</param>
/// <returns>True if the other object is of the same type as this one and has
/// the same value as this one. False otherwise.</returns>
public override bool Equals(object obj) => obj is MonotonicStampContext msc && msc == this;
/// <summary>
/// Test to see whether another value of this type has the same value as this one
/// </summary>
/// <param name="other">the other value</param>
/// <returns>true if same value, false otherwise</returns>
public bool Equals(MonotonicStampContext other) => other == this;
/// <summary>
/// Tests two stamp contexts for value equality
/// </summary>
/// <param name="lhs">left hand comparand</param>
/// <param name="rhs">right hand comparand</param>
/// <returns>true if they have the same value, false otherwise</returns>
public static bool operator ==(in MonotonicStampContext lhs, in MonotonicStampContext rhs) =>
lhs.ContextId == rhs.ContextId && lhs.UtcDateTimeBeginReference == rhs.UtcDateTimeBeginReference &&
lhs.ReferenceTicks == rhs.ReferenceTicks && lhs.TicksPerSecond == rhs.TicksPerSecond &&
lhs.UtcLocalTimeOffset == rhs.UtcLocalTimeOffset;
/// <summary>
/// Tests two stamp contexts for value inequality
/// </summary>
/// <param name="lhs">left hand comparand</param>
/// <param name="rhs">right hand comparand</param>
/// <returns>true if they have the distinct values, false otherwise</returns>
public static bool operator !=(in MonotonicStampContext lhs, in MonotonicStampContext rhs) => !(lhs == rhs);
/// <summary>
/// Test to see whether the left hand comparand is greater than (succeeds in sort order) the right hand comparand.
/// </summary>
/// <param name="lhs">The left hand comparand</param>
/// <param name="rhs">the right hand comparand</param>
/// <returns>true if the left hand comparand is greater than the right hand comparand, false otherwise</returns>
public static bool operator >(in MonotonicStampContext lhs, in MonotonicStampContext rhs) =>
Compare(in lhs, in rhs) > 0;
/// <summary>
/// Test to see whether the left hand comparand is less than (precedes in sort order) the right hand comparand.
/// </summary>
/// <param name="lhs">The left hand comparand</param>
/// <param name="rhs">the right hand comparand</param>
/// <returns>true if the left hand comparand is less than the right hand comparand, false otherwise</returns>
public static bool operator <(in MonotonicStampContext lhs, in MonotonicStampContext rhs) =>
Compare(in lhs, in rhs) < 0;
/// <summary>
/// Test to see whether the left hand comparand is greater than (i.e. succeeds in sort-order) or equal to (i.e. is identical
/// with and therefore holds sample place in sort order as) the right hand comparand.
/// </summary>
/// <param name="lhs">The left hand comparand</param>
/// <param name="rhs">the right hand comparand</param>
/// <returns>true if the left hand comparand is greater than or equal to the right hand comparand, false otherwise</returns>
public static bool operator >=(in MonotonicStampContext lhs, in MonotonicStampContext rhs) =>
!(lhs < rhs);
/// <summary>
/// Test to see whether the left hand comparand is less than than (i.e. precedes in sort-order) or equal to (i.e. is identical
/// with and therefore holds sample place in sort order as) the right hand comparand.
/// </summary>
/// <param name="lhs">The left hand comparand</param>
/// <param name="rhs">the right hand comparand</param>
/// <returns>true if the left hand comparand is less than or equal to the right hand comparand, false otherwise</returns>
public static bool operator <=(in MonotonicStampContext lhs, in MonotonicStampContext rhs) =>
!(lhs > rhs);
/// <summary>
/// Compare this context with another to establish the ordering between them
/// </summary>
/// <param name="other">the other</param>
/// <returns>A negative number if the this value precedes the other value in the sort order.
/// Zero if the this value is identical to the other value (and thus occupies same space in sort order).
/// A positive number if this value succeeds the other value in sort order.</returns>
public int CompareTo(MonotonicStampContext other) => Compare(in this, in other);
static MonotonicStampContext()
{
if (!Stopwatch.IsHighResolution)
{
throw new MonotonicClockNotAvailableException();
}
if (Stopwatch.Frequency < Duration.MinimumSupportedStopwatchTicksPerSecond)
{
throw new UnsupportedStopwatchResolutionException(Stopwatch.Frequency,
Duration.MinimumSupportedStopwatchTicksPerSecond);
}
}
/// <summary>
/// Compare two monotonic stamp contexts to establish their
/// positions relative to each other in the sort order.
/// </summary>
/// <param name="lhs">left-hand comparand</param>
/// <param name="rhs">Right-hand comparand</param>
/// <returns>A negative number if the left-hand comparand precedes the right-hand comparand in the sort order.
/// Zero if the left-hand comparand is identical to the right hand comparand (and thus occupies same space in sort order).
/// A positive number if the left-hand comparand succeeds the right-hand comparand in sort order.</returns>
public static int Compare(in MonotonicStampContext lhs, in MonotonicStampContext rhs)
{
int ret;
int idComp = lhs.ContextId.CompareTo(rhs.ContextId);
if (idComp == 0)
{
int utcComp = lhs.UtcDateTimeBeginReference.CompareTo(rhs.UtcDateTimeBeginReference);
if (utcComp == 0)
{
int refTickCount = lhs.ReferenceTicks.CompareTo(rhs.ReferenceTicks);
if (refTickCount == 0)
{
int tpsComp = lhs.TicksPerSecond.CompareTo(rhs.TicksPerSecond);
ret = tpsComp == 0 ? lhs.UtcLocalTimeOffset.CompareTo(rhs.UtcLocalTimeOffset) : tpsComp;
}
else
{
ret = refTickCount;
}
}
else
{
ret = utcComp;
}
}
else
{
ret = idComp;
}
return ret;
}
[DataMember(IsRequired = false, EmitDefaultValue = true)]
private readonly LocklessWriteOnceValue<Duration> _utcOffsetAsDuration;
}
}