Skip to content
This repository has been archived by the owner on Apr 27, 2019. It is now read-only.

Commit

Permalink
Fix up a bunch of go duration bugs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
highlyunavailable committed Mar 23, 2016
1 parent 2486298 commit 4020d65
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions Consul.Test/Consul.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="SessionTest.cs" />
<Compile Include="StatusTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UtilTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
23 changes: 23 additions & 0 deletions Consul.Test/UtilTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Consul.Test
{
public class UtilTest
{
[Fact]
public void GoDurationTest()
{
Assert.Equal("150ms", new TimeSpan(0, 0, 0, 0, 150).ToGoDuration());
Assert.Equal("26h3m4.005s", new TimeSpan(1, 2, 3, 4, 5).ToGoDuration());
Assert.Equal("2h3m4.005s", new TimeSpan(0, 2, 3, 4, 5).ToGoDuration());
Assert.Equal("3m4.005s", new TimeSpan(0, 0, 3, 4, 5).ToGoDuration());
Assert.Equal("4.005s", new TimeSpan(0, 0, 0, 4, 5).ToGoDuration());
Assert.Equal("5ms", new TimeSpan(0, 0, 0, 0, 5).ToGoDuration());
}
}
}
18 changes: 9 additions & 9 deletions Consul/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,37 @@ internal static string ToGoDuration(this TimeSpan ts)
{
return "0";
}
var outDuration = new StringBuilder();

if (ts.TotalSeconds < 1)
{
outDuration.Append(ts.TotalMilliseconds.ToString("#ms"));
return ts.TotalMilliseconds.ToString("#ms");
}
else
{
var outDuration = new StringBuilder();
if ((int)ts.TotalHours > 0)
{
outDuration.Append(ts.TotalHours.ToString("#h"));
}
if ((int)ts.TotalMinutes > 0)
if (ts.Minutes > 0)
{
outDuration.Append(ts.Minutes.ToString("#m"));
outDuration.Append(ts.ToString("%m'm'"));
}
if ((int)ts.TotalSeconds > 0)
if (ts.Seconds > 0)
{
outDuration.Append(ts.Seconds.ToString("#"));
outDuration.Append(ts.ToString("%s"));
}

if (ts.Milliseconds > 0)
{
outDuration.Append(".");
outDuration.Append(ts.Milliseconds.ToString("#"));
outDuration.Append(ts.ToString("fff"));
}
if (ts.Seconds > 0)
{
outDuration.Append("s");
}
return outDuration.ToString();
}
return outDuration.ToString();
}
internal static TimeSpan FromGoDuration(string value)
{
Expand Down

0 comments on commit 4020d65

Please sign in to comment.