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

Date precision #133

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class CasingExtensions

public class Configurator
{
public Humanizer.DateTimeStrategy.IDateTimeHumanizeStrategy DateTimeHumanizeStrategy { get; set; }
public Humanizer.Localisation.IFormatter Formatter { get; }
}

Expand All @@ -73,6 +74,23 @@ public class DateHumanizeExtensions
public string Humanize(System.DateTime input, bool utcDate, System.Nullable<System.DateTime> dateToCompareAgainst) { }
}

public class DefaultDateTimeStrategy
{
public DefaultDateTimeStrategy() { }
public string Humanize(System.DateTime input, System.DateTime comparisonBase) { }
}

public interface IDateTimeHumanizeStrategy
{
string Humanize(System.DateTime input, System.DateTime comparisonBase);
}

public class PrecisionDateTimeStrategy
{
public PrecisionDateTimeStrategy(double precision) { }
public string Humanize(System.DateTime input, System.DateTime comparisonBase) { }
}

public class EnumDehumanizeExtensions
{
public TTargetEnum DehumanizeTo(string input) { }
Expand Down
12 changes: 11 additions & 1 deletion src/Humanizer.Tests/DateHumanize.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Humanizer.Configuration;
using Humanizer.DateTimeStrategy;
using Humanizer.Localisation;
using Xunit;

Expand All @@ -25,8 +27,13 @@ static void VerifyWithDateInjection(string expectedString, TimeSpan deltaFromNow
Assert.Equal(expectedString, now.Add(deltaFromNow).Humanize(false, now));
}

public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Tense tense)
public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Tense tense, double? precision = null)
{
if (precision.HasValue)
Configurator.DateTimeHumanizeStrategy = new PrecisionDateTimeStrategy(precision.Value);
else
Configurator.DateTimeHumanizeStrategy = new DefaultDateTimeStrategy();

var deltaFromNow = new TimeSpan();
unit = Math.Abs(unit);

Expand All @@ -35,6 +42,9 @@ public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Te

switch (timeUnit)
{
case TimeUnit.Millisecond:
deltaFromNow = TimeSpan.FromMilliseconds(unit);
break;
case TimeUnit.Second:
deltaFromNow = TimeSpan.FromSeconds(unit);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Humanizer.Tests
{
public class DateHumanizeTests
public class DateHumanizeDefaultStrategyTests
{
[Theory]
[InlineData(1, "one second ago")]
Expand Down Expand Up @@ -125,4 +125,4 @@ public void YearsFromNow(int years, string expected)
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Future);
}
}
}
}
187 changes: 187 additions & 0 deletions src/Humanizer.Tests/DateTimeHumanizePrecisionStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using Humanizer.Localisation;
using Xunit.Extensions;

namespace Humanizer.Tests
{
public class DateTimeHumanizePrecisionStrategyTests
{
[Theory]
[InlineData(1, "now")]
[InlineData(749, "now")]
[InlineData(750, "one second ago")]
[InlineData(1000, "one second ago")]
[InlineData(1749, "one second ago")]
[InlineData(1750, "2 seconds ago")]
public void MillisecondsAgo(int milliseconds, string expected)
{
DateHumanize.Verify(expected, milliseconds, TimeUnit.Millisecond, Tense.Past, .75);
}

[Theory]
[InlineData(1, "now")]
[InlineData(749, "now")]
[InlineData(750, "one second from now")]
[InlineData(1000, "one second from now")]
[InlineData(1749, "one second from now")]
[InlineData(1750, "2 seconds from now")]
public void MillisecondsFromNow(int milliseconds, string expected)
{
DateHumanize.Verify(expected, milliseconds, TimeUnit.Millisecond, Tense.Future, .75);
}

[Theory]
[InlineData(1, "one second ago")]
[InlineData(10, "10 seconds ago")]
[InlineData(44, "44 seconds ago")]
[InlineData(45, "a minute ago")]
[InlineData(60, "a minute ago")]
[InlineData(104, "a minute ago")]
[InlineData(105, "2 minutes ago")]
[InlineData(120, "2 minutes ago")]
public void SecondsAgo(int seconds, string expected)
{
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Past, .75);
}

[Theory]
[InlineData(1, "one second from now")]
[InlineData(10, "10 seconds from now")]
[InlineData(44, "44 seconds from now")]
[InlineData(45, "a minute from now")]
[InlineData(60, "a minute from now")]
[InlineData(104, "a minute from now")]
[InlineData(105, "2 minutes from now")]
[InlineData(120, "2 minutes from now")]
public void SecondsFromNow(int seconds, string expected)
{
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Future, .75);
}

