/*
* NatCorder
* Copyright (c) 2020 Yusuf Olokoba.
*/
namespace NatSuite.Recorders.Clocks {
using System;
using System.Runtime.CompilerServices;
using Stopwatch = System.Diagnostics.Stopwatch;
///
/// Realtime clock for generating timestamps
///
public sealed class RealtimeClock : IClock {
#region --Client API--
///
/// Current timestamp in nanoseconds.
/// The very first value reported by this property will always be zero.
///
public long timestamp {
[MethodImpl(MethodImplOptions.Synchronized)]
get {
var time = stopwatch.Elapsed.Ticks * 100L;
if (!stopwatch.IsRunning)
stopwatch.Start();
return time;
}
}
///
/// Is the clock paused?
///
public bool paused {
[MethodImpl(MethodImplOptions.Synchronized)] get => !stopwatch.IsRunning;
[MethodImpl(MethodImplOptions.Synchronized)] set => (value ? (Action)stopwatch.Stop : stopwatch.Start)();
}
///
/// Create a realtime clock.
///
public RealtimeClock () => this.stopwatch = new Stopwatch();
#endregion
private readonly Stopwatch stopwatch;
}
}