Skip to content

Commit

Permalink
Increased precision of sended timestamp on .net 7.0 and greater (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zagrebelin authored Oct 5, 2023
1 parent 593d12c commit e3ceb39
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ namespace Serilog.Sinks.Grafana.Loki.Utils;

internal static class DateTimeOffsetExtensions
{
#if NET7_0_OR_GREATER
internal static string ToUnixNanosecondsString(this DateTimeOffset offset) =>
((offset.ToUnixTimeMilliseconds() * 1000000) +
(offset.Microsecond * 1000) +
offset.Nanosecond).ToString();
#else
internal static string ToUnixNanosecondsString(this DateTimeOffset offset) =>
(offset.ToUnixTimeMilliseconds() * 1000000).ToString();
#endif

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@ public void DateTimeOffsetShouldBeConvertedCorrectly()

result.ShouldBe("1621944000000000000");
}

#if NET7_0_OR_GREATER
[Fact]
public void DateTimeNanosecondsOffsetShouldBeConvertedCorrectly()
{
var dateTimeOffset =
new DateTimeOffset(2021, 05, 25, 12, 00, 00, 777, 888, TimeSpan.Zero).AddMicroseconds(0.999); // There is no other way to set nanoseconds

var result = dateTimeOffset.ToUnixNanosecondsString();

result.ShouldBe("1621944000777888900");
}
#endif
}

0 comments on commit e3ceb39

Please sign in to comment.