Skip to content

Commit

Permalink
Adding microseconds to DateTime produced by ToDateTime helper
Browse files Browse the repository at this point in the history
Resolves #214
  • Loading branch information
soxtoby committed Oct 28, 2024
1 parent 92c674f commit 991da2f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions SlackNet.Tests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand Down
4 changes: 3 additions & 1 deletion SlackNet/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static class Utils
/// <returns>If timestamp is 0, returns null, otherwise a DateTime.</returns>
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;

/// <summary>
Expand Down

0 comments on commit 991da2f

Please sign in to comment.