Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force utc when converting DateTime values #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindgen/templates/TimestampHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions dotnet-tests/UniffiCS.BindingTests/TestChronological.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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))
);
}
Expand All @@ -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));
}
Expand Down