diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 82c5dab..c371176 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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/setup-dotnet@v4.0.0 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 }} \ No newline at end of file diff --git a/src/Serilog.Sinks.Grafana.Loki/Utils/DateTimeOffsetExtensions.cs b/src/Serilog.Sinks.Grafana.Loki/Utils/DateTimeOffsetExtensions.cs index 0be5c6d..96c353c 100644 --- a/src/Serilog.Sinks.Grafana.Loki/Utils/DateTimeOffsetExtensions.cs +++ b/src/Serilog.Sinks.Grafana.Loki/Utils/DateTimeOffsetExtensions.cs @@ -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(); } \ No newline at end of file diff --git a/test/Serilog.Sinks.Grafana.Loki.Tests/Serilog.Sinks.Grafana.Loki.Tests.csproj b/test/Serilog.Sinks.Grafana.Loki.Tests/Serilog.Sinks.Grafana.Loki.Tests.csproj index d87c975..6d0a7a0 100644 --- a/test/Serilog.Sinks.Grafana.Loki.Tests/Serilog.Sinks.Grafana.Loki.Tests.csproj +++ b/test/Serilog.Sinks.Grafana.Loki.Tests/Serilog.Sinks.Grafana.Loki.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net5.0;net6.0;net7.0 false enable enable diff --git a/test/Serilog.Sinks.Grafana.Loki.Tests/UtilsTests/DateTimeOffsetExtensionsTests.cs b/test/Serilog.Sinks.Grafana.Loki.Tests/UtilsTests/DateTimeOffsetExtensionsTests.cs index e7cd1a8..a0f6a2d 100644 --- a/test/Serilog.Sinks.Grafana.Loki.Tests/UtilsTests/DateTimeOffsetExtensionsTests.cs +++ b/test/Serilog.Sinks.Grafana.Loki.Tests/UtilsTests/DateTimeOffsetExtensionsTests.cs @@ -6,6 +6,7 @@ namespace Serilog.Sinks.Grafana.Loki.Tests.UtilsTests; public class DateTimeOffsetExtensionsTests { + #if NET7_0_OR_GREATER [Fact] public void UnixEpochShouldBeConvertedCorrectly() { @@ -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 } \ No newline at end of file