diff --git a/src/core/Akka.Cluster/ClusterMetricsCollector.cs b/src/core/Akka.Cluster/ClusterMetricsCollector.cs index 6d22fdbcbf1..8853b7e201e 100644 --- a/src/core/Akka.Cluster/ClusterMetricsCollector.cs +++ b/src/core/Akka.Cluster/ClusterMetricsCollector.cs @@ -623,7 +623,7 @@ internal static class StandardMetrics public static long NewTimestamp() { - return DateTime.Now.Ticks; + return DateTime.UtcNow.Ticks; } public sealed class SystemMemory diff --git a/src/core/Akka.Persistence/AtLeastOnceDelivery.cs b/src/core/Akka.Persistence/AtLeastOnceDelivery.cs index 940f05f583f..c4f3371bb2b 100644 --- a/src/core/Akka.Persistence/AtLeastOnceDelivery.cs +++ b/src/core/Akka.Persistence/AtLeastOnceDelivery.cs @@ -209,7 +209,7 @@ public void Deliver(ActorPath destination, Func deliveryMessageMap } var deliveryId = NextDeliverySequenceNr(); - var now = IsRecovering ? DateTime.Now - RedeliverInterval : DateTime.Now; + var now = IsRecovering ? DateTime.UtcNow - RedeliverInterval : DateTime.UtcNow; var delivery = new Delivery(destination, deliveryMessageMapper(deliveryId), now, attempt: 0); if (IsRecovering) @@ -254,7 +254,7 @@ public AtLeastOnceDeliverySnapshot GetDeliverySnapshot() public void SetDeliverySnapshot(AtLeastOnceDeliverySnapshot snapshot) { _deliverySequenceNr = snapshot.DeliveryId; - var now = DateTime.Now; + var now = DateTime.UtcNow; var unconfirmedDeliveries = snapshot.UnconfirmedDeliveries .Select(u => new KeyValuePair(u.DeliveryId, new Delivery(u.Destination, u.Message, now, 0))); @@ -302,7 +302,7 @@ private void Send(long deliveryId, Delivery delivery, DateTime timestamp) private void RedeliverOverdue() { - var now = DateTime.Now; + var now = DateTime.UtcNow; var deadline = now - RedeliverInterval; var warnings = new List(); diff --git a/src/core/Akka.Persistence/Snapshot/SnapshotStore.cs b/src/core/Akka.Persistence/Snapshot/SnapshotStore.cs index 0dbabfd68e4..5b423d25af2 100644 --- a/src/core/Akka.Persistence/Snapshot/SnapshotStore.cs +++ b/src/core/Akka.Persistence/Snapshot/SnapshotStore.cs @@ -42,7 +42,7 @@ protected override bool Receive(object message) else if (message is SaveSnapshot) { var msg = (SaveSnapshot)message; - var metadata = new SnapshotMetadata(msg.Metadata.PersistenceId, msg.Metadata.SequenceNr, DateTime.Now); + var metadata = new SnapshotMetadata(msg.Metadata.PersistenceId, msg.Metadata.SequenceNr, DateTime.UtcNow); SaveAsync(metadata, msg.Snapshot).ContinueWith(t => !t.IsFaulted ? (object)new SaveSnapshotSuccess(metadata) diff --git a/src/core/Akka.Remote/Deadline.cs b/src/core/Akka.Remote/Deadline.cs index 42846e6accb..dc1817e564e 100644 --- a/src/core/Akka.Remote/Deadline.cs +++ b/src/core/Akka.Remote/Deadline.cs @@ -21,12 +21,12 @@ public Deadline(DateTime when) public bool IsOverdue { - get { return DateTime.Now > When; } + get { return DateTime.UtcNow > When; } } public bool HasTimeLeft { - get { return DateTime.Now < When; } + get { return DateTime.UtcNow < When; } } public DateTime When { get; private set; } @@ -34,7 +34,7 @@ public bool HasTimeLeft /// /// Warning: creates a new instance each time it's used /// - public TimeSpan TimeLeft { get { return When - DateTime.Now; } } + public TimeSpan TimeLeft { get { return When - DateTime.UtcNow; } } #region Overrides @@ -60,13 +60,13 @@ public override int GetHashCode() #region Static members /// - /// Returns a deadline that is due + /// Returns a deadline that is due /// public static Deadline Now { get { - return new Deadline(DateTime.Now); + return new Deadline(DateTime.UtcNow); } } diff --git a/src/core/Akka.Tests/Actor/ActorSystemSpec.cs b/src/core/Akka.Tests/Actor/ActorSystemSpec.cs index 7954ce3ec95..2946188e604 100644 --- a/src/core/Akka.Tests/Actor/ActorSystemSpec.cs +++ b/src/core/Akka.Tests/Actor/ActorSystemSpec.cs @@ -11,6 +11,7 @@ using Xunit; using System; using System.Collections.Generic; +using System.Diagnostics; namespace Akka.Tests.Actor { @@ -54,11 +55,10 @@ public void AnActorSystemShouldBeAllowedToBlockUntilExit() { var actorSystem = ActorSystem .Create(Guid.NewGuid().ToString()); - var startTime = DateTime.UtcNow; + var st = Stopwatch.StartNew(); var asyncShutdownTask = Task.Delay(TimeSpan.FromSeconds(1)).ContinueWith(_ => actorSystem.Shutdown()); actorSystem.AwaitTermination(TimeSpan.FromSeconds(2)).ShouldBeTrue(); - var endTime = DateTime.UtcNow; - Assert.True((endTime - startTime).TotalSeconds >= .9); + Assert.True(st.Elapsed.TotalSeconds >= .9); } [Fact] diff --git a/src/core/Akka.Tests/Actor/InboxSpec.cs b/src/core/Akka.Tests/Actor/InboxSpec.cs index bd7ee5e5e3e..56bf16f3edb 100644 --- a/src/core/Akka.Tests/Actor/InboxSpec.cs +++ b/src/core/Akka.Tests/Actor/InboxSpec.cs @@ -138,7 +138,7 @@ public void Inbox_have_a_default_and_custom_timeouts() [Fact] public void Select_WithClient_should_update_Client_and_copy_the_rest_of_the_properties_BUG_427() { - var deadline = new DateTime(1919, 5, 24); + var deadline = new TimeSpan(Sys.Scheduler.MonotonicClock.Ticks/2); //Some point in the past Predicate predicate = o => true; var actorRef = new EmptyLocalActorRef(((ActorSystemImpl)Sys).Provider, new RootActorPath(new Address("akka", "test")), Sys.EventStream); var select = new Select(deadline, predicate, actorRef); diff --git a/src/core/Akka/Actor/ChildrenContainer/Internal/ChildStats.cs b/src/core/Akka/Actor/ChildrenContainer/Internal/ChildStats.cs index 86f2bd0c42d..637d8e68302 100644 --- a/src/core/Akka/Actor/ChildrenContainer/Internal/ChildStats.cs +++ b/src/core/Akka/Actor/ChildrenContainer/Internal/ChildStats.cs @@ -6,6 +6,7 @@ //----------------------------------------------------------------------- using System; +using Akka.Util; namespace Akka.Actor.Internal { @@ -79,7 +80,7 @@ private bool RetriesInWindowOkay(int retries, int windowInMilliseconds) // after a restart and if enough restarts happen during this time, it // denies. Otherwise window closes and the scheme starts over. var retriesDone = _maxNrOfRetriesCount + 1; - var now = DateTime.Now.Ticks; + var now = MonotonicClock.Elapsed.Ticks; long windowStart; if (_restartTimeWindowStartTicks == 0) { diff --git a/src/core/Akka/Actor/Inbox.Actor.cs b/src/core/Akka/Actor/Inbox.Actor.cs index 8274779e29d..8c55d9afffe 100644 --- a/src/core/Akka/Actor/Inbox.Actor.cs +++ b/src/core/Akka/Actor/Inbox.Actor.cs @@ -22,7 +22,7 @@ internal class InboxActor : ActorBase private object _currentMessage; private Select? _currentSelect; - private Tuple _currentDeadline; + private Tuple _currentDeadline; private int _size; private ILoggingAdapter _log = Context.GetLogger(); @@ -120,7 +120,7 @@ protected override bool Receive(object message) .With(sw => Context.Unwatch(sw.Target)) .With(() => { - var now = DateTime.Now; + var now = Context.System.Scheduler.MonotonicClock; var overdue = _clientsByTimeout.TakeWhile(q => q.Deadline < now); foreach (var query in overdue) { @@ -169,7 +169,7 @@ protected override bool Receive(object message) { _currentDeadline.Item2.Cancel(); } - var cancelable = Context.System.Scheduler.ScheduleTellOnceCancelable(next.Deadline - DateTime.Now, Self, new Kick(), Self); + var cancelable = Context.System.Scheduler.ScheduleTellOnceCancelable(next.Deadline - Context.System.Scheduler.MonotonicClock, Self, new Kick(), Self); _currentDeadline = Tuple.Create(next.Deadline, cancelable); } diff --git a/src/core/Akka/Actor/Inbox.cs b/src/core/Akka/Actor/Inbox.cs index cd6b87c7d47..04d80198021 100644 --- a/src/core/Akka/Actor/Inbox.cs +++ b/src/core/Akka/Actor/Inbox.cs @@ -16,21 +16,21 @@ namespace Akka.Actor { internal interface IQuery { - DateTime Deadline { get; } + TimeSpan Deadline { get; } IActorRef Client { get; } IQuery WithClient(IActorRef client); } internal struct Get : IQuery { - public Get(DateTime deadline, IActorRef client = null) + public Get(TimeSpan deadline, IActorRef client = null) : this() { Deadline = deadline; Client = client; } - public DateTime Deadline { get; private set; } + public TimeSpan Deadline { get; private set; } public IActorRef Client { get; private set; } public IQuery WithClient(IActorRef client) { @@ -40,7 +40,7 @@ public IQuery WithClient(IActorRef client) internal struct Select : IQuery { - public Select(DateTime deadline, Predicate predicate, IActorRef client = null) + public Select(TimeSpan deadline, Predicate predicate, IActorRef client = null) : this() { Deadline = deadline; @@ -48,7 +48,7 @@ public Select(DateTime deadline, Predicate predicate, IActorRef client = Client = client; } - public DateTime Deadline { get; private set; } + public TimeSpan Deadline { get; private set; } public Predicate Predicate { get; set; } public IActorRef Client { get; private set; } public IQuery WithClient(IActorRef client) @@ -319,7 +319,7 @@ public object ReceiveWhere(Predicate predicate) public object ReceiveWhere(Predicate predicate, TimeSpan timeout) { - var task = Receiver.Ask(new Select(DateTime.Now + timeout, predicate), Timeout.InfiniteTimeSpan); + var task = Receiver.Ask(new Select(_system.Scheduler.MonotonicClock + timeout, predicate), Timeout.InfiniteTimeSpan); return AwaitResult(task, timeout); } @@ -330,7 +330,7 @@ public Task ReceiveAsync() public Task ReceiveAsync(TimeSpan timeout) { - return Receiver.Ask(new Get(DateTime.Now + timeout), Timeout.InfiniteTimeSpan); + return Receiver.Ask(new Get(_system.Scheduler.MonotonicClock + timeout), Timeout.InfiniteTimeSpan); } public void Dispose() diff --git a/src/core/Akka/Event/LogEvent.cs b/src/core/Akka/Event/LogEvent.cs index 65c6c00e8ea..db890fde1de 100644 --- a/src/core/Akka/Event/LogEvent.cs +++ b/src/core/Akka/Event/LogEvent.cs @@ -21,7 +21,7 @@ public abstract class LogEvent : INoSerializationVerificationNeeded /// protected LogEvent() { - Timestamp = DateTime.Now; + Timestamp = DateTime.UtcNow; Thread = Thread.CurrentThread; } diff --git a/src/examples/TimeServer/TimeServer/Program.cs b/src/examples/TimeServer/TimeServer/Program.cs index 7be2a4a6418..2cbd6845d11 100644 --- a/src/examples/TimeServer/TimeServer/Program.cs +++ b/src/examples/TimeServer/TimeServer/Program.cs @@ -33,7 +33,7 @@ public void Handle(string message) { if (message.ToLowerInvariant() == "gettime") { - var time =DateTime.Now.ToLongTimeString(); + var time =DateTime.UtcNow.ToLongTimeString(); Sender.Tell(time, Self); } else