From 991da2f6539d4f916b3e69e75317658180a53642 Mon Sep 17 00:00:00 2001 From: Simon Oxtoby Date: Mon, 28 Oct 2024 18:59:31 +1000 Subject: [PATCH] Adding microseconds to DateTime produced by ToDateTime helper Resolves #214 --- SlackNet.Tests/UtilsTests.cs | 2 ++ SlackNet/Utils.cs | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/SlackNet.Tests/UtilsTests.cs b/SlackNet.Tests/UtilsTests.cs index 87fe0d1..076893e 100644 --- a/SlackNet.Tests/UtilsTests.cs +++ b/SlackNet.Tests/UtilsTests.cs @@ -34,6 +34,7 @@ public void ToDateTime_FromTimestampString_InvalidTimestamp_ReturnsNull() public void ToDateTime_FromTimestampString_ValidTimestamp_ReturnsDateTime() { "42".ToDateTime().ShouldBe(new DateTime(1970, 1, 1, 0, 0, 42)); + "42.123456".ToDateTime().ShouldBe(new DateTime(1970, 1, 1, 0, 0, 42, 123, 456)); "-42".ToDateTime().ShouldBe(new DateTime(1969, 12, 31, 23, 59, 18)); } @@ -60,6 +61,7 @@ public void ToDateTime_FromDecimal_Zero_ReturnsNull() public void ToDateTime_FromDecimal_NonZero_ReturnsDateTime() { 42m.ToDateTime().ShouldBe(new DateTime(1970, 1, 1, 0, 0, 42)); + 42.123456m.ToDateTime().ShouldBe(new DateTime(1970, 1, 1, 0, 0, 42, 123, 456)); (-42m).ToDateTime().ShouldBe(new DateTime(1969, 12, 31, 23, 59, 18)); } diff --git a/SlackNet/Utils.cs b/SlackNet/Utils.cs index 9226de2..10a3c7f 100644 --- a/SlackNet/Utils.cs +++ b/SlackNet/Utils.cs @@ -34,7 +34,9 @@ public static class Utils /// If timestamp is 0, returns null, otherwise a DateTime. public static DateTime? ToDateTime(this decimal timestamp) => timestamp != 0 - ? DateTimeOffset.FromUnixTimeMilliseconds((long)(timestamp * 1000)).UtcDateTime + ? DateTimeOffset.FromUnixTimeMilliseconds((long)(timestamp * 1000)) + .AddTicks((long)(timestamp * 1_000_000 % 1000 * 10)) // 10 ticks per microsecond + .UtcDateTime : null; ///