diff --git a/Iceshrimp.Backend/Core/Federation/Cryptography/HttpSignature.cs b/Iceshrimp.Backend/Core/Federation/Cryptography/HttpSignature.cs index 927e64c7..05e91326 100644 --- a/Iceshrimp.Backend/Core/Federation/Cryptography/HttpSignature.cs +++ b/Iceshrimp.Backend/Core/Federation/Cryptography/HttpSignature.cs @@ -93,7 +93,7 @@ public static class HttpSignature { ArgumentNullException.ThrowIfNull(request.RequestUri); - request.Headers.Date = DateTime.Now; + request.Headers.Date = DateTimeOffset.UtcNow; request.Headers.Host = request.RequestUri.Host; var requiredHeadersEnum = requiredHeaders.ToList(); diff --git a/Iceshrimp.Backend/Core/Federation/Cryptography/LdSignature.cs b/Iceshrimp.Backend/Core/Federation/Cryptography/LdSignature.cs index 8112ad85..59bc0065 100644 --- a/Iceshrimp.Backend/Core/Federation/Cryptography/LdSignature.cs +++ b/Iceshrimp.Backend/Core/Federation/Cryptography/LdSignature.cs @@ -57,7 +57,7 @@ public static class LdSignature { var options = new SignatureOptions { - Created = DateTime.Now, + Created = DateTime.UtcNow, Creator = new ASObjectBase(creator), Nonce = CryptographyHelpers.GenerateRandomHexString(16), Type = ["_:RsaSignature2017"], diff --git a/Iceshrimp.Backend/Core/Middleware/RequestDurationMiddleware.cs b/Iceshrimp.Backend/Core/Middleware/RequestDurationMiddleware.cs index 6c71f72c..75daa681 100644 --- a/Iceshrimp.Backend/Core/Middleware/RequestDurationMiddleware.cs +++ b/Iceshrimp.Backend/Core/Middleware/RequestDurationMiddleware.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Globalization; namespace Iceshrimp.Backend.Core.Middleware; @@ -8,10 +9,10 @@ public class RequestDurationMiddleware : IMiddleware { if (ctx.GetEndpoint()?.Metadata.GetMetadata() == null) { - var pre = DateTime.Now; + var pre = Stopwatch.GetTimestamp(); ctx.Response.OnStarting(() => { - var duration = (int)(DateTime.Now - pre).TotalMilliseconds; + var duration = Math.Truncate(Stopwatch.GetElapsedTime(pre).TotalMilliseconds); ctx.Response.Headers.Append("X-Request-Duration", duration.ToString(CultureInfo.InvariantCulture) + " ms"); return Task.CompletedTask; diff --git a/Iceshrimp.Tests/Concurrency/EventTests.cs b/Iceshrimp.Tests/Concurrency/EventTests.cs index b6312c00..d8b614d3 100644 --- a/Iceshrimp.Tests/Concurrency/EventTests.cs +++ b/Iceshrimp.Tests/Concurrency/EventTests.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using Iceshrimp.Backend.Core.Helpers; namespace Iceshrimp.Tests.Concurrency; @@ -9,7 +10,7 @@ public class EventTests public async Task TestAsyncAutoResetEvent() { var autoResetEvent = new AsyncAutoResetEvent(); - var pre = DateTime.Now; + var pre = Stopwatch.GetTimestamp(); var task = autoResetEvent.WaitAsync(); _ = Task.Run(async () => { @@ -17,7 +18,7 @@ public class EventTests autoResetEvent.Set(); }); await task; - (DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); + Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); autoResetEvent.Signaled.Should().BeFalse(); } @@ -25,7 +26,7 @@ public class EventTests public async Task TestAsyncAutoResetEventWithoutReset() { var autoResetEvent = new AsyncAutoResetEvent(); - var pre = DateTime.Now; + var pre = Stopwatch.GetTimestamp(); var task = autoResetEvent.WaitWithoutResetAsync(); _ = Task.Run(async () => { @@ -33,7 +34,7 @@ public class EventTests autoResetEvent.Set(); }); await task; - (DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); + Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); autoResetEvent.Signaled.Should().BeTrue(); } @@ -41,7 +42,7 @@ public class EventTests public async Task TestAsyncAutoResetEventMulti() { var autoResetEvent = new AsyncAutoResetEvent(); - var pre = DateTime.Now; + var pre = Stopwatch.GetTimestamp(); Task[] tasks = [autoResetEvent.WaitAsync(), autoResetEvent.WaitAsync()]; _ = Task.Run(async () => { @@ -49,7 +50,7 @@ public class EventTests autoResetEvent.Set(); }); await Task.WhenAll(tasks); - (DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); + Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); autoResetEvent.Signaled.Should().BeFalse(); } @@ -57,7 +58,7 @@ public class EventTests public async Task TestAsyncAutoResetEventWithoutResetMulti() { var autoResetEvent = new AsyncAutoResetEvent(); - var pre = DateTime.Now; + var pre = Stopwatch.GetTimestamp(); Task[] tasks = [autoResetEvent.WaitWithoutResetAsync(), autoResetEvent.WaitWithoutResetAsync()]; _ = Task.Run(async () => { @@ -65,7 +66,7 @@ public class EventTests autoResetEvent.Set(); }); await Task.WhenAll(tasks); - (DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); + Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45)); autoResetEvent.Signaled.Should().BeTrue(); } diff --git a/Iceshrimp.Tests/Parsing/MfmTests.cs b/Iceshrimp.Tests/Parsing/MfmTests.cs index 7ca8e6b6..974a25cb 100644 --- a/Iceshrimp.Tests/Parsing/MfmTests.cs +++ b/Iceshrimp.Tests/Parsing/MfmTests.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using Iceshrimp.Backend.Core.Helpers.LibMfm.Serialization; using Iceshrimp.Parsing; using Microsoft.FSharp.Collections; @@ -376,10 +377,9 @@ public class MfmTests double RunBenchmark() { - var pre = DateTime.Now; + var pre = Stopwatch.GetTimestamp(); Mfm.parse(mfm); - var post = DateTime.Now; - var ms = (post - pre).TotalMilliseconds; + var ms = Stopwatch.GetElapsedTime(pre).TotalMilliseconds; Console.WriteLine($@"Took {ms} ms"); return ms; }