From 243a692a01ecbede2e963de1637a81cc00fb78e9 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Sun, 3 Jan 2016 14:11:03 +0100 Subject: [PATCH] Use collection formatted to output of time indicators of humanized timespans. Fixes #497 --- ...provalTest.approve_public_api.approved.txt | 4 +- src/Humanizer.Tests/TimeSpanHumanizeTests.cs | 44 +++++++++++++++++-- src/Humanizer/TimeSpanHumanizeExtensions.cs | 19 +++++--- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index cd3c1ea9f..b888e868f 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -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 diff --git a/src/Humanizer.Tests/TimeSpanHumanizeTests.cs b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs index 1db3b49c5..15d94bc8b 100644 --- a/src/Humanizer.Tests/TimeSpanHumanizeTests.cs +++ b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs @@ -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); } @@ -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); } @@ -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); } diff --git a/src/Humanizer/TimeSpanHumanizeExtensions.cs b/src/Humanizer/TimeSpanHumanizeExtensions.cs index 6459da15c..62d6928f5 100644 --- a/src/Humanizer/TimeSpanHumanizeExtensions.cs +++ b/src/Humanizer/TimeSpanHumanizeExtensions.cs @@ -24,10 +24,11 @@ public static class TimeSpanHumanizeExtensions /// Culture to use. If null, current thread's UI culture is used. /// The maximum unit of time to output. /// The minimum unit of time to output. + /// The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used. /// - 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); } /// @@ -39,13 +40,14 @@ public static string Humanize(this TimeSpan timeSpan, int precision = 1, Culture /// Culture to use. If null, current thread's UI culture is used. /// The maximum unit of time to output. /// The minimum unit of time to output. + /// The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used. /// - 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 timeParts = CreateTheTimePartsWithUperAndLowerLimits(timeSpan, culture, maxUnit, minUnit); timeParts = SetPrecisionOfTimeSpan(timeParts, precision, countEmptyUnits); - return ConcatenateTimeSpanParts(timeParts); + return ConcatenateTimeSpanParts(timeParts, collectionSeparator); } private static IEnumerable CreateTheTimePartsWithUperAndLowerLimits(TimeSpan timespan, CultureInfo culture, TimeUnit maxUnit, TimeUnit minUnit) @@ -182,9 +184,14 @@ private static IEnumerable SetPrecisionOfTimeSpan(IEnumerable ti return timeParts; } - private static string ConcatenateTimeSpanParts(IEnumerable timeSpanParts) + private static string ConcatenateTimeSpanParts(IEnumerable timeSpanParts, string collectionSeparator) { - return string.Join(", ", timeSpanParts); + if (collectionSeparator == null) + { + return Configurator.CollectionFormatter.Humanize(timeSpanParts); + } + + return string.Join(collectionSeparator, timeSpanParts); } } } \ No newline at end of file