[Theory]
[InlineData(1, "a minute ago")]
[InlineData(10, "10 minutes ago")]
[InlineData(44, "44 minutes ago")]
[InlineData(45, "an hour ago")]
[InlineData(60, "an hour ago")]
[InlineData(104, "an hour ago")]
[InlineData(105, "2 hours ago")]
[InlineData(120, "2 hours ago")]
public void MinutesAgo(int minutes, string expected)
{
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Past, .75);
}

[Theory]
[InlineData(1, "a minute from now")]
[InlineData(10, "10 minutes from now")]
[InlineData(44, "44 minutes from now")]
[InlineData(45, "an hour from now")]
[InlineData(60, "an hour from now")]
[InlineData(104, "an hour from now")]
[InlineData(105, "2 hours from now")]
[InlineData(120, "2 hours from now")]
public void MinutesFromNow(int minutes, string expected)
{
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Future, .75);
}

[Theory]
[InlineData(1, "an hour ago")]
[InlineData(10, "10 hours ago")]
[InlineData(17, "17 hours ago")]
[InlineData(18, "yesterday")]
[InlineData(24, "yesterday")]
[InlineData(41, "yesterday")]
[InlineData(42, "2 days ago")]
[InlineData(48, "2 days ago")]
[InlineData(60, "2 days ago")]
public void HoursAgo(int hours, string expected)
{
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Past, .75);
}

[Theory]
[InlineData(1, "an hour from now")]
[InlineData(10, "10 hours from now")]
[InlineData(18, "tomorrow")]
[InlineData(24, "tomorrow")]
[InlineData(41, "tomorrow")]
[InlineData(42, "2 days from now")]
[InlineData(48, "2 days from now")]
[InlineData(60, "2 days from now")]
public void HoursFromNow(int hours, string expected)
{
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Future, .75);
}

[Theory]
[InlineData(1, "yesterday")]
[InlineData(10, "10 days ago")]
[InlineData(20, "20 days ago")]
[InlineData(22, "one month ago")]
[InlineData(23, "one month ago")]
[InlineData(31, "one month ago")]
[InlineData(43, "one month ago")]
[InlineData(44, "two months ago")]
public void DaysAgo(int days, string expected)
{
DateHumanize.Verify(expected, days, TimeUnit.Day, Tense.Past, .75);
}

[Theory]
[InlineData(1, "tomorrow")]
[InlineData(10, "10 days from now")]
[InlineData(20, "20 days from now")]
[InlineData(22, "one month from now")]
[InlineData(23, "one month from now")]
[InlineData(31, "one month from now")]
[InlineData(43, "one month from now")]
[InlineData(44, "two months from now")]
public void DaysFromNow(int days, string expected)
{
DateHumanize.Verify(expected, days, TimeUnit.Day, Tense.Future, .75);
}

[Theory]
[InlineData(1, "one month ago")]
[InlineData(8, "8 months ago")]
[InlineData(9, "one year ago")]
[InlineData(12, "one year ago")]
[InlineData(19, "one year ago")]
[InlineData(20, "2 years ago")]
[InlineData(24, "2 years ago")]
public void MonthsAgo(int months, string expected)
{
DateHumanize.Verify(expected, months, TimeUnit.Month, Tense.Past, .75);
}

[Theory]
[InlineData(1, "one month from now")]
[InlineData(8, "8 months from now")]
[InlineData(9, "one year from now")]
[InlineData(12, "one year from now")]
[InlineData(19, "one year from now")]
[InlineData(20, "2 years from now")]
[InlineData(24, "2 years from now")]
public void MonthsFromNow(int months, string expected)
{
DateHumanize.Verify(expected, months, TimeUnit.Month, Tense.Future, .75);
}

[Theory]
[InlineData(1, "one year ago")]
[InlineData(2, "2 years ago")]
public void YearsAgo(int years, string expected)
{
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Past, .75);
}

