Skip to content

Commit

Permalink
introduce optional considerEmptyParts parameter to TimeSpan Humanize …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
mexx committed Nov 4, 2014
1 parent 6697f26 commit 0c9ff0a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public class StringHumanizeExtensions

public class TimeSpanHumanizeExtensions
{
public string Humanize(System.TimeSpan timeSpan, int precision, System.Globalization.CultureInfo culture) { }
public string Humanize(System.TimeSpan timeSpan, int precision, System.Globalization.CultureInfo culture, bool considerEmptyParts) { }
}

public class To
Expand Down
40 changes: 38 additions & 2 deletions src/Humanizer.Tests/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,42 @@ public void Milliseconds(int ms, string expected)
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, 400 milliseconds")]
[InlineData(2500, 2, "2 seconds, 500 milliseconds")]
[InlineData(120000, 2, "2 minutes")]
[InlineData(62000, 2, "1 minute, 2 seconds")]
[InlineData(62020, 2, "1 minute, 2 seconds")]
[InlineData(62020, 3, "1 minute, 2 seconds, 20 milliseconds")]
[InlineData(3600020, 4, "1 hour, 20 milliseconds")]
[InlineData(3600020, 3, "1 hour, 20 milliseconds")]
[InlineData(3600020, 2, "1 hour, 20 milliseconds")]
[InlineData(3600020, 1, "1 hour")]
[InlineData(3603001, 2, "1 hour, 3 seconds")]
[InlineData(3603001, 3, "1 hour, 3 seconds, 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, 1 second")]
[InlineData(86401200, 2, "1 day, 1 second")]
[InlineData(86401200, 3, "1 day, 1 second, 200 milliseconds")]
[InlineData(1296000000, 1, "2 weeks")]
[InlineData(1296000000, 2, "2 weeks, 1 day")]
[InlineData(1299600000, 2, "2 weeks, 1 day")]
[InlineData(1299600000, 3, "2 weeks, 1 day, 1 hour")]
[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)
{
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precesion);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(0, 3, "no time")]
[InlineData(0, 2, "no time")]
Expand Down Expand Up @@ -104,9 +140,9 @@ public void Milliseconds(int ms, string expected)
[InlineData(1299630020, 4, "2 weeks, 1 day, 1 hour")]
[InlineData(1299630020, 5, "2 weeks, 1 day, 1 hour, 30 seconds")]
[InlineData(1299630020, 6, "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds")]
public void TimeSpanWithPrecesion(int milliseconds, int precesion, string expected)
public void TimeSpanWithPrecisionAndConsideringEmptyParts(int milliseconds, int precision, string expected)
{
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precesion);
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precision: precision, considerEmptyParts: true);
Assert.Equal(expected, actual);
}

Expand Down
68 changes: 9 additions & 59 deletions src/Humanizer/TimeSpanHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Humanizer.Configuration;
using Humanizer.Localisation;
using Humanizer.Localisation.Formatters;
Expand All @@ -20,10 +19,17 @@ public static class TimeSpanHumanizeExtensions
/// <param name="timeSpan"></param>
/// <param name="precision">The maximum number of time units to return. Defaulted is 1 which means the largest unit is returned</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <param name="considerEmptyParts">Controls whether empty time units should be considered when calculating the number of time units to return.</param>
/// <returns></returns>
public static string Humanize(this TimeSpan timeSpan, int precision = 1, CultureInfo culture = null)
public static string Humanize(this TimeSpan timeSpan, int precision = 1, CultureInfo culture = null, bool considerEmptyParts = false)
{
return string.Join(", ", GetTimeParts(timeSpan, culture).Take(precision).Where(x => x != null));
var timeParts = GetTimeParts(timeSpan, culture);
if (!considerEmptyParts)
timeParts = timeParts.Where(x => x != null);
timeParts = timeParts.Take(precision);
if (considerEmptyParts)
timeParts = timeParts.Where(x => x != null);
return string.Join(", ", timeParts);
}

private static IEnumerable<string> GetTimeParts(TimeSpan timespan, CultureInfo culture)
Expand Down Expand Up @@ -65,61 +71,5 @@ private static string GetTimePart(IFormatter formatter, TimeUnit timeUnit, int u
? formatter.TimeSpanHumanize(timeUnit, unit)
: null;
}

private static string GetTimePart(TimeSpan timespan, CultureInfo culture)
{
var formatter = Configurator.GetFormatter(culture);
if (timespan.Days >= 7)
return formatter.TimeSpanHumanize(TimeUnit.Week, timespan.Days/7);

if (timespan.Days >= 1)
return formatter.TimeSpanHumanize(TimeUnit.Day, timespan.Days);

if (timespan.Hours >= 1)
return formatter.TimeSpanHumanize(TimeUnit.Hour, timespan.Hours);

if (timespan.Minutes >= 1)
return formatter.TimeSpanHumanize(TimeUnit.Minute, timespan.Minutes);

if (timespan.Seconds >= 1)
return formatter.TimeSpanHumanize(TimeUnit.Second, timespan.Seconds);

if (timespan.Milliseconds >= 1)
return formatter.TimeSpanHumanize(TimeUnit.Millisecond, timespan.Milliseconds);

return formatter.TimeSpanHumanize_Zero();
}

static TimeSpan TakeOutTheLargestUnit(TimeSpan timeSpan)
{
return timeSpan - LargestUnit(timeSpan);
}

static TimeSpan LargestUnit(TimeSpan timeSpan)
{
var days = timeSpan.Days;
if (days >= 7)
return TimeSpan.FromDays((days/7) * 7);
if (days >= 1)
return TimeSpan.FromDays(days);

var hours = timeSpan.Hours;
if (hours >= 1)
return TimeSpan.FromHours(hours);

var minutes = timeSpan.Minutes;
if (minutes >= 1)
return TimeSpan.FromMinutes(minutes);

var seconds = timeSpan.Seconds;
if (seconds >= 1)
return TimeSpan.FromSeconds(seconds);

var milliseconds = timeSpan.Milliseconds;
if (milliseconds >= 1)
return TimeSpan.FromMilliseconds(milliseconds);

return TimeSpan.Zero;
}
}
}

0 comments on commit 0c9ff0a

Please sign in to comment.