Skip to content

Commit

Permalink
Merge pull request #971 from louis-z/issue-651
Browse files Browse the repository at this point in the history
Fix TimeSpan.Humanize when number of days > _daysInAMonth & precision > 1
  • Loading branch information
clairernovotny authored Aug 31, 2020
2 parents 697ac65 + 036811d commit 67423ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/Humanizer.Tests.Shared/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ public void TimeSpanWithPrecision(long milliseconds, int precision, string expec
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(3 * 7 + 4, 2, "3 weeks, 4 days")]
[InlineData(6 * 7 + 3, 2, "6 weeks, 3 days")]
[InlineData(72 * 7 + 6, 2, "72 weeks, 6 days")]
public void DaysWithPrecision(int days, int precision, string expected)
{
var actual = TimeSpan.FromDays(days).Humanize(precision: precision);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(50)]
[InlineData(52)]
Expand Down
14 changes: 7 additions & 7 deletions src/Humanizer/TimeSpanHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ private static string GetTimeUnitPart(TimeUnit timeUnitToGet, TimeSpan timespan,
{
if (timeUnitToGet <= maximumTimeUnit && timeUnitToGet >= minimumTimeUnit)
{
var isTimeUnitToGetTheMaximumTimeUnit = (timeUnitToGet == maximumTimeUnit);
var numberOfTimeUnits = GetTimeUnitNumericalValue(timeUnitToGet, timespan, isTimeUnitToGetTheMaximumTimeUnit);
var numberOfTimeUnits = GetTimeUnitNumericalValue(timeUnitToGet, timespan, maximumTimeUnit);
return BuildFormatTimePart(cultureFormatter, timeUnitToGet, numberOfTimeUnits, toWords);
}
return null;
}

private static int GetTimeUnitNumericalValue(TimeUnit timeUnitToGet, TimeSpan timespan, bool isTimeUnitToGetTheMaximumTimeUnit)
private static int GetTimeUnitNumericalValue(TimeUnit timeUnitToGet, TimeSpan timespan, TimeUnit maximumTimeUnit)
{
var isTimeUnitToGetTheMaximumTimeUnit = (timeUnitToGet == maximumTimeUnit);
switch (timeUnitToGet)
{
case TimeUnit.Millisecond:
Expand All @@ -109,7 +109,7 @@ private static int GetTimeUnitNumericalValue(TimeUnit timeUnitToGet, TimeSpan ti
case TimeUnit.Hour:
return GetNormalCaseTimeAsInteger(timespan.Hours, timespan.TotalHours, isTimeUnitToGetTheMaximumTimeUnit);
case TimeUnit.Day:
return GetSpecialCaseDaysAsInteger(timespan, isTimeUnitToGetTheMaximumTimeUnit);
return GetSpecialCaseDaysAsInteger(timespan, maximumTimeUnit);
case TimeUnit.Week:
return GetSpecialCaseWeeksAsInteger(timespan, isTimeUnitToGetTheMaximumTimeUnit);
case TimeUnit.Month:
Expand Down Expand Up @@ -148,13 +148,13 @@ private static int GetSpecialCaseWeeksAsInteger(TimeSpan timespan, bool isTimeUn
return 0;
}

private static int GetSpecialCaseDaysAsInteger(TimeSpan timespan, bool isTimeUnitToGetTheMaximumTimeUnit)
private static int GetSpecialCaseDaysAsInteger(TimeSpan timespan, TimeUnit maximumTimeUnit)
{
if (isTimeUnitToGetTheMaximumTimeUnit)
if (maximumTimeUnit == TimeUnit.Day)
{
return timespan.Days;
}
if (timespan.Days < _daysInAMonth)
if (timespan.Days < _daysInAMonth || maximumTimeUnit == TimeUnit.Week)
{
var remainingDays = timespan.Days % _daysInAWeek;
return remainingDays;
Expand Down

0 comments on commit 67423ac

Please sign in to comment.