[Theory]
[InlineData(1, "one year from now")]
[InlineData(2, "2 years from now")]
public void YearsFromNow(int years, string expected)
{
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Future, .75);
}
}
}
3 changes: 2 additions & 1 deletion src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@
<Compile Include="Localisation\sk\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\sv\DateHumanizeTests.cs" />
<Compile Include="Localisation\sv\TimeSpanHumanizeTests.cs" />
<Compile Include="DateTimeHumanizePrecisionStrategyTests.cs" />
<Compile Include="ResourceKeyTests.cs" />
<Compile Include="RomanNumeralTests.cs" />
<Compile Include="StringExtensionsTests.cs" />
<Compile Include="ToQuantityTests.cs" />
<Compile Include="TransformersTests.cs" />
<Compile Include="NumberToOrdinalWordsTests.cs" />
<Compile Include="Localisation\ro-Ro\DateHumanizeTests.cs" />
<Compile Include="DateHumanizeTests.cs" />
<Compile Include="DateHumanizeDefaultStrategyTests.cs" />
<Compile Include="DehumanizeToEnumTests.cs" />
<Compile Include="EnumHumanizeTests.cs" />
<Compile Include="EnumUnderTest.cs" />
Expand Down
27 changes: 13 additions & 14 deletions src/Humanizer.Tests/Localisation/fa/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Xunit;
using Humanizer.Localisation;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.fa
Expand All @@ -13,95 +12,95 @@ public DateHumanizeTests() : base("fa") { }
[InlineData(13, "13 روز بعد")]
public void DaysFromNow(int days, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddDays(days).Humanize());
DateHumanize.Verify(expected, days, TimeUnit.Day, Tense.Future);
}

[Theory]
[InlineData(-1, "دیروز")]
[InlineData(-11, "11 روز پیش")]
public void DaysAgo(int days, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddDays(days).Humanize());
DateHumanize.Verify(expected, days, TimeUnit.Day, Tense.Past);
}

[Theory]
[InlineData(1, "یک ساعت بعد")]
[InlineData(11, "11 ساعت بعد")]
public void HoursFromNow(int hours, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddHours(hours).Humanize());
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Future);
}

[Theory]
[InlineData(-1, "یک ساعت پیش")]
[InlineData(-11, "11 ساعت پیش")]
public void HoursAgo(int hours, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddHours(hours).Humanize());
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Past);
}

[Theory]
[InlineData(1, "یک دقیقه بعد")]
[InlineData(13, "13 دقیقه بعد")]
public void MinutesFromNow(int minutes, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddMinutes(minutes).Humanize());
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Future);
}

[Theory]
[InlineData(-1, "یک دقیقه پیش")]
[InlineData(-13, "13 دقیقه پیش")]
public void MinutesAgo(int minutes, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddMinutes(minutes).Humanize());
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Past);
}

[Theory]
[InlineData(1, "یک ماه بعد")]
[InlineData(10, "10 ماه بعد")]
public void MonthsFromNow(int months, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddMonths(months).Humanize());
DateHumanize.Verify(expected, months, TimeUnit.Month, Tense.Future);
}

[Theory]
[InlineData(-1, "یک ماه پیش")]
[InlineData(-10, "10 ماه پیش")]
public void MonthsAgo(int months, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddMonths(months).Humanize());
DateHumanize.Verify(expected, months, TimeUnit.Month, Tense.Past);
}

[Theory]
[InlineData(1, "یک ثانیه بعد")]
[InlineData(11, "11 ثانیه بعد")]
public void SecondsFromNow(int seconds, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddSeconds(seconds).Humanize());
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Future);
}

[Theory]
[InlineData(-1, "یک ثانیه پیش")]
[InlineData(-11, "11 ثانیه پیش")]
public void SecondsAgo(int seconds, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddSeconds(seconds).Humanize());
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Past);
}

[Theory]
[InlineData(1, "یک سال بعد")]
[InlineData(21, "21 سال بعد")]
public void YearsFromNow(int years, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddYears(years).Humanize());
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Future);
}

[Theory]
[InlineData(-1, "یک سال پیش")]
[InlineData(-21, "21 سال پیش")]
public void YearsAgo(int years, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddYears(years).Humanize());
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Past);
}
}
}
Loading