From af7d0869dddf10ce872a7bc5ca96c6d77f092041 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Thu, 16 Apr 2020 14:27:31 -0500 Subject: [PATCH] Change to show milliseconds when total time is less than 2 seconds --- .vscode/tasks.json | 6 +++-- .../TimeSpanExtensions.cs | 22 ++++++++++++------- .../TimeSpanExtensionTests.cs | 6 ++--- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 57030c1..e9036f6 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -12,7 +12,8 @@ "args": [ "build", "${workspaceFolder}", - "/p:GenerateFullPaths=true" + "/p:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, @@ -27,7 +28,8 @@ "args": [ "test", "${workspaceFolder}", - "/p:GenerateFullPaths=true" + "/p:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, diff --git a/src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs b/src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs index 3eee240..8d20427 100644 --- a/src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs +++ b/src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs @@ -148,14 +148,20 @@ public string ToString(int maxParts, bool shortForm = false, bool includeMillise if (seconds > 10) seconds = Math.Round(seconds); - if (AppendPart(sb, "second", seconds, shortForm, ref partCount)) - if (maxParts > 0 && partCount >= maxParts) - return sb.ToString(); - - if (includeMilliseconds && AppendPart(sb, "millisecond", Milliseconds, shortForm, ref partCount)) - if (maxParts > 0 && partCount >= maxParts) - return sb.ToString(); - + if (Math.Abs(TotalSeconds) > 2) { + if (AppendPart(sb, "second", seconds, shortForm, ref partCount)) + if (maxParts > 0 && partCount >= maxParts) + return sb.ToString(); + + if (includeMilliseconds && AppendPart(sb, "millisecond", Milliseconds, shortForm, ref partCount)) + if (maxParts > 0 && partCount >= maxParts) + return sb.ToString(); + } else { + if (AppendPart(sb, "millisecond", TotalMilliseconds, shortForm, ref partCount)) + if (maxParts > 0 && partCount >= maxParts) + return sb.ToString(); + } + return sb.ToString(); } diff --git a/tests/Exceptionless.DateTimeExtensions.Tests/TimeSpanExtensionTests.cs b/tests/Exceptionless.DateTimeExtensions.Tests/TimeSpanExtensionTests.cs index 43a2de4..dbb358a 100644 --- a/tests/Exceptionless.DateTimeExtensions.Tests/TimeSpanExtensionTests.cs +++ b/tests/Exceptionless.DateTimeExtensions.Tests/TimeSpanExtensionTests.cs @@ -6,13 +6,13 @@ public class TimeSpanExtensionTests { [Fact] public void ToWords() { TimeSpan value = TimeSpan.FromMilliseconds(100); - Assert.Equal("0.1 second", value.ToWords()); + Assert.Equal("100 milliseconds", value.ToWords()); value = TimeSpan.FromMilliseconds(-100); - Assert.Equal("-0.1 second", value.ToWords()); + Assert.Equal("-100 milliseconds", value.ToWords()); value = TimeSpan.FromMilliseconds(100); - Assert.Equal("0.1s", value.ToWords(true)); + Assert.Equal("100ms", value.ToWords(true)); value = TimeSpan.FromMilliseconds(2500); Assert.Equal("2.5 seconds", value.ToWords());