Skip to content

Commit 4f90718

Browse files
authored
Add nanosecond support for timestamps for versions prior to .NET 7 (#249)
* 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
1 parent 21259b1 commit 4f90718

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

.github/workflows/ci.yaml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,29 @@ jobs:
1919
strategy:
2020
fail-fast: false
2121
matrix:
22-
runs-on: [ macOS-latest, ubuntu-latest, windows-latest ]
23-
name: ${{ matrix.runs-on }}
24-
runs-on: ${{ matrix.runs-on }}
22+
os: [ macOS-latest, ubuntu-latest, windows-latest ]
23+
name: ${{ matrix.os }}
24+
runs-on: ${{ matrix.os }}
2525
steps:
2626
- uses: actions/checkout@v4
2727
with:
2828
fetch-depth: 0
2929

30-
- name: Setup .NET Core
30+
- name: Setup .NET
3131
uses: actions/setup-dotnet@v4.0.0
3232
with:
33-
dotnet-version: 7.0.x
33+
dotnet-version: |
34+
5.0.x
35+
6.0.x
36+
7.0.x
3437
3538
- run: dotnet --info
3639

37-
- if: contains(matrix.runs-on, 'macOS') || contains(matrix.runs-on, 'ubuntu')
40+
- if: contains(matrix.os, 'macOS') || contains(matrix.os, 'ubuntu')
3841
run: ./build.sh
39-
- if: matrix.runs-on == 'windows-latest' && !contains(github.ref, 'refs/tags/')
42+
- if: matrix.os == 'windows-latest' && !contains(github.ref, 'refs/tags/')
4043
run: ./build.ps1
41-
- if: matrix.runs-on == 'windows-latest' && github.event_name == 'release' && github.event.action == 'published'
44+
- if: matrix.os == 'windows-latest' && github.event_name == 'release' && github.event.action == 'published'
4245
run: |
4346
./build.ps1
4447
dotnet nuget push .\artifacts\*.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}

src/Serilog.Sinks.Grafana.Loki/Utils/DateTimeOffsetExtensions.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@ namespace Serilog.Sinks.Grafana.Loki.Utils;
1212

1313
internal static class DateTimeOffsetExtensions
1414
{
15-
#if NET7_0_OR_GREATER
16-
internal static string ToUnixNanosecondsString(this DateTimeOffset offset) =>
17-
((offset.ToUnixTimeMilliseconds() * 1000000) +
18-
(offset.Microsecond * 1000) +
19-
offset.Nanosecond).ToString();
20-
#else
21-
internal static string ToUnixNanosecondsString(this DateTimeOffset offset) =>
22-
(offset.ToUnixTimeMilliseconds() * 1000000).ToString();
23-
#endif
15+
private const long NanosecondsPerTick = 100;
16+
private static readonly DateTimeOffset UnixEpoch = new(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
2417

18+
internal static string ToUnixNanosecondsString(this DateTimeOffset offset) =>
19+
((offset - UnixEpoch).Ticks * NanosecondsPerTick).ToString();
2520
}

test/Serilog.Sinks.Grafana.Loki.Tests/Serilog.Sinks.Grafana.Loki.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>

test/Serilog.Sinks.Grafana.Loki.Tests/UtilsTests/DateTimeOffsetExtensionsTests.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Serilog.Sinks.Grafana.Loki.Tests.UtilsTests;
66

77
public class DateTimeOffsetExtensionsTests
88
{
9+
#if NET7_0_OR_GREATER
910
[Fact]
1011
public void UnixEpochShouldBeConvertedCorrectly()
1112
{
@@ -17,25 +18,39 @@ public void UnixEpochShouldBeConvertedCorrectly()
1718
}
1819

1920
[Fact]
20-
public void DateTimeOffsetShouldBeConvertedCorrectly()
21+
public void DateTimeNanosecondsOffsetShouldBeConvertedCorrectly()
2122
{
22-
var dateTimeOffset = new DateTimeOffset(2021, 05, 25, 12, 00, 00, TimeSpan.Zero);
23+
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
2324

2425
var result = dateTimeOffset.ToUnixNanosecondsString();
2526

26-
result.ShouldBe("1621944000000000000");
27+
result.ShouldBe("1621944000777888900");
28+
}
29+
30+
#else
31+
[Fact]
32+
public void UnixEpochShouldBeConvertedCorrectly()
33+
{
34+
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
35+
36+
var result = epoch.ToUnixNanosecondsString();
37+
38+
result.ShouldBe("0");
2739
}
2840

29-
#if NET7_0_OR_GREATER
3041
[Fact]
3142
public void DateTimeNanosecondsOffsetShouldBeConvertedCorrectly()
3243
{
33-
var dateTimeOffset =
34-
new DateTimeOffset(2021, 05, 25, 12, 00, 00, 777, 888, TimeSpan.Zero).AddMicroseconds(0.999); // There is no other way to set nanoseconds
44+
const long nanosecondsPerTick = 100;
45+
46+
var ticks = new DateTimeOffset(2021, 05, 25, 12, 00, 00, TimeSpan.Zero).Ticks;
47+
ticks += 777888999 / nanosecondsPerTick;
48+
49+
var dateTimeOffset = new DateTimeOffset(ticks, TimeSpan.Zero);
3550

3651
var result = dateTimeOffset.ToUnixNanosecondsString();
3752

3853
result.ShouldBe("1621944000777888900");
3954
}
40-
#endif
55+
#endif
4156
}

0 commit comments

Comments
 (0)