Skip to content

Commit

Permalink
Fix PosixTimeval.ToString() when microseconds is < 100000
Browse files Browse the repository at this point in the history
Bug would cause PosixTimeval.ToString() with a seconds of 123 and a microseconds of 1234 to return "123.1234"
but this should be returning "123.001234" (note the leading zeros).

Change from StringBuilder.Append() to StringBuilder.AppendFormat() and add a format that will add leading '0's
until there are 6 digits of microseconds.

Bug reported by github user bucweat.
  • Loading branch information
chmorgan committed Feb 13, 2017
1 parent 6f25511 commit e04f13b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SharpPcap/PosixTimeval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public override System.String ToString()
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(Seconds);
sb.Append('.');
sb.Append(MicroSeconds);
sb.AppendFormat("{0:000000}",MicroSeconds);
sb.Append('s');

return sb.ToString();
Expand Down
9 changes: 9 additions & 0 deletions Test/PosixTimevalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,14 @@ public void OperatorTest ()
Assert.IsTrue(p1 <= p2, "p1 <= p2");
Assert.IsTrue(p2 >= p1, "p2 >= p1");
}

// Test string formatting output
[Test]
public void ToStringTest()
{
var p1 = new PosixTimeval(123, 12345);

Assert.AreEqual("123.012345s", p1.ToString());
}
}
}

0 comments on commit e04f13b

Please sign in to comment.