Skip to content

Commit

Permalink
Change to show milliseconds when total time is less than 2 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Apr 16, 2020
1 parent b7985b8 commit af7d086
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
6 changes: 4 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"args": [
"build",
"${workspaceFolder}",
"/p:GenerateFullPaths=true"
"/p:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
Expand All @@ -27,7 +28,8 @@
"args": [
"test",
"${workspaceFolder}",
"/p:GenerateFullPaths=true"
"/p:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
Expand Down
22 changes: 14 additions & 8 deletions src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit af7d086

Please sign in to comment.