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 loss of precision with BigDateTime #219

Open
wants to merge 1 commit into
base: master
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
10 changes: 5 additions & 5 deletions src/AdoNetCore.AseClient/Internal/StreamReadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ public static DateTime ReadBigDateTime(this Stream stream)
{
var usSinceYearZero = stream.ReadLong();
var usSinceEpoch = usSinceYearZero - Constants.Sql.BigDateTime.EpochMicroSeconds;
var msSinceEpoch = usSinceEpoch / 1000;
var timeSinceEpoch = TimeSpan.FromMilliseconds(msSinceEpoch);
const long ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
var ticksSinceEpoch = usSinceEpoch * ticksPerMicrosecond;

return Constants.Sql.BigDateTime.Epoch + timeSinceEpoch;
return Constants.Sql.BigDateTime.Epoch.AddTicks(ticksSinceEpoch);
}

public static DateTime ReadShortPartDateTime(this Stream stream)
Expand Down Expand Up @@ -222,9 +222,9 @@ public static DateTime ReadTime(this Stream stream)
var buf = new byte[length];

stream.Read(buf, 1, remainingLength);

Array.Reverse(buf);

return new AseDecimal(precision, scale, isPositive, buf);
}

Expand Down
4 changes: 2 additions & 2 deletions src/AdoNetCore.AseClient/Internal/StreamWriteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public static void WriteIntPartDateTime(this Stream stream, DateTime value)
public static void WriteBigDateTime(this Stream stream, DateTime value)
{
var timeSinceEpoch = value - Constants.Sql.BigDateTime.Epoch;
var msSinceEpoch = timeSinceEpoch.Ticks / TimeSpan.TicksPerMillisecond;
var usSinceEpoch = msSinceEpoch * 1000;
const long ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
var usSinceEpoch = timeSinceEpoch.Ticks / ticksPerMicrosecond;
var usSinceYearZero = usSinceEpoch + Constants.Sql.BigDateTime.EpochMicroSeconds;

stream.WriteByte(8); // length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

namespace AdoNetCore.AseClient.Tests.Unit
{
public class StreamWriteTests
public class StreamWriteReadTests
{
[TestCaseSource(nameof(WriteBigDateTime_Succeeds_Cases))]
public void WriteBigDateTime_Succeeds(string _, DateTime value, byte[] expected)
[TestCaseSource(nameof(WriteReadBigDateTime_Succeeds_Cases))]
public void WriteReadBigDateTime_Succeeds(string _, DateTime value, byte[] expected)
{
using (var ms = new MemoryStream())
{
ms.WriteBigDateTime(value);
ms.Seek(0, SeekOrigin.Begin);
Assert.AreEqual(BitConverter.ToInt64(expected, 0), BitConverter.ToInt64(ms.ToArray(), 1));
ms.Seek(1, SeekOrigin.Begin);
var readBack = ms.ReadBigDateTime();
Assert.AreEqual(value, readBack);
}
}

public static IEnumerable<TestCaseData> WriteBigDateTime_Succeeds_Cases()
public static IEnumerable<TestCaseData> WriteReadBigDateTime_Succeeds_Cases()
{
yield return new TestCaseData("0001_1", new DateTime(0001, 01, 01, 0, 0, 0, 0), new byte[] { 0x00, 0x40, 0xEB, 0xA9, 0xC2, 0x1C, 0x00, 0x00 });
yield return new TestCaseData("0001_2", new DateTime(0001, 01, 01, 0, 0, 0, 1), new byte[] { 0xE8, 0x43, 0xEB, 0xA9, 0xC2, 0x1C, 0x00, 0x00 });
Expand All @@ -30,7 +33,8 @@ public static IEnumerable<TestCaseData> WriteBigDateTime_Succeeds_Cases()
yield return new TestCaseData("1753_1", new DateTime(1753, 1, 1, 0, 0, 0, 0, 0), new byte[] { 0x00, 0xA0, 0x7E, 0xDC, 0xB6, 0x88, 0xC4, 0x00 });
yield return new TestCaseData("1900_1", new DateTime(1900, 1, 1, 0, 0, 0, 0, 0), new byte[] { 0x00, 0x60, 0x5A, 0x60, 0xB1, 0x03, 0xD5, 0x00 });
yield return new TestCaseData("1900_2", new DateTime(1900, 1, 1, 23, 59, 59, 999), new byte[] { 0x18, 0xBC, 0x31, 0x7E, 0xC5, 0x03, 0xD5, 0x00 });
yield return new TestCaseData("1900_3", new DateTime(1900, 01, 01).Add(TimeSpan.FromHours(24).Add(TimeSpan.FromTicks(-1))), new byte[] { 0x18, 0xBC, 0x31, 0x7E, 0xC5, 0x03, 0xD5, 0x00 });
// .Net ticks are 1/10 us, so we must lose the last digit of number of ticks
yield return new TestCaseData("1900_3", new DateTime(1900, 01, 01).Add(TimeSpan.FromHours(24).Add(TimeSpan.FromTicks(-10))), new byte[] { 0xFF, 0xBF, 0x31, 0x7E, 0xC5, 0x03, 0xD5, 0x00 });
yield return new TestCaseData("9999_1", new DateTime(9999, 1, 1, 0, 0, 0, 0, 0), new byte[] { 0x00, 0x80, 0x76, 0xE9, 0x1F, 0x04, 0x61, 0x04 });
}
}
Expand Down