From 31509b84a5c457e7ffd29f4e35ba97fb0ccffbf3 Mon Sep 17 00:00:00 2001 From: Samuel Meenzen Date: Thu, 4 Jan 2024 20:50:11 +0100 Subject: [PATCH] fix: force utc DateTime always defaults to local time and needs to be explicitly converted to utc. Signed-off-by: Samuel Meenzen --- bindgen/templates/TimestampHelper.cs | 4 ++-- dotnet-tests/UniffiCS.BindingTests/TestChronological.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bindgen/templates/TimestampHelper.cs b/bindgen/templates/TimestampHelper.cs index 6991671..158cc5b 100644 --- a/bindgen/templates/TimestampHelper.cs +++ b/bindgen/templates/TimestampHelper.cs @@ -17,7 +17,7 @@ public override DateTime Read(BigEndianStream stream) { } var ticks = seconds * TimeSpan.TicksPerSecond; ticks += (nanoseconds / NanosecondsPerTick) * sign; - return DateTime.UnixEpoch.AddTicks(ticks); + return DateTime.UnixEpoch.ToUniversalTime().AddTicks(ticks); } public override int AllocationSize(DateTime value) { @@ -26,7 +26,7 @@ public override int AllocationSize(DateTime value) { } public override void Write(DateTime value, BigEndianStream stream) { - var epochOffset = value.Subtract(DateTime.UnixEpoch); + var epochOffset = value.ToUniversalTime().Subtract(DateTime.UnixEpoch); int sign = 1; if (epochOffset.Ticks < 0) { diff --git a/dotnet-tests/UniffiCS.BindingTests/TestChronological.cs b/dotnet-tests/UniffiCS.BindingTests/TestChronological.cs index 0bc3903..408e699 100644 --- a/dotnet-tests/UniffiCS.BindingTests/TestChronological.cs +++ b/dotnet-tests/UniffiCS.BindingTests/TestChronological.cs @@ -45,7 +45,7 @@ public void DateTimeMinMax() { Assert.Equal(DateTime.MinValue, ChronologicalMethods.ReturnTimestamp(DateTime.MinValue)); - Assert.Equal(DateTime.MaxValue, ChronologicalMethods.ReturnTimestamp(DateTime.MaxValue)); + Assert.Equal(DateTime.MaxValue.ToUniversalTime(), ChronologicalMethods.ReturnTimestamp(DateTime.MaxValue)); } [Fact] @@ -77,7 +77,7 @@ public void PreEpochTimestampsSerializeCorrectly() ); Assert.Equal( - DateTime.Parse("1955-11-05T00:06:01.283000200Z"), + DateTime.Parse("1955-11-05T00:06:01.283000200Z").ToUniversalTime(), ChronologicalMethods.Add(DateTime.Parse("1955-11-05T00:06:00.283000100Z"), TimeSpanSecond(1, 100)) ); } @@ -86,11 +86,11 @@ public void PreEpochTimestampsSerializeCorrectly() public void TestDateTimeWorksLikeRustSystemTime() { // Sleep inbetween to make sure that the clock has enough resolution - var before = DateTime.Now; + var before = DateTime.UtcNow; Thread.Sleep(1); var now = ChronologicalMethods.Now(); Thread.Sleep(1); - var after = DateTime.Now; + var after = DateTime.UtcNow; Assert.Equal(-1, before.CompareTo(now)); Assert.Equal(1, after.CompareTo(now)); }