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

#632 Continued: Fuzzy month years logic for time span humanize extensions #633

Merged
merged 9 commits into from
May 4, 2017
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ In addition, a maximum unit of time may be specified to avoid rolling up to the
TimeSpan.FromDays(7).Humanize(maxUnit: TimeUnit.Day) => "7 days" // instead of 1 week
TimeSpan.FromMilliseconds(2000).Humanize(maxUnit: TimeUnit.Millisecond) => "2000 milliseconds" // instead of 2 seconds
```
The default maxUnit is `TimeUnit.Week` because it gives exact results. You can increase this value to `TimeUnit.Month` or `TimeUnit.Year` which will give you an approximation based on 365.2425 days a year and 30.436875 days a month. Therefore the months are alternating between 30 and 31 days in length and every fourth year is 366 days long.
```C#
TimeSpan.FromDays(486).Humanize(maxUnit: TimeUnit.Year, precision: 7) => "1 year, 3 months, 29 days" // One day further is 1 year, 4 month
TimeSpan.FromDays(517).Humanize(maxUnit: TimeUnit.Year, precision: 7) => "1 year, 4 months, 30 days" // This month has 30 days and one day further is 1 year, 5 months
```

When there are multiple time units, they are combined using the `", "` string:

Expand Down
11 changes: 0 additions & 11 deletions src/Humanizer.Tests.Shared/Localisation/DefaultFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ namespace Humanizer.Tests.Localisation
{
public class DefaultFormatterTests
{
[Theory]
[InlineData(TimeUnit.Month, 1)]
[InlineData(TimeUnit.Month, 2)]
[InlineData(TimeUnit.Month, 10)]
[InlineData(TimeUnit.Year, 1)]
[InlineData(TimeUnit.Year, 2)]
[InlineData(TimeUnit.Year, 10)]
public void TimeSpanHumanizeThrowsExceptionForTimeUnitsLargerThanWeek(TimeUnit timeUnit, int unit)
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DefaultFormatter(CultureInfo.InvariantCulture.Name).TimeSpanHumanize(timeUnit, unit));
}

[Fact]
[UseCulture("es")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ namespace Humanizer.Tests.Localisation.af
public class TimeSpanHumanizeTests
{

[Theory]
[Trait("Translation", "Google")]
[InlineData(366, "1 jaar")]
[InlineData(731, "2 jaar")]
[InlineData(1096, "3 jaar")]
[InlineData(4018, "11 jaar")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Google")]
[InlineData(31, "1 maand")]
[InlineData(61, "2 maande")]
[InlineData(92, "3 maande")]
[InlineData(335, "11 maande")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Fact]
public void TwoWeeks()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ namespace Humanizer.Tests.Localisation.ar
[UseCulture("ar")]
public class TimeSpanHumanizeTests
{
[Theory]
[Trait("Translation", "Google")]
[InlineData(366, "السنة 1")]
[InlineData(731, "سنتين")]
[InlineData(1096, "3 سنة")]
[InlineData(4018, "11 سنة")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Google")]
[InlineData(31, "شهر 1")]
[InlineData(61, "شهرين")]
[InlineData(92, "3 أشهر")]
[InlineData(335, "11 أشهر")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[InlineData(7, "أسبوع واحد")]
[InlineData(14, "أسبوعين")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ namespace Humanizer.Tests.Localisation.bg
[UseCulture("bg-BG")]
public class TimeSpanHumanizeTests
{
[Theory]
[Trait("Translation", "Google")]
[InlineData(366, "една година")]
[InlineData(731, "2 години")]
[InlineData(1096, "3 години")]
[InlineData(4018, "11 години")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Google")]
[InlineData(31, "един месец")]
[InlineData(61, "2 месеца")]
[InlineData(92, "3 месеца")]
[InlineData(335, "11 месеца")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[InlineData(7, "една седмица")]
[InlineData(14, "2 седмици")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ namespace Humanizer.Tests.Localisation.bnBD
[UseCulture("bn-BD")]
public class TimeSpanHumanizeTests
{

[Theory]
[Trait("Translation", "Google")]
[InlineData(366, "এক বছর")]
[InlineData(731, "2 বছর")]
[InlineData(1096, "3 বছর")]
[InlineData(4018, "11 বছর")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Google")]
[InlineData(31, "এক মাসের")]
[InlineData(61, "2 মাস")]
[InlineData(92, "3 মাস")]
[InlineData(335, "11 মাস")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[InlineData(7, "এক সপ্তাহ")]
[InlineData(14, "2 সপ্তাহ")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ namespace Humanizer.Tests.Localisation.cs
public class TimeSpanHumanizeTests
{

[Theory]
[Trait("Translation", "Google")]
[InlineData(366, "1 rok")]
[InlineData(731, "2 roky")]
[InlineData(1096, "3 roky")]
[InlineData(4018, "11 let")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Google")]
[InlineData(31, "1 měsíc")]
[InlineData(61, "2 měsíce")]
[InlineData(92, "3 měsíce")]
[InlineData(335, "11 měsíců")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[InlineData(1, "1 milisekunda")]
[InlineData(2, "2 milisekundy")]
Expand Down Expand Up @@ -80,7 +102,7 @@ public void Days(int number, string expected)
[InlineData(6, "6 týdnů")]
public void Weeks(int number, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(number*7).Humanize());
Assert.Equal(expected, TimeSpan.FromDays(number * 7).Humanize());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
namespace Humanizer.Tests.Localisation.da
{
[UseCulture("da-DK")]
public class TimeSpanHumanizeTests
public class TimeSpanHumanizeTests
{

[Theory]
[Trait("Translation", "Native speaker")]
[InlineData(366, "et år")]
[InlineData(731, "2 år")]
[InlineData(1096, "3 år")]
[InlineData(4018, "11 år")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Native speaker")]
[InlineData(31, "en måned")]
[InlineData(61, "2 måneder")]
[InlineData(92, "3 måneder")]
[InlineData(335, "11 måneder")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[InlineData(7, "en uge")]
[InlineData(14, "2 uger")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,31 @@
namespace Humanizer.Tests.Localisation.de
{
[UseCulture("de-DE")]
public class TimeSpanHumanizeTests
public class TimeSpanHumanizeTests
{

[Theory]
[Trait("Translation", "Native speaker")]
[InlineData(366, "Ein Jahr")]
[InlineData(731, "2 Jahre")]
[InlineData(1096, "3 Jahre")]
[InlineData(4018, "11 Jahre")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Native speaker")]
[InlineData(31, "Ein Monat")]
[InlineData(61, "2 Monate")]
[InlineData(92, "3 Monate")]
[InlineData(335, "11 Monate")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[InlineData(7, "Eine Woche")]
[InlineData(14, "2 Wochen")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ namespace Humanizer.Tests.Localisation.es
[UseCulture("es-ES")]
public class TimeSpanHumanizeTests
{
[Theory]
[Trait("Translation", "Google")]
[InlineData(366, "un año")]
[InlineData(731, "2 años")]
[InlineData(1096, "3 años")]
[InlineData(4018, "11 años")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Google")]
[InlineData(31, "un mes")]
[InlineData(61, "2 meses")]
[InlineData(92, "3 meses")]
[InlineData(335, "11 meses")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Fact]
public void TwoWeeks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
namespace Humanizer.Tests.Localisation.fa
{
[UseCulture("fa")]
public class TimeSpanHumanizeTests
public class TimeSpanHumanizeTests
{

[Theory]
[Trait("Translation", "Native speaker")]
[InlineData(366, "یک سال")]
[InlineData(731, "2 سال")]
[InlineData(1096, "3 سال")]
[InlineData(4018, "11 سال")]
public void Years(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[Trait("Translation", "Native speaker")]
[InlineData(31, "یک ماه")]
[InlineData(61, "2 ماه")]
[InlineData(92, "3 ماه")]
[InlineData(335, "11 ماه")]
public void Months(int days, string expected)
{
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize(maxUnit: Humanizer.Localisation.TimeUnit.Year));
}

[Theory]
[InlineData(7, "یک هفته")]
[InlineData(77, "11 هفته")]
Expand Down
Loading