Skip to content

Commit

Permalink
Add nanosecond support for timestamps for versions prior to .NET 7 (#249
Browse files Browse the repository at this point in the history
)

* Change functions for using ticks

* Adjusted CI for multiple dotnet versions

* Fix test names + added missed test

* Fix pipeline

* CI

* Removed netstandard2.0 from the test project
  • Loading branch information
mishamyte authored Jan 30, 2024
1 parent 21259b1 commit 4f90718
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
19 changes: 11 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@ jobs:
strategy:
fail-fast: false
matrix:
runs-on: [ macOS-latest, ubuntu-latest, windows-latest ]
name: ${{ matrix.runs-on }}
runs-on: ${{ matrix.runs-on }}
os: [ macOS-latest, ubuntu-latest, windows-latest ]
name: ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET Core
- name: Setup .NET
uses: actions/[email protected]
with:
dotnet-version: 7.0.x
dotnet-version: |
5.0.x
6.0.x
7.0.x
- run: dotnet --info

- if: contains(matrix.runs-on, 'macOS') || contains(matrix.runs-on, 'ubuntu')
- if: contains(matrix.os, 'macOS') || contains(matrix.os, 'ubuntu')
run: ./build.sh
- if: matrix.runs-on == 'windows-latest' && !contains(github.ref, 'refs/tags/')
- if: matrix.os == 'windows-latest' && !contains(github.ref, 'refs/tags/')
run: ./build.ps1
- if: matrix.runs-on == 'windows-latest' && github.event_name == 'release' && github.event.action == 'published'
- if: matrix.os == 'windows-latest' && github.event_name == 'release' && github.event.action == 'published'
run: |
./build.ps1
dotnet nuget push .\artifacts\*.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
13 changes: 4 additions & 9 deletions src/Serilog.Sinks.Grafana.Loki/Utils/DateTimeOffsetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ 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
private const long NanosecondsPerTick = 100;
private static readonly DateTimeOffset UnixEpoch = new(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

internal static string ToUnixNanosecondsString(this DateTimeOffset offset) =>
((offset - UnixEpoch).Ticks * NanosecondsPerTick).ToString();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Serilog.Sinks.Grafana.Loki.Tests.UtilsTests;

public class DateTimeOffsetExtensionsTests
{
#if NET7_0_OR_GREATER
[Fact]
public void UnixEpochShouldBeConvertedCorrectly()
{
Expand All @@ -17,25 +18,39 @@ public void UnixEpochShouldBeConvertedCorrectly()
}

[Fact]
public void DateTimeOffsetShouldBeConvertedCorrectly()
public void DateTimeNanosecondsOffsetShouldBeConvertedCorrectly()
{
var dateTimeOffset = new DateTimeOffset(2021, 05, 25, 12, 00, 00, TimeSpan.Zero);
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("1621944000000000000");
result.ShouldBe("1621944000777888900");
}

#else
[Fact]
public void UnixEpochShouldBeConvertedCorrectly()
{
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

var result = epoch.ToUnixNanosecondsString();

result.ShouldBe("0");
}

#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
const long nanosecondsPerTick = 100;

var ticks = new DateTimeOffset(2021, 05, 25, 12, 00, 00, TimeSpan.Zero).Ticks;
ticks += 777888999 / nanosecondsPerTick;

var dateTimeOffset = new DateTimeOffset(ticks, TimeSpan.Zero);

var result = dateTimeOffset.ToUnixNanosecondsString();

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

0 comments on commit 4f90718

Please sign in to comment.