-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonotonicDateTime.cs
53 lines (42 loc) · 1.52 KB
/
MonotonicDateTime.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
using System.Diagnostics;
namespace Searcher;
/// <summary>
/// A monotonic clock that returns the current date and time, or miliseconds as a long
/// </summary>
public sealed class MonotonicDateTime
{
/// <summary>
/// DateTimeOffset when the clock was started (this includes the time zone)
/// </summary>
private readonly DateTimeOffset start;
/// <summary>
/// Stopwatch is a monotonic timer, but doesnt show wall-clock time
/// </summary>
private readonly Stopwatch watch = Stopwatch.StartNew();
public MonotonicDateTime() => start = DateTimeOffset.Now;
public MonotonicDateTime(DateTimeOffset dateTimeOffset) => start = dateTimeOffset;
/// <summary>
/// A monotonic clock that returns the current local date and time.
/// </summary>
public DateTimeOffset Now => start.Add(watch.Elapsed);
/// <summary>
/// A monotonic clock that returns the current UTC DateTime.
/// </summary>
public DateTime NowUTC => start.Add(watch.Elapsed).UtcDateTime;
/// <summary>
/// Monotonic Seconds, just from the Stopwatch
/// </summary>
public double Seconds => watch.Elapsed.TotalSeconds;
/// <summary>
/// Monotonic milliseconds, just from the Stopwatch
/// </summary>
public long Milliseconds => watch.ElapsedMilliseconds;
/// <summary>
/// Monotonic timespan, just from the Stopwatch.
/// </summary>
public TimeSpan TimeSpan => watch.Elapsed;
/// <summary>
/// Monotonic ticks, just from the Stopwatch.
/// </summary>
public long ElapsedTicks => watch.ElapsedTicks;
}