Skip to content

Commit

Permalink
Use collection formatted to output of time indicators of humanized ti…
Browse files Browse the repository at this point in the history
…mespans. Fixes Humanizr#497
  • Loading branch information
ErikSchierboom committed Jan 15, 2016
1 parent 63cb883 commit 243a692
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ public class StringHumanizeExtensions

public class TimeSpanHumanizeExtensions
{
public string Humanize(System.TimeSpan timeSpan, int precision, System.Globalization.CultureInfo culture, Humanizer.Localisation.TimeUnit maxUnit, Humanizer.Localisation.TimeUnit minUnit) { }
public string Humanize(System.TimeSpan timeSpan, int precision, bool countEmptyUnits, System.Globalization.CultureInfo culture, Humanizer.Localisation.TimeUnit maxUnit, Humanizer.Localisation.TimeUnit minUnit) { }
public string Humanize(System.TimeSpan timeSpan, int precision, System.Globalization.CultureInfo culture, Humanizer.Localisation.TimeUnit maxUnit, Humanizer.Localisation.TimeUnit minUnit, string collectionSeparator) { }
public string Humanize(System.TimeSpan timeSpan, int precision, bool countEmptyUnits, System.Globalization.CultureInfo culture, Humanizer.Localisation.TimeUnit maxUnit, Humanizer.Localisation.TimeUnit minUnit, string collectionSeparator) { }
}

public class To
Expand Down
44 changes: 40 additions & 4 deletions src/Humanizer.Tests/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void TimeSpanWithMaxTimeUnit(int ms, string expected, TimeUnit maxUnit)
[InlineData(694922500, "1 week", TimeUnit.Week)]
public void TimeSpanWithMinTimeUnit(int ms, string expected, TimeUnit minUnit)
{
var actual = TimeSpan.FromMilliseconds(ms).Humanize(minUnit: minUnit, precision: 6);
var actual = TimeSpan.FromMilliseconds(ms).Humanize(minUnit: minUnit, precision: 6, collectionSeparator: ", ");
Assert.Equal(expected, actual);
}

Expand Down Expand Up @@ -172,9 +172,9 @@ public void TimeSpanWithMinTimeUnit(int ms, string expected, TimeUnit minUnit)
[InlineData(1299630020, 3, "2 weeks, 1 day, 1 hour")]
[InlineData(1299630020, 4, "2 weeks, 1 day, 1 hour, 30 seconds")]
[InlineData(1299630020, 5, "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds")]
public void TimeSpanWithPrecesion(int milliseconds, int precesion, string expected)
public void TimeSpanWithPrecision(int milliseconds, int precision, string expected)
{
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precesion);
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precision, collectionSeparator: ", ");
Assert.Equal(expected, actual);
}

Expand Down Expand Up @@ -226,7 +226,43 @@ public void TimeSpanWithMinAndMaxUnits_DoesNotReportExcessiveTime(int minutes)
[InlineData(1299630020, 6, "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds")]
public void TimeSpanWithPrecisionAndCountingEmptyUnits(int milliseconds, int precision, string expected)
{
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precision: precision, countEmptyUnits: true);
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precision: precision, countEmptyUnits: true, collectionSeparator: ", ");
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(0, 3, "no time")]
[InlineData(0, 2, "no time")]
[InlineData(10, 2, "10 milliseconds")]
[InlineData(1400, 2, "1 second and 400 milliseconds")]
[InlineData(2500, 2, "2 seconds and 500 milliseconds")]
[InlineData(120000, 2, "2 minutes")]
[InlineData(62000, 2, "1 minute and 2 seconds")]
[InlineData(62020, 2, "1 minute and 2 seconds")]
[InlineData(62020, 3, "1 minute, 2 seconds, and 20 milliseconds")]
[InlineData(3600020, 4, "1 hour and 20 milliseconds")]
[InlineData(3600020, 3, "1 hour and 20 milliseconds")]
[InlineData(3600020, 2, "1 hour and 20 milliseconds")]
[InlineData(3600020, 1, "1 hour")]
[InlineData(3603001, 2, "1 hour and 3 seconds")]
[InlineData(3603001, 3, "1 hour, 3 seconds, and 1 millisecond")]
[InlineData(86400000, 3, "1 day")]
[InlineData(86400000, 2, "1 day")]
[InlineData(86400000, 1, "1 day")]
[InlineData(86401000, 1, "1 day")]
[InlineData(86401000, 2, "1 day and 1 second")]
[InlineData(86401200, 2, "1 day and 1 second")]
[InlineData(86401200, 3, "1 day, 1 second, and 200 milliseconds")]
[InlineData(1296000000, 1, "2 weeks")]
[InlineData(1296000000, 2, "2 weeks and 1 day")]
[InlineData(1299600000, 2, "2 weeks and 1 day")]
[InlineData(1299600000, 3, "2 weeks, 1 day, and 1 hour")]
[InlineData(1299630020, 3, "2 weeks, 1 day, and 1 hour")]
[InlineData(1299630020, 4, "2 weeks, 1 day, 1 hour, and 30 seconds")]
[InlineData(1299630020, 5, "2 weeks, 1 day, 1 hour, 30 seconds, and 20 milliseconds")]
public void TimeSpanWithPrecisionAndAlternativeCollectionFormatter(int milliseconds, int precesion, string expected)
{
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precesion);
Assert.Equal(expected, actual);
}

