Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 4c43d7f

Browse files
Fix inaccurate CurrentServerTime
The `Stopwatch.ElapsedTicks` property is _timer_ ticks, which is not the same as the `DateTime.Ticks` according to the note in the remarks section of [the documentation](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch.elapsedticks?view=netcore-3.1#remarks) (and my testing that led to this discovery). Try the following simple c# sample to see the difference: ``` var serverTimeAtStart = DateTime.UtcNow; var startedAt = Stopwatch.StartNew(); Thread.Sleep(5000); var CurrentServerTime = new DateTime(serverTimeAtStart.Ticks + startedAt.ElapsedTicks, DateTimeKind.Utc); var CorrectServerTime = new DateTime(serverTimeAtStart.Ticks + startedAt.Elapsed.Ticks, DateTimeKind.Utc); Debug.WriteLine($"Current: {CurrentServerTime}\r\nCorrect: {CorrectServerTime}\r\nNow: {DateTime.UtcNow}"); ```
1 parent ed2145a commit 4c43d7f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/ServiceStack.Redis/RedisPubSubServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public bool AutoRestart
6060
set => Interlocked.CompareExchange(ref autoRestart, value ? YES : NO, autoRestart);
6161
}
6262

63-
public DateTime CurrentServerTime => new DateTime(serverTimeAtStart.Ticks + startedAt.ElapsedTicks, DateTimeKind.Utc);
63+
public DateTime CurrentServerTime => new DateTime(serverTimeAtStart.Ticks + startedAt.Elapsed.Ticks, DateTimeKind.Utc);
6464

6565
public long BgThreadCount => Interlocked.CompareExchange(ref bgThreadCount, 0, 0);
6666

@@ -582,4 +582,4 @@ public virtual void Dispose()
582582
DisposeHeartbeatTimer();
583583
}
584584
}
585-
}
585+
}

0 commit comments

Comments
 (0)