Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support negative timespans #438

Merged
merged 1 commit into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Humanizer.Tests/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public TimeSpanHumanizeTests() : base("en-US") { }
[Theory]
[InlineData(14, "2 weeks")]
[InlineData(7, "1 week")]
[InlineData(-14, "2 weeks")]
[InlineData(-7, "1 week")]
public void Weeks(int days, string expected)
{
var actual = TimeSpan.FromDays(days).Humanize();
Expand All @@ -23,6 +25,9 @@ public void Weeks(int days, string expected)
[InlineData(6, "6 days")]
[InlineData(2, "2 days")]
[InlineData(1, "1 day")]
[InlineData(-6, "6 days")]
[InlineData(-2, "2 days")]
[InlineData(-1, "1 day")]
public void Days(int days, string expected)
{
var actual = TimeSpan.FromDays(days).Humanize();
Expand All @@ -32,6 +37,8 @@ public void Days(int days, string expected)
[Theory]
[InlineData(2, "2 hours")]
[InlineData(1, "1 hour")]
[InlineData(-2, "2 hours")]
[InlineData(-1, "1 hour")]
public void Hours(int hours, string expected)
{
var actual = TimeSpan.FromHours(hours).Humanize();
Expand All @@ -41,6 +48,8 @@ public void Hours(int hours, string expected)
[Theory]
[InlineData(2, "2 minutes")]
[InlineData(1, "1 minute")]
[InlineData(-2, "2 minutes")]
[InlineData(-1, "1 minute")]
public void Minutes(int minutes, string expected)
{
var actual = TimeSpan.FromMinutes(minutes).Humanize();
Expand All @@ -52,6 +61,10 @@ public void Minutes(int minutes, string expected)
[InlineData(60, "1 minute")]
[InlineData(2, "2 seconds")]
[InlineData(1, "1 second")]
[InlineData(-135, "2 minutes")]
[InlineData(-60, "1 minute")]
[InlineData(-2, "2 seconds")]
[InlineData(-1, "1 second")]
public void Seconds(int seconds, string expected)
{
var actual = TimeSpan.FromSeconds(seconds).Humanize();
Expand All @@ -63,6 +76,10 @@ public void Seconds(int seconds, string expected)
[InlineData(1400, "1 second")]
[InlineData(2, "2 milliseconds")]
[InlineData(1, "1 millisecond")]
[InlineData(-2500, "2 seconds")]
[InlineData(-1400, "1 second")]
[InlineData(-2, "2 milliseconds")]
[InlineData(-1, "1 millisecond")]
public void Milliseconds(int ms, string expected)
{
var actual = TimeSpan.FromMilliseconds(ms).Humanize();
Expand Down
3 changes: 2 additions & 1 deletion src/Humanizer/TimeSpanHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ private static int GetNormalCaseTimeAsInteger(int timeNumberOfUnits, double tota

private static string BuildFormatTimePart(IFormatter cultureFormatter, TimeUnit timeUnitType, int amountOfTimeUnits)
{
// Always use positive units to account for negative timespans
return amountOfTimeUnits != 0
? cultureFormatter.TimeSpanHumanize(timeUnitType, amountOfTimeUnits)
? cultureFormatter.TimeSpanHumanize(timeUnitType, Math.Abs(amountOfTimeUnits))
: null;
}

Expand Down