Expand Down
19 changes: 13 additions & 6 deletions src/Humanizer/TimeSpanHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public static class TimeSpanHumanizeExtensions
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <param name="maxUnit">The maximum unit of time to output.</param>
/// <param name="minUnit">The minimum unit of time to output.</param>
/// <param name="collectionSeparator">The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used.</param>
/// <returns></returns>
public static string Humanize(this TimeSpan timeSpan, int precision = 1, CultureInfo culture = null, TimeUnit maxUnit = TimeUnit.Week, TimeUnit minUnit = TimeUnit.Millisecond)
public static string Humanize(this TimeSpan timeSpan, int precision = 1, CultureInfo culture = null, TimeUnit maxUnit = TimeUnit.Week, TimeUnit minUnit = TimeUnit.Millisecond, string collectionSeparator = null)
{
return Humanize(timeSpan, precision, false, culture, maxUnit, minUnit);
return Humanize(timeSpan, precision, false, culture, maxUnit, minUnit, collectionSeparator);
}

/// <summary>
Expand All @@ -39,13 +40,14 @@ public static string Humanize(this TimeSpan timeSpan, int precision = 1, Culture
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <param name="maxUnit">The maximum unit of time to output.</param>
/// <param name="minUnit">The minimum unit of time to output.</param>
/// <param name="collectionSeparator">The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used.</param>
/// <returns></returns>
public static string Humanize(this TimeSpan timeSpan, int precision, bool countEmptyUnits, CultureInfo culture = null, TimeUnit maxUnit = TimeUnit.Week, TimeUnit minUnit = TimeUnit.Millisecond)
public static string Humanize(this TimeSpan timeSpan, int precision, bool countEmptyUnits, CultureInfo culture = null, TimeUnit maxUnit = TimeUnit.Week, TimeUnit minUnit = TimeUnit.Millisecond, string collectionSeparator = null)
{
IEnumerable<string> timeParts = CreateTheTimePartsWithUperAndLowerLimits(timeSpan, culture, maxUnit, minUnit);
timeParts = SetPrecisionOfTimeSpan(timeParts, precision, countEmptyUnits);

return ConcatenateTimeSpanParts(timeParts);
return ConcatenateTimeSpanParts(timeParts, collectionSeparator);
}

private static IEnumerable<string> CreateTheTimePartsWithUperAndLowerLimits(TimeSpan timespan, CultureInfo culture, TimeUnit maxUnit, TimeUnit minUnit)
Expand Down Expand Up @@ -182,9 +184,14 @@ private static IEnumerable<string> SetPrecisionOfTimeSpan(IEnumerable<string> ti
return timeParts;
}

private static string ConcatenateTimeSpanParts(IEnumerable<string> timeSpanParts)
private static string ConcatenateTimeSpanParts(IEnumerable<string> timeSpanParts, string collectionSeparator)
{
return string.Join(", ", timeSpanParts);
if (collectionSeparator == null)
{
return Configurator.CollectionFormatter.Humanize(timeSpanParts);
}

return string.Join(collectionSeparator, timeSpanParts);
}
}
}

0 comments on commit 243a692

Please sign in to comment.