Skip to content

Commit

Permalink
Preserve ticks precixion
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsgaeq authored and aloneguid committed Oct 11, 2022
1 parent 2e5ef1e commit 2c6f36b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
38 changes: 24 additions & 14 deletions src/Parquet.Test/File/Values/Primitives/NanoTimeTest.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
using System;
using System.Collections.Generic;
using Parquet.File.Values.Primitives;
using Xunit;

namespace Parquet.Test.File.Values.Primitives
{
public class NanoTimeTest
{
[Fact]
public void ConvertToDateTimeOffset_PreservesTicks()
{
var dto = new DateTimeOffset(2021, 05, 14, 17, 52, 31, TimeSpan.Zero);
dto = dto.Add(TimeSpan.FromTicks(1234567));
var nanoTime = new NanoTime(dto);
var convertedDto = (DateTimeOffset) nanoTime;
namespace Parquet.Test.File.Values.Primitives {
public class NanoTimeTest {
[Theory]
[InlineData(637566187511234567)] // 2021-05-14T17:52:31.1234567+00:00 - Works before fix
[InlineData(637807514459104071)] // 2022-02-18T02:24:05.9104071+00:00 - Does not work before fix
[MemberData(nameof(GenerateRandomDateTime), 1000)] // Generate a bunch of random date times
public void ConvertToDateTimeOffset_PreservesTicks(long ticks) {
DateTimeOffset dto = new DateTime(ticks).ToUniversalTime();
var nanoTime = new NanoTime(dto);
var convertedDto = (DateTimeOffset)nanoTime;

Assert.Equal(dto, convertedDto);
}
}
Assert.Equal(dto, convertedDto);
}

public static IEnumerable<object[]> GenerateRandomDateTime(int num) {
var rand = new Random();
for(int i = 0; i < num; i++) {
var dateTime = new DateTimeOffset(2022, rand.Next(1, 13), rand.Next(1, 28), rand.Next(0, 24),
rand.Next(0, 60), rand.Next(0, 60), rand.Next(0, 1000), TimeSpan.Zero);
dateTime = dateTime.AddTicks(rand.Next(0, (int)TimeSpan.TicksPerMillisecond));
yield return new object[] { dateTime.Ticks };
}
}
}
}
2 changes: 1 addition & 1 deletion src/Parquet/File/Values/Primitives/NanoTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public NanoTime(DateTimeOffset dt)
}

_julianDay = d + (153 * m - 457) / 5 + 365 * y + (y / 4) - (y / 100) + (y / 400) + 1721119;
_timeOfDayNanos = (long)(dt.TimeOfDay.TotalMilliseconds * 1000000D);
_timeOfDayNanos = dt.TimeOfDay.Ticks * 100;
}

public void Write(BinaryWriter writer)
Expand Down

0 comments on commit 2c6f36b

Please sign in to comment.