Skip to content

Commit

Permalink
Merge pull request #344 from betwixt-labs/cs-date-fix
Browse files Browse the repository at this point in the history
fix(cs/serialization): Improve precision in date serialization
  • Loading branch information
andrewmd5 authored Aug 9, 2024
2 parents 8044070 + dde054a commit 1a78cd6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION="3.0.12"
VERSION="3.0.13"
MAJOR=3
MINOR=0
PATCH=12
PATCH=13
2 changes: 2 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: 9.0.x
dotnet-quality: 'preview'

- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
Expand Down
13 changes: 8 additions & 5 deletions Laboratory/C#/Test/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public void Setup()
[Test]
public void WriteRead()
{
var testBytes = new byte[] {0x1, 0x2, 0x3};
var testFloats = new float[] {float.MinValue, float.MaxValue};
var testBytes = new byte[] { 0x1, 0x2, 0x3 };
var testFloats = new float[] { float.MinValue, float.MaxValue };
var testDoubles = new double[] { double.MinValue, double.MaxValue };
var testGuid = Guid.Parse("81c6987b-48b7-495f-ad01-ec20cc5f5be1");
const string testString = @"Hello 明 World!😊";
Expand Down Expand Up @@ -65,7 +65,7 @@ public void WriteRead()
// test float / double
Assert.AreEqual(float.MaxValue, output.ReadFloat32());
Assert.AreEqual(double.MaxValue, output.ReadFloat64());
// test float array
// test float array
var floatArrayLength = output.ReadUInt32();
Assert.AreEqual(testFloats.Length, floatArrayLength);
var parsedFloats = new float[floatArrayLength];
Expand All @@ -88,7 +88,10 @@ public void WriteRead()
// test guid
Assert.AreEqual(testGuid, output.ReadGuid());
// test date
Assert.AreEqual(testDate, output.ReadDate());
var readDate = output.ReadDate();
Console.WriteLine($"Read date: {readDate:O}");
Assert.That(readDate, Is.EqualTo(testDate).Within(TimeSpan.FromMilliseconds(1)),
$"Dates should match within 1ms. Difference: {(readDate - testDate).TotalMilliseconds}ms");
// test byte array
CollectionAssert.AreEqual(testBytes, output.ReadBytes());

Expand All @@ -109,7 +112,7 @@ public void RoundTrip()
new Musician {Name = "Miles Davis", Plays = Instrument.Trumpet}
}
};
var library = new Library {Songs = new Dictionary<Guid, Song> {{testGuid, song}}};
var library = new Library { Songs = new Dictionary<Guid, Song> { { testGuid, song } } };
var decodedLibrary = Library.Decode(library.EncodeImmutably());
Assert.AreEqual(library, decodedLibrary);
}
Expand Down
15 changes: 10 additions & 5 deletions Runtime/C#/Runtime/BebopWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace Bebop.Runtime
/// </summary>
public ref struct BebopWriter
{
const long TicksBetweenEpochs = 621355968000000000L;
const long DateMask = 0x3fffffffffffffffL;

// ReSharper disable once InconsistentNaming
private static readonly UTF8Encoding UTF8 = new();

Expand Down Expand Up @@ -287,7 +290,9 @@ public void WriteFloat64(double value)
[MethodImpl(BebopConstants.HotPath)]
public void WriteDate(DateTime date)
{
WriteInt64(date.ToUniversalTime().ToBinary());
long ms = (long)(date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
long ticks = ms * 10000L + TicksBetweenEpochs;
WriteInt64(ticks & DateMask);
}

/// <summary>
Expand Down Expand Up @@ -321,7 +326,7 @@ public void WriteString(string value)
fixed (char* c = value)
{
var size = UTF8.GetByteCount(c, value.Length);
WriteUInt32(unchecked((uint) size));
WriteUInt32(unchecked((uint)size));
var index = Length;
GrowBy(size);
fixed (byte* o = _buffer.Slice(index, size))
Expand Down Expand Up @@ -365,7 +370,7 @@ public void FillRecordLength(int position, uint messageLength)
[MethodImpl(BebopConstants.HotPath)]
public void WriteFloat32S(float[] value)
{
WriteUInt32(unchecked((uint) value.Length));
WriteUInt32(unchecked((uint)value.Length));
var index = Length;
var floatBytes = AsBytes<float>(value);
if (floatBytes.IsEmpty)
Expand All @@ -382,7 +387,7 @@ public void WriteFloat32S(float[] value)
[MethodImpl(BebopConstants.HotPath)]
public void WriteFloat64S(double[] value)
{
WriteUInt32(unchecked((uint) value.Length));
WriteUInt32(unchecked((uint)value.Length));
var index = Length;
var doubleBytes = AsBytes<double>(value);
if (doubleBytes.IsEmpty)
Expand Down Expand Up @@ -417,7 +422,7 @@ public void WriteBytes(ImmutableArray<byte> value)
[MethodImpl(BebopConstants.HotPath)]
public void WriteBytes(byte[] value)
{
WriteUInt32(unchecked((uint) value.Length));
WriteUInt32(unchecked((uint)value.Length));
if (value.Length == 0)
{
return;
Expand Down

0 comments on commit 1a78cd6

Please sign in to comment.